Welcome to the realm of Python loops! Loops are essential structures in programming that allow you to execute a block of code repeatedly. They provide a powerful way to automate tasks, iterate over data, and streamline your code. In this guide, we’ll explore the two main types of loops in Python: for
and while
.
The for
Loop: Iterating Over Sequences
The for
loop is used for iterating over a sequence (that is either a list, tuple, dictionary, string, or range). Here’s a simple example:
# Example 1: Iterating over a list
persons = ["Imran khan", "SMQ", "Murad Saeed"]
for p in persons:
print(p)
You can also use the range()
function to generate a sequence of numbers:
# Example 2: Using range() in a for loop
for number in range(1, 4):
print(number)
The while
Loop: Iterating While a Condition is True
The while
loop performs a task repeatedly as long as a certain condition is true. Here’s an example:
# Example 3: Using a while loop
count = 0
while count < 3:
print("Count:", count)
count += 1
This while
loop prints the value of count
as long as it is less than 3.
Loop Control Statements: break
and continue
break
Statement: Thebreak
statement is used to exit the loop prematurely. It is often used with anif
statement to break out of the loop based on a certain condition.
# Example 4: Using break in a loop
numbers = [1, 2, 3, 4, 5]
for number in numbers:
if number == 3:
break
print(number)
his will print only the numbers 1 and 2.
continue
Statement: Thecontinue
statement is used to skip the rest of the code inside the loop for the current iteration and move to the next iteration.
# Example 5: Using continue in a loop
numbers = [1, 2, 3, 4, 5]
for number in numbers:
if number == 3:
continue
print(number)
This will print all numbers except 3.
Practical Exercise
Let’s apply what we’ve learned to a practical exercise. Consider the following:
# Exercise: Print even numbers
# Use a loop to iterate through numbers from 1 to 10 and print only the even ones.
for number in range(1, 11):
if number % 2 == 0:
print(number)
This exercise demonstrates using a for
loop and the %
(modulo) operator to identify and print even numbers.
Conclusion
Congratulations on delving into the world of Python loops! Understanding how to use for
and while
loops, along with loop control statements, is a fundamental step in becoming a proficient Python programmer. As you practice more with loops, you’ll gain the ability to automate repetitive tasks and efficiently process data. Happy coding!
Home Exercises:
Exercise 1: Simple Calculator
Create a simple calculator program that takes two numbers and an operator (+, -, *, /) as input and displays the result. Use functions to encapsulate the calculation logic.
Exercise 2: String Reversal
Write a function that takes a string as input and returns the reverse of that string. For example, if the input is “Python,” the output should be “nohtyP.”
Exercise 3: Guess the Number
Generate a random number between 1 and 10 and ask the user to guess the number. Provide feedback on whether the guess is too high, too low, or correct. Repeat until the user guesses the correct number.
Exercise 4: List Manipulation
Create a list of numbers and write a program that prints out all the even numbers in the list. Use a loop and conditionals to achieve this.
Exercise 5: FizzBuzz
Write a program that prints the numbers from 1 to 100. But for multiples of three, print “Fizz” instead of the number, and for the multiples of five, print “Buzz.” For numbers that are multiples of both three and five, print “FizzBuzz.”
Exercise 6: Prime Number Checker
Write a function that takes an integer as input and returns whether it’s a prime number or not.
Exercise 7: Fibonacci Sequence
Write a function that generates the Fibonacci sequence up to a specified number of terms
1 thought on “Exploring Python Loops: Day 10, &11”
Comments are closed.