Vocabulary
Variable |
hold a value and can be change |
String |
a list of character such as number, letter and symbols |
Integer Number |
whole number/ counting number |
float number |
the number in decimal |
syntax |
grammar/ structure of language |
modulo |
find the remainder |
boolean |
true/ flase |
Addition
string + string |
combine together |
string + number |
CRASH! |
number + number |
Addition (math) |
Multiplication and exponents
string* number |
combine that string |
string* string |
CRASH! |
number* number |
Multiply (Math) |
string ** string |
CRASH! |
number ** number |
Exponents (Math) |
string ** number |
CRASH! |
if/ elif/ else
def printdefinition (word):
if word == "function":
print("""
function lets you use code
""")
elif word== "string":
print("""
string is list of character
""")
else:
print ("unknown word")
user_word = input ("Enter a word to define: ")
printdefinition(user_word) |
for loop
"""
list = [2,3,4,5,6,7]
list_num = 0
while (list_num < len(list)):
print (list[list_num])
list_num = list_num+1
"""
forlist = [1,2,3,4]
for item in forlist:
print (item) |
circle
def doubleIt(number):
return number*2
print (doubleIt (3))
print (doubleIt (2.5))
print (doubleIt("hi"))
myvar = doubleIt (doubleIt (3))
print (myvar)
def areaOfCircle (radius):
if (radius<=0):
return "Error: radius <=0"
pi = 3.1415
area = pi(radius*2)
return area
user_radius = input("Enter the radius: ")
radius = float(user_radius)
print ("The area of the circle is", areaOfCircle(radius)) |
|
|
Function
print() |
show information that you want on the screen |
int() |
change number to be number integer |
float() |
change number too be decimal number |
Input() |
gain information from user |
str() |
a list of number, letter and symbols |
len() |
the length of the string |
# |
comment, no effect |
function
_varl = 1
_varl = 3
_varl + 100
print(_varl)
def bacon(): #use the keyword def and end with a colon:
print("hello it's bacon")
return
bacon()
bacon()
bacon()
bacon()
bacon()
def bacon():
print("hello it's bacon")
print("line 2")
print("line 3")
print("line 4")
print("line 5")
print("line 6")
print("line 7")
print("line 8")
return
bacon()
bacon()
bacon()
def myprint(text): #single parameter
print ("" + str(text) + "")
return
myprint(1)
myprint("hello")
myprint(1+2)
def myprint2(text, decoration):
print(decoration + str(text) + decoration)
return
myprint2(12312321312, "++++")
myprint2("hello", "<<>>")
def doubleIt(number):
return number * 2
myvar = 2
myvarDouble = doubleIt(myvar)
print(doubleIt("hello"))
myvar = doubleIt(doubleIt(3)) #same as doubleIt(6)
print (myvar)
def sumIt(num1, num2):
return num1 + num2
print(sumIt("a", "b"))
print(sumIt(2,3))
def areaOfCircle (radius):
pi = 3.1415
area = pi radius*2
return area
user_radius = input('Enter the radius: ')
radius = float(user_radius)
print("The area of the circle is", areaOfCircle(radius))
def areaOfCircle(r):
if r <= 0:
return "Error: radius <= 0"
pi = 3.1415
area = pi r * 2
return area
user_radius = input("Enter the radius: ")
radius = float(user_radius)
print ("The area of the circle is", areaOfCircle(radius))
|
|
|
Math
= = |
equal to |
!= |
no equal to |
< |
less than |
> |
more than |
<= |
less than or equal to |
>= |
more than or equal to |
% |
Modulo, find the remainder |
list
import random
intlist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
random_int = random.choice (intlist)
print(intlist,random_int)
fplist = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
random_fp = random.choice (fplist)
print (fplist,random_fp)
strlist = ["1","2","3","4","5","6","7","8","9"]
random_str = random.choice (strlist)
print (strlist,random_str)
mylist = ["adam","mild","loveadam","levine","3","4.6",424,674,5.733]
random_item = random.choice (mylist)
print (mylist,random_item)
myvar1 = 1
myvar2 = 2
myvar3 = 3
varlist = (myvar1,myvar2,myvar3)
random_var = random.choice (varlist)
print (varlist,random_var) |
shopping list
shoppinglist = ['salmon', 'bacon', 'water', 'jelly', 'ham']
print (shoppinglist)
list_num = 0
while list_num < len(shoppinglist):
print ("List:",shoppinglist[list_num])
list_num = list_num + 1
for item in shoppinglist:
print (item)
numbers = range(120)
for num in numbers:
print (num) |
covert to int
user_word = input ("Please enter a number")
number = int (user_word)
print (number * 10) |
random
import random
mylist = ['mild', 'stamp', 'nae', 'mint']
print(mylist[0])
counter = 0
while counter < 10:
random_item = random.choice(mylist)
print (random_item)
counter = counter + 1 |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment