Show Menu
Cheatography

A Python Cheat Sheet

0,01,0­­12­,­0­12­­3,01234

mystring = ''"
count = 0
while count <= 4:
mystring = mystring + str(count)
print (mystring)
count = count + 1

mystring = "­"
for num in range(5):
mystring = mystring + str(num)
print (mystring)

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
input()
Gain inform­­­ation from user
str()
A list of number, letter and symbols
len()
The length of the string
#
Comment, no effect

Vocabulary

Variable
Hold a value and can be change
String
A list of character such as number, letter and symbols
Integer number
Whole number­­/c­o­u­nting number
Float number
The number in decimal
Syntax
Gramma­­r/­S­t­ru­­cture of lauguage
Modulo
Find the remainder
Boolean
True/False

==

myboolean = 2 == 3

if myboolean:
print ("tr­uth­")
else:
print ("li­es")

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

print (int(1.5)) → 1
print (int(“2”)) → 2
print (float(1)) → 1.0 anything to a float

Modulo­­/R­e­m­ainder %
print (4%2) → 0
print (30%7) → 2

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))

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

Definition

def printD­efi­nit­ion­(word):
if word == "­var­iab­le":
print ("""
A variable is the the thing that can be changed.
"­"­")
elif word == "­par­ame­ter­":
print ("""
A parameter is the limiting factor
"­"­")
elif word == "­arg­ume­nt":
print ("""
An argument is the identifier that you give to function
"­"­")
elif word == "­str­ing­":
print ("""
A string is something that can be repeated by the number.
"­"­")
elif word == "­fun­ction call":
print ("""
A function call is the word you use to reuse the function.
"­"­")
else:
print ("un­known word")

while True:
user_input = input(­"­Please type the word :")
printD­efi­nit­ion­(us­er_­input)
 

Fibonacci

num1 = 0
num2 = 1
fibonacci = num1 + num2
output = "­0,1­"
while fibonacci < 50:
output = output +","+ str(fi­bon­acci)
num1 = num2
num2 = fibonacci
fibonacci = num1 + num2
print (output)

1 * 1 = 1

def multip­lic­ati­onT­abl­e(num):

multi = 0
while multi < 10:
multi = multi + 1
user_o­utput = num*multi
print ( num,"*",­mul­ti,­"­=",u­ser­_ou­tput)


user_num = int(in­put­("Enter the number: "))
multip­lic­ati­onT­abl­e(u­ser­_num)

Hex

user_n­umber = input(­"­Enter number to convert to hex : ")

number = int(us­er_­number)

hex_string = ''
while (number > 0):
remainder = number % 16
if remainder == 10:
remainder = 'A'
elif remainder == 11:
remainder = 'B'
elif remainder == 12:
remainder = 'C'
elif remainder == 13:
remainder = 'D'
elif remainder == 14:
remainder = 'E'
elif remainder == 15:
remainder = 'F'

hex_string = str(re­mai­nder) + str(he­x_s­tring)
number = number // 16

print ("Hex string is 0x",­hex­_st­ring)

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)

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

Boolean

False or True
True
False and True
False
True and False
False
True and True
True
False or False
False

Multip­­­l­i­c­­ation and Exponents

string * number
Combine that string
string* string
CRASH!
number * number
Multiply (Math)
string ** string
CRASH!
number ** number
Exponent (Math)
string ** number
CRASH!

Addition

string + string
Combine together
string + number
CRASH!
number + number
Addition (Math)

Math

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

Even,Odd number

even = 0
odd = 0
while True:
user_num = int(in­put­("Enter the number :"))
if user_num >= 0:
if user_num % 2 == 0:
even = even + 1
else:
odd = odd + 1
else:
print ("Even number :", even)
print ("Odd number :", odd)
break

For loop word

For word in mylist:
print (word)

Guess

import random
chance = 3
score = 0

mylist = ['Hack', 'ToeyD.', 'Patte­r',­'Ti­m',­'Lily']
random­_item = random.ch­oic­e(m­ylist)
while chance > 0:

print (mylist)

print ("Ch­ances Remaining =",c­hance)
guess = input(­"­Guess a word from the above :")

if guess == random­_item:
score = score + 100
print ("That's correc­t!",­"The score is :",s­core)
random­_item = random.ch­oic­e(m­ylist)
else:
print ("Sorry, wrong choice­!")
chance = chance - 1
if guess in mylist:
print ("")
else:
print ("So­rry­,that is not even in the list!")
if chance == 0:
print ("Game Over! The word was"­,ra­ndo­m_item)
print ("Final score: "­,score)

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

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)

Countdown Machine

user_n­­umber = input(­­"What number do you want to count down? ")
number = int(us­­er­_­n­umber)
countd­­ow­n­_­string = ' '

while number > 0:
countd­­ow­n­_­number = countd­­ow­n­_­string + str(nu­­mber) + " "
number = number - 1
#print­­(n­u­mber)

print (count­­do­w­n­_s­­tring)

Convert to binary

user_n­­umber = ' '

while user_n­­umber != ' 0 ' :
user_n­­umber = input ("Enter a number to convert to binary­­")
number = int(us­­er­_­n­umber)
binary­­_s­tring = ' '

while (number > 0):
remainder = number%2
binary­­_s­tring = str(re­­ma­i­n­der)+ binary­­_s­tring
number = number//2

print ("Binary string is", binary­­_s­t­ring)

Reverse Word

while True:
word = input(­­"­P­lease enter a word")
index = 0
reverse = ' '

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

print ("Re­­verse: ", reverse)
 

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.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter