Show Menu

Python Cheat Sheet by

python

Function

str()
make the number or symbol
input()
to get infomation from the user
print()
to show some infomation that in the code
int()
the integers
len()
length of the string
#
comment
"­"­"........................"­"­"­
multiple line comment

List2

mylist = [1,2,3,4,5]
mulist2 = ['hi', 'hello', 'anything']
mylist3 = [1, 'hello', 2.5

print (mylist)
print (mylist2)
print (mylist3)

number = range(100)
for nm in number:
    print (num)

Name strip

firstname = input("what is your first name? ")
lastname = input("what is your lastname? ")

fullname = firstname + " " + lastname

print("Your fullname is ")
print (fullname)

letternumber = input("what is letter number? ")

mynumber = int(letternumber)-1

if (mynumber) > len(fullname):
    print ("invalid letter number, try again")
else:
    print (fullname[mynumber])

    repeat = input("how many times you want to print the letter? ")

    myrepeat = int(repeat)

    if (myrepeat) > 99:
        print ("too many letter! ")
    else:
        print(fullname[mynumber]*(myrepeat))

Import random

import random
chance = 3
score = 0

mylist = ['Hack', 'ToeyD.', 'Patter','Tim','Lily']
random_item = random.choice(mylist)
while chance > 0:

    print (mylist)
    
    print ("Chances Remaining =",chance)
    guess = input("Guess a word from the above :")
    
    if guess == random_item:
        score = score + 100
        print ("That's correct!","The score is :",score)
        random_item = random.choice(mylist)
    else:
        print ("Sorry, wrong choice!")
        chance = chance - 1
    if guess in mylist:
        print ("")
    else:
        print ("Sorry,that is not even in the list!")
    if chance == 0:
        print ("Game Over! The word was",random_item)
        print ("Final score: ",score)

write allthe­outcome

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


OUTCOMEIS
0
01
012
0123
01234

areaof­Ellipse

def areaofEllipse(radius1, radius2):
    pi = 3.142
    area = piradius1radius2
    return area

ra1 = int(input("put in the radius 1"))
ra2 = int(input("put in the radius 2"))
print (areaofEllipse(ra1, ra2))


OUTPUT
put in the radius 14
put in the radius 25
62.839999999999996

DEFINATION

def printDefinition(word):
    if word == "variable":
        print ("""
        A variable is the the thing that can be changed.
        """)
    elif word == "parameter":
        print ("""
        A parameter is the limiting factor
        """)
    elif word == "argument":
        print ("""
        An argument is the identifier that you give to function
        """)
    elif word == "string":
        print ("""
        A string is something that can be repeated by the number.
        """)
    elif word == "function call":
        print ("""
        A function call is the word you use to reuse the function.
        """)
    else:
        print ("unknown word")

while True:
    user_input = input("Please type the word :")
    printDefinition(user_input)


OUTPUT
Please type the word :variable

        A variable is the the thing that can be changed.
 

Vocab

Variable
The unknow that can be change
Float
Decimal number
String
Number, Letter, and symble
Syntax
The programing language
Boolean
True or False
Paramiter
some thing you give to function

Reverse

reverse = ""
letter_num = 0

word = input('type in a word: ')
"""
while letter_num < len(word):
    reverse = word[letter_num] + reverse
    letter_num = letter_num + 1
"""
for letter in word:
    reverse = letter + reverse

print ('reverse: ',reverse)

Reverse2

hello = "hello there!"

out = ""

for letter in hello:
     out = letter+out


print(out)

Number to Binary

user_number = input("Enter number to convert to binary : ")

number = int(user_number)

binary_string = ''
while (number > 0):
    remainder = number % 2
    binary_string = str(remainder) + str(binary_string)
    number = number // 2

print ("Binary string is",binary_string)

Countdown

user_number = input("Please enter a number: ")

number = int(user_number)

countdown_string = ''

while number > 0:
    countdown_string = countdown_string + str(number)
    number = number - 1
print (countdown_string)

Find Area of Circle

def aofc(r):
    if r <= 0:
        return "Error: radius <=0"
    pi = 3.1415
    area = pi r * 2
    return area
user_radius = input("enter the radius: ")
radius = float(user_radius)
print("The area of the circle is", aofc(radius))

Max value

# write a function that returns the largest of two values
# name: max2
# arguments: num1, num2
# return: the largest value


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

    return maxvalue

print(max2(10,9))
print(max2(1,9))


# write a function that returns the largest of three values
# name: max3
# arguments: num1, num2, num3
# return: the largest value


def max3 (num1, num2, num3):
    maxvalue = num1

    if num2 > maxvalue:
        maxvalue = num2
        
    if num3 > maxvalue:
        maxvalue = num3
        
    return maxvalue

print(max3(3,5,9))

Decision Making­/Co­ndi­tional Statem­ents:

if 3 < 2: #if statement must compare two 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 the above are True')
 

MATH

string­+string
Combine
string - number
CRASH
number + number
ADDING
string­*number
combine
string­*string
CRASH
number­*number
Multiply
string­**s­tring
CRASH
string­**n­umber
CRASH
number­%number
remainder
number­**n­umber
Exponent
=
Equal
==
Compare
//
Divide anser in Interger
/
Divide anser in Floot

Symbol

==
Equal to...
!=
Not equal
<
Less than
>
More than
<=
Less than or equal
>=
More than or equal
%
Modulo­/Find remainder

List

shoplist = ['son', 'goo', 'maaa', 'laaaa']
print(shoplist[2])
"""
item_number = 0
while item_number < len(shoplist):
    print ("list item:", shoplist[item_number])
    item_number = item_number + 1
"""
out = 0
for item in shoplist:
    out = out + 1
        #print ('list item:',item)
print (out)

Number to Hex

user_number = input("please enter a number: ")
number = int(user_number)
hex_string = ' '
while (number > 0):
    remaider = number % 16
    if remaider == 10:
        remaider = 'A'
    elif remaider == 11:
            remaider = 'B'
    elif remaider == 12:
            remaider = 'C'
    elif remaider == 13:
            remaider = 'D'
    elif remaider == 14:
            remaider = 'E'
    elif remaider == 15:
            remaider = 'F'
                            
    hex_string = str(remaider) + str(hex_string)
    number = number // 16
print ("Hexadecimal string is 0x", hex_string)

Import list

import random

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

fplist = [float(1), float(2), float(3), float(4), float(5)]
random_fp = random.choice(fplist)
print (fplist,random_fp)

strlist = [str('son'), str('hack'), str('pom'), str('phon')]
random_str = random.choice(strlist)
print (strlist,random_str)

mylist = ['son', 'jan', 'feb', 'mar']
random_my = random.choice(mylist)
print (mylist,random_my)

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

Palindrome

'''
Surawut Sartpant
Son 1002
Python Assignment-Palindrome
'''
def isPalindrome(word):
    numlen = 0
    while numlen < long // 2 + 1:
        if word[numlen] != word[-numlen-1]:
            return False
        elif word[numlen] == 1:
            return True
        numlen += 1
    else:
        return True
        
                
while True:
    word = input(" Insert your word: ")
    long = len(word)

    if word == "quit":
        break
    else:
        print (long)
 
    numlen = 0

    if isPalindrome(word) == True:
        print (word,"it is a palindrome")
    else:
        print (word,"it is not a palindrom")

"""
output
 Insert your word: klk
3
klk it is a palindrome
 Insert your word: quit
 """

Download the Python Cheat Sheet

4 Pages
//media.cheatography.com/storage/thumb/sonnylnw-muids_python.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

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.

          More Cheat Sheets by Sonnylnw MUIDS