Software Development Exam  >  Software Development Notes  >  Basics of Python  >  For Loops in Python

For Loops in Python | Basics of Python - Software Development PDF Download

Introduction

Python is a versatile programming language that offers a variety of tools to help developers efficiently handle repetitive tasks. One such tool is the for loop. In this article, we will explore the concept of for loops in Python, how they work, and how to use them effectively.

Understanding For Loops

A for loop is used to iterate over a sequence (such as a list, tuple, or string) or other iterable objects in Python. It allows you to execute a block of code repeatedly for each item in the sequence.

The general syntax of a for loop in Python is as follows:

for item in sequence:

    # Code block to be executed

Here's how it works:

  • The loop variable '(item)' takes on the value of each item in the sequence, one by one.
  • The code block inside the loop is executed for each iteration.

Let's dive into some examples to understand how for loops work.

Example 1: Iterating over a List

Suppose we have a list of numbers '[1, 2, 3, 4, 5]', and we want to print each number on a new line. We can achieve this using a for loop.

numbers = [1, 2, 3, 4, 5]

for num in numbers:

    print(num)

Output:

1

2

3

4

5

In this example:

  • The list 'numbers' is the sequence we want to iterate over.
  • The loop variable 'num' takes on the value of each item in the list.
  • The 'print(num)' statement is executed for each iteration, printing each number on a separate line.

Example 2: Iterating over a String

For loops can also be used to iterate over characters in a string. Let's say we have a string "Hello" and we want to print each character individually.

string = "Hello"

for char in string:

    print(char)

Output:

H

e

l

l

o

In this example:

  • The string "Hello" is the sequence we want to iterate over.
  • The loop variable 'char' takes on the value of each character in the string.
  • The 'print(char)' statement is executed for each iteration, printing each character on a new line.

Example 3: Using the range() Function

The 'range()' function in Python is commonly used with for loops to generate a sequence of numbers. It takes in one, two, or three arguments: 'range(start, stop, step)'.

Let's say we want to print the numbers from 1 to 10. We can use the 'range()' function along with a for loop to achieve this.

for num in range(1, 11):

    print(num)

Output:

1

2

3

4

5

6

7

8

9

10

In this example:

  • The 'range(1, 11)' function generates a sequence of numbers from 1 to 10 (inclusive).
  • The loop variable 'num' takes on the value of each number in the sequence.
  • The 'print(num)' statement is executed for each iteration, printing each number on a separate line.

Sample Problems

Problems 1: Write a program that prints the first 10 even numbers using a for loop.

for i in range(0, 20, 2):

    print(i)

Problems 2: Write a program that prints the sum of all the numbers in a list using a for loop.

numbers = [10, 20, 30, 40, 50]

sum = 0

for num in numbers:

    sum += num

print("The sum is:", sum)

Problems 3: Write a program that prints the multiplication table of a given number using a for loop.

num = 5

for i in range(1, 11):

    result = num * i

    print(num, "x", i, "=", result)

Problems 4: Write a program that counts the number of vowels in a given string using a for loop.

string = "Hello, World!"

vowels = "aeiou"

count = 0

for char in string:

    if char.lower() in vowels:

        count += 1

print("Number of vowels:", count)

Conclusion

For loops are a powerful tool in Python for iterating over sequences and performing repetitive tasks. They provide a convenient way to automate operations and handle large amounts of data. By understanding the basic syntax and examples provided in this article, you should now have a solid foundation for using for loops

The document For Loops in Python | Basics of Python - Software Development is a part of the Software Development Course Basics of Python.
All you need of Software Development at this link: Software Development
49 videos|38 docs|18 tests

Top Courses for Software Development

49 videos|38 docs|18 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

For Loops in Python | Basics of Python - Software Development

,

For Loops in Python | Basics of Python - Software Development

,

shortcuts and tricks

,

Previous Year Questions with Solutions

,

Extra Questions

,

Semester Notes

,

practice quizzes

,

Objective type Questions

,

pdf

,

mock tests for examination

,

Free

,

Important questions

,

Viva Questions

,

video lectures

,

past year papers

,

study material

,

ppt

,

Sample Paper

,

Summary

,

Exam

,

For Loops in Python | Basics of Python - Software Development

,

MCQs

;