When you start learning programming, one thing you quickly realise is that it looks more intimidating from the outside than it actually is. Before starting, I knew Python was popular. I knew it was used for things like data analysis, automation, websites and AI. But knowing what Python is used for and actually writing Python code are two different things.
The ultimate roadmap is to start with the basics instead of trying to jump into complicated projects.
The goal is simple:
- Understand what Python actually is
- Print things to the screen
- Store information in variables
- Understand basic data types
- Get information from a user
- Convert between data types
- Use f-strings
- Work with operators
- Make decisions using
if,elifandelse - Combine conditions using
and,orandnot - Understand nested
ifstatements
If you're completely new to Python, this is basically where I started.
So, What Is Python?
Python is a programming language. It does sound obvious, but as a beginner, I think it helps to think about programming in a simpler way.
A programming language is basically a way of giving instructions to a computer. Take a recipe for example:
Think of it like writing a recipe. You write clear, step-by-step instructions and the computer follows them exactly.
Python is also a good language because its syntax is relatively easy to read.
Let's get into it.
My First Line of Python
The first thing I learned was print().
print("Hello World")
Output:
Hello World
print() is Python's way of showing something on the screen.
You put whatever you want Python to display inside the brackets.
For example:
print("I am learning python")
print("My name is Sylvia")
print(3.14)
You can also print more than one thing:
print("Python is", "great")
Output:
Python is great
Printing Blank Lines
You can also use print() without putting anything inside it.
print("Line one")
print()
print("Line three")
Output:
Line one
Line three
The empty print() gives us a blank line.
Another way of doing something similar is \n.
print("Line one\n")
print("Line three")
\n represents a new line.
These small things become useful when you're trying to make the output of your program easier to read.
Variables: Giving Information a Name
The easiest way I've found to understand a variable is to think of it as a labelled box. You put some information inside the box, give the box a name and then use that name whenever you need the information.
For example:
name = "Sylvia Ndili"
age = 26
city = "Nairobi"
Here, I've created three variables:
nameagecity
And I've stored different pieces of information in them.
I can then print them:
print(name)
print(age)
print(city)
Instead of writing "Sylvia Ndili" every time I need the name, I can just use name.
You can also combine them:
print(name, "is", age, "years old and lives in", city)
Output:
Sylvia Ndili is 26 years old and lives in Nairobi
This is one of those simple concepts that will come in handy later.
What Does the = Sign Actually Mean?
When I first saw something like this:
name = "Sylvia"
it was easy to look at = and think of it as "equals".
In Python, when we're creating a variable, it's better to think of it as assignment.
name = "Sylvia"
means we're assigning "Sylvia" to the variable name.
We can also change what is stored in a variable.
score = 50
print("Score before:", score)
score = 75
print("Score after:", score)
Output:
Score before: 50
Score after: 75
The old value has been replaced.
We can also use the current value to calculate a new one:
score = 75
score = score + 10
print("Score plus 10:", score)
Now the score is 85.
With this, you learn that variables aren't necessarily permanent. The value inside them can change as the program runs.
Naming Variables
There are rules for naming variables.
The common style in Python is called snake_case.
For example:
student_name = "Sylvia"
phone_number = "0712345678"
total_score = 450
The words are separated using underscores.
There are also names that won't work:
2name = "Sylvia"
my-name = "Sylvia"
class = "Python"
For example, a variable can't start with a number, class is a reserved keyword and - isn't used to separate words in variable names.
Python variable names are also case-sensitive. That means: age, Age, AGE are three different variable names.
Data Types: What Type of Information Are We Storing?
This is an important part as Python treats different kinds of data differently.
There are four main data types:
| Type | Meaning | Example |
|---|---|---|
str |
String/text | "Sylvia" |
int |
Integer/whole number | 26 |
float |
Number with a decimal | 1.68 |
bool |
True or False | True |
Let's look at each one.
Strings
A string is text and normally written inside quotes.
name = "Sylvia Ndili"
city = "Nairobi"
Integers
An integer is a whole number. There are no quotes around the numbers.
age = 26
score = 87
Floats
A float is a number containing a decimal.
height = 1.68
balance = 2350.50
Booleans
A boolean has only two possible values:
True
False
For example:
is_student = True
has_id_card = False
Booleans might not make sense right now, but become extremely useful when we get to conditions.
How Do I Know What Type Something Is?
Python has a function called type().
name = "Sylvia"
age = 26
height = 1.68
has_id_card = False
print(type(name)) #string
print(type(age)) #integer
print(type(height)) #float
print(type(has_id_card)) #boolean
Python will tell us what type each value is. This is useful when you're not sure what kind of data you're actually working with.
"5" Is Not the Same as 5
These two things look almost identical but Python sees them differently.
number_a = 5
number_b = "5"
5 is an integer while "5" is a string.
For example:
print(number_a + number_a)
gives: 10
But:
print(number_b + number_b)
gives: 55
This is because Python is treating "5" as text, so + joins the two strings together.
The same thing happens here:
print("5" + "3")
Output: 53
But:
print(5 + 3)
Output: 8
This was a very useful beginner lesson for me. Python needs to know what type of data it is dealing with.
Something that looks like a number isn't necessarily a number.
Converting Between Types
Sometimes you get data in one type but need another type. Python gives us functions for converting between some common types.
For example, we can convert a string into an integer using int().
text_number = "42"
real_number = int(text_number)
print(real_number)
Python now treats 42 as a number, so we can do arithmetic with it.
We can also convert a number into a string:
age = 24
age_str = str(age)
print(type(age))
print(type(age_str))
The first variable is an integer and the second is a string.
We can also convert something to a float:
price = float("99.99")
print(price)
input(): Letting the User Talk to Python
So far, Python has mostly been talking to us. With input() the user now gets a chance to talk back.
For example:
name = input("What is your name? ")
print()
print("Hello,", name)
print("Welcome to Python class")
When Python reaches input(), it stops and waits for the user to type something.
input() always returns a string as default data type even if the user enters a number.
So if you write:
age = input("How old are you? ")
and enter: 26 , Python receives "26" as a string.
If you want to treat it as a number, you need to convert it as shown below, using int and input at once:
age = int(input("How old are you? "))
print("You are", age, "years old.")
First, input() gets the user's answer. Then int() converts that answer from a string into an integer. The same idea works with floats also.
f-Strings: A Cleaner Way to Print
There is another way of putting variables and text together: f-strings.
Before f-strings, we could do something like:
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello", name, "!", "You are", age, "years old.")
This works, but it can get difficult to read with many variables.
With an f-string:
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello {name}! You are {age} years old.")
Output:
Hello Daniel! You are 24 years old.
The f before the quote tells Python that we're going to put variables or expressions inside {}.
Operators and Conditionals
Here, we are now giving python conditions and saying:
If this is true, do this.
The first thing we need for that is comparison operators.
Comparison Operators
A comparison operator compares two values and gives us either:
True or: False
Think of it as asking a yes-or-no question.
For example:
age = 20
print(age == 20)
Python answers: True
But:
age = 20
print(age == 25)
gives:False
Here are the comparison operators I learned:
| Operator | Meaning | Example |
|---|---|---|
== |
Equal to | 7 == 7 |
!= |
Not equal to | 7 != 2 |
> |
Greater than | 12 > 9 |
< |
Less than | 4 < 2 |
>= |
Greater than or equal to | 6 >= 6 |
<= |
Less than or equal to | 5 <= 3 |
One thing that confused me initially was the difference between:
=and==. A single=is used when assigning a value, while two equals signs==are used when asking whether two values are equal:
Comparing Strings
We can compare text too.
track = "Data Engineering"
print(track == "Data Engineering")
This gives: True
But Python is case-sensitive. So:
print(track == "data engineering")
gives: False
Strings can also be compared alphabetically:
print("Amina" < "Collins")
This is True because A comes before C.
if: Conditionals
An if statement allows Python to make a decision.
For example:
score = int(input("Enter your score: "))
if score >= 50:
print("You passed")
print("Thank you for taking the test.")
The important parts here are:
if score >= 50:
and the indentation:
print("You passed")
The colon : is required and indentation is also important. The indented code belongs to the if statement. If the score is 50 or higher, Python prints: You passed. If the score is less than 50, Python skips that line.
But this line: print("Thank you for taking the test.") will still run because it isn't indented under the if.
That's something you need to be keen on because Python uses indentation to show which code belongs together.
if and else
What if we want Python to do something when the condition is false too?
That's where else comes in.
score = int(input("Enter your score: "))
if score >= 50:
print("Pass - Well done")
else:
print("FAIL - Please try again")
print(f"Your score was: {score}")
Basically, if the condition is true, Python takes the if path. If it's false, Python takes the else path. Only one of them runs.
The final print() isn't part of either path because it isn't indented.
elif: ELSE IF
This is for more than two possible outcomes. For example, we're assigning grades.
score = int(input("Enter your score: "))
if score >= 80:
print("Grade A - Excellent")
elif score >= 70:
print("Grade B - Good")
elif score >= 60:
print("Grade C - Average")
elif score >= 50:
print("Grade D - Below Average")
else:
print("Grade F - Failed")
Here, there are several possibilities. Python checks them from the top down.
Python stops at the first condition that is true.
So if the score is 85, Python checks:
score >= 80
That's true. It prints Grade A and stops checking the other conditions.
Why the Order of if and elif Matters
This was one of the more interesting mistakes to understand.
Look at this:
score = 85
if score >= 50:
print("Grade D")
elif score >= 70:
print("Grade B")
elif score >= 80:
print("Grade A")
You might expect 85 to produce Grade A. But it won't.
The first condition is:
score >= 50
That's already true. So Python prints: GRADE D and stops. It never gets to the Grade B or Grade A conditions. That's why we need to put the highest boundary first, which is why we start with the highest score first. Python checks from top to bottom and stops when it finds the first true condition.
Logical Operators: When One Condition Isn't Enough
Sometimes asking one question isn't enough.
For example, imagine we want to give someone access only if: they are at least 18 and they have an ID.
We can use and.
There are three logical operators:
| Operator | Meaning |
|---|---|
and |
Both conditions must be true |
or |
At least one condition must be true |
not |
Flips True to False and False to True |
and
With and, both conditions must be true.
age = int(input("Age: "))
has_id = input("Do you have an ID? (yes/no) ")
if age >= 18 and has_id == "yes":
print("Access granted - Welcome")
else:
print("Access Denied")
Someone who is 20 but doesn't have an ID is denied. Someone who has an ID but is 15 is also denied. Both conditions need to be true.
You can think about it like:
True and True → True
True and False → False
False and True → False
False and False → False
or
With or, we only need one condition to be true.
For example, imagine a discount for students and senior citizens.
is_student = input("Are you a student? (yes/no): ")
is_senior = input("Are you 60+ ? (yes/no): ")
if is_student == "yes" or is_senior == "yes":
print("Discount Applied - 25% off")
else:
print("Full price applies")
A student qualifies. A senior qualifies. Someone who is both also qualifies. Only when both answers are no do they pay full price.
So:
True or True → True
True or False → True
False or True → True
False or False → False
The easiest way I've found to remember it is:
and = both. or = at least one
not
The third logical operator is not. It basically flips a boolean value.
not True → False
not False → True
A simple real-life way to think about it would be:
The alarm rings if the door is not locked.
We're basically reversing the condition.
Combining Conditions
We can combine comparison operators and logical operators.
For example, imagine a student needs: a score of at least 50 and and attendance of at least 75% to pass.
score = int(input("Score: "))
attendance = int(input("Attendance %: "))
if score >= 50 and attendance >= 75:
print("PASS")
elif score >= 50 and attendance < 75:
print("FAIL - Poor Attendance")
elif score < 50 and attendance >= 75:
print("FAIL - Low Score")
else:
print("FAIL - Both score and attendance below minimum")
Now Python is evaluating more than one piece of information before deciding what to do. This is where variables, data types, comparison operators and logical operators start coming together.
Nested if: A Decision Inside a Decision
The last concept here is a nested if.
A nested if is simply an if statement inside another if statement. This is useful when the second question only makes sense after the first question has been answered.
Take an example of a login system.
First:
Is this the correct username?
Only if the answer is yes do we need to ask:
Is this the correct password?
We can write it like this:
username = input("Username: ")
if username == "admin":
password = input("Password: ")
if password == "kenya2025":
print("Welcome Admin")
else:
print("Wrong Password. Try again")
else:
print("User not found")
Notice that the second if is inside the first one.
The password question only happens if: username == "admin" is true.
This is the main reason to use a nested if: the second decision depends on the first one.
Nested if vs elif
I initially found these two concepts a bit hard to differentiate. You can think about it like this:
Use elif when you're asking the same question with different possible answers.
For example:
track = input("Your track (DS or DE): ")
if track == "DS":
print("Your next course: Pandas and NumPy")
elif track == "DE":
print("Your next course: Airflow and Kafka")
else:
print("Unknown track")
We're asking: Which track are you in?. There are different possible answers.
But with a nested if, the second question depends on the first answer.
For example:
has_laptop = input("Do you have a laptop? (yes/no) ")
if has_laptop == "yes":
os_type = input("Windows or Mac? ")
if os_type == "Windows":
print("Install Python from python.org")
else:
print("Python may already be installed - check with python3 --version")
else:
print("Please borrow a laptop for this session")
There's no point asking: Windows or Mac?, if the person doesn't even have a laptop. That's the kind of situation where a nested if makes sense.
Bringing the concepts together
For the final, you can try incorporating all concepts together and build a small mini program.
The program below asks whether someone has a bank account. If they don't, we're done. If they do, we ask how long they've been a customer. If they've been a customer for at least six months, we then ask about their salary.
has_account = input("Do you have a bank account? (yes/no): ")
if has_account == "no":
print("Sorry, you need an account first.")
else:
months = int(input("How many months have you been a customer? "))
if months < 6:
print("You need at least 6 months of account history.")
else:
salary = int(input("What is your monthly salary? "))
if salary < 20000:
print("Minimum salary for loan is Ksh 20,000")
else:
print("Congratulations! You qualify for a loan")
This example uses quite a few things from the concepts covered:
input(), Variables, Strings, Integers, int() conversion, Comparison operators, if, else, Nested if
My biggest takeaway from this is that, the individual concepts aren't really isolated concepts, they eventually start working together.
Final Thoughts
Although these concepts seem straightforward, they are important because they form the foundation for writing more useful programs. Once you understand how Python stores information, compares values and makes decisions, it becomes much easier to build on top of these ideas. You don't have to build a program on your first try.
For me, a fair progression in mastering python would look like this:
Tell Python something
↓
Store information
↓
Understand what type the information is
↓
Get information from the user
↓
Work with the information
↓
Compare the information
↓
Make decisions based on the comparison
And that last part is where programming starts getting interesting. We're no longer just telling Python what to say. We're starting to tell it how to respond depending on what happens.
There's so much more Python to learn. But for now, understanding these fundamentals gives us a solid starting point for moving into more advanced topics. And I think that's a good place to start.
Top comments (0)