Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Python Sheet Cheat Sheet

by Salisa Stamp via cheatography.com/25742/cs/6860/

Operators using a while loop to print each item in list Definition program code

+ addition wlist = [2,4,5,6,7,8] def printDefinitions(word):

- subtra​ction index = 0 ​ if word == "​var​iab​le":


while index < len(wl​ist): ​ ​ ​ ​print( 'A variable is things
* multip​lic​ation
​ ​ ​print (wlist​[in​dex]) that able to change')
/ division
​ ​ ​index = index +1 ​ elif word == "​fun​cti​on":
// divisi​on(​floor division)
​ ​ ​ ​print( "A function is to help
** exponent
Area of Circle Code to use a code")
% module ​ elif word == "​var​iab​le":
while True:
== equal to ​ ​ ​ ​print( 'A variable is the
​ ​use​r_r​adius = input(​"What is the
!= unequal to things that help you to change')
radius​?")
< lesser than ​ elif word == "​return variab​le":
​ ​radius = float(​use​r_r​adius)
​ ​ ​ ​print( 'A return variable is
<= lesser than or equal to ​ pi = 3.1415
something that return the function
> greater than ​ ​area= pi radius * 2
back to you')
>= greater than or equal to ​ ​print ("The area of the circle
​ elif word == "​arg​ume​nt":
is", area)
​ ​ ​ ​pri​nt('A argument is something
Addition
that give the function to you')
Area of a Triangle
string + string combine together ​ elif word == "​par​ame​ter​":
def areaOfTriangle (base,height):
string + number crash ​ ​ ​ ​pri​nt('A parameter is something
​ ​ ​return base heigh 0.5
number​+number math-a​ddition that give function')
base = float(​inp​ut(​'Enter the base
​ elif word == "​str​ing​":
Subtra​ction of the triang​le'))
​ ​ ​ ​pri​nt('A string is the text,
height = float(​inp​ut(​inp​ut(​'Enter
String - String crash number or anything that is list the
the height of the triangle: '))
charac​ters')
string​-number crash print('The area of the triangle
​ ​else:
number​-number math-s​ubt​raction is',ar​eaO​fTr​ian​gle​(ba​se,​hei​ght))
​ ​ ​ ​pri​nt(​'un​known word')
def volume​OfPrism (area,​hei​ght):
user_word = input( "​Enter a word
Multip​lic​ation ​ ​ ​return areaOf​Prism* height
to define: ")
String * String crash base = float(​inp​ut(​'Enter the area
print(​Def​ini​tio​ns(​use​r_w​ord))
String​*number print string for number times of the prism'))
height = float(​inp​ut(​'Enter the
number​*number math-m​ult​ipl​ication Function
height of the prism: '))
print( ) displays inform​ation on the screen

input( ) receives inform​ation from the user

int( ) converts a value to an integer

By Salisa Stamp Published 15th February, 2016. Sponsored by ApolloPad.com


cheatography.com/salisa-stamp/ Last updated 22nd March, 2016. Everyone has a novel in them. Finish Yours!
Page 1 of 3. https://apollopad.com
Python Sheet Cheat Sheet
by Salisa Stamp via cheatography.com/25742/cs/6860/

Function (cont) Palindrome Guessing Game code (cont)

float( ) converts a value to a decimal while True: random.ch​oic​e(m​ylist)


number ​ ​ def palind​rom​e(w​ord): while start_over < 1:
str( ) converts a value to a string ​ ​ ​ ​ ​ ​reverse = "​" ​ ​print ("-=​-=-​=-=​-=-​=-=​-=-​=-=​-

while... : loop statement ​ ​ ​ ​ ​ ​myr​esult = "​" =-​")

if ... : if statement used as a condition ​ ​ ​ ​ ​ for letters in word: ​ ​print ("Gu​essing Game")

or loop in python ​ ​ ​ ​ ​ ​ ​ ​ ​reverse = letters + ​ ​print ("-=​-=-​=-=​-=-​=-=​-=-​=-=​-


reverse =-​")
else : another condition used after if
statement ​ ​ ​ ​ ​ if word == reverse : ​ ​pri​nt(​"​wor​ds:​", mylist)
​ ​ ​ ​ ​ ​ ​ ​ ​return True ​ ​guess = input(​"​Guess a word: ")
"​"​" multi-line comment
​ ​ ​ ​ ​else: ​ if (guess in mylist):
#... a line comment
​ ​ ​ ​ ​ ​ ​ ​ ​return False ​ ​ ​ ​ ​if(​guess == random​_item ):
for ... in ... a list
​ ​ ​ ​ ​reverse = "​" ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​That's correc​t!")
True a condition in a loop
​ word = input(​"​please enter a ​ ​ ​ ​ ​ ​ ​ ​score = score + 100
False a condition in a loop word: ") ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​Sco​re:​", score)
len( ) length of the string ​ if word == "​qui​t": ​ ​ ​ ​ ​ ​ ​ ​sta​rt_over = 2

... [ x ] the x'th letter of the string ​ ​ ​ ​ ​break ​ ​ ​ ​ ​else:

import ... import a codeor something like ​ ​the​result = palind​rom​e(word) ​ ​ ​ ​ ​ ​ ​pri​nt(​"​Sorry, wrong choice!
formula in python ​ ​pri​nt(​"This word ")

random.ch​oi to random item from the list has"​,le​n(w​ord​),"l​ett​er") ​ ​ ​ ​ ​ ​ ​chances = int(ch​ances) -1


c​e(...) ​ ​else:
Random code ​ ​ ​ ​ ​ ​pri​nt(​"​Sorry, that is not
Exponent even in the list")
import random
​ ​ ​ ​ ​ ​chances = int(ch​ances) -1
string​**s​tring crash mylist = ['Dog'​,'F​ish', 'Cat',
​ ​if(​chances > 0):
string​**n​umber crash 'Bear']
​ ​ ​ ​ ​ ​pri​nt(​"​Chances
number​**n​umber math-e​xponent counter = 0
remain​ing​:",c​hances)
while counter < 10:
​ ​else:
Division ​ ​ ​ran​dom​_item =
​ ​ ​ ​ ​sta​rt_over = 2
random.ch​oic​e(m​ylist)
String / String crash ​ ​ ​ ​ ​pri​nt(​"Game Over! The word was
​ ​ ​print (rando​m_item)
String​/number crash ", random​_item)
​ ​ ​counter = counter + 1
Number​/number math-d​ivision ​ ​ ​ ​ ​pri​nt(​"​Chance remain​ing​:",
chances)
Guessing Game code
print(​"​Final score:​", score)
import random
mylist =
Vocabulary
['beag​le'​,'p​ome​ran​ian​','​pug']
variable holds a value and can be changed
score = 0
chances = 3 string a list of characters such as numbers,
letters, symbols
start_over = 0
random​_item =

By Salisa Stamp Published 15th February, 2016. Sponsored by ApolloPad.com


cheatography.com/salisa-stamp/ Last updated 22nd March, 2016. Everyone has a novel in them. Finish Yours!
Page 2 of 3. https://apollopad.com
Python Sheet Cheat Sheet
by Salisa Stamp via cheatography.com/25742/cs/6860/

Vocabulary (cont) number to binary code Quit word (def code) (cont)

floating number with a decimal point user_number = "" def createList (quitw​ord):
number while user_n​umber != "​0": ​ ​ ​mylist = [ ] #create an empty
integer number with no decimal point ​ ​ ​use​r_n​umber = input ( "​enter a list

input something that the user types in number​" ) ​ ​ ​while True:

syntax grammar or rules on ​ ​ ​number = int(us​er_​number) #get the item from the user

progra​mming ​ ​bin​ary​_string = "​" ​ ​ ​ item = input(​'Please enter a


while (number > 0 ):#the number is list item')
loop the condition used in python
greater than 0 # when the user enters an item that
operator the signs used for mathem​atics
condition ​ ​ ​rem​ainder = number % 2 is equal to quitword
​ ​ ​bin​ary​_string = str( ​ ​ ​ if item == quitword:
module text for storing the python code
remain​der)+ binary​_string ​ ​ ​ ​ ​ ​ ​ ​return mylist
​ ​ ​number = number//2 # check if the list already in the
Change the text
​ ​ ​print (number) list
( ... . change the text to upper case
print ( "​binary string is ", ​ ​ ​ ​dup​lic​ateword = False
upper(
binary​_string ) # figure out if the word isalready
))
in the list
( ... change the text to lower case
Count down code ​ ​ ​ for word in mylist:
.lower(
)) ​ ​ ​ ​ ​ ​ if item == word:
user_number= input("enter number")
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​dup​lic​ateword = True
( ... . change the first letter of the text to number = int(us​er_​number)
capita​li upper case and convert other letters ​ ​ ​ ​ ​ ​ if duplic​ateword == True:
countd​own​_string = "​"
ze( ) ) to lower case while number > 0:
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​print ('Dupl​icate

( ... . change the first letter of each word word!')


countd​own​_string =
title( ) from the text to upper case and countd​own​_string + str(nu​mber) +
​ ​ ​ ​ ​ ​ ​else:
) convert other letter to lower case # add this item to the end of the
"​"
list
number = number-1
print number in separate line in list mylist ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​myl​ist.ap​pen​d(item)
print (count​dow​n_s​tring)
mylist = [1,2,3,4,5] #function call

for number in mylist: mylist = creatL​ist​("st​op")


Number printing( for loop)
​ ​ ​print (number) print(​mylist)
for number in range(5):
print (number)
# the output will be 0-4 in
separate lines

Quit word (def code)

# create a function that allows a


user to create a list
#function name: word
#param​ater: word
#return the list

By Salisa Stamp Published 15th February, 2016. Sponsored by ApolloPad.com


cheatography.com/salisa-stamp/ Last updated 22nd March, 2016. Everyone has a novel in them. Finish Yours!
Page 3 of 3. https://apollopad.com

You might also like