Python Cheat Sheet
by Iamtle via cheatography.com/25873/cs/6991/
Function List Random (cont)
print() Display an information on the screen '''SHOPPING_LIST= varlist = [myvar1, myvar2, myvar3]
int() Change number to an integer ['bags','shirts','pants'] random_var =
list_num=0 random.choice(varlist)
float() Change number to an decimal number
while list_num < print (random_var) #print the
input() Ask for the information from the user
len(shopping_list) entire list and the random item
str() A list of number, letter and symbols
print(shopping_list[list_num])
len() Length of the string
list_num = list_num + 1 Operations
# Comment for item in in shopping lost:
== equal to
''' ''' Multiple Line Comment print (item)
!= no equal to
Reverse A Word < less than
Random
> more than
while True: import random
word = input("Please enter a <= less than or equal
# Create a list of integers
word") % Modulo, Calculate for Remainder
intlist = [1,2,3,4]
index = 0 random_int = + Add
reverse = ' ' random.choice(intlist) - Subtract
#while int(index) < len(word): print (random_int) #print the * Multiplication
# reverse = word[index] + entire list and the random item / Division
(reverse) # Create a list of floating point
** Exponent
# index = int(index) + 1 numbers
for letter in word: fplist = [0.2,0.3,0.4] Convert To Hex
reverse = letter + reverse random_fp = random.choice(fplist)
while True:
print ("Reverse: ", reverse) print (random_fp) #print the
user_number = input("Please
entire list and the random item
enter your number: ")
Countdown Code # Create a list of strings
number = int(user_number)
strlist = ("ABC","DEF","GHI")
hex_string = ''
user_number = input("Please enter a number: random_str =
while (number > 0 ):
") random.choice(strlist)
remainder = number % 16
number = int(user_number) print (random_str) #print the
countdown_string = '' if remainder == 10:
entire list and the random item
while number > 0: hex_string = "A" +
# Create a list of integers and
countdown_string = countdown_string + hex_string
floating point numbers and strings
str(number) elif remainder == 11:
number = number - 1 mylist = (2,3,0.4,"Hello")
hex_string = "B" +
print (countdown_string) random_item =
hex_string
random.choice(mylist)
elif remainder == 12:
print (random_item) #print the
hex_string = "C" +
entire list and the random item
hex_string
# Create a list of the following
elif remainder == 13:
variables
hex_string = "D" +
myvar1 = 1
hex_string
myvar2 = 2
elif remainder == 14:
myvar3 = 3
hex_string = "E" +
hex_string
By Iamtle Published 5th February, 2016. Sponsored by CrosswordCheats.com
cheatography.com/iamtle/ Last updated 11th February, 2016. Learn to solve cryptic crosswords!
Page 1 of 2. http://crosswordcheats.com
Python Cheat Sheet
by Iamtle via cheatography.com/25873/cs/6991/
Convert To Hex (cont) Example Of Codes Guessing Game (cont)
elif remainder == 15: 2 – integer print ("Word:",(word))
hex_string = "F" + 2.5 – floating point guess_word = input("Guess a
Print (“Hello”) – string
hex_string word: ")
mystring = 123 – variable
elif remainder < 10:
Print (mystr,”Hi”,2,1.0) -- commas
hex_string = if guess_word == random_word:
mystr = “Hi”
str(remainder) + hex_string mystr ← variable name print ("That's Correct
number = number // 16 “Hi” ← value that can be change Guess!")
print ("Hexadeimal string is print (int(1.5)) → 1 print ("----------------
0x"+ hex_string) print (int(“2”)) → 2 ---------------------------------
print (float(1)) → 1.0
-----")
Modulo/Remainder %
Area Of The Circle Code score = score + 100
print (4%2) → 0
print (30%7) → 2 print ("Score:",(score))
while True:
random_word =
user_radius = input("What is
your radius of a circle? ") Multiplication And String random.choice(word)
else:
radius = float(user_radius) String * Number
if guess_word !=
pi = float(3.1415) Ex. String * 5 = String String
random_word:
area = (pi) (radius) * 2 String String String
chances = chances - 1
print("The area of the String * String = Crash !
print ("Chances
circle", area) Number * Number = Multiply (Math)
Remaining:",chances)
Ex. 5 * 4 = 20
if guess_word in word:
Upper and Lower Case String ** String = CRASH!
print
Number ** Number = Exponent (Math)
("Sorry,Wrong Choice!")
name = "one two" Ex. 5**2 = 25
print ("----------
String ** Number = CRASH!
---------------------------------
print (name.upper()) → ONE TWO
print (name.lower()) → one two -----------")
Guessing Game
print (name.capitalize()) → One two else:
print (name.title()) → One Two import random print ("Sorry That
game_over = 1 is not even in the list")
Vocabularty chances = 5 print ("----------
score = 0 ---------------------------------
Variable reserved memory locations to store
word =
values. This means that when you -----------")
create a variable you reserve some ['Dog','Cat','Fish','Pig','Elephant if chances == 0:
space in memory '] game_over = 0
String A list of character such as number, random_word = random.choice(word) print ("Game Over!")
letter and symbols while game_over != 0: print ("The word
Boolean True/False was",(random_word))
Integer Whole Number or Counting Number print ("Final Score:",
Number (score))
Syntax Grammar of Python
Floating Number in Decimal
Point
By Iamtle Published 5th February, 2016. Sponsored by CrosswordCheats.com
cheatography.com/iamtle/ Last updated 11th February, 2016. Learn to solve cryptic crosswords!
Page 2 of 2. http://crosswordcheats.com