When we write programs, we often deal with numbers. You use numbers to keep scores in games, calculate data, make charts, or store values in applications. Python handles numbers in a very smart way. It allows us to work with them easily.
The simplest type of number in Python is called an integer. Let’s take a look how integers work.
Integers in Python
An integer means a whole number. It can be positive, negative, or even zero. Python allows you to perform different calculations with integers. You can add, subtract, multiply, and divide them directly.
Addition
>>> 2 + 3
5
Here, Python adds 2 and 3.
The result is 5.
Subtraction
>>> 3 - 2
1
In this case, Python subtracts 2 from 3.
The result is 1.
Multiplication
>>> 2 * 3
6
The star symbol (*
) is used for multiplication.
The answer is 6.
Division
>>> 3 / 2
1.5
Here is something important.
When you divide numbers in Python. The result always comes as a decimal (float) even if the answer looks like a whole number. So, 3 / 2
gives 1.5
.
Exponents
Python also supports exponents.
Exponents mean raising a number to a power.
In Python, you write two multiplication signs (**
) for this.
>>> 3 ** 2
9
>>> 3 ** 3
27
>>> 10 ** 6
1000000
3 ** 2
means 3 raised to the power of 2 → result is 9.3 ** 3
means 3 raised to the power of 3 → result is 27.10 ** 6
means 10 raised to the power of 6 → result is 1,000,000.
This is very useful when you want to work with large numbers or scientific calculations.
Floats in Python
In Python, any number with a decimal point is called a float. This word is also used in most other programming languages.
Why do we call it a float?
Because the decimal point can “float” to any position in the number. For example, 0.1
, 3.14
, and 100.0
are all floats.
Every programming language is designed carefully to handle decimal numbers. This is important so numbers behave correctly. No matter where the decimal point appears.
Working with Floats
Most of the time, you can use floats in Python without any issues. You simply type the numbers, and Python does what you expect.
Let’s see some examples:
>>> 0.1 + 0.1
0.2
>>> 0.2 + 0.2
0.4
>>> 2 * 0.1
0.2
>>> 2 * 0.2
0.4
0.1 + 0.1
gives0.2
.0.2 + 0.2
gives0.4
.2 * 0.1
gives0.2
.2 * 0.2
gives0.4
.
So far, everything looks normal and simple.
Floating-Point Issue
Now, here is something important to notice. Sometimes, you get results with many decimal places, even when you don’t expect them.
For example:
>>> 0.2 + 0.1
0.30000000000000004
>>> 3 * 0.1
0.30000000000000004
Instead of 0.3
, Python shows 0.30000000000000004
.
Why does this happen?
This is not a mistake in Python. This happens in all programming languages, not just Python.
Computers store numbers in binary (0s and 1s). Some decimal numbers cannot be represented exactly in binary form. So Python tries its best to give you the most precise value possible. That’s why you sometimes see extra digits after the decimal point.
Integers and Floats
In Python, you work with both integers and floats. Integers are whole numbers without decimals, and floats are numbers that include decimals. Now, here is something important to remember. When you divide two numbers in Python, the result is always a float, even if the answer looks like a whole number.
For example:
>>> 4 / 2<br>2.0
Notice the result is 2.0 and not 2. Python adds the .0
because it treats the result as a float.
Another rule is: if you mix an integer and a float in any operation, the answer will always be a float. Python does this to keep results consistent. Let’s see an example:
>>> 1 + 2.0
3.0
>>> 2 * 3.0
6.0
>>> 3.0 ** 2
9.0
In each case, the result is a float, even though the answer is a whole number. Python defaults to a float whenever a float is part of the calculation.
This behavior is helpful because it avoids confusion and makes sure your results are accurate in more complex calculations. So, whenever you see .0
in your output, it’s just Python reminding you that the number is a float.
Underscores in Numbers
In Python, you sometimes work with very large numbers. When numbers get too long, it becomes hard to read them quickly. To solve this problem, Python allows you to use underscores (_
) inside numbers. These underscores act like visual separators and make the number easier for humans to read.
For example:
>>> universe_age = 14_000_000_000
Here, the underscores help us read the number as 14 billion more clearly.
Now, if you print this number, Python will not show the underscores:
>>> print(universe_age)
14000000000
Notice how Python gives you only the digits. This is because Python ignores underscores when storing the number. They are only for readability in your code.
Another thing to remember is that you don’t have to group digits in threes like we usually do in math. Python doesn’t care where the underscores are placed. For Python, the following values are exactly the same:
1000
1_000
10_00
All three represent the same value: 1000.
This feature also works with floats, not just integers. You can write:
>>> pi_value = 3.14_159_265
>>> print(pi_value)
3.14159265
Again, Python removes the underscores when it stores or prints the number.
So, underscores are just a tool for you, the programmer, to make numbers easier to read in your code. They do not change the actual value in any way.
Multiple Assignment
In Python, you can assign values to more than one variable in a single line. This makes your code shorter and more readable. Instead of writing many lines, you can group assignments together.
For example, look at this code:
>>> x, y, z = 0, 0, 0
Here, Python assigns the value 0 to x
, 0 to y
, and 0 to z
— all in one step.
How does it work?
- You separate the variable names with commas.
- You also separate the values with commas.
- Python matches each value to the variable in the same order.
So, as long as the number of variables and the number of values are the same, Python will assign them correctly.
This feature is very useful when you want to initialize a group of variables at the beginning of your program.
Constants
A constant is a value that does not change while your program runs. For example, if your program always needs the same maximum limit or a fixed rate, you can use a constant.
Python does not have a special data type for constants. But programmers follow a naming rule to show that a variable should be treated as a constant. The rule is:
- Write the name of the constant in ALL CAPITAL LETTERS.
For example:
MAX_CONNECTIONS = 5000
Here, MAX_CONNECTIONS
is a constant. Even though Python will not stop you from changing it, the capital letters remind you and other programmers that this value should stay the same throughout the program.
So, use constants for values that are fixed and should not be updated. It makes your code more reliable and easier to understand.
Comments
Comments are a very important feature in almost every programming language. Until now, everything you have written in your programs is Python code. But as your programs grow bigger and more complex, you will need a way to add notes inside your code. These notes are called comments.
A comment is not Python code. Instead, it is a message you write for yourself or other programmers to explain what the code is doing. You can write comments in your spoken language, and Python will ignore them while running your program.
How Do You Write Comments?
In Python, you create a comment using the hash mark #
. Anything written after #
on the same line is treated as a comment, not as code.
For example:
# Say hello to everyone.
print("Hello Python people!")
When you run this program, Python ignores the first line (because it is a comment) and executes only the second line. The output is:
Hello Python people!
So, comments are for humans to read, not for Python to execute.
What Kinds of Comments Should You Write?
The main purpose of comments is to explain what your code is doing and how it works.
- When you are actively writing a program, you may remember why you wrote certain code.
- But if you return to the same program after some weeks or months, you might forget the details.
- Instead of wasting time trying to re-understand your own logic, good comments help you recall the purpose and structure quickly.
For example, a comment can describe the overall approach you are using to solve a problem.
Tips for Writing Comments
When you are deciding whether to write a comment, ask yourself:
- Did I have to think of multiple approaches before solving this problem?
- Did I choose one solution after comparing different possibilities?
If the answer is yes, then you should definitely write a comment to explain your reasoning.
It is always easier to delete unnecessary comments later than to add comments to code that has almost none.
From now on, you will see comments in many code examples throughout this book. These comments will explain what specific parts of the program are doing, so you can learn the logic step by step.
TRY IT YOURSELF
Adding Comments
Now it’s your turn to practice writing comments.
- Open two of the programs you have written so far.
- Add at least one comment to each program.
- If your programs are very simple and you don’t know what to write, don’t worry. Just add your name and the current date at the top of the file.
- Also, write one short sentence that explains what the program does.
Example:
Let’s say you have a program called hello.py
. You can add comments like this:
# Author: Muneeb Tariq
# Date: September 17, 2025
# This program prints a simple greeting message.
print("Hello Python world!")
Here:
- The first two lines tell who wrote the program and when.
- The third line explains the purpose of the program.
You can follow the same style for any other program you wrote earlier.
Also Read