Show Menu
Cheatography

function

def nameOf­Fun­cti­on():
print (‘This function has no parame­ters’)
print (‘This function has no return value’)
return # no value, just exits the function
#function call
nameOf­Fun­ction()
#function with 1 parame­ter­/ar­gument
def testFu­nct­ion­(pa­ram):
print (‘This function has 1 parame­ter’)
print (param)
#function call
testFu­nction (“this is the parameter value”)
#function with 2 parameters and a return value
def functi­on3­(pa­ram1, param2):
print(­‘This function has 2 parame­ters’)
return param1 + param2 # return value
#function call and store the result in a variable
return­Value = functi­on3(2, 3)
print (retur­nValue)

Countdown

user_n­umber = input(­"What is the number­?" )
number = int(us­er_­number)
countd­own­_string =' '
while number > 0:
countd­own­_string = countd­own­_string + str(nu­mber)
number = number-1
print(­cou­ntd­own­_st­ring)

Countdown

user_n­umber = input(­"What is the number­?" )
number = int(us­er_­number)
countd­own­_string =' '
while number > 0:
countd­own­_string = countd­own­_string + str(nu­mber)
number = number-1
print(­cou­ntd­own­_st­ring)

one word per line

mystr = input(­" Please enter your word")
letter_num = 0
while letter_num < len(my­str):
print (mystr­[le­tte­r_num])
letter_num = letter_num + 1

one word per line

mystr = input(­" Please enter your word")
letter_num = 0
while letter_num < len(my­str):
print (mystr­[le­tte­r_num])
letter_num = letter_num + 1

Function of name

mystr = "­hello THERE"
print (mystr.up­per()) > HELLO THERE
print (mystr.lo­wer()) > hello there
print (mystr.ca­pit­ali­ze()) > Hello there
print (mystr.ti­tle()) > Hello There

asking name

firstname = input(­"What is your fisrt name?")
lastname = input(­"What is your last name?")
fullname = firstname + " " + lastname
print(­ful­lname)
letter­number = input(­"What is the letter of number­?")
letter­number = int(le­tte­rnu­mber)
if letter­num­ber­>le­n(f­ull­name):
print(­"­Invalid letter number, try again")
else:
print(­ful­lna­me[­let­ter­num­ber])
times = input(­"How many times to print the letter­?")
times = int(times)
if times>100:
print(­"Too many letters to print")
else:
print(­ful­lna­me[­let­ter­num­ber­]*t­imes)
Result
What is your fisrt name? Pear
What is your last name? Tan
Pear Tan
What is the letter of number? 4
r
How many times to print the letter? 12
rrrrrr­rrrrrr

List

myself= "­hel­lo1­23"

numbers = [1,2,3­,4,5,6]
print(­num­bers)

shoppi­nglist = ['shoe­','­bag­s',­'pa­nts­','­shirt']
print(­sho­ppi­nglist)

mixed=­[1,­'He­llo­',2.5, True, False]
print(­mixed)

letter_num = 0

while letter_num < len(my­str):
print (mystr­[le­tte­r_num])
letter_num = letter_num + 1

for mylett­eri­saw­esome in mystr:
print(­myl­ett­eri­saw­esome)

for tientien in shoppi­nglist:
print(­opal)
shoppi­nglist. append­('t­ies')
print(­sho­ppi­nglist)


out = 0

for mrtim in shoppi­nglist:
out=out + 1
print(­mrtim)
print (out)

largelist = range(100)

for num in largelist:
print(num)

Symbol n vocab

variable-A value or thing that can be changed
string-A list of character such as letter or symbol
modulo­-Find the remainder
**-exp­onent
/-divide and quotient (result) is float
//-divide and quotient (result) is integer
!= - not equal to
<= - less than or eqaul to
>= - more than or equal to
True or anything
Always true
False and anything
False

for loop print 0 01 012 0123 01234

mystring = "­"
for num in range(5):
mystring = mystring + str(num)
print (myste­ring)
 

print thing in list while loop

my list = [1,2,3­,4,5]
num = 0
while num<len(my list):
print(­myl­ist­[num])
num=num+1

convert binary

user_n­umber = input(­"­Please enter a number­")
number = int(us­er_­number)
binary­_string =''
while (number > 0):
remainder= number%2
binary­_string = str(re­mai­nder) + binary­_string
number= number//2
print(­"­Binary string is", binary­_st­ring)
Result
Please enter a number 36
Binary string is 100100

range

#creates a list of numbers from 0 to the specified
number
numberlist = range(5)
# is the same as creating the following list
number­list2 = [0, 1, 2, 3, 4]
for num in range(­100):
print (num) # prints all numbers from 0 – 99
for num in range(5, 50):
print(num) #prints all numbers from 5 - 49

palindrome

def palind­rom­e(w­ord):
letter_num = 0
reverse = "­"
for letter_num in word:
reverse = letter_num + reverse
if word == reverse:
return True
else:
return False
while True:
user_word = input(­"­Please enter a word: ")
if user_word != "­qui­t":
print(­"This word has"­,le­n(u­ser­_wo­rd)­,"le­tte­rs")
if user_word == "­qui­t":
break
if palind­rom­e(u­ser­_word) == True:
print(­use­r_w­ord­,"is palind­rom­e")
else:
print(­use­r_w­ord­,"is not palind­rom­e")

0 01 012 0123 01234

expected out put
0 01 012 0123 01234
mystring = "­"
count = 0
while count<5:
mystring = mystri­ng+­str­(count)
print(­mys­tring)
count = count+1

function multip­lic­ation table 5'1-5 5'2-10

def muHipl­ica­tio­nTa­ble():
user_input = input(­"­enter a number­:")
num = int(us­er_­input)
count = 1
while count <=10:
print(­num­,""­,co­unt­,"=",numcount)
count = count + 1

what is the output of the following code

x = false
print (x and True or 1 == 1
ans- true

y = true
print (not y or 2<3)
and-true

area of triangle and prism

def areaOf­Tri­ang­le(­b,h):
if user_b­ase­<=0:
return "­Error: invalid radius­"
if user_h­eig­ht<=0:
return "­Error: invalid radius­"
area = 0.5 b h
return area
user_base =float­(in­put­('Enter the base of the triang­le:'))
user_h­eight = float(­inp­ut(­'Enter the height of the triangle: '))
print ('The area of the triangle is',ar­eaO­fTr­ian­gle­(us­er_­bas­e,u­ser­_he­ight))

def volume­OfP­ris­m(b­,h,l):
volume = bhl
return volume
user_l­ength = float(­inp­ut(­'Enter the length of the prism:'))
print('The volume of the prism is', volume­OfP­ris­m(u­ser­_ba­se,­use­r_h­eig­ht,­use­r_l­ength))

area of circle

def areaOf­Circle (user_­rad­ius):
if user_r­adi­us<=0:
return "­Error: invalid radius­"
pi = 3.1415
area = pi(user_­radius*2)
return area
user_r­adius = float(­inp­ut(­"­Enter the radius: "))
print('The area of the circle is', areaOf­Cir­cle­(us­er_­radius)

Rules of naming var

# letters
# numbers
# underscore (_)
# can either start with letter or under­­­scores ONLY
# no space
Exam­­ple
Hello_­­­there
me2
_mynumber
Invalid names
# 3my =cannot start with number
# last name = no spaces allowed
# last­-­­­­name = dashes are not accepted

multip­lic­ation

string * number - Repeat those thing for the number of time
string * string - Crash!
number * number - Multiply like in math
string ** number - Crash!
number ** number - Exponent in Math
number ** string - Crash!
sring + string - combine those strings together
string + number - program will be crash
 

print all even num 1-100 while loop

num = 0
while num<100
num=num+2
print(num)

fibonacci from 0-50

num1 = 0
num2 = 0
fibonacci = num1+num2
myoutput = "­0,1­"
while fibonacci < 50:
myoutput = myouput + "­," + str(fi­bon­acci)
num1=num2
num2 = fibonacci
fibonacci = num1+ num2
print(my output)
0,1,1,­2,3­,5,­8,1­3,...

ask user and is num divisible by3

num = int(in­put­(in­put­("enter a number­"))
remainder = num % 3
if remainder == 0:
print (num, "is divisible by 3")
else:
print (num, "­isn't divisible by 3")

ยากๆอะ

write a program that repeatly receive positive int from user enters a negative integer exit the loop print how many of numbers entered were even and odd
evenco­unt=0
oddcount=0
while True:
num = int(in­put­("enter a positive intege­r"))
if num<0:
print(­"even number­:",e­ven­count)
print(­"odd number­s:",­odd­COunt)
break
else:
if(num­%2)==0
evencount - evenco­unt+1
else:
oddcount = oddcount + 1

Reverse

word = input(­"­Please enter a word to reverse: ")
letter_num = 0
reverse = "­"
while letter_num <le­n(w­ord):
reverse = word[l­ett­er_num] + reverse
letter_num = letter_num + 1
print(­"­Rev­erse: "­,re­verse)

guess game

import random
mylist = ['lion­','­che­eta­h',­'pa­nth­er'­,'c­oug­ar'­,'l­eop­ard']
random­_item = random.ch­oic­e(m­ylist)
print(­ran­dom­_item)
print(­myl­ist[0])
user_guess = input(­"­Guess a word: ")
if user_guess == random­_item:
print(­"­Cor­rec­t")
else:
if user_guess in mylist:
print(­"Yes, it is in the list")
else:
print(­"No, it is not in the list")

guess game

import random
mylist = ['lion­','­che­eta­h',­'pa­nth­er'­,'c­oug­ar'­,'l­eop­ard']
random­_item = random.ch­oic­e(m­ylist)
print(­ran­dom­_item)
print(­myl­ist[0])
user_guess = input(­"­Guess a word: ")
if user_guess == random­_item:
print(­"­Cor­rec­t")
else:
if user_guess in mylist:
print(­"Yes, it is in the list")
else:
print(­"No, it is not in the list")

def of word

def printD­efi­nit­ion­(word):
# write a definition in your own words for the folllowing words:
# use multi-line strings to print the definition
#variable
if word == "­var­iab­le":
print(­"­"­"A variable is thing that can be change­d""")
elif word == "­fun­cti­on":
#function
print (""" A function is a thing that reuse block or quote. "­"­")
elif word == "­par­ame­ter­":
#parameter
print(­"­"­"A parameter is thing inside blacket of function "­"­")
elif word == "­agu­men­t":
#argument
print(­"­"­" A argument is the same thing as parameter. It is thinfg inside blacket f function "­"­")
elif word == "­fun­ction call":
#function call
print(­"­"­"­Fun­ction is the thing make fuction run.""")
elif word == "­str­ing­":
#string
print(­"­"­" A string is a list of charac­ter­"­"­")
else:
print(­"­unknown word")
while True:
word = input ("Enter the word")
printD­efi­nit­ion­(word)
Result
Enter the wordva­riable
A variable is thing that can be changed
Enter the wordfu­nction
A function is a thing that reuse block or quote.
Enter the wordag­ument
A argument is the same thing as parameter. It is thinfg inside blacket f function
Enter the wordfu­nction call
Function is the thing make fuction run.
Enter the wordstring
A string is a list of character
Enter the wordpear
unknown word
 

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 mmildmilds