You are on page 1of 3

Python Cheat Sheet

by bakumanz via cheatography.com/25889/cs/7009/

Function Sort word per line 0,01,012,0123,01234 (cont)

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

print (int(2)) 2 if myboolean:


== equal to
print (float(1)) 1.0 anything to a float print ("truth")
else: != no equal to
Modulo/Remainder %
print (4%2) 0 print ("lies") < less than
print (30%7) 2 > more than
0,01,012,0123,01234
<= less than or equal to
mystring = ''"
>= more than or equal to
count = 0
% Modulo, Find the remainder
while count <= 4:
mystring = mystring + str(count)
print (mystring)
count = count + 1
mystring = ""
for num in range(5):

By bakumanz Published 5th February, 2016. Sponsored by CrosswordCheats.com


cheatography.com/bakumanz/ Last updated 23rd March, 2016. Learn to solve cryptic crosswords!
Page 1 of 3. http://crosswordcheats.com
Python Cheat Sheet
by bakumanz via cheatography.com/25889/cs/7009/

Addition Hex Boolean

string + string Combine together user_number = input("Enter number to convert False or True True

string + number CRASH! to hex : ") False and True False


number = int(user_number)
number + number Addition (Math) True and False False
hex_string = ''
while (number > 0): True and True True
Multiplication and Exponents remainder = number % 16 False or False False
if remainder == 10:
string * number Combine that string
remainder = 'A'
string* string CRASH! Reverse Word
elif remainder == 11:
number * number Multiply (Math) remainder = 'B' while 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)

By bakumanz Published 5th February, 2016. Sponsored by CrosswordCheats.com


cheatography.com/bakumanz/ Last updated 23rd March, 2016. Learn to solve cryptic crosswords!
Page 2 of 3. http://crosswordcheats.com
Python Cheat Sheet
by bakumanz via cheatography.com/25889/cs/7009/

Sort fruit list Even,Odd number

fruits = [] #an empty list even = 0


for number in range(5): odd = 0
user_fruit = input("Please enter a fruit") while True:
fruits.append(user_fruit) user_num = int(input("Enter the number :"))
print ("Size of fruit list is", len(fruits)) if user_num >= 0:
fruits.sort() if user_num % 2 == 0:
for fruit in fruits: even = even + 1
print ("Fruit: ", fruit) else:
odd = odd + 1
Print Name else:
print ("Even number :", even)
name = "tim GIRARD"
print ("Odd number :", odd)
print (name.upper()) TIM GIRARD
break
print (name.lower()) tim girard
print (name.capitalize()) Tim girard
For loop word
print (name.title()) Tim Girard
For word in mylist:
Guess print (word)

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)

By bakumanz Published 5th February, 2016. Sponsored by CrosswordCheats.com


cheatography.com/bakumanz/ Last updated 23rd March, 2016. Learn to solve cryptic crosswords!
Page 3 of 3. http://crosswordcheats.com

You might also like