1/10
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is a parameter?
A parameter is a function definition for incoming values.
Why are parameters useful?
They are useful because it makes it easier to code so you don’t have to write as many functions and it also makes it easier to organize the code and be able to alter it later.
Consider the following two functions, which return the area of a circle with a radius of 5 and a radius of 10, respectively.
The issue is that we would have to write a function for every possible radius. Solve this problem by writing a function with a parameter.
def areaOfCircle(radius):
return 3.14 * radius + radius
Make a list of numbers with 4 elements
numbers = [1, 2, 3, 4]
Use a for loop to traverse the list, printing every element in it
for number in numbers:
print (number)
How do lists manage complexity in a program? (hint: too many variables)
Lists manage complexity in programs so you don’t have to write a new variable for every value. It also helps by organizing different variables in different lists making it easier and clearer to code.
What is the distinction between defining and calling a function?
The difference between defining and calling a function is that defining it is creating it and defining what it does. When you call a function it is making it execute and use the function.
Write a function that calculates the average of two numbers, it should have two parameters, num1 and num2:
def average(num1, num2): or function average(num1, num2){}
it should not print the average, but instead return it.
def average (num1, num2):
sum = num1 + num2
average = sum/2
return average
Show a call to the function, storing the return result in a variable
result = average (5,6)
Assume we have a list with variable name “accessories”. Write an algorithm that counts how many times “cat” occurs in the list.
accessories = [“hat”, “scarf”, “cat”, “cat”, “earrings”]
catCount = 0
for accessory in accessories:
if accessory == “cat”:
catCount = catCount +1
AP score question
APscores = [5,3,4,2]
sum = 0
for score in APscores:
sum = sum + score