/
Python Fundamentals and Core Concepts
Save to my account
Sign up
Report bug
Python Fundamentals and Core Concepts
Python Fundamentals and Core Concepts
Study
1
Question
How does a Python interpreter run a program saved in a `.py` file?
9:13
Answer
The interpreter reads the code and translates it into instructions the computer can understand. For example, `python hello.py` runs the program in `hello.py`.
2
Question
How do arguments influence a function such as `print`?
11:08
Answer
Arguments are inputs passed to a function that influence its behavior. The argument to `print` determines what it displays.
3
Question
How does assignment store a function’s returned value for later use?
21:27
Answer
The assignment operator copies the value returned by the expression on the right into the variable on the left. For example, `name = input(...)` stores the user’s response in `name`.
4
Question
How do parameters differ from arguments in a function call?
39:14
Answer
Parameters describe what inputs a function can accept. Arguments are the actual values passed to the function when it is called.
5
Question
How does `strip()` change a string when applied to user input?
51:56
Answer
It removes whitespace from the left and right ends of the string, but not from between its characters.
6
Question
How can `split()` separate a full name into two variables?
62:56
Answer
Call `name.split(" ")` to split at a space, then assign the returned values to two variables, such as `first, last`.
7
Question
Why can adding two values from `input()` produce concatenated text instead of a numeric sum?
70:46
Answer
`input()` returns user input as a string. Convert numeric input with `int()` before addition to perform integer arithmetic.
8
Question
How does a Python function use a parameter with a default value?
98:07
Answer
The parameter receives its default value when the function is called without an argument. Supplying an argument uses that value instead.
9
Question
Why must a value be passed into `hello` when `name` is local to `main`?
104:54
Answer
A variable exists only within the scope where it is defined. Passing its value as an argument makes it available to the called function, where the parameter may have a different name.
10
Question
How does `return` change a function’s result compared with printing?
105:46
Answer
Printing displays output as a side effect. `return` explicitly hands a value back, allowing the caller to pass it to another function or use it in further computation.
11
Question
How do `if`, `elif`, and `else` reduce unnecessary conditional checks?
120:11
Answer
`if` tests the first condition; `elif` tests another only when earlier conditions are false. `else` handles the remaining case without testing another condition. This mutually exclusive structure stops checking once a true branch is reached.
12
Question
How can a function determine whether an integer is even using modulo?
145:01
Answer
Test whether `n % 2 == 0`. The modulo operator gives the remainder, and a zero remainder means the integer divides evenly by two; the comparison evaluates to a Boolean value.
13
Question
How does Python’s `match` statement group several values into one case?
164:21
Answer
Separate the alternatives with vertical bars (`|`) in a single `case`, such as `case "Harry" | "Hermione" | "Ron"`.
14
Question
Why can a `while` loop run indefinitely, and how can a counter prevent this?
172:44
Answer
If the loop condition remains true because its variables never change, the loop continues indefinitely. Update the counter so it eventually makes the condition false.
15
Question
How does a `for` loop use `range(n)` to repeat an action?
187:22
Answer
It iterates over values from zero up to, but not including, `n`, repeating the indented action once for each value.
16
Question
How does a Python for-loop iterate over a dictionary by default?
219:19
Answer
It iterates over the dictionary’s keys. Each loop iteration assigns the next key to the loop variable.
17
Question
How do nested loops generate a two-dimensional square of characters?
240:23
Answer
The outer loop processes each row. For every row, the inner loop prints the characters across it; a newline after the inner loop moves output to the next row.
18
Question
How should a program handle invalid integer input with exceptions?
256:01
Answer
Place the integer conversion in a `try` block and handle a `ValueError` in an `except` block. Keep the `try` block limited to the operation that can raise that exception.
19
Question
Why can a `NameError` occur after `int(input(...))` raises a `ValueError`?
264:34
Answer
The conversion fails before the assignment completes, so no value is assigned to the variable. Code that later uses that variable can therefore raise a `NameError`.
20
Question
How does `random.choice` support a simulated coin toss?
296:29
Answer
It selects one element from a sequence at random. Choosing between a two-element list of heads and tails gives each outcome a 50% probability.
21
Question
How do command-line arguments provide input to a program?
310:59
Answer
They are words, numbers, or phrases entered after the command when the program is executed. The program receives them as input without prompting separately through `input()`.
22
Question
How does Python’s `sys.argv` represent command-line input?
312:46
Answer
It is a list containing the program’s filename at index `0`, followed by the words supplied at the command line.
23
Question
How can a program prevent invalid `sys.argv` indexing when a required argument is missing?
317:09
Answer
Check the length of `sys.argv` before indexing it. If the argument count is invalid, report the problem and use `sys.exit` to stop execution.
24
Question
How does Python retrieve and use data from a web API in the demonstrated example?
350:58
Answer
The `requests` package sends an HTTP request, and the response’s JSON can be accessed as Python data. The example iterates through the `results` list and prints each item’s `trackName`.
25
Question
Why should a Python module guard its main call with `if __name__ == "__main__"`?
366:54
Answer
The guarded call runs when the file is executed directly, but not when the file is imported. This prevents importing a function from unintentionally running the module’s main program.
26
Question
How can representative test inputs expose a bug that one passing example misses?
375:43
Answer
Different inputs can reveal cases where incorrect behavior coincidentally matches the expected result. For example, replacing multiplication with addition still gives the correct square for 2 and 0, but fails for 3 and negative inputs.
27
Question
Why should a testable function return a value rather than only print it?
411:00
Answer
Assertions compare function return values with expected values. A function that only prints has a side effect instead, so its output cannot be tested by directly comparing its return value.
28
Question
How can a loop test several inputs within one pytest function?
415:39
Answer
Loop over the inputs and assert the expected result for each one. The loop remains part of a single test function.
29
Question
How do Python file modes `w` and `a` differ when writing?
427:04
Answer
`w` creates or recreates the file, replacing its existing contents. `a` adds new content to the end of the file.
30
Question
How does `with open(...) as file` manage a file?
434:18
Answer
It assigns the opened file to `file` and automatically closes it when execution leaves the `with` block.