NEW
Font size
WorksheetsLECTURE 06 Quizs(26.11.2025)
Total questions: 20
Worksheet time: 10mins
What is the best way to use code again in a big project?
Write all code in one file
Put code you want to use again in modules and use a script to start the program
Never use modules
Put all code inside `if __name__ == "__main__":`
When you use a Python file in another file, which code does NOT run? (Study tip: Try importing a file with different code blocks and see what happens.)
Code inside any function
Code inside a try...except block
Code inside `if __name__ == "__main__":`
Function definitions
True or False: The main reason for `if __name__ == "__main__":` is to make a main() function.
True
False
True or False: Code inside `if __name__ == "__main__":` only runs when you run the file. (You can test this by putting a print statement inside this block and running/importing the file.)
True
False
What is the value of `__name__` when you run a Python file? (You can add `print(__name__)` to your file and run it in a compiler to check.)
"script"
"__main__"
"module"
"imported"
Why do we use modules in Python?
To make the program slower
To repeat code
To use code again easily
To make more errors
Which one is NOT a step in making software?
Planning
Testing
Importing
Maintenance
Look at this code. What will it print if you run the file? (You can copy and paste this code into a compiler to see what happens.) ```python def greet(): print("Hello!") if __name__ == "__main__": greet() ```
Nothing
Hello!
greet
Error
Which Python feature lets you write code that only runs when the file is run, not when used in another file?
try...except block
`if __name__ == "__main__":` block
for loop
global variable
Which word tells you if a Python file is being run or used in another file? (You can print `__name__` in your file and run it in a compiler to see what it shows.)
\_\_file\_\_
\_\_main\_\_
\_\_name\_\_
\_\_import\_\_
True or False: A Python module is a file you use in another program to use its code. (Study tip: Try making two Python files and use `import` in one to use code from the other.)
True
False
You have a file called `math_utils.py` with a function `add(a, b)`. How do you use this function in another file? (You can try this by making two files and calling the function.)
add(a, b)
import math_utils; math_utils.add(a, b)
include math_utils; add(a, b)
using math_utils.add(a, b)
Why is `if __name__ == "__main__":` useful?
It stops errors
It lets you use code in other files without running main code
It makes the program faster
It makes variables global
How do you use a module named `helper` in Python? (If you want, try making a file called `helper.py` and import it in another file.)
import helper
include helper
using helper
require helper
If you have two files, `main.py` and `helper.py`, and `main.py` uses `helper.py`, what happens to code inside `if __name__ == "__main__":` in `helper.py`? (You can test this by making two files and adding print statements.)
It runs
It is skipped
It causes an error
It becomes a function
You have a file called `toolbox.py`: ```python def tool(): print("Tool used.") if __name__ == "__main__": print("Running as script.") ``` If you use `toolbox` in another file and call `toolbox.tool()`, what will be printed? (You can try this in a compiler to see what happens.)
Tool used.
Running as script.
Tool used. \n Running as script.
Nothing
Pick the code that prints "Application started." only when you run the script. (You can copy and run these code blocks in a compiler to check.)
```python def start_application(): print("Application started.") start_application() ```
```python def start_application(): print("Application started.") if __name__ == "__main__": start_application() ```
```python def start_application(): print("Application started.") if __name__ == "__main__": print("Application started.") ```
```python def start_application(): print("Application started.") ```
True or False: Code reusability means you can use the same code in different programs.
True
False
True or False: A Python script is a file you run to start your program. (This is a study guide: If you are not sure, try making a simple Python file and run it in a compiler.)
True
False
Where should you put a function call if you want it to run only when the file is run?
Inside the function
Inside `if __name__ == "__main__":`
At the top of the file
Outside any block
