Loops in Python
Loops in Python are used to execute a block of code repeatedly. Python mainly provides
two types of loops:
1. for Loop
The for loop is used to iterate over a sequence such as a list, tuple, string, or range. It
automatically goes through each item in the sequence.Syntax:
for variable in sequence:
# code block
Example:
for i in range(1, 6):
print(i)
Output:
12345
---
2. while Loop
The while loop continues executing a block of code as long as the given condition is
true.Syntax:
while condition:
# code block
Example:
count = 1
while count <= 5:
print(count)
count += 1
Output:
12345
---
Inheritance in Python
Inheritance is an object-oriented programming concept where one class (child class)
inherits properties and methods from another class (parent class). It promotes code
reusability and logical hierarchy. Example of Single Inheritance:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
# Creating object of child class
d = Dog()
d.speak() # Inherited method
d.bark() # Child's own method output : Animal speaks Dog Barks
Loops in Python are used to execute a block of code repeatedly. Python mainly provides
two types of loops:
1. for Loop
The for loop is used to iterate over a sequence such as a list, tuple, string, or range. It
automatically goes through each item in the sequence.Syntax:
for variable in sequence:
# code block
Example:
for i in range(1, 6):
print(i)
Output:
12345
---
2. while Loop
The while loop continues executing a block of code as long as the given condition is
true.Syntax:
while condition:
# code block
Example:
count = 1
while count <= 5:
print(count)
count += 1
Output:
12345
---
Inheritance in Python
Inheritance is an object-oriented programming concept where one class (child class)
inherits properties and methods from another class (parent class). It promotes code
reusability and logical hierarchy. Example of Single Inheritance:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
# Creating object of child class
d = Dog()
d.speak() # Inherited method
d.bark() # Child's own method output : Animal speaks Dog Barks