You've learned this already. ✅
Click here to view the next lesson.
Chapter 3: Basic Python Programming
3.4 Practical Exercises Chapter 3: Basic Python Programming
Exercise 1: Your First Script
- Create a Python script named
print_even_numbers.py
. - Make the script print even numbers from 2 to 20.
# print_even_numbers.py
for i in range(2, 21, 2):
print(i) - Run your script from the terminal to verify it works.
Exercise 2: Command-Line Arguments
- Create a Python script named
greet_user.py
. - 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}!") - Run the script from the terminal, passing in different usernames to make sure it works.
Exercise 3: CSV File Reader
- Create a CSV file with columns
Name
,Age
, andEmail
and populate it with sample data. - 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) - Run the script to make sure it works.
Download here the sample.csv file
Exercise 4: Simple Task Automation
- Create two text files,
file1.txt
andfile2.txt
, and put some text in them. - Write a Python script named
concat_files.py
.# concat_files.py
with open('file1.txt', 'r') as f1, open('file2.txt', 'r') as f2, open('combined.txt', 'w') as combined:
combined.write(f1.read() + '\\n' + f2.read()) - Run your script and check that
combined.txt
contains the text from both files.
Exercise 5: Debugging Practice
- Add a logical bug to one of your previous scripts.
- Use Python's built-in debugger
pdb
to debug the script.# Insert the following line where you want to start debugging
import pdb; pdb.set_trace() - Document your debugging steps and the solution to the bug.
Exercise 6: Script Logging
- Pick one of your existing scripts.
- Add logging to the script using Python's
logging
module.# Add these lines at the beginning of your script
import logging
logging.basicConfig(level=logging.INFO) - Include different types of log messages (e.g.,
info
,warning
,error
). - Run the script and inspect the generated log messages.
3.4 Practical Exercises Chapter 3: Basic Python Programming
Exercise 1: Your First Script
- Create a Python script named
print_even_numbers.py
. - Make the script print even numbers from 2 to 20.
# print_even_numbers.py
for i in range(2, 21, 2):
print(i) - Run your script from the terminal to verify it works.
Exercise 2: Command-Line Arguments
- Create a Python script named
greet_user.py
. - 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}!") - Run the script from the terminal, passing in different usernames to make sure it works.
Exercise 3: CSV File Reader
- Create a CSV file with columns
Name
,Age
, andEmail
and populate it with sample data. - 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) - Run the script to make sure it works.
Download here the sample.csv file
Exercise 4: Simple Task Automation
- Create two text files,
file1.txt
andfile2.txt
, and put some text in them. - Write a Python script named
concat_files.py
.# concat_files.py
with open('file1.txt', 'r') as f1, open('file2.txt', 'r') as f2, open('combined.txt', 'w') as combined:
combined.write(f1.read() + '\\n' + f2.read()) - Run your script and check that
combined.txt
contains the text from both files.
Exercise 5: Debugging Practice
- Add a logical bug to one of your previous scripts.
- Use Python's built-in debugger
pdb
to debug the script.# Insert the following line where you want to start debugging
import pdb; pdb.set_trace() - Document your debugging steps and the solution to the bug.
Exercise 6: Script Logging
- Pick one of your existing scripts.
- Add logging to the script using Python's
logging
module.# Add these lines at the beginning of your script
import logging
logging.basicConfig(level=logging.INFO) - Include different types of log messages (e.g.,
info
,warning
,error
). - Run the script and inspect the generated log messages.
3.4 Practical Exercises Chapter 3: Basic Python Programming
Exercise 1: Your First Script
- Create a Python script named
print_even_numbers.py
. - Make the script print even numbers from 2 to 20.
# print_even_numbers.py
for i in range(2, 21, 2):
print(i) - Run your script from the terminal to verify it works.
Exercise 2: Command-Line Arguments
- Create a Python script named
greet_user.py
. - 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}!") - Run the script from the terminal, passing in different usernames to make sure it works.
Exercise 3: CSV File Reader
- Create a CSV file with columns
Name
,Age
, andEmail
and populate it with sample data. - 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) - Run the script to make sure it works.
Download here the sample.csv file
Exercise 4: Simple Task Automation
- Create two text files,
file1.txt
andfile2.txt
, and put some text in them. - Write a Python script named
concat_files.py
.# concat_files.py
with open('file1.txt', 'r') as f1, open('file2.txt', 'r') as f2, open('combined.txt', 'w') as combined:
combined.write(f1.read() + '\\n' + f2.read()) - Run your script and check that
combined.txt
contains the text from both files.
Exercise 5: Debugging Practice
- Add a logical bug to one of your previous scripts.
- Use Python's built-in debugger
pdb
to debug the script.# Insert the following line where you want to start debugging
import pdb; pdb.set_trace() - Document your debugging steps and the solution to the bug.
Exercise 6: Script Logging
- Pick one of your existing scripts.
- Add logging to the script using Python's
logging
module.# Add these lines at the beginning of your script
import logging
logging.basicConfig(level=logging.INFO) - Include different types of log messages (e.g.,
info
,warning
,error
). - Run the script and inspect the generated log messages.
3.4 Practical Exercises Chapter 3: Basic Python Programming
Exercise 1: Your First Script
- Create a Python script named
print_even_numbers.py
. - Make the script print even numbers from 2 to 20.
# print_even_numbers.py
for i in range(2, 21, 2):
print(i) - Run your script from the terminal to verify it works.
Exercise 2: Command-Line Arguments
- Create a Python script named
greet_user.py
. - 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}!") - Run the script from the terminal, passing in different usernames to make sure it works.
Exercise 3: CSV File Reader
- Create a CSV file with columns
Name
,Age
, andEmail
and populate it with sample data. - 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) - Run the script to make sure it works.
Download here the sample.csv file
Exercise 4: Simple Task Automation
- Create two text files,
file1.txt
andfile2.txt
, and put some text in them. - Write a Python script named
concat_files.py
.# concat_files.py
with open('file1.txt', 'r') as f1, open('file2.txt', 'r') as f2, open('combined.txt', 'w') as combined:
combined.write(f1.read() + '\\n' + f2.read()) - Run your script and check that
combined.txt
contains the text from both files.
Exercise 5: Debugging Practice
- Add a logical bug to one of your previous scripts.
- Use Python's built-in debugger
pdb
to debug the script.# Insert the following line where you want to start debugging
import pdb; pdb.set_trace() - Document your debugging steps and the solution to the bug.
Exercise 6: Script Logging
- Pick one of your existing scripts.
- Add logging to the script using Python's
logging
module.# Add these lines at the beginning of your script
import logging
logging.basicConfig(level=logging.INFO) - Include different types of log messages (e.g.,
info
,warning
,error
). - Run the script and inspect the generated log messages.