0% found this document useful (0 votes)
6 views9 pages

Operators in Python

Uploaded by

Arnav Rajesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
6 views9 pages

Operators in Python

Uploaded by

Arnav Rajesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

DAY-2

Topics Covered:
• Operators in Python
• Conditional Statements

Operators in Python
In Python, operators are special symbols or keywords that perform
operations on operands. Here's an overview of the main types of
operators in Python:

Arithmetic Operators:
1. Addition (+): Adds two operands.

2. Subtraction (-): Subtracts the right operand from the left operand.

3. Multiplication (*): Multiplies two operands.


4. Division (/): Divides the left operand by the right operand (always
results in a float/decimal value).

5. Floor Division (//): Divides the left operand by the right operand and
returns the largest integer less than or equal to the result.

6. Modulus (%): Returns the remainder of the division of the left


operand by the right operand.

7. Exponentiation (): Raises the left operand to the power of the right
operand.

Comparison Operators:
1. Equal to (==): True if the operands are equal.

2
2. Not equal to (!=): True if the operands are not equal.

3. Greater than (>): True if the left operand is greater than the right
operand.

4. Less than (<): True if the left operand is less than the right operand.

5. Greater than or equal to (>=): True if the left operand is greater


than or equal to the right operand.

6. Less than or equal to (<=): True if the left operand is less than or
equal to the right operand.

3
Logical Operators:
1.

2. Logical OR (or): True if at least one operand is true.

3. Logical NOT (not): True if the operand is false, and vice versa.

Membership Operators:
In Python, membership operators are used to test whether a value is a
member of a sequence or collection. There are two membership
operators: `in` and `not in`. They are commonly used with strings, lists,
tuples, and other iterable data types.

1. `in` Operator:

4
The `in` operator returns `True` if a specified value is found in the
sequence.
Example:

2. `not in` Operator:


The `not in` operator returns `True` if a specified value is not found in
the sequence.
Example:

Membership operators are particularly useful when you need to check


whether a specific element exists in a collection before performing
certain actions. They provide a concise and readable way to express
such conditions in Python code.

These are the fundamental operators in Python, and they play a


crucial role in various programming tasks. Understanding how to
use them is essential for writing effective and concise code.

5
Conditional Statements

Conditional statements in Python allow you to control the flow of your


program based on certain conditions. The main conditional statements
in Python are `if`, `else`, and `elif` (short for "else if"). Here's a brief
overview:

1. `if` Statement:
The `if` statement is used to execute a block of code if a specified
condition evaluates to `True`. If the condition is `False`, the code block
is skipped.

2. `else` Statement:
The `else` statement is used to define a block of code that will be
executed if the `if` condition is `False`.

6
3. `elif` Statement:
The `elif` statement allows you to check multiple conditions. It comes
after an `if` statement and before an optional `else` statement. If the `if`
condition is `False`, it checks the `elif` conditions one by one until a
true condition is found, and the corresponding block of code is
executed.

Nested Conditional Statements:


You can nest conditional statements inside each other to handle more
complex scenarios.
Examples:-

7
These conditional statements are crucial for controlling the program's
logic and allowing it to make decisions based on different conditions.
They provide the foundation for building more sophisticated and
responsive programs in Python.

8
Sample Program:
1) Write a python program to determine whether a number is odd
or even
Answer:

2) Write a Python Program to find greatest of three numbers:


Answer:

You might also like