Skip to main content

prasath M

 

Types of Operators in Python 

In Python, operators are special symbols used to perform operations on variables and values. They are an essential part of programming because they allow calculations, comparisons, and logical decisions.

Python provides several types of operators.


 Arithmetic Operators

These operators perform mathematical calculations.

OperatorMeaningExample
+Addition5 + 3 = 8
-Subtraction5 - 3 = 2
*Multiplication5 * 3 = 15
/Division6 / 3 = 2.0
%Modulus (remainder)5 % 2 = 1
**Exponent (power)2 ** 3 = 8
//Floor division7 // 2 = 3


Example

a = 10
b = 3
print(a + b)
print(a % b)

 Comparison (Relational) Operators

These operators compare two values and return True or False.

OperatorMeaningExample
==Equal to5 == 5
!=Not equal5 != 3
>Greater than7 > 5
<Less than3 < 5
>=Greater than or equal5 >= 5
<=Less than or equal3 <= 4

Example

x = 5
y = 10
print(x < y)

 Logical Operators

Logical operators are used to combine conditional statements.

OperatorMeaning
andReturns True if both conditions are True
orReturns True if one condition is True
notReverses the result

Example

a = 5
b = 10
print(a < b and b > 0)

 Assignment Operators

These operators are used to assign values to variables.

OperatorExample
=x = 5
+=x += 3
-=x -= 2
*=x *= 4
/=x /= 2

Example

x = 5
x += 3
print(x)

 Membership Operators

Used to check if a value exists in a sequence.

OperatorMeaning
inValue exists
not inValue does not exist

Example

list = [1,2,3,4]
print(3 in list)

 Identity Operators

Used to compare the memory location of objects.

OperatorMeaning
isObjects are the same
is notObjects are not the same

Example

x = 5
y = 5
print(x is y)

Summary Table

Operator TypePurpose
ArithmeticMathematical calculations
ComparisonCompare values
LogicalCombine conditions
AssignmentAssign values
MembershipCheck values in sequence
IdentityCompare memory location

Comments

Popular posts from this blog

Prasath M

  Data Types in Python In Python, a data type specifies the kind of value a variable can store and determines the operations that can be performed on it. Python is dynamically typed , meaning you do not explicitly declare the data type—Python infers it at runtime. Below is a structured explanation of the primary built-in data types. 1. Numeric Types 1.1 int Represents whole numbers (positive, negative, or zero). x = 10 y = - 5 No decimal point Unlimited precision (only limited by memory) 1.2 float Represents decimal (floating-point) numbers. pi = 3.14 temp = - 2.5 Uses double precision (64-bit IEEE 754) 1.3 complex Represents complex numbers (real + imaginary part). z = 3 + 4j 3 → real part 4j → imaginary part 2. Sequence Types 2.1 str (String) Represents textual data. name = "Prasath" msg = 'Hello' Immutable (cannot be changed after creation) Supports indexing and slicing 2.2 list Ordered, mutabl...

prasath M

  Types of Loops in Python  In Python , loops are fundamental control structures that allow a program to execute a block of code repeatedly . They are commonly used for tasks like processing lists, iterating through data, or performing repeated calculations. Python mainly provides two primary types of loops . 1. For Loop The for loop is used to iterate over a sequence such as a list, tuple, string, or a range of numbers. It is typically used when the number of iterations is known . Syntax for variable in sequence : # code block Example for i in range ( 5 ): print ( i ) Output 0 1 2 3 4 Explanation range(5) generates numbers from 0 to 4 . The loop runs once for each value in the sequence. 2. While Loop The while loop repeatedly executes a block of code as long as a condition remains true . It is commonly used when the number of iterations is not predetermined . Syntax while condition : # code block Example i = 1 while i <=...
  Date and Time Module in Python  Python provides built-in modules to work with date and time . The most commonly used modules are: datetime module – used to work with dates and times. time module – used for time-related functions such as delays and timestamps. 1. datetime Module The datetime module helps to get the current date, time, and perform date calculations . Importing the module import datetime Example: Display Current Date and Time import datetime now = datetime . datetime . now() print ( "Current Date and Time:" , now ) Output (example): Current Date and Time: 2026-03-09 10:30:15.234567 Example: Display Only Current Date from datetime import date today = date . today() print ( "Today's Date:" , today ) Output: Today's Date: 2026-03-09 Example: Formatting Date and Time from datetime import datetime now = datetime . now() formatted = now . strftime( "%d-%m-%Y %H:%M:%S" ) print ( "Formatt...