Show Menu
Cheatography

Python Cheat sheet - Pat Cheat Sheet by

Function

print()
displays inform­ation that you want to show on the screen
int()
Change number to be number integer
input()
Gain inform­ation from user
str()
A list of number, letter and symbols
len()
The length of the string
#
Comment, no effect

Multip­lic­ation and Exponents

string * string
Crash!
string * number
combine that string
number * number
multiply (Math)
string ** string
Crash!
number ** number
Expone­nt(­Math)
string ** number
Crash!

Example

 
print (2.5) - Float
print (2) - Integer
print ("He­llo­") - string
print (mystr) - variable
print (mystr­,"Hi­,2,1,0) - commas

mystr = "­noo­bs"
mystr - name
"­noo­bs" - can be change

print (int(1.5)) - 1
print (int("2­")) - 2

Countdown Machine

user_number = input("What number do you want to countdown?")
number = int(user_number)
countdown_String = ' '

while number > 0:
    countdown_number = countdown_string + str(number) + ""
    number = number + 1
#print number
print (countdown_string)

Input loop

while True:
    user_input = int(input("what is your number:"))
    integer = user_input*10
    print (integer)
recieves input loop then convert to integer and * 10

Decision Making­/Co­ndi­tional statement

if 3 < 2:                      #if statement must compare 2 booleans
    print ('3 is less than 2')
elif 4 < 2:                  #can have 0 or more elif statements.
    print ('4 is less than 2')
elif 5 < 2:
    print ('5 is less than 2')
else:                        #can have 0 or 1 else statement at the end
    print ('none of above are true')
 

Vocabulary

variable
something that can be change
string
a list of characters such as number­,letter and symbols
Integer number
Whole number­/co­unting number
Float number
The number in decimal
Syntax
Gramma­r/S­tru­cture of language
Modulo
Find the remainder
Boolean
True/False

Addition

string + string
combine number
string +number
CRASH!
number + number
Addition (Math)

Naming Convention

 
Rule for giving name
- letter
- numbers
- underscore _

Valid name
- Allahu­_akbar
- _gg3
- _print

Invalid Name
- 3my = "­hi" -- cannot start with number
- first name = "­hi"
-first­-name

Math Operation

def calc(num1, num2, operation):
    if operation == "sum":
        return sum(num1, num2)

    elif operation == "product":
        return product(num1, num2)

    elif operation == "diff":
        return diff(num1, num2)

    elif operation == "div":
        return div(num1, num2)
        
    
def sum(a, b):
    return (a+b)
def product(a, b):
    return (a*b)
def diff(a, b):
    return (a-b)
def div(a, b):
    if b!=0:
        return (a//b)
    else:
        print ("error")
print (calc(10, 0, "div"))
print(calc(1,2,"sum"))
print(calc(4,2,"diff"))
print(calc(9,3,"div"))
print(calc(2,12,"product"))

for loop each item

forlist = ['hi', 'hello', 'goodbye']
for gg in forlist:
    print(gg)
 

Math

==
equal to
!=
no equal to
<
less than
>
more than
<=
less than or equal to
>=
more than or equal to
%
Modulo, Give remainder when dividing
and
or
not
*
multiply
/
divide with answer as a float 5/2 = 2.5
//
divide with answer as an interger 5//2 = 2
**
exponent 2**3 = 8
True or anything is always True
False and anything is always False

Convert to binary

user_number = ''

while user_number != "0" :
    user_number = input ("Enter a number to convert to binary")
    number = int(user_number)
    binary_string = '"'"

while (number > 0)
    remainder = number%2
    binary_String = str(remainder) + binary_String
    number + number//2

print ("Binary string is", binary_string)

Reverse

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

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

print("Reverse:", reverse)

Making list & random item

import random

intlist = [1, 2, 3, 4, 5]
random_int = random.choice(intlist)
print(intlist, random_int)

fplist = [1.1, 1.2, 1.3, 1.4]
random_fp = random.choice(fplist)
print (fplist, random_fp)

strlist = ["Allo", "Stego", "Carno", "T-rex"]
random_str = random.choice(strlist)
print(strlist, random_str)

mylist = ["Yasashii", 1.3, 2]
random_item = random.choice(mylist)
print(mylist, random_item)

myvar1 = 1
myvar2 = 2
myvar3 = 3
varlist = [myvar1, myvar2, myvar3]
random_var = random.choice(varlist)
print(varlist, random_var)

While Loop each item

wlist = [2, 4, 5, 6, 7, 8]
gg = 0
while gg < len(wlist):
    print (wlist[gg])
    gg = gg + 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.