Show Menu
Cheatography

cheat sheet tientien Cheat Sheet by

Function

print()
Show the inform­ation that you want on screen
int()
Change number to be number integer
float()
Change number to be decimal number
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 Exponent

string­*number
combine that string
string­*string
CRASH
number­*number
Multiply
string­**s­tring
CRASH
number­**n­umber
Exponent
string­**n­umber
CRASH

convert to binary

user_n­umber= ' '

while user_n­umber ! = '0'
user_n­umber = input ("Enter a number to convert to binary­")
        number­=in­t(u­ser­_nu­mber)
        binary­_st­ring= ' '

        while (numbe­r>0):
                 remainder = number%2
                 binary­_st­rin­g=s­tr(­rem­ain­der­)+b­ina­ry_­string
                 number­=nu­mber//2
print(­"­Binary string is ,binar­y_s­tring)

Sort word per line

mystr = "Hello"

letter_num = 0

while letter_num<len(mystr):
print(mystr[letter_num])
letter_num=letter_num+1


H
E
L
L
O

Print Name

name="tim GIRARD"

print(name.upper())

area of circle

def areaOfcircle(radius):
    if radius<=0:
        return "Error invalid radius"
    pi=3.1415

    area = piradius*2
    return area
user_radius = float(input("Enter the radius: "))

print ("The area of the circle is",areaOfcircle(user_radius))
Enter the radius: 0
The area of the circle is Error invalid radius
>>>
Enter the radius: 7
The area of the circle is 153.9335

doublet

def doublet(number):
    return number * 2

print (doublet(3))


print (doublet(doublet(4)))
6
16

function call

def printDefinitions(word):
     if   word == "variable":
        print ("""A variable is the value that can be changed""")
     elif word == "function":
        print ("""A function is blog of code that we can reuse""")
     elif word == "parameter":
        print ("""A parameter is something is given in the function""")
     elif word == "arguement":
        print ("""A arguement is something is given in the function""")
     elif word == "function call":
        print ("""A function call is something that make the function runs""")
     elif word == "string": 
        print ("""A string is character such as symbol, number""")
     else:
         print("unknown word")
     return

while True:
     user_input=input("Enter word:")
     printDefinitions(user_input)
Enter word:v­ariable
A variable is the value that can be changed
Enter word:f­unction
A function is blog of code that we can reuse
Enter word:hello
unknown word
Enter word:

Maxlist

def maxlist[list]
    maxvalue = list[0]

    for item in list:
        if item > maxvalue:
            maxvalue = item

    return maxvalue
mylist = [1,2,3,4,55,66,777,0,1]
print(maxlist(mylist))
777

areaOf­Tri­ang­le,­vol­ume­OfPrism

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

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

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


def volumeOfPrism(b,h,l):
    volume = areaOfTriangle(b,h)*l
    return(volume)

user_base = float(input('Enter the base of the prism'))
user_height = float(input('Enter the height of the prism'))
user_length = float(input('Enter the length of the prism'))

print('The volume of the Prism is',volumeOfPrism(user_base,user_height,user_length))
Enter the base of the triangle6
Enter the height of the triangle6
The area of the triangle is 18.0
Enter the base of the prism6
Enter the height of the prism6
Enter the length of the prism6
The volume of the Prism is 108.0
 

Addition

string­+string
Combine together
string­+number
CRASH
number­+number
Addition

Vocabulary

variable
Hold a value and can be changed
string
A list of character such as number­,letter and symbol
integer number
whole number/ counting number
float number
the number in decimal
syntax
Gramma­r/S­tru­cture of language
Boolean
True/False

Math

==
equal to
!=
no equal to
<
less than
>
more than
<=
less than or equal to
>=
more than or equal to
%
Modulo, Find the remainder

Countdown Machine

user_n­umb­er=­inp­ut(­"What number do you want to count down?")
numer=­int­(us­er_­number)
countd­own­_st­ring=' '

while number > 0
countd­own­_nu­mber= countd­own­_st­ring+
str(nu­mber) + " "
number­=nu­mber-1
#print­(nu­mber)

print(countdown_string)

Naming Cinvention

Rule for giving name
-letter
-numbers
-under­score
Valid name
-myStr
-my3
-Hello­_there
Invalid name
-3my="h­i" -- cannot start with number
-first number­='hi'
-first­-name
-first­+name

random stuff

import random

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

fplist=[1.1,2.2,3.3,4.4,5.5]
random_fp= random.choice(fplist)
print(fplist,random_fp)

strlist=['opal','love','tientien']
random_str= random.choice(strlist)
print(strlist,random_str)

mylist=['tientien',1,5.5]
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)
[1, 2, 3, 4, 5] 4
[1.1, 2.2, 3.3, 4.4, 5.5] 4.4
['opal', 'love', 'tient­ien'] opal
['tien­tien', 1, 5.5] tientien
[1, 2, 3] 3
>>>

My print

def myprint(text): 
    print(""+str(text)+"")

    return

myprint (1)
myprint ("hello")
myprint (2.5)

def myprintnew(text, decoration):
    print(decoration + str(text) + decoration)

    return

myprintnew(1,"+++")
myprintnew ('hello','=-=-=-=-=-=-=-=')

reverse

reverse = ""
letter_num=0

user_input = input("type in a number")

while  letter_num<len(user_input):
    reverse = user_input[letter_num]+reverse
    letter_num = letter_num+1

if reverse == user_input:
    print ('parindorm')
else:
    print ('not parindorm')
type in a number­12321
parindorm
>>>
type in a number
3
parindorm
>>>
type in a number13
not parindorm
>>>

maxvalue

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


print (max2(4,5))
print (max2(33,5))


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

    if num2 > maxvalue:
        maxvalue = num2
    if num3 > maxvalue:
        maxvalue = num3
    
    return maxvalue
print (max3(1,2,3))
5
33
3
 

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.