Operators
and Control Structures in Python (Complete Beginner Guide)
After learning about
variables and data types, the next step in programming is understanding
how to perform operations and how to control the flow of a program.
This is where operators and control structures play an important
role.
What are
Operators?
Operators are
symbols that are used to perform operations on variables and values.
They help us in calculations, comparisons, and logical decisions in a program.
The variables and values are called operands.
For example:
x = 10
y = 5
print(x + y)
Here, "+"
is an operator that adds two values.
Types of
Operators in Python
➤ 1. Arithmetic Operators
Arithmetic operators
are those operators that are used to perform mathematical
|
Operators |
Meaning |
Examples |
|
+ |
Addition |
5+3=8 |
|
- |
Subtraction |
5-3=2 |
|
* |
Multiplication |
5*3=15 |
|
/ |
Division |
6/3=2 |
|
% |
Modulus(Remainder) |
5%2=1 |
|
** |
Power |
2**3=8 |
|
// |
Floor Division |
5//2=2 |
👉 Example:
1. a = 10, b = 2
print(a
* b)
2. For Addition
num1=10, num2=20
sum=num1+num2
print(“Sum is:”,sum)
Output:
Sum is 30
3. For Subtraction
Num1=20, Num2=10
Sub=Num1-Num2
print(“Subtraction
is:”,sub)
Output:
Subtraction
is 10
➤ 2. Comparison Operators (Relational Operators)
Comparison Operators
are those operators that are used to compare two values.
|
Operators |
Meaning |
|
== |
Equal to(Check if both values are equal) |
|
!= |
Not
equal |
|
> |
Greater than |
|
< |
Less
than |
|
>= |
Greater than or equal |
|
<= |
Less
than or equal |
· These operators return True or False
Example:
c=100, d=150
print(c==d) #False
print(c!=d) #True
print(c<d) #True
print(c>d) #False
print(c<=d) #True
print(c>=d) #False
➤
3.Assignment Operators
Assignment operators are used to assign value
to variables.
|
Operators |
Examples |
|
= |
x=5 |
|
+= |
x+=2 |
|
-= |
x-=2 |
|
*= |
x*=2 |
|
/= |
x/=2 |
Example:
a=10
print(a)
a+=20
print(a)
➤ 4. Logical Operators
These are type of operators that are used to combine two or more conditions. There are basically three types of logical operators:
1. And : gives true when both conditions must be true
2.
Or : gives true when at least one condition is
true
3.
Not(!) : reverses the condition
Example:
1.
Val1=True,
Val2=False
result=val1
and val2
print(result)
Output:
False
2. x = 10
print(x > 5 and x < 20)
Output:
True
What are
Control Structures?
Control structures
decide how a program runs step by step and in which way a control executes the
program. They exactly control the flow of execution based on conditions or
repetitions.
Types of
Control Structures
There are
three main types of control structure
1. Conditional Structure
2. Iteration or Loop structure
3.
Sequence or Selection Structure
Click here for part 2: Operators and Control Structures in Python (Complete Beginner Guide) Part 2
Variables and Data Types in Python (Complete Beginner Guide)

Comments
Post a Comment