Python Programming: Variables, Functions, Loops, and Debugging

0.0(0)
studied byStudied by 1 person
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/98

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

99 Terms

1
New cards

What is the most appropriate value for a variable named 'state'?

B. 'Virginia'

2
New cards

How do you declare a single-line comment in Python?

C. # This is a comment

3
New cards

What does the function sillyString print when my_string is 'apple'?

D. 'applea'

4
New cards

What is the output of the function looping(5)?

D. 15

5
New cards

Which function call would raise an error for function f(a, b=6, c='hi')?

G. Options D and E would raise an error

6
New cards

Evaluate the boolean expression 'cat and dog' when cat=True and dog=False.

F (false)

7
New cards

What is the output of the code 'print((True and not False) and (True and False and True))'?

F (false)

8
New cards

What is the output of the code 'y = 5 % 10'?

5

9
New cards

What is the output of the code 'z = 2**3; answer = z/2; print(answer)'?

4

10
New cards

What is the expected output of 'print(1,2,3,sep='a')'?

1a2a3

11
New cards

How do you make the turtle called 'my_turtle' spin 5 times?

my_turtle.spin(5)

12
New cards

What does grade(90) return in the grading function?

'A'

13
New cards

What does grade(50) return in the grading function?

'F'

14
New cards

What is the bug in line 7 of the orchard function?

Bug on line 7: There is an assignment operator '=' instead of a comparison operator '=='

15
New cards

What would be printed by 'print(carrots_left())' in the given code?

17

16
New cards

What is the scope of the variable 'adrian' in the function 'carrots_left'?

Local variable

17
New cards

Would line 11 'print(adrian)' run without errors?

No, it will raise a NameError because 'adrian' is not defined in the global scope.

18
New cards

Name one debugging technique.

Using Print Statements to trace variable values. It’s useful because it lets you see what the program is actually doing at each step and helps identify where values go wrong or where the logic fails. 

19
New cards

What is one property of good algorithms?

Unambiguous: Each step in the algorithm is clear and precise, with no room for confusion.

20
New cards

What is another property of good algorithms?

Terminating: The algorithm must stop after a finite number of steps; it can’t run forever.

21
New cards

What is the equivalent while loop for printing each letter of 'python'?

i = 0; while i < len(word): print(word[i]); i += 1

22
New cards

How can you fix the infinite loop in the countdown from 10?

Decrement 'i' inside the loop: i -= 1

23
New cards

How can you convert multiple if statements into one if-elif-else statement?

if x < 7: print('x is less than 7'); elif x > 7: print('x is greater than 7'); else: print('x is equal to 7')

24
New cards

What is a valid variable name in Python?

A valid variable name cannot start with a number, cannot contain spaces, and cannot use special characters like hyphens. Example: 'my_name' is valid.

25
New cards

What is the variable type of x if x = '24.0'?

The variable type of x is 'string'.

26
New cards

Which keyword is used to define a function in Python?

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

27
New cards

How would you best describe a function in Python?

A function in Python is a set of statements that performs a specific task.

28
New cards

What will be the output of the following code snippet?

def add_list(numbers):

result = 0

for num in numbers:

result += num

return result

my_numbers = [2, 3, 4, 5]

output = add_list(my_numbers)

print(output)

The output will be 14.

29
New cards

True or False: In a chained conditional statement, Python evaluates each if and elif until one is True.

True.

30
New cards

True or False: A Python if-statement can have multiple else clauses.

False.

31
New cards

True or False: The '==' operator in Python is used to assign a value to a variable.

False; it is used to compare two values for equality.

32
New cards

True or False: The behaviors of two code snippets can differ even with the same input.

True.

33
New cards

True or False: A boolean expression can be made up of one or more boolean expressions chained with 'and's and 'or's.

True.

34
New cards

True or False: A function must take in arguments.

False; a function can be defined without any arguments.

35
New cards

How can you modify the code to print 'Zero' if the variable 'number' is 0?

Add an if statement: 'if number == 0: print("Zero")'.

36
New cards

What is the error in the following code?

number = 6

if number % 2 != 0 and number % 3 == 0:

print("The number is divisible by both 2 and 3.")

else:

print("The number is not divisible by both 2 and 3.")

The condition is incorrect; it should check for divisibility by both 2 and 3 separately.

37
New cards

What line can replace #ADD LINE in the function to print 9?

Replace #ADD LINE with 'return x'.

38
New cards

What is the error in the following code?

number = 10

target = 5

while number >= target:

print("Number:", number)

number -= 1

The indentation of the print statement is incorrect; it should be indented to be part of the while loop.

39
New cards

How can you write a for loop equivalent to the given while loop?

word = 'python'

i = 0

while (i < len(word)):

print(word[i])

i += 1

for letter in word: print(letter)

40
New cards

What is the output of the following code: x = 'cs'; y = 1112; print(x, y)?

cs 1112

41
New cards

What is the output of the following code: x = 'cs'; y = 1112; print(x + ' ' + y)?

TypeError (because y is an integer)

42
New cards

What is the output of the following code: print('CS 1112\n\tIS FUN')?

CS 1112

IS FUN

43
New cards

What is the output of the following code: y = 5 % 10; print(y)?

5

44
New cards

What is the output of the following code: z = 2**3; answer = z/2; print(answer)?

4

45
New cards

What is the output of the following code: print(1, 2, 3, sep='a')?

1a2a3

46
New cards

What value for the variable year would result in off_grounds being True?

Any value greater than 1 and less than or equal to 4 (e.g., 2, 3, or 4)

47
New cards

What value for the variable year would result in the print statement being executed in the given code?

Any value less than 1 or greater than 4 (e.g., 0 or 5)

48
New cards

What does the function grade(percent) return for grade(90)?

A

49
New cards

What does the function grade(percent) return for grade(50)?

F

50
New cards

Does the function grade(percent) work as intended?

Yes, it correctly categorizes grades into letter grades.

51
New cards

What is the output of the following code: y = 4; def add_nums(num1, num2): z = num1 + num2; w = z + x; return w; x = 10; print(add_nums(x, y))?

14

52
New cards

In the code provided, which variables are local and which are global?

Local: num1, num2, z; Global: y, x

53
New cards

Can a variable be both local and global? Explain.

No, a variable cannot be both local and global at the same time; it is either defined within a function (local) or outside of it (global).

54
New cards

What is the difference between a for-loop and a while-loop?

A for-loop iterates over a sequence or range, while a while-loop continues until a specified condition is false.

55
New cards

Give an example of a scenario when you might want to use a for-loop over a while-loop.

When you need to iterate over a fixed number of items, such as processing elements in a list.

56
New cards

Name one debugging technique and explain why it can be useful.

Print debugging: Inserting print statements helps track variable values and program flow, making it easier to identify where errors occur.

57
New cards

Name and explain two properties of good algorithms.

1) Efficiency: A good algorithm should solve problems using the least amount of resources possible. 2) Clarity: A good algorithm should be easy to understand and follow, facilitating maintenance and updates.

58
New cards

print("hello")

hello

59
New cards

print(a, b)

hi bye

60
New cards

print("CS", "1112", sep="*", end="!")

CS*1112!

61
New cards

\n

Newline

62
New cards

\t

Tab (spacing)

63
New cards

Four main Python data types

int, float, string, boolean

64
New cards

7 // 3

2 (integer division)

65
New cards

7 / 3

2.333... (float division)

66
New cards

7 % 3

1 (remainder)

67
New cards

2 ** 3

8 (exponentiation)

68
New cards

= vs ==

= is assignment, == is equality comparison

69
New cards

'and' in a condition

Both conditions must be True

70
New cards

'or' in a condition

At least one condition must be True

71
New cards

'not' in a condition

Reverses the truth value

72
New cards

Truthy and falsy values

Truthy values evaluate to True in conditionals (e.g. nonzero numbers, non-empty strings/lists). Falsy values evaluate to False (0, None, '', [], False).

73
New cards

Conditional statement output

A is bigger

74
New cards

Condition for x between 5 and 10 inclusive

if 5 <= x <= 10:

75
New cards

General syntax for defining a function

def function_name(parameters): # code return value

76
New cards

Positional argument

An argument passed based on its order

77
New cards

Keyword argument

An argument passed by explicitly naming it

78
New cards

Default parameter

A parameter with a value used if no argument is provided

79
New cards

Function with zero parameters

Yes

80
New cards

Error in function definition

Parameters with defaults must come after required parameters

81
New cards

Local vs global variables

Local exist only inside a function; global exist throughout the file

82
New cards

global keyword

Allows a function to modify a global variable

83
New cards

Preventing infinite while loop

The guard condition will eventually become False

84
New cards

Convert while loop to for loop

for i in range(2, 5): print(i)

85
New cards

Rewrite while loop as for loop

for i in range(5): print(i)

86
New cards

Code print output

d-o-g-

87
New cards

Every for loop to while loop

Yes

88
New cards

Every while loop to for loop

Yes

89
New cards

Common debugging technique

Using print statements to check values step by step

90
New cards

Rubber ducking in debugging

Explaining your code out loud to find mistakes in logic

91
New cards

Commenting in debugging

Helps break down logic so errors are easier to spot

92
New cards

5 conditions for correct code

1. Syntax is correct (no typos/indent errors) 2. Runs without crashing (no runtime errors) 3. Produces expected outputs (logic is correct) 4. Handles edge cases properly 5. Easy to read and maintain (clear structure)

93
New cards

Different debugging techniques

1. Print statements: show variable values 2. Rubber ducking: explain logic aloud 3. Code tracing: step through execution manually 4. Break down problem: test small pieces separately 5. Commenting: clarify and check intent vs result

94
New cards

Find the bug in code

Mistake: Missing indentation on print(i). Fix: for i in range(5): print(i) print('done')

95
New cards

Pseudocode

A simplified, plain-language outline of an algorithm

96
New cards

Good algorithm characteristics

Clear, finite steps that always produce the correct result

97
New cards

Function random(x, y)

a) x % 3 == 1 and y == x (e.g. x=1, y=1) b) x % 3 == 1 and y != x (e.g. x=1, y=2) c) x % 3 == 0 and y != x (e.g. x=3, y=2)

98
New cards

Code print output for square function

13

99
New cards

Code print output for a and b comparison

B bigger