Hello Friends,
Python Operators
Arithmetic Operators
a = 10 b = 5 print(a + b) print(a - b) print(a * b) print(a / b)
Comparison Operators
print(a > b) print(a == b)================================================================================
Conditional Statements in Python
Conditional statements control the flow of execution.
Example
marks = 75
if marks >= 60:
print("First Division")
elif marks >= 40:
print("Second Division")
else:
print("Fail")
==================================================================================Loops in Python
For Loop
for i in range(1, 6):
print(i)
Python for Loop
The for loop in Python is used to iterate (loop) over a sequence such as a list, tuple, string, or range of numbers. It executes a block of code repeatedly for each item in the sequence.
Syntax of for Loop
for variable in sequence:
statements
- variable – takes value of each element in the sequence
- sequence – list, tuple, string, or range
- statements – code executed for each iteration
Example 1: for Loop with range()
The range() function generates a sequence of numbers.
for i in range(1, 6):
print(i)
Output:
1 2 3 4 5
Explanation:
The loop runs 5 times. The variable i takes values from 1 to 5.
Example 2: for Loop with List
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print(fruit)
Output:
apple banana mango
Explanation:
Each element of the list is assigned to the variable fruit one by one.
Example 3: for Loop with String
name = "Python"
for char in name:
print(char)
Output:
P y t h o n
Explanation:
Strings are sequences of characters. The loop iterates over each character.
Example 4: for Loop with range(start, stop, step)
for i in range(0, 11, 2):
print(i)
Output:
0 2 4 6 8 10
Explanation:
The loop starts at 0, ends at 10, and increments by 2.
Example 5: Nested for Loop
A nested for loop means one loop inside another.
for i in range(1, 4):
for j in range(1, 4):
print(i, j)
Output:
1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3
Example 6: for Loop with break
The break statement stops the loop immediately.
for i in range(1, 10):
if i == 5:
break
print(i)
Output:
1 2 3 4
Example 7: for Loop with continue
The continue statement skips the current iteration.
for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1 2 4 5
Example 8: for Loop with else
The else block executes when the loop finishes normally.
for i in range(1, 4):
print(i)
else:
print("Loop completed successfully")
Output:
1 2 3 Loop completed successfully
Real-Life Example
Printing student marks:
marks = [65, 72, 80, 90]
for mark in marks:
print("Student scored:", mark)
Common Mistakes
- Missing colon
:after for statement - Incorrect indentation
- Using wrong variable name inside loop
Summary
- for loop is used to iterate over sequences
- range() is commonly used with for loops
- Supports break, continue, and else
- Very useful in automation and data processing
====================================================================
While Loop
i = 1
while i <= 5:
print(i)
i += 1
=====================================================================What is is in Python?
In Python, is is an identity operator used to check whether two variables refer to the same object in memory.
Unlike the == operator, which compares values, is compares object identity.
Definition of is Operator
The is operator returns True if both variables point to the same memory location, otherwise it returns False.
Syntax:
x is y
Difference Between is and ==
| is | == |
|---|---|
| Checks memory identity | Checks value equality |
| Compares object reference | Compares data/content |
| Used for singletons | Used for value comparison |
| x is y | x == y |
Example 1: Using is with Integers
x = 10 y = 10 print(x == y) # True print(x is y) # True
Python caches small integers (usually from -5 to 256).
So both x and y refer to the same memory object.
Example 2: When == is True but is is False
x = 1000 y = 1000 print(x == y) # True print(x is y) # False
Here, the values are equal, but Python creates separate objects in memory.
Example 3: is with Lists
a = [1, 2, 3] b = [1, 2, 3] print(a == b) # True print(a is b) # False
Lists are mutable objects, so Python does not reuse memory for them.
Example 4: Using is with None
The most common and recommended use of is is checking against None.
x = None
if x is None:
print("x is None")
❌ Bad Practice:
if x == None:
pass
✔ Best Practice:
if x is None:
pass
Using id() to Understand is
The id() function returns the memory address of an object.
x = 10 y = 10 print(id(x)) print(id(y))
If both IDs are same, is will return True.
When Should You Use is?
- To compare with
None - To check singleton objects
- To verify object identity
When NOT to Use is?
- For string comparison
- For numeric value comparison
- For list or dictionary value comparison
Interview Questions on is Operator
- What is the difference between
isand==? - Why should we use
iswithNone? - Does
iscompare values? - What does
id()return?
Summary
ischecks object identity, not value==checks value equalityiscompares memory references- Use
ismainly withNone
✅ Understanding is helps avoid logical bugs and improves Python coding best practices.
Related Topics: == vs is, id() function, Mutable vs Immutable Objects, Python Operators
No comments:
Post a Comment