In my Python learning journey, I’ve officially entered Phase 2, where I explore intermediate programming concepts. After understanding the basics like variables, loops, functions, and conditionals, I felt ready to take the next step. And yes – it was both exciting and a little challenging!
Object-Oriented Programming (OOP)
This was one of the biggest mindset shifts for me. Instead of writing code line-by-line, I began thinking in terms of objects – things with properties and actions. Here’s a simple example of a student class
:
class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
self.attendance = 0
def update_grade(self, new_grade):
self.grade = new_grade
def update_attendance(self):
self.attendance += 1
def display_info(self):
print(f"Name: {self.name}")
print(f"Grade: {self.grade}")
print(f"Attendance: {self.attendance}")
With this, I can create a Student
object and update or display the data:
student1 = Student("John", 90)
student1.update_attendance()
student1.update_grade(95)
student1.display_info()
This was a huge milestone because it taught me how to organize data and functions together.
File Handling
The moment I learned to read and write from actual text files in Python, it felt like I unlocked a whole new layer of control. For instance, here’s how I use Python to write data to a file and read it back:
# Writing to a file
with open('student_scores.txt', 'w') as file:
file.write("John: 95\nJane: 88\n")
# Reading from a file
with open('student_scores.txt', 'r') as file:
data = file.read()
print(data)
Additionally, I also worked with JSON files:
import json
# Writing to a JSON file
data = {"name": "John", "score": 95}
with open('data.json', 'w') as json_file:
json.dump(data, json_file)
# Reading from a JSON file
with open('data.json', 'r') as json_file:
data = json.load(json_file)
print(data)
This opened my eyes to how Python can interact with real-world data, and it’s useful for automation, data storage, and more.
Error Handling
Mistakes happen – and Python taught me how to handle them smartly. Here’s an example where I use try
and except
to prevent errors from crashing my program:
try:
number = int(input("Enter a number: "))
print(f"Your number is {number}")
except ValueError:
print("Oops! That was not a valid number.")
This simple error handling ensures that the program doesn’t break if the user inputs something unexpected, like text instead of a number.
List Comprehensions & Lambda Functions
This part was mind-bending at first, but then it clicked. Here’s a way to use list comprehensions to filter and transform data:
# List comprehension to square each number in a list
numbers = [1, 2, 3, 4, 5]
squared_numbers = [x ** 2 for x in numbers]
print(squared_numbers) # Output: [1, 4, 9, 16, 25]
Additionally, lambda functions let me write quick one-liner functions. Here’s an example:
# Lambda function to add two numbers
add = lambda a, b: a + b
print(add(3, 4)) # Output: 7
This made my code much cleaner and faster.
Solving Complex Problems
As a test, I built a simple quiz app using everything I’d learned. The app allows users to answer questions, stores scores, and provides feedback:
class Quiz:
def __init__(self):
self.questions = {
"What is 2 + 2?": 4,
"What is the capital of France?": "Paris"
}
self.score = 0
def ask_question(self, question, answer):
if self.questions.get(question) == answer:
self.score += 1
return "Correct!"
else:
return "Wrong!"
def show_score(self):
print(f"Your score: {self.score}")
Final Reflection
his phase was not just about learning Python – it was about learning how to think like a programmer. I’m more confident now in solving real-world problems, organizing code efficiently, and preparing for future projects like automation or AI.
Next up, I’ll continue building small projects, and maybe even share some of them on this blog.