Lecture 9: Python Functions, Scope, Tuples & Mutability

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

1/112

encourage image

There's no tags or description

Looks like no tags are added yet.

113 Terms

1
New cards

What is the purpose of functions in programming?

To prevent re-writing the same code repeatedly and to conceptually separate parts of the code.

2
New cards

What is a function call?

A function call is when a function is invoked to execute its code.

3
New cards

What is meant by 'return value' in functions?

A return value is the data that a function sends back to the place it was called from.

4
New cards

What is a tuple?

A tuple is an immutable sequence of values, similar to a list but cannot change values.

5
New cards

How do you define a function in Python?

Using the syntax: def (): followed by the code to execute.

6
New cards

What is the difference between mutable and immutable data types?

Mutable data types can be changed after creation (like lists), while immutable data types cannot (like tuples).

7
New cards

What is the significance of parentheses in a function call?

Parentheses are required after the function name and may contain arguments.

8
New cards

How can you return multiple values from a function?

By returning a tuple, which can then be unpacked into multiple variables.

9
New cards

What does it mean to pass data to a function?

Passing data to a function means providing input values through arguments.

10
New cards

What happens if you try to change an item in a tuple?

You will receive a TypeError because tuples are immutable.

11
New cards

What is a function header?

The first line of a function definition that starts with 'def' and includes the function name and parameters.

12
New cards

What is the purpose of arguments in a function?

Arguments allow you to pass data into the function for it to use during execution.

13
New cards

What are built-in functions in Python?

Functions that are included by default in Python, such as print() and input().

14
New cards

What is the role of the return statement in a function?

The return statement ends the function execution and specifies the value to be returned.

15
New cards

What is the syntax to call a function with arguments?

()
16
New cards

What is meant by 'functions as a black box'?

Functions can be viewed as a black box that takes input, processes it, and returns output without needing to understand the internal workings.

17
New cards

What is the output of the function call sqrt(16)?

4, since it returns the square root of 16.

18
New cards

How can you define a tuple in Python?

Using parentheses, e.g., my_tuple = (1, 2, 3).

19
New cards

What happens when you try to assign a new value to a tuple element?

You will encounter a TypeError because tuples do not support item assignment.

20
New cards

What is the significance of the 'def' keyword?

'def' is used to define a function in Python.

21
New cards

What is the output of print(my_tuple[1:3]) given my_tuple = (1, 2, 3, 4)?

(2, 3), as it slices the tuple from index 1 to 2.

22
New cards

What is the difference between a list and a tuple in terms of mutability?

Lists are mutable (can change), while tuples are immutable (cannot change).

23
New cards

How do you unpack a tuple in Python?

By assigning the tuple to multiple variables, e.g., a, b = my_tuple.

24
New cards

What is an example of a function that does not take any arguments?

input() is an example of a function that can be called without arguments.

25
New cards

What is the output of len(my_tuple) if my_tuple = (1, 2, 3)?

3, since it counts the number of elements in the tuple.

26
New cards

What is the purpose of using commas in function arguments?

Commas separate multiple arguments when passing them to a function.

27
New cards

What is the general structure of a function definition in Python?

def ():

28
New cards

How many spaces are commonly used for indentation in Python?

4 spaces (equivalent to 1 tab)

29
New cards

What is the purpose of a docstring in a function?

To document what the function does, its purpose, inputs, and outputs.

30
New cards

How do you access a function's docstring?

Using the help command: help()

31
New cards

What is an example of a simple function that prints a warning?

def warn(): print(' WARNING!!! ')

32
New cards

What must you do before calling a function in Python?

You must define the function before calling it.

33
New cards

What is the output of the function call warn() if it is defined as follows: def warn(): print(' WARNING!!! ')?

WARNING!!!

34
New cards

What happens if you try to call a function that has not been defined?

A NameError will occur indicating that the function is not defined.

35
New cards

What is a common practice regarding function definitions in a program?

List all functions near the beginning of the program, before the main code.

36
New cards

What is the purpose of the main code in a program?

To execute function calls after defining them.

37
New cards

How can a function be called multiple times in a program?

By defining the function once and calling it as needed with different parameters.

38
New cards

What is the output when calling the function area_of_circle(radius) defined as: def area_of_circle(radius): return pi * radius ** 2?

It returns the area of a circle based on the given radius.

39
New cards

Can one function call another function in Python?

Yes, a function can call another function.

40
New cards

What happens when a function is called within another function?

The program executes the called function's body and then returns to the calling function.

41
New cards

What will happen if a function is defined after it is called?

A NameError will occur because the function is not defined at the time of the call.

42
New cards

What is the output of the following code? def doublewarn(): warn(); warn() and then calling doublewarn()?

It will print the warning message twice.

43
New cards

What is the significance of the return statement in a function?

It specifies the value that the function will output when called.

44
New cards

What does the interpreter do when it encounters a function definition?

It remembers the function's name and parameters but does not execute the body until the function is called.

45
New cards

What is the output of the following function call? def warn(): print('Warning!'); warn()?

Warning!

46
New cards

What is the result of calling a function that does not exist?

The program will complete without error if the call is not reached.

47
New cards

What is the purpose of the print statement within a function?

To output information to the console when the function is called.

48
New cards

What is the correct syntax for defining a function with parameters?

def (, ):

49
New cards

What is a common error when calling functions in Python?

Calling a function before it has been defined.

50
New cards

What is the purpose of the import statement in a Python program?

To include external modules or libraries that provide additional functionality.

51
New cards

What does the function 'warn()' do?

It prints a warning message to the console.

52
New cards

What happens when 'doublewarn()' is called?

'doublewarn()' calls a nonexistent function, but it is never executed, so no error occurs.

53
New cards

What is the scope of a variable?

The scope of a variable refers to the regions of the program where that variable is valid and can be accessed.

54
New cards

What is a local variable?

A local variable is defined within a function and is not accessible outside of that function.

55
New cards

What is a global variable?

A global variable is defined throughout the program and can be accessed in any function unless shadowed by a local variable.

56
New cards

What happens to the variable 'a' in the main program when 'my_function()' is called?

'my_function()' creates a new variable 'a' in its own memory, which is separate from the main program's 'a'.

57
New cards

What is the output of 'print(a)' after calling 'my_function()' if 'a' is set to 5 in the main program?

The output will be 5, as the function's 'a' does not affect the main program's 'a'.

58
New cards

What does the 'my_function(x)' do when called with an argument?

It creates a new variable 'x' in the function's memory and prints its value.

59
New cards

What happens to the variable 'x' after the function 'my_function(x)' completes?

The variable 'x' is released from memory after the function finishes.

60
New cards

What is the correct way to access data from a function?

By passing data through arguments/parameters in the function call.

61
New cards

What does 'x += 1' do in the context of 'my_function(x)'?

It increments the value of 'x' by 1 within the function's local scope.

62
New cards

What will happen if a variable is referenced in a function without being defined first?

An UnboundLocalError will occur, indicating that the variable was referenced before assignment.

63
New cards

What is the output of 'print(a)' after calling 'F(a)' if 'a' is set to 5 in the main program?

The output will be 5, as 'F(a)' does not modify the main program's 'a'.

64
New cards

What is the result of trying to modify a variable in the main program from within a function?

It is considered bad programming style and can lead to confusion; functions should be self-contained.

65
New cards

What does the function 'F(a)' do when called?

It prints the value of the parameter 'a' passed to it.

66
New cards

What happens when a local variable in a function has the same name as a global variable?

The function will use the local variable, and the global variable will be shadowed.

67
New cards

What is the output of 'print(a)' after calling 'F()' if 'a' is not defined in 'F()'?

An UnboundLocalError will occur because 'a' is referenced before assignment.

68
New cards

What is the purpose of parameters in a function definition?

Parameters allow the function to accept input values that can be used within the function.

69
New cards

What happens to the memory of a function after it completes execution?

The memory allocated for the function's local variables is released.

70
New cards

What is the output of 'print(x)' in 'my_function(x)' if 'x' is passed the value 5?

The output will be 6, as 'x' is incremented by 1 within the function.

71
New cards

What does the statement 'a += 3' do in the context of a function?

It attempts to increase the value of 'a' by 3, but will raise an error if 'a' is not defined locally.

72
New cards

What is the correct way to define a function that takes two parameters?

Use the syntax 'def my_function(param1, param2):'.

73
New cards

What is the output of 'print(a)' after calling 'F(a)' if 'a' is defined globally as 5?

The output will be 5, as the function does not modify the global variable.

74
New cards

What does 'my_function(3, "Joe")' do?

It calls 'my_function' with 3 and 'Joe' as arguments for its parameters.

75
New cards

What is the purpose of the 'global' keyword in a function?

It allows the function to access and modify a global variable.

76
New cards

What happens when a function uses the 'global' keyword?

The function can read and write to the global variable specified.

77
New cards

Why should global variables generally be avoided?

They make code harder to maintain and can introduce bugs.

78
New cards

What is the syntax to declare a global variable inside a function?

Use 'global ' within the function.

79
New cards

What does the 'return' statement do in a function?

It ends the function and returns a specified value.

80
New cards

What will the function 'thirty()' return?

It will return the value 30.

81
New cards

What does the function 'is_three_more(a, b)' return if a is 3 greater than b?

It returns True.

82
New cards

What is the output of 'print(is_three_more(10, 7))'?

True.

83
New cards

What happens to the lines of code after a 'return' statement in a function?

They are never executed because the function ends at 'return'.

84
New cards

How can default parameters be defined in a function?

By assigning values using the assignment operator '=' in the parameter list.

85
New cards

What will the function 'F(a="Bryan", b="Texas", c="USA")' print if called with no arguments?

It will print 'Bryan Texas USA'.

86
New cards

What error occurs if a function is called without required parameters?

A TypeError occurs, indicating missing required positional arguments.

87
New cards

What is the rule regarding default parameters in a function?

All parameters after a default parameter must also have default values.

88
New cards

What is the output of 'print(x)' after calling 'dosomething(x, y)' with x = 1 and y = 2?

It will print 1, as x is immutable.

89
New cards

How do lists behave differently than other data types when passed to functions?

Lists are mutable, so changes within the function affect the original list.

90
New cards

What is the output of 'print(x)' after calling 'dosomething(x, y)' with x = [1] and y = [2]?

It will print [1, 2], as the list x is modified.

91
New cards

What does the function 'F(a)' do if a is passed the value 4?

It increases a by 1 and returns 5.

92
New cards

What will happen if a function has both required and default parameters?

Required parameters must be specified, while default parameters can be omitted.

93
New cards

What is the significance of mutable vs immutable data types in Python?

Mutable types can be changed in place, while immutable types cannot.

94
New cards

What will the output be when 'F(

TypeError: F() missing 1 required positional argument: 'a'.

95
New cards

What type of data structure is a list in Python?

A mutable data type.

96
New cards

What are examples of immutable data types in Python?

Tuples and strings.

97
New cards

Can the elements of a list be modified?

Yes, the values of elements in a list can be changed.

98
New cards

Can a list be reassigned to a new value?

No, a list cannot be assigned a brand-new value.

99
New cards

What happens when you assign a new list to a parameter in a function?

It does not change the original list; it only changes the local reference.

100
New cards

What happens when you modify an element of a list inside a function?

It changes the corresponding element in the original list.