Show Menu
Cheatography

Python Cheat Sheet by

Vocabulary

Variable
sth that can change
String
a list of characters
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

Print name

name = "time GIRARD"

print (name.upper()) > TIM GIRARD
print (name.lower()) > time girard
print (name.capitalize()) > Tim girard
print (name.title()) > Tim Girard

Convert

word = str(3) #converts 3 to a string "­3"
num = int("3.5­") #converts "­3.5­" to an integer 3
num = float(­"­3") #converts "­3" to a float 3.0
 

Addition

string­+string
combines the string together
string­+number
crash
number­+number
Math(a­ddi­tion)

Muitiply

string­*string
crash
string­*number
combines he string multiple times
number­*number
math(m­ult­iply)
string­**n­umber
crash

Random code

import ramdom

mylist = ["Dog", "Fish", "Cat", "Bear"]
counter = 0

while counter < 10 :

          random_item = random.choice (mylist)
          print (random_item)
          counter = counter + 1
 

Functions

print()
displays inform­ation on the screen
input()
receives info from the user
float()
converts a value to floating point
str()
converts

Example Code

mylists3 =[1, 'hello' , 2.5]
print (mylists)
print (mylists2)
print (mylists3)
#how to make a list with all numbers from 0-99
mynumbers = range(5)
for num in numbers:
print(num)

Naming Conver­sation

Rule for giving name
-letter
-numbers
-under­score

Valid name
-_myStr
-my3
-Hello­_there

Invalid name
-3my="h­i" --cannot start with number
-first name="h­i"
-first­-name
-first­+name

func: largenum

def max2(num1, num2):
    if num1 > num2:
        maxvalue = num1
    elif num2 > num1:
        maxvalue = num2
    return maxvalue

#function call
print(max2(92, 3))
print(max2(4, 5))

def max3(num1, num2, num3):
    maxvalue = num1
    if num2 > maxvalue:
        maxvalue = num1
    elif num3 > maxvalue:
        maxvalue = num3
    return maxvalue

#function call
print(max3(92, 5, 6))
print(max3(8, 9, 7))
print(max3(99, 40, 49))

def maxlist(list):
    maxvalue = list[0]
    for num in list:
        if num > maxvalue:
            maxvalue = num
    return maxvalue

mylist = 3, 5657, 90333, 33333333
print(maxlist(mylist))
 

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.