Basic Python Programming Assignment
GEM252: Basic Python Programming Assignment
SUBMISSION DEADLINE
Wednesday, September 10th, 2025 at 9:00 PM
DEFENSE REQUIREMENT
Every student MUST defend their assignment in a one-on-one session with Mr. John (the course
instructor).
What to Expect During Defense:
Explain your code logic and approach
Answer questions about your solutions
Demonstrate understanding of Python concepts used
Walk through specific parts of your code when requested
ACADEMIC INTEGRITY WARNING
COPYING IS STRICTLY PROHIBITED
Submitting another student's work is completely unacceptable
Collaboration on concepts is fine, but code must be your own original work
DEFENSE IS MANDATORY
If you cannot adequately defend your solution, it is equivalent to not completing the assignment
Being unable to explain your own code will NOT be tolerated
You must demonstrate genuine understanding of every line you submit
Basic Python Concepts Assessment
Instructions
Complete all the following programming tasks. Write clean, well-commented code that demonstrates your
understanding of Python fundamentals.
Problem 1: Variables and Data Types (10 points)
Write a program that:
1. Creates variables for a city's information:
city_name (string): "Lagos"
population (integer): 15000000
1
Basic Python Programming Assignment
coordinates (tuple): (6.5244, 3.3792)
is_capital (boolean): False
2. Print each variable with its type using type() function
3. Calculate and print the population density assuming area = 1000 km²
Expected Output:
City: Lagos, Type: <class 'str'>
Population: 15000000, Type: <class 'int'>
Coordinates: (6.5244, 3.3792), Type: <class 'tuple'>
Is Capital: False, Type: <class 'bool'>
Population Density: 15000.0 people per km²
Problem 2: Lists and Basic Operations (15 points)
Given this list of Nigerian cities:
cities = ["Lagos", "Abuja", "Kano", "Ibadan", "Port Harcourt"]
Write code to:
1. Add "Benin City" to the end of the list
2. Insert "Kaduna" at position 2
3. Remove "Kano" from the list
4. Print the length of the list
5. Print the cities in alphabetical order (don't modify original list)
6. Print every second city using slicing
Problem 3: Dictionaries (15 points)
Create a dictionary called city_data with the following information:
{
"Lagos": {"population": 15000000, "region": "Southwest"},
"Abuja": {"population": 3000000, "region": "North Central"},
"Kano": {"population": 4000000, "region": "Northwest"}
}
Write code to:
1. Add a new city: "Enugu" with population 722000 and region "Southeast"
2. Update Lagos population to 16000000
2
Basic Python Programming Assignment
3. Print all city names
4. Print the region of Abuja
5. Calculate and print the total population of all cities
Problem 4: Loops (20 points)
Part A: For Loops
Write a program that:
1. Uses a for loop to print numbers 1 to 10
2. Uses a for loop to print each character in the string "PYTHON"
3. Uses a for loop to calculate the sum of numbers from 1 to 100
Part B: While Loop
Write a program that:
1. Uses a while loop to print numbers from 10 down to 1
2. Uses a while loop to find the first number greater than 1000 that is divisible by 17
Problem 5: Conditional Statements (15 points)
Write a function called classify_temperature(temp) that:
Returns "Hot" if temperature > 30
Returns "Warm" if temperature is between 20-30 (inclusive)
Returns "Cool" if temperature is between 10-19 (inclusive)
Returns "Cold" if temperature < 10
Test your function with temperatures: 35, 25, 15, 5
Expected Output:
Temperature 35°C: Hot
Temperature 25°C: Warm
Temperature 15°C: Cool
Temperature 5°C: Cold
Problem 6: Functions (25 points)
Part A: Basic Function
Write a function calculate_distance(x1, y1, x2, y2) that calculates the Euclidean distance between
two points using the formula:
3
Basic Python Programming Assignment
distance = √[(x2-x1)² + (y2-y1)²]
Test with points (0, 0) and (3, 4). Expected result: 5.0
Part B: Function with Default Parameters
Write a function greet_user(name, greeting="Hello") that:
Takes a name and optional greeting
Returns a formatted greeting string
Test cases:
print(greet_user("John")) # "Hello, John!"
print(greet_user("Mary", "Hi")) # "Hi, Mary!"
Part C: Function Returning Multiple Values
Write a function analyze_numbers(numbers) that takes a list of numbers and returns:
The minimum value
The maximum value
The average value
Test with: [10, 25, 30, 15, 40]
Problem 7: String Operations (10 points)
Given the string: location = "Lagos, Nigeria, West Africa"
Write code to:
1. Convert to uppercase
2. Split into a list using comma as separator
3. Check if the string contains "Nigeria"
4. Replace "West Africa" with "Western Africa"
5. Count how many times the letter 'a' appears (case insensitive)
Problem 8: File Operations (15 points)
Write a program that:
1. Creates a file called [Link] with this content:
Lagos,15000000
Abuja,3000000
4
Basic Python Programming Assignment
Kano,4000000
2. Reads the file and prints each line
3. Processes the data to calculate and print the total population
4. Handles errors using try/except if file doesn't exist
Problem 9: List Comprehensions (10 points)
Given: numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Write list comprehensions to create:
1. A list of squares of all numbers: [1, 4, 9, 16, ...]
2. A list of even numbers only: [2, 4, 6, 8, 10]
3. A list of numbers greater than 5: [6, 7, 8, 9, 10]
Problem 10: Error Handling (15 points)
Write a program that:
1. Asks the user to input two numbers
2. Calculates and prints their division
3. Handles these errors appropriately:
ValueError: if input is not a number
ZeroDivisionError: if dividing by zero
4. Uses a finally block to print "Calculation completed"
Sample Run:
Enter first number: 10
Enter second number: 0
Error: Cannot divide by zero!
Calculation completed
Submission Requirements
Format:
Single Python file: python_test.py
Include comments explaining your code
Use meaningful variable names
Test all your functions with the provided examples
5
Basic Python Programming Assignment
HOW TO SUBMIT
Step-by-Step Submission Process:
1. Create a GitHub repository containing your python_test.py file
2. Send the repository link to: johnosifotec@[Link]
3. Email Subject Line Format: [MATRIC NUMBER] [YOUR FULL NAME] - GEM252 Basic Python
Programming Assignment
Example: [GEM/2021/001] [John Able] - GEM252 Basic Python Programming Assignment
4. Email should include:
Repository link
Your matric number
Your full name
SUBMISSION CHECKLIST
Before submitting, ensure:
Your code file is named python_test.py
All 10 problems are completed
Code is well-commented and readable
You can explain every part of your solution
You tested your code and it produces expected outputs
You understand the Python concepts used in each problem
Your GitHub repository is public and accessible
Your email follows the required format
4. Email should include:
Repository link
Your matric number
Your full name
Brief confirmation that you're ready for the defense session
Code Structure:
# Problem 1: Variables and Data Types
print("=== Problem 1 ===")
# Your code here
# Problem 2: Lists and Basic Operations
print("=== Problem 2 ===")
# Your code here
# Continue for all problems...
6
Basic Python Programming Assignment
Grading Criteria:
Correctness (60%): Code works as expected
Code Quality (25%): Clean, readable, well-commented
Understanding (15%): Demonstrates grasp of concepts
Python Concepts Being Tested:
✓ Variables and Data Types: int, str, bool, float, tuple
✓ Collections: Lists, dictionaries, basic operations
✓ Control Flow: for loops, while loops, if/elif/else
✓ Functions: definition, parameters, return values, defaults
✓ String Methods: split, replace, upper, count, contains
✓ File I/O: reading and writing text files
✓ Error Handling: try/except/finally blocks
✓ List Comprehensions: filtering and transforming data
✓ Built-in Functions: len, type, sum, min, max
Total Points: 150
FINAL REMINDER
Your grade depends not only on correct code but also on your ability to explain and defend your work.
Come prepared to discuss your solutions in detail.
Submission Deadline: Wednesday, September 10th, 2025 - 9:00 PM
Good luck with your assignment! Make sure you understand every concept and can confidently explain your
solutions.