Introduction
When I started learning Python, one thing that stood out to me was how readable the language felt. Compared with what I imagined programming would look like, Python seemed almost conversational. You could write a few lines of code, run them, and immediately see something happen.
Of course, that simplicity can be a little misleading. Python may have a beginner-friendly syntax, but there's still quite a bit you need to understand before your code starts making sense.
One of the best places to start is with the fundamentals: variables, data types, input and output, and basic Python syntax.
These concepts might not sound particularly exciting, but they are the foundation for almost everything you will eventually do in Python. Whether you're building an application, automating a task, analysing data, or working with databases, you'll constantly be using the ideas introduced here.
So let's start from the beginning.
Getting to Know Python
Python is a high-level, general-purpose programming language designed for readability and simplicity. It is widely used in web development, automation, data analysis, data science, artificial intelligence, and machine learning.
One reason Python is so popular is its relatively straightforward syntax.
For example, if we want Python to display a message, we can write:
print("Hello, Python!")
There is no complicated setup required. We tell Python what we want it to do, and Python does it.
That simplicity is one reason Python is such a good language for beginners. But before we can start building anything meaningful, we need to understand how Python stores and works with information.
Variables: Giving Information a Name
Almost every program needs to work with information.
A program might need to remember someone's name, store their age, track a product's price, or calculate the total amount spent by a customer.
This is where variables come in.
A variable is a name or label that references an object stored in your computer's memory.
For example:
name = "Ian"
age = 25
Here, name refers to "Ian" and age refers to 25.
We can then use those variables later in our program:
name = "Ian"
age = 25
print(name)
print(age)
The output is:
Ian
25
The useful thing about variables is that we don't have to keep writing out the actual values every time. Instead, we give the information a meaningful name and work with that name.
Variables can also change.
age = 25
age = 26
print(age)
The output will be:
26
This ability to store and update information is fundamental to programming.
Choosing Good Variable Names
Python provides rules for naming variables, but good practices matter too.
For example:
student_name = "Brian"
student_age = 22
These names tell us exactly what the variables represent.
Compare that with:
x = "Brian"
y = 22
The second example is valid Python, but x and y don't tell us much.
As programs become larger, meaningful variable names become increasingly important. Good names make your code easier to read, understand, and maintain.
Python variable names can contain letters, numbers, and underscores, but they cannot begin with a number or contain spaces.
For example:
student_name = "Brian"
student2_name = "Mary"
are valid, while:
2student = "Brian"
student name = "Mary"
are not.
Python is also case-sensitive, meaning name, Name, and NAME would be treated as different names.
Data Types: Not All Data Is the Same
Now that we know how to store values, there is another important question:
What kind of values are we storing?
Python uses data types to describe the kind of information a value represents.
Some of the most common types you'll encounter are strings, integers, floats, and Booleans.
A string, represented by str, is text:
name = "Ian"
city = "Nairobi"
An integer, represented by int, is a whole number:
age = 25
students = 40
A float is a number containing a decimal value:
price = 1500.50
average_score = 78.2
And a Boolean, represented by bool, can have one of two values:
is_logged_in = True
has_paid = False
Booleans become particularly useful when we start working with conditions because they allow programs to make decisions based on whether something is True or False.
For example:
age = 20
is_adult = age >= 18
print(is_adult)
Python evaluates age >= 18 and produces:
True
Checking a Data Type
Python provides a built-in function called type() that allows us to check what type of value we're working with.
name = "Ian"
age = 25
height = 1.75
is_student = True
print(type(name))
print(type(age))
print(type(height))
print(type(is_student))
The output will be:
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
Understanding data types is important because different types behave differently.
For example, these two variables may look similar:
age = 25
and:
age = "25"
But they aren't the same.
The first contains an integer, while the second contains a string containing the characters 2 and 5.
That distinction becomes important when performing calculations.
Working with Numbers
Python can perform the basic mathematical operations you would expect.
For example:
a = 10
b = 3
print(a + b)
print(a - b)
print(a * b)
print(a / b)
The output is:
13
7
30
3.3333333333333335
Python also provides operators for other types of calculations.
The % operator, for example, gives us the remainder after division:
print(10 % 3)
This produces:
1
The ** operator is used for exponentiation:
print(5 ** 2)
which produces:
25
These operators may seem simple, but they become extremely useful when we start writing programs that calculate totals, averages, percentages, financial values, and other metrics.
Giving Information to the User
So far, we've given Python all the information it needs directly in the code.
But programs become much more useful when users can interact with them.
Python gives us two particularly important tools for this: input() and print().
We've already seen print(), which is used to display information:
print("Welcome to Python!")
The input() function works in the opposite direction. It allows the program to ask the user for information.
For example:
name = input("Enter your name: ")
print(name)
When the program runs, the user might enter:
Enter your name: Davis
Python then stores that input in the name variable.
We can combine this with an f-string to create a more natural message:
name = input("Enter your name: ")
print(f"Hello, {name}!")
If the user enters Davis, Python produces:
Hello, Davis!
This is a small example, but it introduces an important idea: programs don't always have to work with information known in advance, before the code was written.
They can receive information while they are running.
The Important Relationship Between input() and Data Types
There is one detail about input() that every Python beginner should understand:
input() always returns a string.
Consider this:
age = input("Enter your age: ")
print(type(age))
If the user enters:
25
Python still treats the value as:
"25"
which is a string.
This can cause problems if we perform mathematical operations on it.
For example:
age = input("Enter your age: ")
print(age + 5)
Python will produce an error because it cannot directly add an integer to a string.
To solve this, we can convert the input to an integer using int():
age = int(input("Enter your age: "))
print(age + 5)
Now, if the user enters 25, Python stores it as the integer 25, and the calculation works.
We can also convert input to a floating-point number:
price = float(input("Enter the price: "))
If the user enters 1500.50, Python stores the value as a float.
This process of changing a value from one data type to another is called type conversion.
Putting Variables, Data Types, Input, and Output Together
Once we understand these individual concepts, we can start combining them to create small but useful programs.
Imagine we want to create a simple program that collects a student's name and three scores and then calculates their average.
We could write:
name = input("Enter student name: ")
score1 = int(input("Enter first score: "))
score2 = int(input("Enter second score: "))
score3 = int(input("Enter third score: "))
average = (score1 + score2 + score3) / 3
print(f"Student: {name}")
print(f"Average score: {average}")
If the user enters:
Enter student name: Ian
Enter first score: 80
Enter second score: 75
Enter third score: 90
The program calculates the average and produces:
Student: Ian
Average score: 81.66666666666667
We're using strings for the student's name, integers for the scores, input() to collect information, int() to convert the scores, arithmetic operators to calculate the average, and print() and f-strings to display the result.
This is where Python starts becoming interesting. Individual concepts that seemed small on their own begin working together to solve an actual problem.
A Quick Look at Python Syntax
Learning Python isn't only about knowing what different functions do. You also need to understand how Python expects code to be structured.
One of the most important characteristics of Python is its use of indentation.
For example:
age = 20
if age >= 18:
print("You are an adult.")
Notice the spaces before print().
The indentation tells Python that the print() statement belongs to the if statement.
This is different from some programming languages that use curly braces to define blocks of code. In Python, indentation is part of the syntax.
Python is also strict about capitalization.
For example:
print("Hello")
works, but:
Print("Hello")
does not.
Similarly:
name = "Ian"
Name = "Brian"
creates two different variables.
These details may seem small, but they become second nature with practice.
Comments: Leaving Notes in Your Code
Python also allows us to add comments to our code.
A comment begins with #:
# Store the student's name
name = "Ian"
# Display the name
print(name)
Python ignores comments when running the program.
Comments can be useful when explaining why a particular piece of code exists or leaving notes for other developers. However, they shouldn't explain every obvious line of code. Ideally, your code should be readable enough to explain itself wherever possible.
From Small Building Blocks to Bigger Programs
At this point, variables, data types, input, output, operators, and syntax might seem like separate topics.
In reality, they are closely connected.
A typical Python program might receive information from a user using input(), store the information in variables, convert it into the appropriate data types, perform calculations using operators, and finally display the result using print().
For example:
name = input("Enter your name: ")
salary = float(input("Enter your monthly salary: "))
expenses = float(input("Enter your monthly expenses: "))
savings = salary - expenses
print(f"{name}, your monthly savings are Ksh {savings}.")
A user might enter:
Enter your name: Ian
Enter your monthly salary: 60000
Enter your monthly expenses: 35000
and receive:
Ian, your monthly savings are Ksh 25000.0.
It's a simple program, but it demonstrates the basic flow of many real programs:
Input → Process → Output
The user provides information, the program processes it, and the result is displayed.
Final Thoughts
Learning Python starts with understanding a handful of fundamental ideas.
Variables give us a way to store and work with information. Data types tell Python what kind of information we're dealing with. input() allows our programs to interact with users, while print() allows us to display information and results.
Then there is syntax: indentation, capitalization, operators, comments, and the structure of Python statements. These may seem like small details at first, but becoming comfortable with them is what allows you to write Python code without constantly fighting the language.
The important thing is not to rush through these concepts simply because they are considered "basics." These are the building blocks you'll keep coming back to as you learn more advanced topics.
Eventually, you'll move from variables and input() to conditionals and loops. Then you'll start writing functions, working with lists and dictionaries, handling files, building classes, querying databases, and analysing data.
But every one of those steps builds on the same foundation.
When you're learning Python, you don't need to understand everything at once. Write small programs. Make mistakes. Read the error messages. Change the code and run it again.
Eventually, those strange-looking lines of Python stop feeling like instructions you're memorizing and start feeling like ideas you're expressing.
And that's when learning to code starts becoming genuinely fun.
Top comments (0)