Chapter 2: Python Basics Assignments

Strengthen your understanding of Python fundamentals by completing the following exercises.

Basic (10 Questions)

  1. Write a Python program that prints your name and age on separate lines.
  2. Add comments to explain what your program does in Question 1.
  3. Use the print() function to display the result of 5 + 7.
  4. Write a script that prints three lines using a single print() with newline characters.
  5. Write a multiline comment explaining the purpose of a small script.
  6. Create a Python script that contains syntax errors and fix them one by one.
  7. Write a program using proper indentation that prints a message if a number is greater than 10.
  8. Use line continuation (\) to split a long expression over two lines.
  9. Create a basic script that uses variables and prints them using formatted strings.
  10. Write a program with nested indentation that prints messages at different levels.

Intermediate (10 Questions)

  1. Run a Python script using both the Python interpreter and a code editor. Note the differences.
  2. Use the command line to run a script and pass arguments using sys.argv.
  3. Write a script that prints the current year and your expected graduation year using variables.
  4. Demonstrate both single-line and multi-line comments in the same script.
  5. Print a formatted string showing your name, department, and college using f-strings.
  6. Write a program that uses indentation correctly to define a function that prints "Hello!".
  7. Use a continuation character to break a mathematical expression that spans more than 80 characters.
  8. Analyze a script using pylint and fix at least 3 warnings or errors.
  9. Write a script that contains incorrect indentation. Then correct it and explain the changes.
  10. Compare and demonstrate how print behaves with and without end-line characters (end="").

Advanced (10 Questions)

  1. Write a script that accepts command-line input and prints a customized greeting.
  2. Document a script using comments and multiline strings for maintainability.
  3. Create a script that calculates area and perimeter of a rectangle using variables and formatted output.
  4. Use indentation to structure a decision tree using if-elif-else blocks clearly.
  5. Use the continuation character in an if-statement condition spanning multiple lines.
  6. Create a script with nested loops and use proper indentation to maintain clarity.
  7. Introduce at least 3 common indentation errors and explain how Python handles them.
  8. Write a Python script that fails pylint checks, then modify it to score 10.0.
  9. Create a script with user-defined functions, include docstrings, and test their output.
  10. Build a command-line Python tool that prints help messages and argument values using argparse.