Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Symbol Rule for naming the variable (cont) Define Function (cont)
float() number with decimal point # last name = no spaces allowed myprintnew (1,"@@@@@@@")
_varl = 3 myvar = 12
Multiply
_varl + 100 myvar = doubleit(myvar)
string * Repeat those thing for the
print(_varl) myvar = doubleit(myvar)
number number of time
def bacon(): # defines a functio print(myvar)
string * Crash!
named bacon Result
string
print ("hello it's bacon") 3
number * Multiply like in math
print ("line2") hello it's bacon
number
print ("line3") line2
print ("line4") line3
Exponents
print ("line5") line4
string ** number Crash!
print ("line6") line5
number ** number Exponent in Math line6
print ("line7")
number ** string Crash! return #exit the fuction line7
Symbol (cont) Meaning of the word (cont) Meaning of the word (cont)
>= more than or equal to """) word = input ("Enter the word")
# one line comment that will not elif word == "function": printDefinition(word)
included in the code #function Result
def nameOfFunction(): num = int(input(" Enter a word")) variable A value or thing that can be changed
print (‘This function has no remainder = num%3 string A list of character such as letter or
parameters’) if remainder ==o: symbol
print (‘This function has no print(num,"is divisible by 3") boolean True False
return value’) else:
modulo Find the remainder
return # no value, just exits print(num,"is not divisible by
syntax There are error such as grammar or
the function 3")
structure of language
#function call
float A number with decimal point
nameOfFunction() Fibonacci fron o to 50
integer Rounded number which do not have
#function with 1
num1 = 0 decimal point
parameter/argument
num2 = 0
def testFunction(param):
fibonacci = num1+num2 Condition
print (‘This function has 1
myoutput = "0,1"
while... While this is true loop the command
parameter’)
while fibonacci < 50: under the conditional
print (param)
myoutput = myouput + ","
while Forever loop
#function call
+ str(fibonacci) True
testFunction (“this is the
num1=num2
for For every item in the list repeat the
parameter value”)
num2 = fibonacci each command under the loop that many
#function with 2 parameters and a
fibonacci = num1+ num2 item in times. (a string is a list too)
return value name
print(my output)
def function3(param1, param2):
0,1,1,2,3,5,8,13,... of list
print(‘This function has 2 If...: If the statement in 'if ' is true, it will
parameters’) then... follow the statement in 'then'. If it is
Sample
return param1 + param2 # else not, it will follow statement under
return value
def test() : 'else'.
while True:
#function call and store the result
user_input = input("Please
in a variable
enter a word: ")
returnValue = function3(2, 3)
if user_input == 'quit':
print (returnValue)
break
return
Determine zero positive and negative
test()
num = int (input("Enter a number")
keep asking word till input quit
if num>0:
print (num,"is positive")
elif num<0:
print (num,"is negative")
else:
print (num,"is zero")
The largest value The largest value (cont) Area of Triangle & Volume of prism (cont)
#write a function that returns the # name: maxlist #write a function that computes the
largest of two values #argument : list volume of a prism
#name : max2 #returns the largest value in the # name: volumeOfPrism
#agruments: num1, num2 list # parameters :b,h,l
# return: largest value def maxlist(list): # return : volume
# write a functrion that returns maxvalue = list(0) def volumeOfPrism(b,h,l):
the largest of three values for item in list: volume = b hl
# name : max3 if item > maxvalue: return volume
#agrument: num1, num2, num3 value = item user_length = float(input('Enter
# return: largest value the length of the prism:'))
def max2(num1,num2): return maxvalue print('The volume of the prism is',
if num1 >= num2: mylist = [1,2,3,4,55,66,777,0,1] volumeOfPrism(user_base,user_height
max_value = (num1) print (maxlist(list)) ,user_length))
if num2 > num1: Result
max_value = (num2) Area of Triangle & Volume of prism Enter the base of the triangle:12
return max_value Enter the height of the triangle: 6
# write a function that computes
num1 = input('Enter the the first The area of the triangle is 36.0
the area of triangle
value') Enter the length of the prism:3
# name : areaOfTriangle
num2 = input('Enter the the second The volume of the prism is 216.0
# parameters :b,h
value')
# return : area
print (max2(num1,num2)) Useful Function for Name
def areaOfTriangle(b,h):
def max3(num1,num2,num3):
if user_base<=0: mystr = "hello THERE"
if num1 >= num2 and num1 >=
return "Error: invalid print (mystr.upper()) > HELLO
num3:
radius" THERE
max_value = (num1)
if user_height<=0: print (mystr.lower()) > hello
if num2 > num1 and num2 >=
return "Error: invalid there
num3:
radius" print (mystr.capitalize()) > Hello
max_value = (num2)
area = 0.5 b h there
if num3 >= num2 and num3 >=
return area print (mystr.title()) > Hello
num1:
user_base =float(input('Enter the There
max_value = (num3)
base of the triangle:'))
user_height = float(input('Enter How to convert to binary
return max_value
the height of the triangle: '))
user_number = input("Please enter
num3 = input('Enter the the third
print ('The area of the triangle
a number")
value')
is',areaOfTriangle(user_base,user_h
number = int(user_number)
print (max3(num1,num2,num3))
eight))
binary_string =''
# write a function that returns the
while (number > 0):
largest value