Show Menu
Cheatography

Python Cheat Sheet AimPor Cheat Sheet by

Vocabulary

Variable
hold a value and can be change
String
a list of character such as number, letter and symbols
Integer Number
whole number/ counting number
float number
the number in decimal
syntax
grammar/ structure of language
modulo
find the remainder
boolean
true/ flase

Addition

string + string
combine together
string + number
CRASH!
number + number
Addition (math)

Multip­lic­ation and exponents

string* number
combine that string
string* string
CRASH!
number* number
Multiply (Math)
string ** string
CRASH!
number ** number
Exponents (Math)
string ** number
CRASH!

if/ elif/ else

def printd­­ef­i­n­ition (word):

if word == "­­fu­n­c­ti­­on":
print(­­"­"­­"
function lets you use code
"­­"­")

elif word== "­­st­r­i­ng­­":
print(­­"­"­­"
string is list of character
"­­"­")
else:
print ("un­­known word")
user_word = input ("Enter a word to define: ")
printd­­ef­i­n­it­­ion­­(u­s­e­r_­­word)

for loop

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

circle

def double­­It­(­n­um­­ber):
return number*2
print (doubleIt (3))
print (doubleIt (2.5))
print (doubl­­eI­t­(­"­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(­­us­e­r­_r­­adius)
print ("The area of the circle is", areaOf­­Ci­r­c­le­­(ra­­dius))
 

Function

print()
show inform­ation that you want on the screen
int()
change number to be number integer
float()
change number too be decimal number
Input()
gain inform­ation from user
str()
a list of number, letter and symbols
len()
the length of the string
#
comment, no effect

function

_varl = 1
_varl = 3
_varl + 100
print(_varl)

def bacon():     #use the keyword def and end with a colon:
    print("hello it's bacon")
    return

bacon()
bacon()
bacon()
bacon()
bacon()

def bacon():
    print("hello it's bacon")
    print("line 2")
    print("line 3")
    print("line 4")
    print("line 5")
    print("line 6")
    print("line 7")
    print("line 8")
    return

bacon()
bacon()
bacon()

def myprint(text): #single parameter
    print ("" + str(text) + "")
    return

myprint(1)
myprint("hello")
myprint(1+2)

def myprint2(text, decoration):
    print(decoration + str(text) + decoration)
    return

myprint2(12312321312, "++++")
myprint2("hello", "<<>>")

def doubleIt(number):

    return number * 2

myvar = 2
myvarDouble = doubleIt(myvar)
print(doubleIt("hello"))

myvar = doubleIt(doubleIt(3)) #same as doubleIt(6)
print (myvar)

def sumIt(num1, num2):
    return num1 + num2

print(sumIt("a", "b"))
print(sumIt(2,3))

def areaOfCircle (radius):
    pi = 3.1415
    area = pi  radius*2 
    return area

user_radius = input('Enter the radius: ')
radius = float(user_radius)
print("The area of the circle is", areaOfCircle(radius))

def areaOfCircle(r):
    if r <= 0:
        return "Error: radius <= 0"

    pi = 3.1415
    area = pi  r * 2
    return area

user_radius = input("Enter the radius: ")
radius = float(user_radius)

print ("The area of the circle is", areaOfCircle(radius))
 

Math

= =
equal to
!=
no equal to
<
less than
>
more than
<=
less than or equal to
>=
more than or equal to
%
Modulo, find the remainder

list

import random
intlist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
random_int = random.choice (intlist)
print(­­­i­n­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­­­s­t­,­­r­­an­­­do­m­­­_str)

mylist = ["ad­­­a­m­"­­,­"­­­mi­l­­­d",­­"­­l­­o­v­e­­­ada­­­m­"­,­"­­l­e­­vi­­­n­e",­­­"­3­­"­,­"­4.6­­",­­­42­4­­­,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­­­s­t­,­­r­­an­­­do­m­­­_var)

shopping list

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

print (shopp­­­i­n­g­­list)

list_num = 0

while list_num < len(sh­­­o­p­p­­i­­ng­­­list):
print ("Li­­­s­t­:­­"­­­,sh­­­o­p­p­­i­­ng­­­li­s­­­t[­­l­i­­st­­­_­num])
list_num = list_num + 1

for item in shoppi­­­n­g­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(­­my­l­i­st[0])
counter = 0
while counter < 10:
random­­_item = random.ch­­oi­c­e­(m­­ylist)
print (rando­­m_­item)
counter = counter + 1
 

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.