0% found this document useful (0 votes)
40 views

Operator and If-Else

Python provides various operators like arithmetic, comparison, assignment and logical operators to perform operations. It uses indentation through whitespace to define code blocks instead of curly braces. The if-else statement executes code based on a condition being true or false, while elif allows checking multiple conditions. Nested if statements allow if-else constructs inside other if-else constructs, enabling complex conditional logic.

Uploaded by

tarun agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Operator and If-Else

Python provides various operators like arithmetic, comparison, assignment and logical operators to perform operations. It uses indentation through whitespace to define code blocks instead of curly braces. The if-else statement executes code based on a condition being true or false, while elif allows checking multiple conditions. Nested if statements allow if-else constructs inside other if-else constructs, enabling complex conditional logic.

Uploaded by

tarun agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Python Operators

The operator can be defined as a symbol which is responsible for a


particular operation between two operands. Operators are the pillars of a
program on which the logic is built in a specific programming language.
Python provides a variety of operators, which are described as follows.

1. Arithmetic operators (+, -, *, /, **, %, //)


2. Comparison operators or relational operator (==,!
=,<,>,<=,>=)
3. Assignment Operators (=, +=, -=, **=, *=)
4. Logical Operators (and, or , not )
Python if-else (deicision making statement)

Python indentation
Python indentation uses to define the block of the code. The other programming
languages such as C, C++, and Java use curly braces {}, whereas Python uses an
indentation. Whitespaces are used as indentation in Python.

Indentation uses at the beginning of the code and ends with the unintended line. That
same line indentation defines the block of the code (body of a function, loop, etc.)

Generally, four whitespaces are used as the indentation. The amount of indentation
depends on user, but it must be consistent throughout that block.
The if-else statement
The if-else statement provides an else block combined
with the if statement which is executed in the false case of
the condition.
If the condition is true, then the if-block is executed.
Otherwise, the else-block is executed.
The elif statement
The elif statement enables us to check multiple conditions
and execute the specific block of statements depending
upon the true condition among them. We can have any
number of elif statements in our program depending upon
our need. However, using elif is optional.
The elif statement works like an if-else-if ladder statement
in C. It must be succeeded by an if statement.
The syntax of the elif statement is given below.
Python nested IF statements
In a nested if construct, you can have
an if...elif...else construct inside
another if...elif...else construct.
Example 1:
# Python program to demonstrate
# nested if statement
  
   
num = 15
if num >= 0:
    if num == 0:
        print("Zero")
    else:
        print("Positive number")
else:
    print("Negative number")

Example 2:
Syntax
The syntax of the nested if...elif...else construct may be −
if expression1:
statement(s)
if expression2:
statement(s)
elif expression3:
statement(s)
elif expression4:
statement(s)
else:
statement(s)
else:
statement(s)

var = 100
if var < 200:
print "Expression value is less than 200"
if var == 150:
print "Which is 150"
elif var == 100:
print "Which is 100"
elif var == 50:
print "Which is 50"
elif var < 50:
print "Expression value is less than 50"
else:
print "Could not find true expression"

print "Good bye!"

JAYSHREE PERIWAL INTERNATIONAL SCHOOL


Python Programming (IF-ELSE STATEMENT TASKS)
Task1: Wap to Find Minimum Number between Two
Numbers.
Task2: Wap to Find Whether a Number Is Positive Or
Negative Or Zero
Task3: Wap to Find Minimum Number among Three
Numbers.
Task4: Wap to Find Minimum Number among Three
Number.
Task5: Wap to Find Any Five State Capital (E.G the
Capital of Rajasthan Is Jaipur).
Task6: Wap to check whether a student is Topper or not
in Your Class. We Have Five Subjects Name Hindi, Eng.,
Computer, and Math’s And Science. Each Subject Max
Marks Is 100. And assume Topper Parentage Is 99.99.
Task7: Wap To Find A Given Number Is Leap Year Or
Not.
Task8: Wap To Find A Summer Temperature Is High Or
Normal And Winter Temperature Is High Or Normal
(Please Consider Standard Values).

You might also like