Hi there, we’re Harisystems
"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".
For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.
1Python For Loop
The for loop is a control structure in Python that allows you to iterate over a sequence of elements such as a list, tuple, string, or range. It is used when you want to perform a set of instructions for each element in the sequence. Let's explore the syntax and usage of the for loop in Python with examples.
Syntax
The basic syntax of a for loop in Python is as follows:
for item in sequence:
# Code to execute for each item in the sequence
Example 1: Printing Numbers
Consider the following example:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
In this example, we have a list of numbers. The for loop iterates over each element in the list and assigns it to the variable "num". Inside the loop, we print the value of "num". The loop repeats for each number in the list, resulting in the numbers being printed one by one.
Example 2: Summing Numbers
You can also use a for loop to perform calculations. Here's an example that calculates the sum of numbers from 1 to 10:
sum = 0
for num in range(1, 11):
sum += num
print("The sum is:", sum)
In this example, we use the range() function to generate a sequence of numbers from 1 to 10 (excluding 11). The for loop iterates over each number in the range and adds it to the variable "sum". After the loop terminates, we print the final value of "sum," which is the sum of numbers from 1 to 10.
Example 3: Iterating Over Strings
The for loop can also iterate over characters in a string. Here's an example:
message = "Hello, World!"
for char in message:
print(char)
In this example, the for loop iterates over each character in the string "message" and assigns it to the variable "char". Inside the loop, we print each character, resulting in the string being printed character by character.
Conclusion
The for loop is a versatile construct in Python that allows you to iterate over sequences and perform operations on each element. By understanding the syntax and usage of the for loop, you can efficiently process data, perform calculations, and manipulate strings in your Python programs.
4.5L
Learners
20+
Instructors
50+
Courses
6.0L
Course enrollments
Future Trending Courses
When selecting, a course, Here are a few areas that are expected to be in demand in the future:.
Future Learning for all
If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.
View CoursesMost Popular Course topics
These are the most popular course topics among Software Courses for learners