Python Basics

 Understanding Operators and Expressions in Python

Python Operators and Expressions: Understanding the Basics

.Python Operators and Expressions are essential building blocks for writing efficient Python programs. In this beginner-friendly guide, you’ll learn how these operators work, how expressions are evaluated, and how to use them to perform calculations, comparisons, and logical decisions in your Python code. Welcome to our Python for Beginners series! In this lesson, we’ll dive into Python operators and expressions, essential concepts that allow you to perform calculations, compare values, and control the flow of your programs. Understanding these basics is crucial for writing effective and efficient Python code.

By the end of this guide, you’ll be comfortable using Python operators and expressions in real-world scenarios, including arithmetic, comparison, logical operations, and more. Let’s get started

Welcome back to our Python for Beginners series!

In the previous lessons, we covered:

What Python is and why it’s popular

How to create and use variables

Basic data types like int, float, string, and bool

How to take input from the user and display output

Now it’s time to level up — let’s learn how to perform operations with those variables using operators.


 What Are Operators?

Operators are symbols that tell Python to perform a certain operation, like addition, comparison, or logical decision-making.

For example:

a =

b =

print(a + b)  # Adds two numbers

Here, + is an operator that adds the two operands a and b.
a and b are called operands — the values on which the operation is performed.


 1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, etc.

OperatorDescriptionExampleOutput
+Addition5 + 38
Subtraction9 – 45
*Multiplication6 * 212
/Division (returns float)10 / 42.5
//Floor Division (integer part only)10 // 42
%Modulus (remainder)10 % 42
**Exponentiation (power)2 ** 38

Example:

a =

b =

print(“Addition:”, a + b)

print(“Subtraction:”, a – b)

print(“Multiplication:”, a * b)

print(“Division:”, a / b)

print(“Floor Division:”, a // b)

print(“Remainder:”, a % b)

print(“Power:”, a ** b)

Output:

Addition:13

Subtraction:7

Multiplication:30

Division:3.3333333333333335

Floor Division:3

Remainder:1

Power:1000


2. Comparison Operators

Comparison operators help us compare two values.
They return a Boolean value — either True or False.

OperatorDescriptionExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
Greater than8 > 5True
Less than2 < 10True
>=Greater than or equal to5 >= 5True
<=Less than or equal to3 <= 2False

 Example:

a =

b =

print(a == b)   # False

print(a != b)   # True

print(a > b)    # True

print(a < b)    # False

print(a >= b)   # True

print(a <= b)   # False


3. Logical Operators

Logical operators are used to combine conditional statements (i.e., comparisons).
They are extremely important in decision-making and control flow (which we’ll explore in the next post!).

OperatorMeaningExampleOutput
andTrue only if both conditions are True(5 > 3) and (8 > 6)True
orTrue if at least one condition is True(5 > 10) or (8 > 6)True
notReverses the conditionnot(5 > 3)False

 Example:

x =

print(x > 5and x < 20)  # True (both conditions are true)

print(x > 15or x == 10) # True (second condition is true)

print(not(x == 10))      # False (because x == 10 is True)


4. Assignment Operators

Assignment operators are used to assign values to variables and even update them easily.

OperatorExampleSame As
=x = 5Assigns value 5 to x
+=x += 3x = x + 3
-=x -= 2x = x – 2
*=x *= 2x = x * 2
/=x /= 2x = x / 2
//=x //= 3x = x // 3
%=x %= 2x = x % 2
**=x **= 3x = x ** 3

 Example:

x =

x +=

print(x)  # 15

x *=

print(x)  # 30

x //=

print(x)  # 7


 5. Bitwise Operators (Optional but Fun!)

Bitwise operators work on binary numbers (bits: 0 and 1).
They are mostly used in low-level programming, but it’s good to know them.

OperatorMeaningExampleResult
&AND5 & 31
``OR`5
^XOR5 ^ 36
~NOT~5-6
<< Left shift5 << 110
>> Right shift5 >> 12

 Example:

a = 5   # 0101

b = 3   # 0011

print(a & b)  # 1

print(a | b)  # 7

print(a ^ b)  # 6


 6. Expressions in Python

An expression is a combination of values, variables, and operators that produces a result.

 Examples:

x =

y =

z = (x + y) * 2  # Expression

print(z)  # Output: 30

Even a simple line like:

print(3 + 4)

is an expression because Python evaluates it and returns a value.


 Practice Time!

Try these mini challenges

Write a program to take two numbers as input and:

Print their sum, difference, product, and remainder

 Write a program to check if a number is even or odd using modulus %.

 Write a program that asks for two numbers and checks:

If both are positive

If at least one is greater than 100

Bonus: Use an expression to calculate the area of a circle (πr²) given radius r.


 Summary

In this post, you learned about:

Arithmetic, Comparison, Logical, and Assignment operators

How Python evaluates expressions

How to combine operators for complex conditions

These concepts are foundational — you’ll use them in almost every Python program!

About the author

guestpostlinkingum@gmail.com

Leave a Comment