Show Menu
Cheatography

Python Quiz Cheat Sheet (DRAFT) by

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

Multip­lic­ation and Exponents

string * number
string­­­s­t­i­­n­­g...(­­­nu­mber)
string* string
Fail!!
number * number
Multiply
string ** string
Fail!!
number ** number
Exponent
string ** number
Fail!!

Random

import random intlist = [1,2,3­­,4­,­5­,6­­,7,­­8,9,0] random_int = random.ch­­oi­c­e­(i­­ntlist) print(­­in­t­l­is­­t,r­­an­d­o­m_int)
fplist = [4.6,3.2,­­7.7­­,6.2] random_fp = random.ch­­oi­c­e­(f­­plist) print(­­fp­l­i­st­­,ra­­nd­o­m_fp)
strlist = ['uik'­­,'­l­o­k'­­,'p­­ki­'­,­'roo'] random_str = random.ch­­oi­c­e­(s­­trlist) print(­­st­r­l­is­­t,r­­an­d­o­m_str)
mylist = [589,5­­6.3­­,­'s­­uay'] random­­_m­ylist = random.ch­­oi­c­e­(m­­ylist) print(­­my­l­i­st­­,ra­­nd­o­m­_m­­ylist)
myvar1 = 1 myvar2 = 2 myvar3 = 3 varlist = [myvar­­1,­m­y­va­­r2,­­my­var3] random_var = random.ch­­oi­c­e­(v­­arlist) print(­­va­r­l­is­­t,r­­an­d­o­m_var)

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

Capital letter

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­­­­­­­li­­­ze()) → Tim girard
print (name.t­­­­­­­­­i­­­t­­­­le()) → Tim Girard

Vocabulary

Variable
holds a value and can be change
String
a list of characters such as numbers, letters, symbols
Integer number
whole number­/co­unting number
Float number
Number in decimal
Syntax
Gramma­­r/­S­t­ru­­cture of language
Length
the length of the string
 

Countdown Number

user_n­­umber = input(­­"­P­lease enter the number­­") number = int(us­­er­_­n­umber) countd­­ow­n­_­string = "­­" while number­­>0: countd­­ow­n­_­string = countd­­ow­n­_­string + str(nu­­mber) number = number - 1 print (count­­do­w­n­_s­­tring)

Convert to Binary String

user_n­umber = input ("Please enter the number­") number = int(us­er_­number) binary­_string = "­" while (numbe­r>0): remainder = number%2 binary­_string = str(re­mai­nder) + (binar­y_s­tring) number = number//2 print ("Binary string is", binary­_st­ring)

Reverse Number

word = input(­"­enter the word") letter_num = 0 reverse = "­" while letter_num < len(word): reverse = word[l­ett­er_num] + reverse letter_num = letter_num + 1 print ("Re­verse: "­,re­verse)

Boolean Operators

not x
x and y
x or y

Convert to Binary String

user_n­­umber = input ("Please enter the number­­")
number = int(us­­er­_­n­umber)
binary­­_s­tring = "­­"
while (numbe­­r>0):
remainder = number%2
binary­­_s­tring = str(re­­ma­i­nder) + (binar­­y_­s­t­ring)
number = number//2
print ("Binary string is", binary­­_s­t­ring)

Symbol

==
equal to
!=
not equal to
<
less than
<=
less than or equal to
>
greater than
>=
greater than or equal to
+
add
-
subtract
*
multiply
/
divide and quotient is float
//
divide and quotient is integer
**
exponent
%
modulo: the remainder
 

Naming Conven­­­­­tions

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

user_r­­­adius = input(­­­"­­Enter the radius of the circle­­­") radius = float(­­­u­s­e­­r­­_r­­­adius) pi = 3.1415 answer = pi / (radius * 2) print ("The area of the circle is",­­­a­n­swer)

Addition

string + string
combine together
string + number
Fail
number + number
plus
number - number
minus

Operation

x in s
True if x is contained in s
x not in s
True if x is not contained in s
s.in­d­­ex­­­(­i­tem)
position in s of item
s[i]
ith item in s (0-based)
len(s)
length of s

Function

Print()
Show inform­­ation that you want on screen
Int()
Change number to be number integer
input()
receives info from the user
str()
converts the value to a string
float()
converts the value to a floating point
len()
The length of the string
#
comment,no effect
'''
Multi-line comment

Countdown Number

user_n­umber = input(­"­Please enter the number­") number = int(us­er_­number) countd­own­_string = "­" while number­>0: countd­own­_string = countd­own­_string + str(nu­mber) number = number - 1 print (count­dow­n_s­tring)