Show Menu
Cheatography

Gam's Python Cheat Sheet by

Defini­tions

def printDefinitions(word):
    if word == "variable":
       print ("""A variable is the value that we can change""")

    elif word == "function":
        print ("""A function is when we define the block of code that can be reused when we call""")
    elif word == "parameter":
        print ("""parameter is the thing that we give to function in the blanket """)
    elif word == "argument": 
        print ("""argument  is the thing that we give to function in the blanket""")
    elif word == "Function call":
        print ("""Function call is when we tell the function (all the code inside) to run""")
    elif word == "String":
        print ("""String is the list of characters such as letter, mumber, etc""")
    else:
        print ("unKnown word")
    return
while True:
    user_input = input("Enter word:")
    printDefinitions(user_input)

Name

first name = input(­"what is your first name? ")  

lastname = input(­"what is your lastname? ")  


fullname = firstname + " " + lastname 


print(­"Your fullname is ")  

print (fullname)  


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


mynumber = int(le­tte­rnu­mber)-1 


if (mynumber) > len(fu­lln­ame): 

   print ("in­valid letter number, try again") 

else: 

   print (fulln­ame­[my­num­ber]) 


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


myrepeat = int(re­peat) 


if (myrepeat) > 99:  

  print ("too many letter! ") 

  else: 

print(­ful­lna­me[­myn­umb­er]­*(m­yre­peat))
`

Additional

string + string
Combine together
string + number
Crash
number + number
Addition (Math)

Guessing Game

"""
Group Members: Mind and Gam

Class: 10-05
"""
chance = 5
score = 0


mylist = ['coke','bacon', 'chicken', 'pocky', 'pepsi', 'pizza']

import random
random_item = random.choice(mylist)




while chance > 0:
    
    print ("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
    print ("Guessing Game")
    print ("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")

    
    print ("Words:", mylist)
    



    user_guese = input("Guese the word: ")
    if user_guese == random_item:
        score = score+100
        print ("That's correct! Score:", score)
        random_item = random.choice(mylist)
    else:
        chance = chance-1
        if user_guese in mylist:
            print ("Sorry, wrong choice!")
            print ("Chances Remaining:", chance)
        else:
            print ("Sorry, that is not ever in the list")
            print ("Chances Remaining:", chance)

print ("Game Over! The word was", random_item)
print ("Final Score:", score)

Maxfun­ction

#write a function that returns the largest number in a list
#name: maxlist
#argument: numlist
#return the largest value in a list

def maxlist(numlist):
    maxvalue = numlist[0]
    for item in numlist :
       if item >= maxvalue:
           maxvalue = item
    return maxvalue
numlist = [1,2,35,2654,232,5,2,5]
print(maxlist(numlist))

Max value of three

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


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

user_num1 = int(input("Enter the first number:"))
user_num2 = int(input("Enter the second number:"))
print ("The largest value is:",max2(user_num1, user_num2))

#write a function that returns the largest number of three value
#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
user_num3 = int(input("Enter the third number:"))
print ("The largest value is:",max3(user_num1, user_num2, user_num3))
 

Area of The Circle

user_r­adius = input(­"­Enter the radius of the circle­")     

radius = float(­use­r_r­adius) 

pi = 3.1415 

TheAre­aOf­The­Circle = (pi(radius*2)) 

print (TheAr­eaO­fTh­eCi­rcle) 

Rules for naming valuables

- letters
- numbers
- unders­cores
- start with letters or unders­cores only. Don't start with number
- No space
- No dashes
Valid name
mystr_1
_mystr1
invalid name
1mystr
my-str
my str

Function

str()
convert to string
int()
convert to integer
float()
convert to decimal number
print()
to show the inform­ation on the screen
len()
the length on the string
#
comment, nothing happen

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

mix the item

my str = "hello123"
numbers = [1,2,3,4,5,6]
print (numbers)

shoppinglist = ['shoes', 'bags', 'pants', 'shirts']
print (shoppinglist)

mixed = [1, 'hello', 2.5, True,False]
print (mixed)

area of circle

def areaOfCircle(r):
    if r <= 0:
        return "Error: invalid radius"
    pi = 3.1415
    area = pi * r \ ** 2
    return area 
user_radius = float(input("Enter the radius:"))

print('The area of the circlr is', areaOfCircle(user_radius))

function

 
def nameOf­Fun­cti­on(­par­ame­ter­s,a­rgument 0 or more): don't forget :
(indet­ation) print(­"­1")

ต้องcall function ก่อนนะ อิอิ
nameOf­fun­cti­on(2,4) need a value for each parameter
mynum = nameOf­Fun­cti­on(3,4)
print(­mynum)
 

Operator

<
less than
>
greater than
==
equal
!=
not equal
<=
less than or equal
>=
greater than or equal
%
modulo , find the remainder
+
plus
-
minus
*
multiply
/
divide with decimal number
//
divide no decimal number
**
power

Vocabulary

variable
value that can change
string
a list of numbers, letters, symbols
integer
the number that can do math
input
the person type the inform­ation
syntax
grammar
print
to show the inform­ation on the screen
upper
capital letter
lower
small letter
float number
decimal number
boolean
True or False

Spelling a string out in reverse code

word = input("Type in an word: ")
reverse = ""
for letter in word:
    reverse = letter + reverse
print ("Reverse: ", reverse)

Countdown Code

user_number = input("enter a number: ")

number = int(user_number)

countdown_string = ""

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

print (countdown_string)

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)

Random list

import random

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

fplist = [1.01, 1.02, 2.03, 3.04]
random_fp = random.choice(fplist)
print (fplist, random_fp)

strlist = ["hello", "hi", "good", "bye"]
random_str = random.choice(strlist)
print (strlist, random_str)

mylist = [1, 2.01, "hi"]
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)

Palindrome


reverse = ""
letter_num = 0

user_input = input("type in a word:")
user_input = str(user_input)

while letter_num < len(user_input):
    reverse = user_input[letter_num] + reverse
    letter_num = letter_num + 1
if reverse == user_input:
    print("the string is palindrome")
else:
    print ("the string is not palindrome")

area of triangle and volume of prism

def areaOfTriangle(b,h):
    area = 0.5user_baseuser_height
    return area


user_base = float(input('Enter the base of the triangle:'))
user_height = float(input('Enter the high of the triangel:'))


print('The area of triagle is',areaOfTriangle(user_base,user_height))
def areaOfTriangle(b,h):
    area = 0.5user_baseuser_height
    return area


user_base = float(input('Enter the base of the triangle:'))
user_height = float(input('Enter the height of the triangel:'))


print('The area of triagle is',areaOfTriangle(user_base,user_height))

def volumeOfPrism(b,h,l):
    volume = areaOfTriangle(b,h)*l
    return volume
user_length = float(input('Enter the length of the prism:'))
print('The volume of the prism is:',volumeOfPrism(user_base,user_height,user_length))
 

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.