IKH

Loops with else block

  • Inside loop execution, if break statement not executed, then only else part will be executed.
  • else means loop without break

Example

Python
cart=[10,20,30,40,50]
for item in cart:
    if item>=500:
        print("We cannot process this order")
        break
    print(item)
else:
    print("Congrats ...all items processed successfully")

Output

PowerShell
10
20
30
40
50
Congrats ...all items processed successfully

Example

Python
cart=[10,20,600,30,40,50]
for item in cart:
    if item>=500:
        print("We cannot process this order")
        break
    print(item)
else:
    print("Congrats ...all items processed successfully")

Output

PowerShell
10
20
We cannot process this order

Question

What is the difference between for loop and while loop in Python?

  • Repeat code for every item in sequence ⇒ for loop
  • Repeat code as long as condition is true ⇒ while loop

pass keyword

If block is required which won’t do anything then we can define that empty block with pass keyword.

  • It is an empty statement.
  • It is null statement.
  • It won’t do anything.

Example

Python
if True:

Output

PowerShell
SyntaxError: unexpected EOF while parsing

Example

Python
if True:
  pass

Output

PowerShell
# Valid Statement
  • Sometimes in the parent class we have to declare a function with empty body and child class responsible to provide proper implementation. Such type of empty body we can define by using pass keyword.

Example

Python
for i in range(100):
    if i%9==0:
      print(i)
    else:
      pass

Output

PowerShell
0
9
18
27
36
45
54
63
72
81
90
99

del keyword

  • After using a variable, it is highly recommended to delete that variable if it is no longer required, so that the corresponding object is eligible for Garbage Collection.
  • We can delete variable by using del keyword.

Example

Python
x=10 
print(x) 
del x

Output

Python
10
  • After deleting a variable we cannot access that variable otherwise we will get NameError.

Example

Python
x=10
del x
print(x)

Output

PowerShell
NameError: name 'x' is not defined

Note

  • We can delete variables which are pointing to immutable objects. But we cannot delete the elements present inside immutable object.

Example

Python
s="ajay"
print(s)
del s[0]

Output

PowerShell
ajay
TypeError: 'str' object doesn't support item deletion

Difference between del and None

  • In the case del, the variable will be removed and we cannot access that variable. But in the case of None assignment the variable won’t be removed but the corresponding object is eligible for Garbage Collection. Hence after assigning with None value, we can access that variable.

Example

Python
s="ajay"
del s
print(s)

Output

PowerShell
NameError: name ‘s’ is not defined

Example

Python
s="ajay"
s=None
print(s)

Output

PowerShell
None

Ungraded Questions

Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!


Name
Email
Phone

Report an error