Loops and Iteration

Loops and Iteration

Part 4 : Python for Scripting

Welcome to Part 4 of the "Python for Scripting " series! In this installment, we will explore the essential concepts of loops and iteration in Python. These concepts are fundamental for automating repetitive tasks and processing collections of data efficiently. Here's what we'll cover in this blog post:

  1. for Loops

    • Basic syntax

    • The range() function

  2. while Loops

    • Basic syntax

    • Loop control statements (break and continue)

  3. Iterating Through Lists and Other Data Structures

  4. List Comprehensions

    • A concise way to create lists

1. for Loops

Basic Syntax

A for loop is used to iterate over a sequence (like a list, tuple, or string) or other iterable objects. The basic syntax looks like this:

for item in iterable:
    # Code to be executed for each item

Here's an example:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

Output:

apple
banana
cherry

The range() Function

The range() function generates a sequence of numbers that can be used in a for loop. It's commonly used when you need to repeat an operation a specific number of times.

for number in range(5):
    print(number)

Output:

0
1
2
3
4

2. while Loops

Basic Syntax

A while loop continues executing as long as a condition is True. The basic syntax is as follows:

while condition:
    # Code to be executed while the condition is True

Here's an example:

count = 0
while count < 5:
    print(count)
    count += 1

Output:

0
1
2
3
4

Loop Control Statements

  • break: It is used to exit the loop prematurely.

  • continue: It is used to skip the rest of the current iteration and continue with the next one.

3. Iterating Through Lists and Other Data Structures

Iterating through data structures like lists, tuples, dictionaries, and sets is a common task. You can use for loops to process each item or key-value pair.

Example with a list:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

Example with a dictionary:

person = {"name": "Virat", "age": 30, "city": "New Delhi"}
for key, value in person.items():
    print(key, ":", value)

4. List Comprehensions

List comprehensions provide a concise way to create lists based on existing lists or other iterable objects. They can be a powerful tool for data manipulation.

Example: Creating a list of squares from 1 to 5 using a list comprehension:

squares = [x**2 for x in range(1, 6)]
print(squares)

Output:

[1, 4, 9, 16, 25]

Exercise

Python Exercises and Interesting Codes - TechNData - Tech & Data

Now, let's put your knowledge of loops and iteration to the test with some exercises:

Exercise 1: Write a Python program to calculate the factorial of a given number using a for loop.

Exercise 2: Create a list of numbers and use a while loop to find the sum of all the numbers in the list.

Exercise 3: Given a list of words, use a for loop to count and print the number of letters in each word.

Exercise 4: Write a program that takes a list of numbers and uses a list comprehension to create a new list containing only the even numbers from the original list.

Feel free to share your solutions to these exercises in the comments section below. In the next part of our series, we'll explore advanced topics in Python scripting, so stay tuned for more Python scripting !

Solutions to Previous Exercises (Part 3):

Certainly! Here are solutions to the exercises in Part 3, along with explanations and expected outputs:

Exercise 1: Create a list of your favorite movies and print them.

# Define a list of favorite movies
favorite_movies = ["Infinity war", "Vijay Thalapathy's Master", "3 Idiots", "Drishyam"]

# Print the list of favorite movies
print("My Favorite Movies:")
for movie in favorite_movies:
    print(movie)

Explanation: In this exercise, we've defined a list favorite_movies containing the titles of your favorite movies. We then use a for loop to iterate through the list and print each movie title.

Expected Output:

My Favorite Movies:
Infinity wars
Vijay Thalapathy's Master
3 Idiots
Drishyam

Exercise 2: Create a tuple of your personal information (name, age, city) and display it.

# Define a tuple with personal information
personal_info = ("Virat", 34, "Delhi")

# Display the personal information
print("Personal Information:")
print("Name:", personal_info[0])
print("Age:", personal_info[1])
print("City:", personal_info[2])

Explanation: In this exercise, we've defined a tuple personal_info containing your name, age, and city. We then use indexing to access and display each piece of personal information.

Expected Output:

Personal Information:
Name: Virat
Age: 34
City: Delhi

Exercise 3: Build a dictionary representing a book with keys like "title," "author," and "year," and then print the book's details.

# Define a dictionary representing a book
book = {
    "title": "Shriman Yogi",
    "author": "Ranjit Desai",
    "year": 1963
}

# Print the book's details
print("Book Details:")
print("Title:", book["title"])
print("Author:", book["author"])
print("Year:", book["year"])

Explanation: In this exercise, we've defined a dictionary book with keys for the title, author, and year of the book. We use these keys to access and print the book's details.

Expected Output:

Book Details:
Title: Shriman Yogi
Author: Ranjit Desai
Year: 1963

Exercise 4: Create two sets with your favorite colors and a friend's favorite colors. Find the common colors between the sets and print them.

# Define sets of your favorite colors and your friend's favorite colors
my_favorite_colors = {"red", "green", "blue", "yellow"}
friend_favorite_colors = {"blue", "green", "purple", "orange"}

# Find and print the common colors
common_colors = my_favorite_colors.intersection(friend_favorite_colors)
print("Common Favorite Colors:", common_colors)

Explanation: In this exercise, we've defined two sets, one for your favorite colors and one for your friend's favorite colors. We use the intersection() method to find the common colors between the sets.

Expected Output:

Common Favorite Colors: {'green', 'blue'}

Exercise 5: Given a list of words, write a Python program that finds and prints the longest word(s) in the list. If there are multiple longest words, print all of them.

# Define a list of words
words = ["apple", "banana", "cherry", "date", "fig"]

# Find the length of the longest word(s)
max_length = max(len(word) for word in words)

# Find and print the longest word(s)
longest_words = [word for word in words if len(word) == max_length]
print("Longest Word(s):", longest_words)

Explanation: In this exercise, we first find the length of the longest word(s) in the list using the max() function and a generator expression. Then, we use a list comprehension to collect and print the longest word(s).

Expected Output:

Longest Word(s): ['banana', 'cherry']

These solutions should help you practice working with Python data structures. If you have any questions or need further explanations, feel free to ask. Stay tuned for the next installment of our series, where we'll explore more advanced topics in Python scripting!

Did you find this article valuable?

Support Dhananjay Kulkarni by becoming a sponsor. Any amount is appreciated!