Show Menu
Cheatography

Python

string­+string
combine together
string­+number
crash
number­+number
math-a­ddition
number­-number
math-s­ubs­tar­ction
number­*number
math-m­uti­pli­cation
number­/number
math-d­ivision
**
exponent
%
modulo
boolean
True/False
#
single line comment
"­"­"
multi-line comment

Reverse Word

while True:
word = input ("Please enter a word")
index = 0
reverse = " "
while int (index) < len(word):
reverse = word(index )+ (reverse)
index = int(in­dex)+ 1
print ("Re­vea­rs:­", reverse)

Letter

name = "tim GIRARD­­"
print (name. upper())
print (name. lower())
print (name. capita­­li­ze())
print (name. title())

TIM GIRARD
tim girard
Tim girard
Tim Girard

For loop and list

shoppi­­nglist = ['salmon', 'bacon', 'water', 'jelly', 'ham']

print (shopp­­in­g­list)

list_num = 0

while list_num < len(sh­­op­p­i­ng­­list):
print ("Li­­st­:­"­­,sh­­op­p­i­ng­­lis­­t[­l­i­st­­_num])
list_num = list_num + 1

for item in shoppi­­ng­list:
print (item)

numbers = range(120)

for num in numbers:
print (num)

covert to int

user_word = input ("Please enter a number­")
number = int (user_­word)
print (number * 10)

random

import random

mylist = ['mild', 'stamp', 'nae', 'mint']
print(­myl­ist[0])
counter = 0
while counter < 10:
random­_item = random.ch­oic­e(m­ylist)
print (rando­m_item)
counter = counter + 1

random game

import random
mylist = ['mild­','­lil­y',­'st­amp­','­nae', 'mint']
chance=3
score=0
random­_item = random.choice (mylist)

while chance > 0 :
print (mylist)
guess = input ("Guess a word: ")

if (guess in mylist):

if (guess == random­_item):
print ("That's correc­t!")
score= score+100
print ("sc­ore­"­,score)
random­_item = random.choice (mylist)
else:
print ("Sorry, wrong choice­!")
chance = chance-1
print ("chance remain­ing­:",c­hance)

else:
print ("No,not in the list")
chance= chance-1
print ("chance remain­ing­"­,ch­ance)

if (chanc­e<1):
print ("Game over!the word was", random­_item)
print ("final score", score)
 

vocabulary

str
string
int
integer
float
decimal number
len
lengh
syntax
a structure of the program
print
An instru­ction that causes the Python interp­reter to display a value on the screen.
Variable
The name of something that the code has given a value to
Single Equal (=)
assigns the value on the right to a variable on the left
Double Equal (==)
Tests if two things have the same value
input
to convert things you enter as if they were Python code

Convert to binary

user_n­umber = " "
user_n­umber != "­0" :
user_n­umber = input ("enter a number to convert to binary­")
number = int (user_­number)
binary­_string = " "

list

import random
intlist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
random_int = random.choice (intlist)
print(­­in­t­l­is­­t,r­­an­d­o­m_int)

fplist = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
random_fp = random.choice (fplist)
print (fplis­­t,­r­a­nd­­om_fp)

strlist = ["1",­­"­2­"­,"3­­"­­,"4­"­,­"­­5","6­­"­,­"­7",­­"­­8",­"­9­"]
random_str = random.choice (strlist)
print (strli­­st­,­r­an­­dom­­_str)

mylist = ["ad­­am­"­,­"­­mil­­d",­"­l­o­ve­­ada­­m",­"­l­e­vi­­ne",­­"­3­"­,"4.6­",­­424­­,6­7­4­,5.733]
random­­_item = random.choice (mylist)
print (mylis­­t,­r­a­nd­­om_­­item)

myvar1 = 1
myvar2 = 2
myvar3 = 3
varlist = (myvar­­1,­m­y­va­­r2,­­my­var3)
random_var = random.choice (varlist)
print (varli­­st­,­r­an­­dom­­_var)

Math-c­ircle

while True:
pi = 3.1415
user_r­­adius = input( " Insert radius here... " )
radius = float(­­us­e­r­_r­­adius)
area = pi radius*2
print ( " the area of the circle is",­­area)
print ( " Allahu Akbar")

triangle

def areaof­tri­angle (base,­height) :
return baseh­eig­ht0.5
base = float (input­('Enter the base of the triang­le'))
height = float (input ('Enter the height of the triang­le'))
print(­"The area of the triangle is",­are­aof­tri­ang­le(­bas­e,h­eight))

def volume­ofprism (area,­hei­ght):
return areaof­tri­ang­le*­height
print ("The volume of the prism is",­vol­ume­ofp­ris­m(a­rea­,he­ight))

if/ elif/ else

def printd­efi­nition (word):

if word == "­fun­cti­on":
print(­"­"­"
function lets you use code
"­"­")

elif word== "­str­ing­":
print(­"­"­"
string is list of character
"­"­")
else:
print ("un­known word")
user_word = input ("Enter a word to define: ")
printd­efi­nit­ion­(us­er_­word)

text+d­eco­ration

def myprint (text):
print ("­" + str(text) + "­­")
return
myprint (1)
myprint ("he­llo­")

def myprint2 (text, decora­tion):
print (decor­ation + str(text)+ decora­tion)
return
myprin­t2(­123­,"++­+++­++")
myprint2 ("he­llo­"­,"--­--")

myvar = "­hel­lo"
def myvarprint (myvar):
print (myvar)
return
myvarprint ("hi­")
print (myvar)
 

symbol

if/else
condit­ional
while
loop
for
list all the things
==
test if two values are the same
<
less than
>
more than
<=
if the value of left operand is less than or equal to the value of right operan­d,then condition becomes true
>=
if the value of left operand is greater than or equal to the value of right operan­d,then condition becomes true

guess word game

import random
guesslist = ['grape', 'orange', 'apple']
chance = 3
score = 0
print (guess­list)
while chance = 0
random­_item = random.choice (guess­list)
user_input = input ('please guess a word: ')
if user_input ==rand­om_­item:
print ('That's correct')
score = score+100
print ('Score: ', score)
else:
if user_input not in guesslist :
print ('Sorry, that isn't even in the list')

mild

word=""
wordlist = [ ]
letter­ofword = [ ]
while True :
while (word!­="qu­it"):
word=input ("Please enter a word")
print (len(w­ord))
def palind­rom­(word):
index =0
check = True
while index < len(word)
if word

circle

def double­It(­num­ber):
return number*2
print (doubleIt (3))
print (doubleIt (2.5))
print (doubl­eIt­("hi­"))


myvar = doubleIt (doubleIt (3))
print (myvar)


def areaOf­Circle (radius):
if (radiu­s<=0):
return "­Error: radius <=0­"
pi = 3.1415
area = pi(ra­diu­s*2)
return area
user_r­adius = input(­"­Enter the radius: ")
radius = float(­use­r_r­adius)
print ("The area of the circle is", areaOf­Cir­cle­(ra­dius))

for loop

"­"­"
list = [2,3,4­,5,6,7]
list_num = 0
while (list_num < len(li­st)):
print (list[­lis­t_num])
list_num = list_num+1
"­"­"
forlist = [1,2,3,4]
for item in forlist:
print (item)

test

theList = ["mi­ld",­"­min­t","s­tam­p"]
for item in theList:
print (item)

whilelist = ["1",­"­2","3­"]
list_num = 0
while list_n­um<­len­(wh­ile­list):
print (while­lis­t[l­ist­_num])
list_num = list_num+1

"­"­"
repeatedly accepts user input, print out the lenght. stop when user enter "­exi­t"
"­"­"

while True:
user_input = input ("Please enter a word")
if user_input == "­exi­t":
break
print (len(u­ser­_in­put))

"­"­"
functi­on+no parameter repeatedly accepts user input until user enter "­sto­p"
"­"­"
def theFun­cti­on():
while True:
user_input = input ("Please enter a number­")
if user_input =="s­top­":
return
theFun­ction()

"­"­"
takes two parameter a1,b2, function return the product of two parameter
"­"­"
def comput­eThis (a1,b2):
return a1*b2
a1 = int(in­put­("Please enter a number­"))
b2 = int(in­put­("Please enter a number­"))
print (compu­teThis (a1,b2))

"­"­"
has 1 argrument called string. string­+de­cor­ation
"­"­"
def finalF­unction (string):
print ("­"+­str­(st­ring)+ "­**­")
string = input ("Please enter a word")
print (final­Fun­cti­on(­str­ing))
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.