Recitations 4 & 6 (Intro to Computing, Test 1 Review)

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/26

encourage image

There's no tags or description

Looks like no tags are added yet.

27 Terms

1
New cards

Functions allow you to return ____value or _____of values.


a, a set

2
New cards

Returning vs Printing

Returning allows you to store a value for future use by setting a variable equal to the function call

  • Ex: def multiplier(num1, num2):

product = num1 * num2

return product

my_product = multipler(5, 6)

What will my_product be equal to? 30


Printing simply displays the value to the console, which is useful for debugging but doesn’t allow you to store the value anywhere

  • Ex: def multiplier(num1, num2):

product = num1 * num2

print(product)

my_product = multipler(5, 6)

What will my_product be equal to? None

3
New cards

Parts of a Function (Conceptual)

def function_name(parameter1, parameter2):

[lines of code]

return output_value

value = function_name(argument1, argument2)

print(value)

4
New cards

1| def multiplier(num1, num2):

2| product = num1 * num2

3| return product

4| print(multiplier(1, 2))

Label the parts of the function:

1| def multiplier(num1, num2):

2| product = num1 * num2

3| return product

4| print(multiplier(1, 2))

Line 1: Function Header (Function Name, Parameters: the variable placeholders defined in the function header)

Line 2-3: Function Body: Everything indented under the header

Line 4: Function Call (Arguments: values passed into the function when you call it)

5
New cards

Parameter vs Argument? In a variable analogy, what are parameters and what are arguments?

Parameters: the variable placeholders defined in the function header

  • def multipler(num1, num2):


Arguments: values passed into the function when you call it

  • print(multiplier(1, 2))


Variable Analogy

  • Parameters: variable

  • Arguments: values assigned to the variables


6
New cards

Do all functions need parameters?

no

7
New cards

Give the num2 parameter a default value of 0:

1| def multiplier(num1, num2):

2| product = num1 * num2

3| return product

4| print(multiplier(1))

1| def multiplier(num1, num2 = 0):

2| product = num1 * num2

3| return product

4| print(multiplier(1)

8
New cards

What is the output of this code?

1| def multiplier(num1, num2):

2| ——product = num1 * num2

3| ——return product

4| print(multiplier(1))

1| def multiplier(num1 = 2, num2):

2| ——product = num1 * num2

3| ——return product

4| print(multiplier(1))

(line 4) TypeError: multiplier() missing missing 1 required positional argument: 'num2'

(line 1) SyntaxError: parameter with a default value defined before parameter without a default value

9
New cards

What is the output of this code?

1| def multiplier(num1, num2 = 0):

2| product = num1 * num2

3| return product

4| print(multiplier(1))

0

10
New cards

Arguments are passed into parameters in the order that_______.

they are listed

11
New cards

T or F: Functions can call other functions.

True
Ex: def multiplier(num1, num2):

product = num1 * num2

return product

def powers(int1, int2, int3):

product = multiplier(int1, int2)

return product ** int3

print(powers(1,2,3))

12
New cards

_____let you run a piece of code repeatedly.

loops

13
New cards

2 Main Types of loops:

1)

2)

2 Main Types of loops:

1) For: specifies the number of times a statement will be executed; the variable used for looping increments in every loop; for n amount of times, perform some action

  • for i in range(start, stop, step); for i in range(start, stop); for i in range(stop)

  • for each character in a string, for each item in a list

2) While: executes a block of code while some condition is True and stops once the condition is False; while some condition is true, perform some action

14
New cards

Basic Structure of While Loops

While conditional:

[perform some task]

[modify/change conditional]

15
New cards

Infinite loop occur when…

There is no way for the conditional to be broken; conditional is never changes to False

  • Remember to have code that beaks the loop eventually


16
New cards

How do infinite loops break your code?

the loop runs ‘forever’ and eventually raises a RunTime error; Note*: the error is not raised initially

17
New cards

range(start, stop, step)

  • ______is the only required parameter.

  • Start is _______ and stop is______.


  • Stop is the only required parameter

  • Start is inclusive and stop is exclusive. This means the range starts at the start input and ends before the end input.

    • Ex: for i in range(1, 5), print(i) —> 1,2,3,4


18
New cards

Range(start, stop, step)

  • Default start =

  • Default step =


  • Default start = 0

  • Default step = 1


19
New cards

Python is_______. This means that the first item in a string or the first value in a list is has the index____.

zero-indexed, 0

20
New cards

2 Main Types of For Loops

1) For a range of numbers: for i in range(start, stop, step):, for i in range(start, stop):, for i in range (stop)

  • Ex: for i in range(len(“Aparna”)):

print(“Aparna”[i])

print(i)

2) For-Each Loops: Iterating through characters in string or items in a list a numbers

  • Ex: count = 0

for char in “Aparna”:

print(char)

print(count)


21
New cards

What is the advantage of using a for loop with range() over a for-each loop?

A) There is no advantage

B) W/ range(), you iterate directly over the items

C) W/ range(), you know exactly how many iterations have a occured

D) W/ a for-each loop, you have to manually look up each item by index

C

22
New cards

True or False: Both for loops and while loops are generally interchangeable, but one might be more relevant/useful in a scenario than another

True

23
New cards

When might you use a while loop instead of a for loop?

A) Not all loops can be done relatively easily in a for loop.

B) All loops are interchangeable, there is no real advantage to using a while loop. It’s all personal preference.

C) While loops run a predetermined number of iterations, unlike a for loop.

D) The looping can be done with expressions that evaluate to True or False like conditionals.

AD

24
New cards

For which of the following problems should you use a for-loop?

• A) Repeating some action while some condition remains true

• B) Repeating some action for every letter in a string

• C) Repeating some action a pre-determined number of times

• D) Repeating some action until a certain goal is accomplished

• E) Repeating some action for every item in a list

BCE

  • A —> While loop

  • D —> Do-while loops (can be recreated with regular while loops in Python)


25
New cards

Allow you to run some code provided some condition is met.

conditional

26
New cards

portion of a program’s execution where a variable can be seen and accessed

scope

27
New cards