Strengthen your understanding of Python fundamentals by completing the following exercises.
Basic (10 Questions)
- Write a Python program that prints your name and age on separate lines.
- Add comments to explain what your program does in Question 1.
- Use the
print()
function to display the result of 5 + 7.
- Write a script that prints three lines using a single
print()
with newline characters.
- Write a multiline comment explaining the purpose of a small script.
- Create a Python script that contains syntax errors and fix them one by one.
- Write a program using proper indentation that prints a message if a number is greater than 10.
- Use line continuation (
\
) to split a long expression over two lines.
- Create a basic script that uses variables and prints them using formatted strings.
- Write a program with nested indentation that prints messages at different levels.
Intermediate (10 Questions)
- Run a Python script using both the Python interpreter and a code editor. Note the differences.
- Use the command line to run a script and pass arguments using
sys.argv
.
- Write a script that prints the current year and your expected graduation year using variables.
- Demonstrate both single-line and multi-line comments in the same script.
- Print a formatted string showing your name, department, and college using
f-strings
.
- Write a program that uses indentation correctly to define a function that prints "Hello!".
- Use a continuation character to break a mathematical expression that spans more than 80 characters.
- Analyze a script using
pylint
and fix at least 3 warnings or errors.
- Write a script that contains incorrect indentation. Then correct it and explain the changes.
- Compare and demonstrate how print behaves with and without end-line characters (
end=""
).
Advanced (10 Questions)
- Write a script that accepts command-line input and prints a customized greeting.
- Document a script using comments and multiline strings for maintainability.
- Create a script that calculates area and perimeter of a rectangle using variables and formatted output.
- Use indentation to structure a decision tree using if-elif-else blocks clearly.
- Use the continuation character in an if-statement condition spanning multiple lines.
- Create a script with nested loops and use proper indentation to maintain clarity.
- Introduce at least 3 common indentation errors and explain how Python handles them.
- Write a Python script that fails
pylint
checks, then modify it to score 10.0.
- Create a script with user-defined functions, include docstrings, and test their output.
- Build a command-line Python tool that prints help messages and argument values using
argparse
.