DEV Community

Cover image for Python phase 3 moringa revamp
wathika
wathika

Posted on

Python phase 3 moringa revamp

What I've Learned in Four Weeks: From Python Basics to SQLAlchemy

Introduction

Over the past four weeks, I've embarked on an exciting journey to master Python programming. Starting from the basics, I gradually delved into more advanced topics, culminating in the use of SQLAlchemy for database interactions. In this blog post, I will share my learning journey, highlighting key concepts and showcasing some code samples to illustrate what I've learned.

Week 1: Getting Started with Python
My journey began with the fundamentals of Python. During this week, I focused on understanding the syntax and basic constructs of the language.

Key Concepts
Variables and Data Types: I learned about different data types such as integers, floats, strings, and booleans.
Control Structures: I explored if-else statements, for and while loops, and how to use them to control the flow of my programs.
Functions: I understood the importance of functions for code reusability and modularity.
Code Sample: Basic Python

`def greet(name):
return f"Hello, {name}!"

print(greet("World"))`

Week 2: Data Structures and Libraries
In the second week, I focused on Python's built-in data structures and some essential libraries.

Key Concepts
Lists, Tuples, and Dictionaries: I learned how to store and manipulate collections of data.
List Comprehensions: I discovered a concise way to create lists.
Libraries: I got introduced to libraries like math and datetime.
Code Sample: List Comprehensions

squares = [x**2 for x in range(10)]
print(squares)

Week 3: Object-Oriented Programming (OOP)
The third week was dedicated to understanding the principles of object-oriented programming in Python.

Key Concepts
Classes and Objects: I learned how to define classes and create objects.
Inheritance: I explored how to inherit attributes and methods from other classes.
Polymorphism and Encapsulation: I understood how to design flexible and reusable code.
Code Sample: OOP in Python

`class Animal:
def init(self, name):
self.name = name

def speak(self):
    raise NotImplementedError("Subclasses must implement this method")
Enter fullscreen mode Exit fullscreen mode

class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"

dog = Dog("Buddy")
print(dog.speak())`

Week 4: Working with Databases using SQLAlchemy
In the final week, I delved into SQLAlchemy, a powerful ORM (Object-Relational Mapping) library for Python, which allowed me to interact with databases seamlessly.

Key Concepts
Engine and Sessions: I learned how to set up the engine and create sessions to interact with the database.
Models and Schemas: I explored how to define models and map them to database tables.
CRUD Operations: I practised performing Create, Read, Update, and Delete operations using SQLAlchemy.

Code Sample: SQLAlchemy Basics

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Create an engine

engine = create_engine('sqlite:///example.db', echo=True)

Define a base class for declarative class definitions

Base = declarative_base()

Define a model

class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
age = Column(Integer)

Create the table

Base.metadata.create_all(engine)

Create a new session

Session = sessionmaker(bind=engine)
session = Session()

Add a new user

new_user = User(name='Alice', age=30)
session.add(new_user)
session.commit()

Query the database

users = session.query(User).all()
for user in users:
print(user.name, user.age)

Conclusion
The past four weeks have been incredibly rewarding. Starting from the basics of Python, progressing through data structures and OOP, and finally mastering SQLAlchemy has equipped me with a solid foundation for future projects. Each step has been a building block, and I am excited to continue expanding my knowledge and applying these skills to real-world applications.

If you're just starting with Python, I encourage you to take it one step at a time and enjoy the learning process. Happy coding!

Top comments (0)