Apisara1999 Python
Apisara1999 Python
Conditionals
If.....
A list of
:then.....
number/letter/symbols
else.......
Whole number/counting
while......
number
Example8-Guessing game
loops forever
import random
While
number
True
boolean
True/False
for each
Modulo
item in
name of
random_item =
syntax
Grammar/structure of
integer
list
lauguage
length
Function
print()
Naming Conventions
- letters
- numbers
- underscores (_)
random.choice(mylist)
Chances = 5
Score = 0
while Chances > 0:
print("Words:['lion',
'cheetah', 'panther', 'cougar',
'leopard']")
- NO SPACES
user_guess = input("Guess a
input()
Float()
int()
- _mystr
if user_guess == random_item:
str()
A list of number/letter/symbols
- my3
print("That's
len()
To note, no effect
""
Multi-line comment
Valid names:
Hello_there
Invalid names:
word: ")
correct!")
Score = Score+100
print("Chances
remaining",'',Chances)
random_item =
random.choice(mylist)
print("Score
is",'',Score)
else:
if user_guess in mylist:
print("Sorry, wrong
choice!")
Chances = Chances 1
print("Chances
remaining",'',Chances)
print("Score
is",'',Score)
By Apisara1999
Sponsored by CrosswordCheats.com
cheatography.com/apisara1999/
Page 1 of 7.
http://crosswordcheats.com
else:
#argument
print("Sorry, that
print ("""
#name : max2
""")
# function call
# name : max3
print ("""
A function call is
""")
max_value = (num1)
#string
max_value = (num2)
print ("""
return max_value
A string is list of
character
""")
else:
value')
print("unknown word")
print (max2(num1,num2))
def max3(num1,num2,num3):
return
user_input=input("Enter word")
printDefinitions(user_input)
be change
def max2(num1,num2):
""")
num3:
#function
max_value = (num2)
print ("""
num1:
A function is block of
max_value = (num3)
""")
#parameter
elif word == "parameter":
print ("""
A parameter is value that
inside the blacket of the function
""")
By Apisara1999
Sponsored by CrosswordCheats.com
cheatography.com/apisara1999/
Page 2 of 7.
http://crosswordcheats.com
string ** number
CRASH!
forlist = [3, 4, 5, 2, 1]
number ** number
exponent(Math)
value')
print (max3(num1,num2,num3))
Symbols
Addition
Example5-print out each item in list
string + string
string + number
CRASH!
mystr = "hello123"
number + number
math(addition)
numbers = [1,2,3,4,5,6]
==
equal to
!=
not equal to
<
less than
Important
<=
True or anything =
True
>
greater than
False
>=
range(5) =
[0,1,2,3,4]
add
print("hello", "there")
print (mixed)
subtract
print("hello" + "there")
#displays hellothere
letter_num = 0
multiply
"hi" + "there"
== "hithere"
"hi" * 5
== "hihihihihi"
//
while True:
# forever
print (numbers)
shoppinglist =
print (shoppinglist)
mixed = [1, 'hello', 2.5, True,
False]
print (mystr[letter_num])
letter_num = letter_num + 1
for myletterisawesome in mystr:
**
exponent
string * string
CRASH!
item
math (multiply)
number *
['shoes','bags','pants','shirts']
number
string *
each item
number
time
index = index + 1
print(myletterisawesome)
for tientien in shoppinglist:
print(tiemtiem)
out = 0
for mrtim in shoppinglist:
out = out + 1
By Apisara1999
Sponsored by CrosswordCheats.com
cheatography.com/apisara1999/
Page 3 of 7.
http://crosswordcheats.com
def printDefinitions(word):
print(decoration + str(text) +
decoration)
return
#variable
run
myprintnew(1, "+++")
if word == "variable":
""")
myprintnew('hello', '-=-=-=-=-=-=-
print ("""
#string
=-=')
myprintnew(1, "@@@@@@@")
be change
""")
Example12-Circle area
def areaOfCircle (r):
if r <= 0:
return "Error: invalid
#function
pi = 3.1415
area = pi*r**2
return area
user_radius = float(input("Enter
the radius: "))
print('The area of the circle is',
areaOfCircle(user_radius))
Example11 - doubleit
def doubleit(number):
return number * 2
print (doubleit(3))
print (doubleit(doubleit(4)))
myvar = 12
character
""")
print ("""
else:
A function is block of
print("unknown word")
""")
A string is list of
radius"
print ("""
#parameter
elif word == "parameter":
print ("""
A parameter is value that
inside the blacket of the function
return
while True:
user_input=input("Enter
word")
printDefinitions(user_input)
""")
#argument
print ("""
#name: maxlist
#argument:list
""")
# function call
list
def maxlist(list):
print ("""
myvar = doubleit(myvar)
maxvalue = list[0]
for item in list:
myvar = doubleit(myvar)
print (myvar)
maxvalue = item
By Apisara1999
Sponsored by CrosswordCheats.com
cheatography.com/apisara1999/
Page 4 of 7.
http://crosswordcheats.com
(cont)
code (cont)
return maxvalue
mylist = [1,2,3,4,55,66,777,0,1]
print(maxlist(mylist))
Example18- Palindrome+loop
while True :
print(len(user_word))
if user_word == ("quit"):
break
reverse = ""
letter_num = 0
while letter_num <
len(user_word):
reverse =
user_word[letter_num] + reverse
letter_num = 0
print(True)
print (2<3)
print (2 != 2)
reverse = word[letter_num] +
reverse
letter_num = letter_num + 1
letter_num = letter_num + 1
if user_word == reverse:
reverse = ""
"""
Example3-Countdown Code
if user_word != ("quit"):
Example2-Using boolean
Example6
"""
for letter in word:
reverse = letter + reverse
print ("Reverse: ",reverse)
out = 0
for letter in word:
out = out + 1
print(out)
Example7-Convert to binary
user_number = input("Please enter
a number")
Example4-Print Name
name = jaja YOOYUEN
print (name.upper()) --- JAJA YOOYUEN
print (name.lower()) --- jaja yooyuen
print (name.capitalize()) --- Jaja yooyuen
print (name.title()) --- Jaja Yooyuen
number = int(user_number)
binary_string =''
while (number > 0):
remainder= number%2
binary_string =
str(remainder) + binary_string
number= number//2
code
binary_string)
reverse = ""
for letter in word:
reverse = letter + reverse
By Apisara1999
Sponsored by CrosswordCheats.com
cheatography.com/apisara1999/
Page 5 of 7.
http://crosswordcheats.com
Example14-reverse (cont)
letter_num = letter_num + 1
random_int = random.choice(inlist)
print (inlist, '', random_int)
fplist =
Example15-palindrome
#create a function that will ask
user for a string
#and then say if that string is
(1.1,2.1,3.1,4.1,5.1,6.1,7.1)
random_fp = random.choice(fplist)
print (fplist, '', random_fp)
strlist =
palindrome or not
reverse = ""
letter_num = 0
word = input('type in a word: ')
('love','captain','verymuch')
random_str =
random.choice(strlist)
print (strlist, '', random_str)
mylist =
(1,2,3,4,5,6,7,1.1,2.1,3.1,4.1,5.1,
6.1,7.1,'love','captain','verymuch'
)
(Prism) (cont)
area = b * h / 2
import random
inlist = (1,2,3,4,5,6,7)
random_item =
random.choice(mylist)
(Prism)
return area
user_base = float(input('Enter the
base of the triangle: '))
user_height = float(input('Enter
the height of the triangle: '))
print ('The area of triangle is',
areaOfTriangle(user_base,
user_height))
#write a function that computes the
volume of a prism
#name: volumeOfPrism
#return: volume
def volumeOfPrism(b, h, l):
if user_length <= 0:
return "Error: invarid
radius"
volume = b * h * l / 2
return volume
user_length = float(input('Enter
the length of the prism: '))
myvar1 = 1
myvar2 = 2
area of triangle
myvar3 = 3
varlist = (myvar1,myvar2,myvar3)
random_var =
random.choice(varlist)
print (varlist, '', random_var)
Example14-reverse
reverse = ""
letter_num = 0
word = input('type in a word: ')
#name: areOfTriangle
#parameter: b, h
#return: area
def areaOfTriangle(b, h):
if user_base <= 0:
return "Error: invarid
radius"
if user_height <= 0:
return "Error: invarid
radius"
By Apisara1999
Sponsored by CrosswordCheats.com
cheatography.com/apisara1999/
Page 6 of 7.
http://crosswordcheats.com
By Apisara1999
Sponsored by CrosswordCheats.com
cheatography.com/apisara1999/
Page 7 of 7.
http://crosswordcheats.com