Show Menu
Cheatography

python cheat sheet mildjung Cheat Sheet (DRAFT) by

my cheat sheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Function

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

Multip­lic­ation and Explonants

string * number
combine that string
string * string
CRASH
number * number
multiply (math)
string ** string
CRASH
number ** number
exponent (math)
string ** number
CRASH

Convert to binary

user_n­umber = ' '

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

while (number > 0):
remainder = number%2
binary _string = str(re­mai­nder)+ binary _string
number = number//2

print ("Binary string is", binary _string)

Sort fruit list

fruits = [] #an empty list

for number in range(5):
user_fruit = input(­­"­P­lease enter a fruit")
fruits.ap­­pe­n­d­(u­­ser­­_f­ruit)

print ("Size of fruit list is", len(fr­­uits))

fruits.sort()

for fruit in fruits:
print ("Fruit: ", fruit)

Selecting largest value

def max2 (num1,­num2):
if num1>num2:
return num1
if num1<num2:
return num2

def max3 (num1,­num­2,n­um3):
if num1>num2 and num1>num3:
return num1
if num2>num1 and num2>num3:
return num2
if num3>num1 and num3>num2:
return num3
num1=i­npu­t("Enter your num1:")
num2=i­npu­t("Enter your num2:")
num3=i­npu­t("Enter your num3:")

print(­"the largest number of max3 is:"­,ma­x3(­num­1,n­um2­,num3))
print(­"the largest number of max2 is:"­,ma­x2(­num­1,n­um2))
 

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
Floating number
the number in decimal
Syntax
grammar of python
Modulo
find the remainder
Boolean
true/false

Addition

string + string
combine together
string + number
CRASH
number + number
addition (math)

Example

Print (2) – integer
Print (2.5) – floating point
Print (“Hello”) – string
Print (mystr) – variable
Print (mystr ,”H i”, 2,1.0) -- commas

mystr = “Hi”
mystr ← name
“Hi” ← value can change

Countdown machine

user_number = input( "What number do you want to count down? ")
number = int(user_ number)
countdown _string = ' '

while number > 0:
countdown _number = countdown _string + str(number) + " "
number = number - 1
#print (number)

print (countdown_string)

Sort word per line

mystr = "­­He­l­l­o"

letter_num = 0

while letter_num < len(my­­str):
print (mystr­­[l­e­t­te­­r_num])
letter_num = letter_num + 1
H
e
l
l
o

Print name

name = "tim GIRARD­­"

print (name.u­­p­p­er()) → TIM GIRARD
print (name.l­­o­w­er()) → tim girard
print (name.c­­a­p­i­ta­­lize()) → Tim girard
print (name.t­­i­t­le()) → Tim Girard
 

Math

==
equal to
!=
not equal to
<
less than
>
more than
<=
less than or equal to
>=
more than or equal to
%
modulo, find the remainder

Reverse word

while True:
word = input( " Please enter a word")
index = 0
reverse = ' '

while int(index) < len(word):
reverse = word[i ndex] + (reverse)
index = int(index) + 1

print ("Re verse: ", reverse)

Naming convention

Rule for giving name
- letter
- numbers
- underscore _

Valid name
- _myStr
- my3
- Hello_ there

Invalid name
- 3my=”hi” -- cannot start with number
- first name=”hi”
- first-name
- first+name

Area of circle

"­­"­"
Python Intro Assignment #2
name
student number
"­­"­"

#Ask the user for a radius of a circle
user_r­­adius = input(­­"What is a radius of a circle­­?")

#Convert the given radius to a floating point
radius = float(­­us­e­r­_r­­adius)

#Make a variable called pi
pi = float(­­3.1­415)

#Calculate the area of the circle using exponents
area = pi(ra­­diu­­s*2)

#Display the area of the circle to the user
print ("The area of the circle is", area