Introduction
Python is a simple, powerful, and widely used programming language. For anyone starting their programming journey, understanding its foundations is essential.
In this article, we’ll cover the basic building blocks of Python, including variables, data types, operators, conditions, loops, and functions. These concepts form the foundation you need before moving into more advanced areas like data analysis, automation, and AI.
What is Python?
Python is a high level programming language that is used to give the computer instructions.
"Think of it like writing a recipe. You write clear, step-by-step instructions and the computer follows them exactly”
Why is python good for beginners?
- Reads almost like English (easier to understand)
- Can build websites, analyse data, build AI models, automate tasks
- Free and open source
- One of the most in-demand skills in tech jobs today
1. Printing Output with print()
print() is Python’s way of showing you something on the screen
“Think of it like sending a text message - you tell Python what to say and it says it”
Everything you want to display/show goes INSIDE the brackets ().
Text must go inside quotes. Numbers DO NOT need quotes.
print("Hello World")
print() tells Python to display something on the screen.
The output will be:
Hello World
python
2. Understanding Variables
Variable is a labelled box where you store a piece of information
Instead of writing ‘Philip’ every time you need the name, you store it once in a box called name
Then you use name wherever you need it
You create a variable with = (a single equals sign)
name = ‘Philip’ → means: create a box called name and put ‘Philip’ inside it.
name = "Philip"
print(name)
Output:
Philip
Python variable naming rules:
- Can contain letter, digits and underscore(_)
- Must start with a letter or underscore - cannot start with a digit
- Cannot contain spaces or special character (-, @, !,%)
- Cannot be reserved keyword
- Case-sensitive (age , Age and AGE) are three different variables
3. Understanding Data Types
Python treats different kinds of data differently
You can add two numbers: 10 + 5 = 15
You can join two text: ‘Hello’ + ‘World’ = ‘Hello World’
But you CANNOT add a number and text: 5 + ‘hello’ → ERROR
→ Python needs to know WHAT TYPE of data something is so it knows what to do with it
The Four main types of data : *string, integer, float, boolean *
String
name = "Philip"
print(name)
Output:
Philip
Integer
age = 25
print(age)
Output:
25
Float
height = 5.9
print(height)
Output:
5.9
Boolean
is_student = True
print(is_student)
Output:
True
Adding two numbers
print(10 + 5)
Output:
15
Joining two strings
print("Hello " + "World")
Output:
Hello World
Mixing a number and text
print(5 + "Hello")
Output:
TypeError
4. Getting User Input with input()
So far Python has been talking to YOU (print).
input() lets the USER talk to python
When python hits input(), it stops and waits. The user types something and presses** Enter*.
Whatever the user typed is returned as a **STRING ALWAYS* even if the user types a number. If you need a number, you must convert it: int(input(‘enter a number’))
name = input("What is your name? ")
print(name)
Output:
What is your name? Philip
Philip
input() always returns the user's answer as a string, even when they enter a number.
age = input("How old are you? ")
print(age)
Output:
How old are you? 25
25
If you need the input as a number, convert it using int():
age = int(input("How old are you? "))
print(age + 5)
Output:
How old are you? 25
30
Top comments (0)