W13 Flashcards

1
Q

What is the scope of Local variables?

A

Inside the function they are defined in.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does a variable scope mean?

A

The parts of the program in which the variable is visible (accessed or modified).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Are parameter variables local or global?

A

Local

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What type of errors will occur when using a variable out of its scope?

A

A compiler error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can two different local variables have the same name and different values?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Variables defined outside of functions are considered as what type of variables?

A

Global variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Global variables can be visible (accessed and modified) by what functions?

A

A global variable is visible to all functions that are defined after it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you create or modify a global variable inside a function?

A

Using the keyword “global”. Otherwise, instead of accessing the global variable, a local variable will be created with the same global variable name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the Python collections?

Give an example

A

Collections are

containers that store data, such as Lists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What operator do you use to access a list element?

A

The subscript operator [ ].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What operator do you use to create a list element?

A

The square brackets [ ].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What operator do you use to print, access or modify a part of a list?

A

The slice operator [ n1 : n2 ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

list[ : 6 ], what indexes will be printed?

A

from 0 to 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

list[ 6 : ], what indexes will be printed?

A

from 6 to last index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

list[ 3 : 9 ], what indexes will be printed?

A

from 3 to 8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

You can assign new values to a slice.
Ex:
List [ 2 : 5 ] = [11, 21, 88]
True or False?

A

True

17
Q

list[-4]= equals what index???

list[-1]=equals what index???

A

list[-4] = list[last index - 3] or list[len(list)-4]

list[-1] = list[last index - 0] or list[len(list)-1]

18
Q

What type of errors will occur when accessing a non-existing list element?

A

A run-time error:

IndexError: list index out of range

19
Q

A list variable contains a “reference” to the list content?

True or False?

A
True
list_name (list variable)= [ content] (reference)--> which is the list's location in the memory
20
Q

What is Aliasing?

A

Aliasing is when two or more variables reference the same memory location.
The second variable is an alias for the first because both variables reference the same list

21
Q

s = [ 3, 4 , 8]
s=r
r[0]=10
print(s)

A

10, 4 , 8

22
Q

When passing a ‘list variable’ to a function’s parameter, modifying the parameter will not affect the original list?

True or False?

A

False.
This is called as ‘ Variables call by reference’.

Therefore, instead of modifying the parameter, pass the parameter to a new variable then do what you want.
Or, use ‘ Variables call by value’

23
Q

A Python function can mutate the contents of a list in the calling function because it receives a reference to the list
—the parameter variable becomes an alias for the original list.

True or False?

A

True.

24
Q

How to add elements to a list?

A

list.append()

25
Q

IMPORTANT!

summary

A

1- When you assign a list variable to another variable, both variables refer to the same list —this is true even when using list as arguments, the parameter variable becomes an alias.

2- lists are references to memory locations

3- lists are aliases

4- lists are containers

26
Q

A function that calls itself?

A

A recursive function

27
Q

For a recursion function to terminate, there must be a ?

A

Base case

28
Q

Every recursive call must simplify the computation in some way.
True or False

A

True.

29
Q

A that function calls itself over and over with no end in sight and your program
will shut down and report a “stack overflow”

A

The Infinite recursion function

30
Q

the one branch of the recursive function that does not call itself.

A

The base case. Therefore, they prevent infinite recursion

31
Q

A special purpose function which allow additional parameters to be used without modifying the recursive function

A

Helper function

32
Q

Disadvantages of recursive functions (list three):

A

More memory
Difficult to debug
Slower than using iteration

33
Q

“Mutable” vs. “Immutable”

A

changeable and unchangeable

34
Q

A process of using a loop to go over a group of items

A

Iteration