Practical Exercises Chapter 3
Exercise 1: Your First Script
1. Create a Python script named print_even_numbers.py.
2. Make the script print even numbers from 2 to 20.
# print_even_numbers.py
for i in range(2, 21, 2):
print(i)
3. Run your script from the terminal to verify it works.
Exercise 2: Command-Line Arguments
1.Create a Python script named greet_user.py.
2. Modify the script to accept a username as a command-line argument.
# greet_user.py
import sys
username = sys.argv[1]
print(f"Hello, {username}!")
3. Run the script from the terminal, passing in different usernames to make sure it works.
Exercise 3: CSV File Reader
1. Create a CSV file with columns Name, Age, and Email and populate it with sample data.
2. Write a Python script named read_csv.py.
# read_csv.py
import csv
with open('sample.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
3. Run the script to make sure...