|
Variable
|
Holds a value and can be change
|
|
String
|
a list of characters such as numbers, letters, symbols
|
|
Integer number
|
Whole number/counting number
|
|
Float number
|
Number in decimal
|
|
Syntax
|
Grammar/Structure of language
|
|
Boolean
|
True/False
|
|
length
|
the length of the string
|
|
Print()
|
Show information that you want on screen
|
|
Int()
|
Change number to be number integer
|
|
input()
|
receives info from the user
|
|
str()
|
converts the value to a string
|
|
float()
|
converts the value to a floating point
|
|
len()
|
The length of the string
|
|
#
|
comment,no effect
|
|
'''
|
Multi-line comment
|
|
string * number
|
stringsting...(number)
|
|
string* string
|
Fail!!
|
|
number * number
|
Multiply
|
|
string ** string
|
Fail!!
|
|
number ** number
|
Exponent
|
|
string ** number
|
Fail!!
|
def printdefinitions(word):
if word == ("variable"):
print ("""A variable is the value that can change""")
elif word == ("function"):
print ("""A function is the blog of code that can be reused""")
elif word == ("parameter"):
print ("""A parameter is something given to the function""")
elif word == ("agrument"):
print ("""An agrument is something given to the function""")
elif word == ("string"):
print ("""A string is a lsit of characters""")
elif word == ("function call"):
print ("""A function call makes your function run""")
else:
print ("Unknown word")
return
while True: #keep the loop go forever
user_input = input("Enter word: ")
printdefinitions(user_input)
def printdefinitions(word):
if word == ("variable"):
print ("""A variable is the value that can change""")
elif word == ("function"):
print ("""A function is the blog of code that can be reused""")
elif word == ("parameter"):
print ("""A parameter is something given to the function""")
elif word == ("agrument"):
print ("""An agrument is something given to the function""")
elif word == ("string"):
print ("""A string is a lsit of characters""")
elif word == ("function call"):
print ("""A function call makes your function run""")
else:
print ("Unknown word")
return
while True: #keep the loop go forever
user_input = input("Enter word: ")
printdefinitions(user_input)
Enter word: hello
Unknown word
Enter word: function
A function is the blog of code that can be reused
Enter word: variable
A variable is the value that can change
Enter word:
area/volume of
|
==
|
equal to
|
|
!=
|
not equal to
|
|
<
|
less than
|
|
<=
|
less than or equal to
|
|
>
|
greater than
|
|
>=
|
greater than or equal to
|
|
+
|
add
|
|
-
|
subtract
|
|
*
|
multiply
|
|
/
|
divide and quotient is float
|
|
//
|
divide and quotient is integer
|
|
**
|
exponent
|
|
%
|
modulo: the remainder
|
|
string + string
|
combine together
|
|
string + number
|
Fail
|
|
number + number
|
plus
|
|
number - number
|
minus
|
mystr = "hellp THERE"
print (mystr.upper()) -all letters will become big HELP THREE
print (mystr.lower()) -all letters will become small help three
print (mystr.capitalize()) -First letter of first word will become big Help three
print (mystr.title())- first letter of each words will become big Help Three
def max2(num1,num2):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
return maxvalue
print (max2(4,5))
print (max2(33,5))
def max3(num1,num2,num3):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
if num3 > maxvalue:
maxvalue = num3
return maxvalue
print (max3(1,2,3))
5
33
3
def maxlist(list):
maxvalue = list[0]
for item in list:
if item > maxvalue:
maxvalue = item
return maxvalue
mylist = [1,2,3,4,55,66,777,0,1]
print(maxlist(mylist))
777
Rule for giving name
-letter
-numbers
-underscore_
Valid name
- _myStr
- my3
- Hello_there
Invalid name
- 3my="hi" -- cannot start with number
- first name = "hi"
- first-name
- first+name
def areaofcircle(radius):
if radius <= 0:
return "Error: invalid raadius"
pi = 3.1415
area = pi (radius*2)
return area
user_radius = float(input("Enter the radius: "))
print('The area of the circle is', areaofcircle(user_radius))
Enter the radius: 2
The area of the circle is 12.566
Enter the radius: 0
The area of the circle is Error: invalid raadius
def isPalindrome(word):
reverse = ""
letter_num=0
while letter_num<len(user_input):
reverse = user_input[letter_num]+reverse
letter_num = letter_num+1
if reverse==word:
return True
else:
return False
while True :
user_input = input("Enter a word")
if user_input == "quit":
break
isPal = isPalindrome(user_input)
if isPal == True:
print (user_input,'is parindorm')
else:
print (user_input,'is not parindorm')
break
Enter a word113311
113311 is parindorm
Enter a word123
123 is not parindorm
Enter a wordquit
mystr = "Hello"
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1
H
e
l
l
o
def myprint(text):
print ("***" + str(text) + "***")
return
myprint("opal")
hello it's bacon
***opal***
def myprintnew(text, decoration):
print (decoration + str(text) + decoration)
return
myprintnew("opal", "m")
hello it's bacon
***opal***
mopalm
def doubleit(number):
return number * 2
print (doubleit(3))
print (doubleit(doubleit(4)))
hello it's bacon
***opal***
mopalm
6
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.