Python Cheat Sheet: by Via
Python Cheat Sheet: by Via
Python Cheat Sheet: by Via
print() Show information that you want on the mystr = "Hello" mystring = mystring + str(num)
screen letter_num = 0 print (mystring)
while letter_num < len(mystr):
int() Change number to be number integer
print (mystr[letter_num]) Definition
float() Change number to be decimal number
letter_num = letter_num + 1
input() Gain information from user def printDefinition(word):
H if word == "variable":
str() A list of number, letter and symbols e print ("""
len() The length of the string l A variable is the the thing that can be changed.
l """)
# Comment, no effect
o elif word == "parameter":
print ("""
Vocabulary
Selecting Largest Value A parameter is the limiting factor
Variable Hold a value and can be change """)
def max2 (num1,num2):
String A list of character such as number, elif word == "argument":
if num1>num2:
letter and symbols print ("""
return num1
An argument is the identifier that you give to
Integer Whole number/counting number if num1<num2:
function
number return num2
""")
Float The number in decimal def max3 (num1,num2,num3):
elif word == "string":
number if num1>num2 and num1>num3:
print ("""
return num1
Syntax Grammar/Structure of lauguage A string is something that can be repeated by
if num2>num1 and num2>num3:
the number.
Modulo Find the remainder return num2
""")
Boolean True/False if num3>num1 and num3>num2:
elif word == "function call":
return num3
print ("""
num1=input("Enter your num1:")
Example A function call is the word you use to reuse the
num2=input("Enter your num2:")
function.
Print (2) – integer num3=input("Enter your num3:")
""")
Print (2.5) – floating point print("the largest number of max3
else:
Print (“Hello”) – string is:",max3(num1,num2,num3))
print ("unknown word")
Print (mystr) – variable print("the largest number of max2
while True:
Print (mystr,”Hi”,2,1.0) -- commas is:",max2(num1,num2))
user_input = input("Please type the word :")
mystr = “Hi”
printDefinition(user_input)
mystr ← name ==
“Hi” ← value can change
print (int(1.5)) → 1 myboolean = 2 == 3 Math
string + string Combine together user_number = input("Enter number to convert False or True True
string ** string CRASH! elif remainder == 12: word = input("Please enter a word")
remainder = 'C' index = 0
number ** number Exponent (Math)
elif remainder == 13: reverse = ' '
string ** number CRASH! remainder = 'D' while int(index) < len(word):
elif remainder == 14: reverse = word[index] + (reverse)
Naming Convention remainder = 'E' index = int(index) + 1
elif remainder == 15: print ("Reverse: ", reverse)
Rule for giving name
remainder = 'F'
- letter
hex_string = str(remainder) + str(hex_string) Convert to binary
- numbers
number = number // 16
- underscore _ user_number = ' '
print ("Hex string is 0x",hex_string)
Valid name while user_number != ' 0 ' :
- _myStr user_number = input ("Enter a number to
1*1=1
- my3 convert to binary")
- Hello_there def multiplicationTable(num): number = int(user_number)
Invalid name multi = 0 binary_string = ' '
- 3my=”hi” -- cannot start with number while multi < 10: while (number > 0):
- first name=”hi” multi = multi + 1 remainder = number%2
- first-name user_output = num*multi binary_string = str(remainder)+ binary_string
- first+name print ( num,"*",multi,"=",user_output) number = number//2
user_num = int(input("Enter the number: ")) print ("Binary string is", binary_string)
Area of Circle multiplicationTable(user_num)
Countdown Machine
"""
Fibonacci
Python Intro Assignment #2 user_number = input("What number do you
name num1 = 0 want to count down? ")
student number num2 = 1 number = int(user_number)
""" fibonacci = num1 + num2 countdown_string = ' '
#Ask the user for a radius of a circle output = "0,1" while number > 0:
user_radius = input("What is a radius of a while fibonacci < 50: countdown_number = countdown_string +
circle?") output = output +","+ str(fibonacci) str(number) + " "
#Convert the given radius to a floating point num1 = num2 number = number - 1
radius = float(user_radius) num2 = fibonacci #print(number)
#Make a variable called pi fibonacci = num1 + num2 print (countdown_string)
pi = float(3.1415) print (output)
#Calculate the area of the circle using
exponents
area = pi(radius*2)
#Display the area of the circle to the user
print ("The area of the circle is", area)
import random
chance = 3
score = 0
mylist = ['Hack', 'ToeyD.', 'Patter','Tim','Lily']
random_item = random.choice(mylist)
while chance > 0:
print (mylist)
print ("Chances Remaining =",chance)
guess = input("Guess a word from the above :")
if guess == random_item:
score = score + 100
print ("That's correct!","The score is :",score)
random_item = random.choice(mylist)
else:
print ("Sorry, wrong choice!")
chance = chance - 1
if guess in mylist:
print ("")
else:
print ("Sorry,that is not even in the list!")
if chance == 0:
print ("Game Over! The word
was",random_item)
print ("Final score: ",score)