Function
print() |
Show information that you want on the screen |
int() |
Change number to be number integer |
float() |
Change number to be decimal number |
input() |
Gain information from user |
str() |
A list of number, letter and symbols |
len() |
The length of the string |
# |
Comment, no effect |
Vocabulary
Variable |
Hold a value and can be change |
String |
A list of character such as number, letter and symbols |
Integer number |
Whole number/counting number |
Float number |
The number in decimal |
Syntax |
Grammar/Structure of language |
Modulo |
Find the remainder |
Boolean |
True/False |
Example
Print (2) - integer |
Print (2.5) - floating point |
Print ("Hello") - string |
Print (mystr) - variable |
Print (mystr, "Hi", 2, 1.0) -- commas |
Reverse
#Finish this program so that it gets a word from the user and prints
#that word backwards
reverse = "" #do not change
letter_num = 0 #do not change
word = input("Please enter a word: ")#get a word from the user
'''
while letter_num < len(word):#compare the letter_num to the lenght of the word
reverse = word[letter_num]+reverse#kepp adding the letter to the front of reverse
letter_num = letter_num+1#go to the next letter in the word
'''
for lette in word :
reverse = letter + revers
print ("Reverse: ",reverse)
#creating list
mylist = [1,2,3,4,5,6]
mylist2 = ['hi', 'hello','anything']
mylist3 = [1, 'hello', 2.5]
|
Area of the circle
_varl = 1
_varl = 3
_varl + 100
print(_varl)
def bacon(): #use the keyword def and end with a colon:
print("hello it's bacon")
return
bacon()
bacon()
bacon()
bacon()
bacon()
def bacon():
print("hello it's bacon")
print("line 2")
print("line 3")
print("line 4")
print("line 5")
print("line 6")
print("line 7")
print("line 8")
return
bacon()
bacon()
bacon()
def myprint(text): #single parameter
print ("" + str(text) + "")
return
myprint(1)
myprint("hello")
myprint(1+2)
def myprint2(text, decoration):
print(decoration + str(text) + decoration)
return
myprint2(12312321312, "++++")
myprint2("hello", "<<>>")
def doubleIt(number):
return number * 2
myvar = 2
myvarDouble = doubleIt(myvar)
print(doubleIt("hello"))
myvar = doubleIt(doubleIt(3)) #same as doubleIt(6)
print (myvar)
def sumIt(num1, num2):
return num1 + num2
print(sumIt("a", "b"))
print(sumIt(2,3))
def areaOfCircle (radius):
pi = 3.1415
area = pi radius*2
return area
user_radius = input('Enter the radius: ')
radius = float(user_radius)
print("The area of the circle is", areaOfCircle(radius))
def areaOfCircle(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", areaOfCircle(radius))
|
|
|
Math
== |
equal to |
!= |
no equal to |
< |
less than |
> |
more than |
<= |
less than or equal to |
>= |
more than or equal to |
% |
Modulo, Find the remainder |
Addition
string+string |
Combine together |
string+string |
CRASH! |
Number+number |
Addition(Math) |
Multiplication and Exponents
string*number |
Combine the string |
string*string |
CRASH! |
number*number |
Multiply(math) |
string**string |
CRASH! |
number**number |
Exponent(math) |
string**number |
CRASH! |
Convert Hexadecimal
#write a program that convert a number to binary
while True:
#get a number from the user
user_number = input("please enter the number")
#convert to integer
number = int(user_number)
hex_string = ''
while (number > 0):#the number is greater than 0)
remainder = number % 16#user Modulo %
if remainder == 10:
remainder = 'A'
elif remainder == 11:
remainder = 'B'
elif remainder == 12:
remainder = 'C'
elif remainder == 13:
remainder = 'D'
elif remainder == 14:
remainder = 'E'
elif remainder == 15:
remainder = 'F'
hex_string = str(remainder) + hex_string #remainder + hexadecimal string
number = number // 16#must use // when you divide
#after the loop print the Hexadecimal string
print ("Hexadecimal string is 0x" + hex_string)
#expected output - 5 = 101
#expected output - 3 = 11
#expected output - 2 = 10
|
Convert Binary
#write a program that convert a number to binary
while True:
#get a number from the user
user_number = input("please enter the number")
#convert to integer
number = int(user_number)
binary_string = ''
while (number > 0):#the number is greater than 0)
remainder = number % 2#user Modulo %
binary_string = str(remainder) + binary_string #remainder + binary string
number = number // 2#must use // when you divide
#after the loop print the binary string
print ("Binary string is",binary_string)
#expected output - 5 = 101
#expected output - 3 = 11
#expected output - 2 = 10
|
Print definitions calc
def calc(num1, num2, operation):
# use if/elif/else to check what operation to do
if operation == "sum":
return sum(num1, num2)
elif operation == "div":
return div(num1, num2)
elif operation == "product":
return product(num1, num2)
else:
print ("unknown operation")
# use the function below to compute the operation
# return the answer
def sum(a, b):
# calculate the sum of a and b
return a + b
# return the answer
def product(a, b):
# calculate the product of a and b
return a * b
# return the answer
def diff(a, b):
# calculate the difference between a and b
return a - b
# return the answer
def div(a, b):
# calculate the division of a and b
return a / b
# return the answer
print (calc (1, 2, "sum")) #output should be 3
print (calc (4, 2, "diff")) #output should be 2
print (calc (9, 3, "div")) #output should be 3
print (calc (2, 12, "product")) #output should be 24
|
Create/Write a Function
# how to create a function
def nameOfFunction(myvar1, myvar2): #parameters or arguments
print ("hello") #must indent each line that is part of the function
return myvar1 + myvar2
#function call
nameOfFunction('hi')
#write a function
#name : areaOfTriangle
#parameters : base height
#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', )
|
|
|
Countdown
# Create a program that receives a number from the user and counts down
# from that number on the same line
# receive the number from the user as a string
user_number = input("7")
#convert the user number to an integer
number = int(user_number)
#setup the countdown string
countdown_string = '7 6 5 4 3 2 1 0'
while number > 0:
# add the number to the string
countdowm_string = something + str(somethingelse)
# subtract 1 from the number
number = number - 1
print (countdown_string)
|
Radius of Circle
while True:
#Ask the user for a radius of a circle
user_radius = input("Please enter the radius of the circle")
#Convert the given radiusto a floating point
radius = float(user_radius)
#make a variable called pi
pi = 3.1415
#Calculate the area of the circle using exponents
area = pi radius *2
#display the area of the circle to the user
print("The area of the circle is", area)
|
Random
import random
# Create a list of integers
inlist = [1,2,4,5,7,9]
random_int = random.choice(intlist)
print (inlist, random_int) #print the entire list andthe random item
# Create a list of floating point numbers
fplist = [1.5,2.2,1.0,100.999]
random_fp = random.choice(fplist)
print (fplist, random_fp) #print the entire list and the random item
# Create a list of strings
strlist = ['dog', "cat", 'match', "it's me", '"hi"']
random_str = random.choice(strlist)
print (strlist, random_str) #print the entire list and the random item
# Create a list of integers and floating point numbers and string
mylist = [1,2,2.2,3.2, 'string', "hi"]
random_item = random.choice(mylist)
print (mylist, random_item) #print the entire list and the random item
# create alist of following variable
myvar1 = 1
myvae2 = 2
myvar3 = 3
varlist = [myvar1, myvar2, myvar3]
random_var = random.choice(varlist)
print (varlist, random_var) #print the entire list and the random item
|
Print Definitions
# write definitions for the following words and print them using
# a multi-line string
def printDefinitions ():
if word == "variable":
#variable
print ("""
A variable is ....
""")
elif word == "function":
#function
print ("""
A function is something
""")
elif word == "parameter":
print ("""
A parameter is ...
""")
elif word == "argument":
print ("""
A argument is
""")
elif word == "string":
print ("""
A string is ...
""")
elif word == "function call":
print ("""
A function call is ...
""")
else:
return "unknown word"
#ask the user for the name of the word to define
user_input = input ("Enter the word to define: ")
printDefinitions(user_input )
|
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))
|
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets