Learning Python – Post 3: Doing Math with Python – Basic Operators and Calculations

Learning Python – Post 3: Doing Math with Python – Basic Operators and Calculations

Today I practiced how to do basic math with Python — and to be honest, it felt really satisfying to see Python acting like a calculator, but smarter.

Here’s a breakdown of the things I tried and what I learned while playing with numbers.

Basic Arithmetic

I started by typing some simple math operations directly into the notebook:

2 + 3

Output:

5
10 - 4

Output:

6
3 * 4

Output:

12
8 / 2

Output:

4.0

Interesting note: even if the division is clean, Python gives a float result (4.0 instead of 4).

Modulus, Exponents and Floor Division

These were new for me, but once I tried them out, they made sense.

Modulus: %

This gives you the remainder of a division.

10 % 3

Output:

1

(10 divided by 3 is 3, remainder 1)

Exponentiation: **

This raises a number to a power.

2 ** 3

Output:

8

Because 2 × 2 × 2 = 8

Floor Division: //

This gives you the whole number part of a division (drops the decimals):

10 // 3

Output:

3

Operation Order (PEMDAS in action)

I also experimented with more complex expressions to see how Python handles operator precedence:

2 + 3 * 4

Output:

14

Because multiplication happens before addition.

But when I added parentheses:

(2 + 3) * 4

Output:

20

So, just like in regular math, parentheses come first. Good to know.

Working with Floats

7 / 2

Output:

3.5

Python handles float division by default when you use /.

Type Conversion

I played around with converting between types:

int(3.9)

Output:

3

It cuts off the decimal part — no rounding.

float(3)

Output:

3.0

Nice and clean.

Mixing Types

Python can handle mixed types in expressions:

3 + 2.5

Output:

5.5

Here, it automatically treats everything as a float.

Quick Exercises I Tried

I tried combining different operations just to test myself:

(3 + 5) * 2 / (4 - 1)

Output:

5.333333333333333
2 ** 3 ** 2

Output:

512

(This one surprised me — it’s 2 ** (3 ** 2), not (2 ** 3) ** 2.)

So Python follows right-to-left for exponentiation.

Leave a Reply

Your email address will not be published. Required fields are marked *