0% found this document useful (0 votes)
5 views2 pages

Unit 5

Uploaded by

Diya Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Unit 5

Uploaded by

Diya Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

5.

1 Functions and Parameters 1


Functions
⁃ ex. int, input
⁃ allow us to break our program into smaller parts, and make the program
easier to understand
⁃ ex. def my_function(x):
…do something…
⁃ x is an input parameter (pass a number from program to our function)

5.2 Functions and Parameters 2


Parameters
⁃ variable name
⁃ make function behave differently every time
• def print_a_number(num):
print(num)
⁃ inside function () once it’s called is an argument
⁃ multiple parameter names separated with commas
⁃ parameters can be set to have a default value if the argument doesn’t
specify a value
• have to be at the end of the list

5.3 Functions and Parameters 3


⁃ calling a function
• build_house(“blue”)
⁃ defining a function
• def draw circle (radius, color, x, y) :
• circle = Circle (radius)
• circle. set_position (x, y)
• circle. set_color (color)
• add (circle)

5.4 Functions and Return Values


Type
⁃ can act as function call and value
⁃ variable
• print(type(…)) —> special type of return value
Return
⁃ function call replaced with return value
• def number():
return 10
x = number
print(x)
⁃ function calling another function

5.5 Functions and Return Values 2


Functions take in parameters and can give back results called return values.
⁃ def sum(first, second):
total = first + second
return total

results = sum(5, 8)
print results

def add_one(x):
return x + 1

y = add_one(100)
print(y)
• Code Outcome
• 13
101

5.6 Name Spaces


⁃ namespace - the collection of variable names that exist at a certain
point in your code, names don’t exist throughout the entire program, they only
exist within a certain namespace
⁃ scope - where the variable exists within a program, if a variable
doesn’t exist at a certain place, then it is “out of scope”

• Code
• global_var = 10
other_ var = 20

print(global_var)

• Code Outcome
• global_var —> 10
other_var —> 20

5.7 Exceptions
⁃ Try/Except
numerator = int (input ("Enter #: "))
denominator = int (input ("Enter #: "))
try:
quotient = numerator / denominator
if quotient * denominator = numerator:
print ("Divisible!")
else: print ("Not divisible.")
except ZeroDivisionError:
print ("Cannot divide by zero!")

print ("All done!")

5.8 Python vs Karel


API (Karel)
⁃ APIs - an API (application programming interface) is a set of tools for
building programs
⁃ API provide all the building blocks for the programmer to put together
⁃ Karel API abstracts away the complex details involved in getting the
Karel image to move across the screen

Documentation
⁃ provides information to show programmers how to use it
• docs tab of editor
⁃ chart.js
⁃ HTML

You might also like