🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Iterating over tuples in Python is very similar to iterating over lists. Tuples are a fundamental data structure in Python, used to store an ordered collection of items. Although tuples are immutable, meaning their contents cannot be changed after creation, they can still be iterated over to perform operations on their elements.
Definition
To iterate a tuple in Python means to go through each element of the tuple one by one. This can be done using several methods, including the for loop and the while loop, which are the most common and straightforward ways to iterate through any sequence in Python.
2. Program Steps
1. Create a tuple that you want to iterate over.
2. Use a for loop to iterate through each element of the tuple.
3. Use a while loop to iterate over the tuple using indices.
4. Apply a function to all items in the tuple using the map() function.
5. Convert the tuple to a list and use list comprehension for iteration.
3. Code Program
# Step 1: Create a tuple
my_tuple = (1, 2, 3, 4, 5)
# Step 2: Use a for loop to iterate through each element
print("For loop iteration:")
for item in my_tuple:
print(item)
# Step 3: Use a while loop to iterate over the tuple using indices
print("\nWhile loop iteration:")
index = 0
while index < len(my_tuple):
print(my_tuple[index])
index += 1
# Step 4: Use the map() function to apply a function to each item
incremented_tuple = tuple(map(lambda x: x + 1, my_tuple))
# Step 5: Print the tuple after the map operation
print("\nTuple after incrementing with map:")
print(incremented_tuple)
# Step 6: Convert the tuple to a list and use list comprehension for iteration
tuple_as_list = [x**2 for x in my_tuple]
# Step 7: Print the new list created from the tuple
print("\nList comprehension from tuple:")
print(tuple_as_list)
Output:
For loop iteration: 1 2 3 4 5 While loop iteration: 1 2 3 4 5 Tuple after incrementing with map: (2, 3, 4, 5, 6) List comprehension from tuple: [1, 4, 9, 16, 25]
Explanation:
1. my_tuple is created containing five integer elements.
2. The for loop iterates through my_tuple, printing each element directly.
3. The while loop uses an index to print each element of my_tuple until the end is reached.
4. incremented_tuple is created by applying a lambda function that increments each element, achieved using the map() function.
5. The resulting incremented_tuple is printed, showing each element incremented by 1.
6. tuple_as_list is created by converting my_tuple into a list and then squaring each element using list comprehension.
7. The newly created tuple_as_list is printed, displaying the squared values of the original tuple's elements.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment