DEV Community

Victor Karanja
Victor Karanja

Posted on

Python for Beginners (Part 2): Mastering Python Fundamentals

Welcome back!

In Part 1 we learned variables and how to print information. Now it’s time to make our programs smarter and more useful.

In this part you will learn:

  • Comparison operators
  • if, elif, and else
  • for and while loops
  • Functions
  • Lists
  • Dictionaries
  • A complete Student Grade Calculator project

Let’s get started.

1. Comparison Operators

Comparison operators check the relationship between two values and return True or False.

x = 10
y = 5

print(x > y)    # True  (greater than)
print(x < y)    # False (less than)
print(x == y)   # False (equal to)
print(x != y)   # True  (not equal to)
print(x >= 10)  # True  (greater than or equal)
print(x <= 5)   # False (less than or equal)
Enter fullscreen mode Exit fullscreen mode

These operators are the foundation of decision-making in Python.

2. if, elif, and else

These statements let your program choose different actions based on conditions.

score = 85

if score >= 90:
    print("Grade A")
elif score >= 80:
    print("Grade B")
elif score >= 70:
    print("Grade C")
else:
    print("Grade D")
Enter fullscreen mode Exit fullscreen mode

How it works:

  • if checks the first condition
  • elif checks the next condition only if the previous ones were False
  • else runs when none of the conditions are True

Always remember to:

  • Put a colon : at the end of each condition
  • Indent the code under each block (4 spaces)

3. for and while Loops

Loops allow you to repeat code.

for loop – best when you know how many times to repeat:

for i in range(5):
    print(i)
Enter fullscreen mode Exit fullscreen mode

Output:

0
1
2
3
4
Enter fullscreen mode Exit fullscreen mode

while loop – best when you want to keep repeating as long as a condition is True:

count = 0

while count < 3:
    print(count)
    count += 1
Enter fullscreen mode Exit fullscreen mode

Output:

0
1
2
Enter fullscreen mode Exit fullscreen mode

Extra tools:

  • break → stop the loop immediately
  • continue → skip the rest of the current loop and move to the next one

4. Functions

Functions help you write reusable code. You define it once and use it many times.

def greet(name):
    print("Hello,", name)

greet("Alex")
greet("Sam")
Enter fullscreen mode Exit fullscreen mode

You can also return a value:

def add(a, b):
    return a + b

result = add(10, 5)
print(result)   # 15
Enter fullscreen mode Exit fullscreen mode

5. Lists

A list stores multiple items in a specific order.

fruits = ["apple", "banana", "orange"]

print(fruits[0])          # apple
fruits.append("mango")    # add a new item
print(len(fruits))        # 4
Enter fullscreen mode Exit fullscreen mode

You can also loop through a list:

for fruit in fruits:
    print(fruit)
Enter fullscreen mode Exit fullscreen mode

6. Dictionaries

A dictionary stores data as key-value pairs.

student = {
    "name": "Alex",
    "age": 20,
    "grade": "B"
}

print(student["name"])     # Alex
student["grade"] = "A"     # update a value
print(student)
Enter fullscreen mode Exit fullscreen mode

Mini Project: Student Grade Calculator

Now let’s combine everything we learned into a small real project.

def calculate_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"

# List of students (each student is a dictionary)
students = [
    {"name": "Alex", "score": 92},
    {"name": "Sam", "score": 78},
    {"name": "Jordan", "score": 65},
    {"name": "Taylor", "score": 55}
]

print("Student Grade Report")
print("-------------------")

for student in students:
    grade = calculate_grade(student["score"])
    print(f"{student['name']}: {student['score']} → Grade {grade}")
Enter fullscreen mode Exit fullscreen mode

Output:

Student Grade Report
-------------------
Alex: 92 → Grade A
Sam: 78 → Grade C
Jordan: 65 → Grade D
Taylor: 55 → Grade F
Enter fullscreen mode Exit fullscreen mode

What You Learned Today

  • How to compare values
  • How to make decisions with if / elif / else
  • How to repeat actions with loops
  • How to create reusable functions
  • How to store data with lists and dictionaries
  • How to build a complete mini project

Great work!

Try changing the scores or adding more students to practice. The more you experiment, the faster you will improve.

See you in the next part!

Top comments (0)