Python Cheat Sheet: by Via
Python Cheat Sheet: by Via
String A list of characters such as numbers, while item_number < len(shoppinglist): chances = chances - 1
letters, symbols print ("list item:", shoppinglist[item_number]) print ("Chances left: ", chances)
item_number = item_number + 1 if user_guess in mylist:
Variable Holds a value and can be changed
#for loop print ("That's incorrect")
Syntax The set of rules that defines the
other = 0 else:
combinations of symbols
for cat in shoppinglist: print ("Sorry, that is not even in the list!")
Boolean Identified True or False (true is not other = other + 1 if chances == 0:
the same as True, false is not the # print ("List item:", cat) print ("The word was: ", random_item)
same as False) print (other) print ("Final score =", scores)
Modulo Finds the remainder after division of print ("GAME OVER!!!!!!!!")
Good Variable Name user_radius = input("What is the radius of the if operation == "sum":
underscores #Calculate the area of the circle using elif operation == "div":
email@ = 2 print("The area of the circle is", area) def sum(a, b):
1value = 2 return a + b
Guessing Game def product(a, b):
return a * b
Upper Lower List #PWTK 1002
def diff(a, b):
mystr = "hello THERE" scores = 0
return a - b
print (mystr.upper()) #Upper case all the letter in chances = 3 def div(a, b):
a word while chances > 0: if b == 0:
print (mystr.lower()) #Lower case all the letter in print ("-=-=-=-=-=-=-=-=-=-=-Guessing Game-
return ("Error: Undefined value")
a word =-=-=-=-=-=-=-=--=-=-=-")
else:
print (mystr.capitalize()) #Capital only first letter import random
return a // b
of first word mylist = ['apple', 'banana', 'papaya', 'melon', print(calc(12, 12, "sum"))
print (mystr.title()) #Capital first letter of every 'orange', 'grape', 'mango'] print(calc(9, 18, "diff"))
word. print (mylist)
print(calc(20, 10, "product"))
#List in python random_item = random.choice(mylist)
print(calc(12, 4, "div"))
shoppinglist = ['Dogs', 'Cats', 'Mouses', 'Giraffe'] user_guess = input("Guess a word: ")
int() Set the integer = int(20) #with mylist2.append('another item') #Adding item in
num1 = 0 a list
number to no decimal
num2 = 1
interger print (mylist2)
fibonacci = num1 + num2
output = "0,1" str() Convert a String = str(integer)
Binary
while fibonacci < 50: variable to
output = output + "," + str(fibonacci) string while True:
num1 = num2 input() Gain Name = input(" Put user_number = input("Put the number: ")
num2 = fibonacci information your name here: " number = int(user_number)
fibonacci = num1 + num2 from the binary_string = ''
print (output) user while (number > 0):
remainder = (number % 2)
float() Convert the Num = float(2) #the
Loop Positive Integer binary_string = str(remainder) + binary_string
number answer will be 2.0
number = (number // 2)
even = 0 with
print ("binary string is", binary_string)
odd = 0 decimal
while True: len() Find the num1 = ("George"),,
Basic Info
user_input = int(input("Enter a number :")) length of num2 = len(num1)
user = user_input % 2 the string #Answer will be 6 Basic Python Programming Language
if user_input > 0: :(colon) = syntax
if user == 0: Reverse Syntax (=) Grammar
even = even + 1 Variable = Something that cahnges(numbers,
elif user != 0: word = input("Input a word: ") words)
odd = odd + 1 reverse = "" Number vs Strings: my var = 1 + 2
print(user_input) letter_num = 0 print my var = 3
else: ''' my var 2 = "1" + "2"
print ("Even number = ", even) while letter_num < len(word): print my var "12"
print ("Odd number = ", odd) reverse = word[letter_num] + reverse hello= "hello" + "It's me"
break letter_num = letter_num + 1 print hello = "helloIt'sme"
''' If 1==2:
for item in word: When you do division in programming the
reverse = item + reverse program will add decimal even if it doesn't
print ("Reverse: ",reverse) have the decimal EX: 10.0
my var = "yourname"[0] (the first letter in def isPalindrome(word): "String" + Put both string together
programming is 0 not 1) reverse = "" "String"
== equal to for item in user_word:
Number + CRASH!
!= not equal to reverse = item + reverse
"String"
> Greater than reverse_item = reverse
Number + Addition(Math)
>= Greater than or equal to if reverse_item == user_word:
Number
<= Less than or equal to return True#(reverse_item, ("is a palindrome"))
< Less than else: "String" * CRASH!
print (len(fullname)) return False#(reverse_item, ("is not a "String"
if 1 == 2: palindrome")) "String" * Print that string that number
print ("true") while True: Number times
else: user_word = input("Enter a word: ")
Number * Multiplication(Math)
print ("false") length = len(user_word)
Number
if 2 == 2: if user_word == 'quit':
print ("true") break String ** String CRASH!
else: else: String ** CRASH!
print ("flase") print (length) Number
print ("false2") numlen = 0
Number ** Exponent(Math)
while numlen < length / 2 + 1:
Number
Return Max Number if user_word[numlen] != user_word[-numlen-1]:
print (user_word,"is not a palindrom")
def max2(num1, num2): Hexadecimal
break
maxvalue = num1 numlen += 1 while True:
if num2 > maxvalue: else: user_number = input("Put the number: ")
masvalue = num2 print (user_word,"is a palindrome") number = int(user_number)
return maxvalue
#Loop the command
def max3(num1, num2, num3):
Mathematics hex_string = ''
maxvalue = num1
while (number > 0):
if num2 > maxvalue: + Addition
remainder = number % 16
maxvalue = num2
- Subtraction if remainder == 10:
if num3 > maxvalue:
* Multiplication remainder = 'A'
maxvalue = num3
elif remainder == 11:
return maxvalue / Division (Result with floating point)
remainder = 'B'
def maxlist(list): // Division elif remainder == 12:
maxvalue = list[0]
** Exponent remainder = 'C'
for num in list:
elif remainder == 13:
if num > maxvalue: % Modulo (Find remainder)
remainder = 'D'
maxvalue = num == Equal to
elif remainder == 14:
return maxvalue
>= Greater than or equal to remainder = 'E'
print (maxlist([1,2,3,4,5]))
<= Less than or equal to elif remainder == 15:
remainder = 'F'
!= Not equal to
hex_string = str(remainder) + hex_string
< Less than number = number // 16
> More than print ("hexadecimal string is 0x"+ hex_string)
user_number = input("Please enter a number: print ("The area of the triangle is", random_var = random.choice(varlist)
") areaofTriangle(user_base, user_height)) print (varlist, random_var)
number = int(user_number) area = areaofTriangle(user_base,
countdown_string = "" user_height) Multiplication Table
while number > 0: def volumeofPrism(area, height):
def multiplicationTable():
countdown_string = countdown_string + return area * height
innum = int(input("Enter a number: "))
str(number) user_height2 = float(input("Enter the second
for i in range(1,11):
number = number - 1 height of the triangle: "))
output = innum*i
#add the number to the string print ("The volume of the triangular prism is",
print (str(innum) + "*" + str(i) + "=" +
#subtract 1 from the number volumeofPrism(area, user_height2))
str(output))
print (countdown_string)
multiplicationTable()
Loop Review
List RandomChoice
#While loop
import random mylist = [1,2,3]
intlist = [1, 2, 3, 4, 5] index = 0 #set to 0 because that is the first item
random_int = random.choice(intlist) in the list
print (intlist, random_int) while index < len(mylist):
fplist = [2.2, 3.5, 4.8, 6.2, 7.9] print (mylist[index])
random_fp = random.choice(fplist) index = index + 1
print (fplist, random_fp) #For loop
strlist = ['burger', 'cheese', 'ham', 'bacon', for item in mylist:
'sandwich', 'pizza'] print(item)
random_str = random.choice(strlist)
print (strlist, random_str) List Practice
mylist = [4, 6, 8, 11.4, 12.8, 17.6,'coco', 'latte',
import random
'mocha']
intlist = [1, 2, 3, 4, 5]
random_item = random.choice(mylist)
random_int = random.choice(intlist)
print (mylist, random_item)
print (intlist, random_int)
myvar1 = 1
myvar2 = 2 fplist = [2.2, 3.5, 4.8, 6.2, 7.9]