DEV Community

Nitinn S Kulkarni
Nitinn S Kulkarni

Posted on

Python Fundamentals – Data Types, Control Flow, and Loops

Before diving into advanced Python topics, it’s essential to have a solid understanding of basic data types, control flow, and loops. These fundamental building blocks are what make Python such a flexible and powerful language.

In this post, we’ll cover:

✅ Python's core data types and how they work
✅ Control flow statements like if-else, match-case, and try-except
✅ Loops in Python (for, while, and iterators)

Let’s get started! 🚀

1️⃣ Python’s Built-in Data Types

Python provides several built-in data types, categorized as follows:

Category Data Type Example
Numbers int, float, complex 42, 3.14, 1+2j
Text str "Hello, Python!"
Boolean bool True, False
Sequences list, tuple, range [1, 2, 3], (1, 2, 3), range(5)
Mapping dict {"name": "Alice", "age": 25}
Sets set, frozenset {1, 2, 3}, frozenset({1, 2, 3})
Binary bytes, bytearray, memoryview b'hello', bytearray(5), memoryview(bytes(5))
NoneType None None

🔍 Examples of Basic Data Types in Python

Numbers

x = 10        # Integer
y = 3.14      # Float
z = 1 + 2j    # Complex Number
Enter fullscreen mode Exit fullscreen mode

Strings

name = "Python"
Enter fullscreen mode Exit fullscreen mode

Booleans

is_python_easy = True
Enter fullscreen mode Exit fullscreen mode

Lists

fruits = ["apple", "banana", "cherry"]
Enter fullscreen mode Exit fullscreen mode

Tuples

coordinates = (10, 20)
Enter fullscreen mode Exit fullscreen mode

Dictionaries

person = {"name": "Alice", "age": 25}
Enter fullscreen mode Exit fullscreen mode

Sets

unique_numbers = {1, 2, 3, 4, 5}
Enter fullscreen mode Exit fullscreen mode

🔥 Key Takeaways:

✅ Python dynamically assigns types – you don’t need to declare them explicitly.
✅ list and tuple are ordered, while set is unordered and unique.
✅ dict stores key-value pairs.

2️⃣ Control Flow: Making Decisions in Python

Python provides several control flow statements that help us direct program execution based on conditions.

🔹 1. if-elif-else Statements

Python executes blocks of code based on conditions.

age = 18

if age < 18:
    print("You're a minor.")
elif age == 18:
    print("You're exactly 18!")
else:
    print("You're an adult.")
Enter fullscreen mode Exit fullscreen mode

🔹 2. match-case (Python 3.10+)

Python 3.10 introduced match-case, which works like a switch statement.

def check_number(num):
    match num:
        case 1:
            print("One")
        case 2:
            print("Two")
        case _:
            print("Something else")

check_number(2)  # Output: Two
Enter fullscreen mode Exit fullscreen mode

🔹 3. Exception Handling with try-except-finally

Python uses try-except blocks to handle errors gracefully.

try:
    result = 10 / 0  # This will cause an error
except ZeroDivisionError as e:
    print(f"Error: {e}")
finally:
    print("This block runs no matter what.")  # Always executes
Enter fullscreen mode Exit fullscreen mode

3️⃣ Loops: Iterating Over Data

Loops allow us to repeat actions efficiently.

🔹 1. for Loop: Iterating Over a Sequence

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)
Enter fullscreen mode Exit fullscreen mode

🔹 Looping with range()

for i in range(5):  # Prints 0 to 4
    print(i)
Enter fullscreen mode Exit fullscreen mode

🔹 2. while Loop: Repeating Until a Condition is False

counter = 3
while counter > 0:
    print(counter)
    counter -= 1
Enter fullscreen mode Exit fullscreen mode

🔹 Using break and continue

for i in range(5):
    if i == 3:
        break  # Stops the loop when i == 3
    if i == 1:
        continue  # Skips the iteration when i == 1
    print(i)
Enter fullscreen mode Exit fullscreen mode

4️⃣ Iterators and Generators in Python

🔹 1. What is an Iterator?

An iterator is an object that can be iterated over using next().

my_list = [1, 2, 3]
iterator = iter(my_list)

print(next(iterator))  # 1
print(next(iterator))  # 2
print(next(iterator))  # 3
Enter fullscreen mode Exit fullscreen mode

🔹 Using Custom Iterators

class Counter:
    def __init__(self, max):
        self.max = max
        self.current = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.current >= self.max:
            raise StopIteration
        self.current += 1
        return self.current

counter = Counter(3)
for num in counter:
    print(num)
Enter fullscreen mode Exit fullscreen mode

🔹 2. Generators: Efficient Iteration

A generator is a special function that yields values one at a time, saving memory.

def count_up_to(max):
    num = 1
    while num <= max:
        yield num  # Yield instead of return
        num += 1

counter = count_up_to(3)
print(next(counter))  # 1
print(next(counter))  # 2
print(next(counter))  # 3
Enter fullscreen mode Exit fullscreen mode

🔹 Conclusion

✅ Data Types form the foundation of Python programming.
✅ Control Flow Statements (if-else, match-case, try-except) allow for decision-making.
✅ Loops (for, while, iterators, generators`) allow efficient iteration over data.

Understanding these basics will help you write more efficient, structured Python code. 🚀

What’s Next?

In the next post, we’ll explore Functions and Object-Oriented Programming (OOP) in Python. Stay tuned! 🔥

💬 What Do You Think?

What’s your favorite Python feature? Do you use match-case in your projects? Let’s discuss in the comments! 💡

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →