Python Loops for Beginners | Step-by-Step Guide with Code Examples

0

Python Loops for Beginners | Step-by-Step Guide with Code Examples
Python Loops for Beginners | Step-by-Step Guide with Code Examples


📑 Table of Contents

Python Loop Definition

Python Loops: A loop in Python is a control flow statement that allows code to be executed repeatedly based on a condition. Python primarily supports two types of loops for loops and while loops. Loop is very important part of python programming languages. Loop is part of control flow statements. To run the particular block of code continuously until a required condition is fulfil is called looping operations. Loop can be used when there is a need to execute a part of program execute multiple times.

Types of Loops in Python

  • While Loop
  • For loop

Definition of While Loop in Python

While Loop: A while loop in Python repeatedly executes block of code as long as a specified condition is true. The loop continues executing the code block until the condition. While loop can be used continues running python program.

Syntax While Loop in Python

while condition:
        body Statements
        increment/decrement

Example of While Loop in Python

i=1
while i<=5:
    print("Your Number is:",i)
    i=i+1

============OUTPUT============
Your Number is:1
Your Number is:2
Your Number is:3
Your Number is:4
Your Number is:5

Find Factorial of a Number Using While Loop in Python

i=1
f=1
n=int(input("Enter any number: "))
while i<=n:
        f=f*i
        i=i+1
print("Factorial is:",f)

============OUTPUT============
Enter any number:5
Factorial is:120

Using while Loop with Else in Python

i=1
while i<=5:
        
print("Your Number is:",i)
        i=i+1
else:
    print("Welcome to Jiocoding")

============OUTPUT============
Your Number is:1
Your Number is:2
Your Number is:3
Your Number is:4
Your Number is:5
Welcome to Jiocoding

Write a Program to Print Table of any Number in Python using While Loop

n=int(input("Enter any number: "))
i=1
while i<=10:
     print(f"{n} x {i} = {n*i}")
     i=i+1
============OUTPUT============
Enter any number:6
6 x 1 = 6

6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60

Definition of For Loop in Python

For Loop: A python for loop can be used for sequential traversal. for loop in python is used to iterate over a sequence (like a list, tuple, dictionary, set, or string) or other inerrable objects. It allows you to execute a block of code repeatedly for each item in the sequence.

For Loop Syntax in Python

for variable_name in sequence:
            body of for loop

Here for is keyword variable_neme will take the value of the item inside the sequence on each iteration.

Syntax of Range Function in Python

for variable_name in range(number_of_sequence):
            body of for loop

Range() function is used to generate sequence of numbers.
Sequence of range() function is range(start,stop,step_size).default step_size is 1.

Explain Range Function for Loop in Python

for i in range(2,8):
    print("Your Number is:",i)

============OUTPUT============
Your Number is:2
Your Number is:3
Your Number is:4
Your Number is:5
Your Number is:6
Your Number is:7

Explain Range Function in Python

for i in range(2,10,2):
    print("Your Number is:",i)

============OUTPUT============
Your Number is:2
Your Number is:4
Your Number is:6
Your Number is:8

Explain For Loop with String

s="Jiocoding"
for name in s:
    print(name)

============OUTPUT============
J
i
o
c
o
d
i
n
g

Explain For Loop with List

data=["Jiocoding", "C", "C++","JAVA")
for name in data:
    print("Your List Data:",name)

============OUTPUT============
Your List Data:Jiocoding
Your List Data:C
Your List Data:C++
Your List Data:Java

Write a Program to Print Table of any Number in Python using for Loop

n=int(input("Enter any number: "))
for i in range(1,11):
     print(f"{n} x {i} = {n*i}")

============OUTPUT============
Enter any number:6
6 x 1 = 6

6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60

Conclusion of Python Loops

Conclusion: This is a all about python loops. python language support only two types loops like for and while so explain in above. I can explain more example for and while loop next chapter coming soon. If your any query for in this article than you can contact me by email i can provide best solutions.

Read more Loops:

Post a Comment

0 Comments
Post a Comment (0)
To Top