0% found this document useful (0 votes)
6 views2 pages

Python Programs - Loops

The document provides various Python programs demonstrating the use of loops, including for loops and while loops. It covers tasks such as printing numbers, calculating sums and factorials, printing multiplication tables, checking for prime numbers, generating Fibonacci sequences, and reversing strings. Each program is accompanied by comments explaining its functionality.

Uploaded by

abhishek13990233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Python Programs - Loops

The document provides various Python programs demonstrating the use of loops, including for loops and while loops. It covers tasks such as printing numbers, calculating sums and factorials, printing multiplication tables, checking for prime numbers, generating Fibonacci sequences, and reversing strings. Each program is accompanied by comments explaining its functionality.

Uploaded by

abhishek13990233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PYTHON PROGRAMS - LOOPS

1. Print Numbers from 1 to 10 Using a For Loop

# For loop to print numbers from 1 to 10

for i in range(1, 11):

print(i)

2. Print Even Numbers Between 1 and 20 Using a For Loop

# For loop to print even numbers between 1 and 20

for i in range(2, 21, 2):

print(i)

3. Calculate the Sum of First 10 Natural Numbers Using a For Loop

# Calculate the sum of first 10 natural numbers

total = 0

for i in range(1, 11):

total += i

print("Sum of first 10 natural numbers:", total)

4. Print Multiplication Table of a Given Number Using a For Loop

# Print multiplication table of a given number

num = 5

for i in range(1, 11):

print(f"{num} x {i} = {num * i}")

5. Calculate the Factorial of a Number Using a For Loop

# Calculate the factorial of a number

num = 5

factorial = 1

for i in range(1, num + 1):

factorial *= i

print(f"Factorial of {num} is {factorial}")

6. Print Elements of a List Using a For Loop

# Print elements of a list

fruits = ["Apple", "Banana", "Cherry", "Date"]

for fruit in fruits:

print(fruit)

7. Print Numbers from 10 to 1 Using a While Loop

# While loop to print numbers from 10 to 1


i = 10

while i > 0:

print(i)

i -= 1

8. Check if a Number is Prime Using a For Loop

# Check if a number is prime

num = 29

is_prime = True

for i in range(2, num):

if num % i == 0:

is_prime = False

break

if is_prime:

print(f"{num} is a prime number")

else:

print(f"{num} is not a prime number")

9. Print Fibonacci Sequence up to n Terms Using a While Loop

# Print Fibonacci sequence up to n terms

n = 10

a, b = 0, 1

count = 0

while count < n:

print(a)

a, b = b, a + b

count += 1

10. Reverse a String Using a For Loop

# Reverse a string

string = "Hello, World!"

reversed_string = ""

for char in string:

reversed_string = char + reversed_string

print("Reversed string:", reversed_string)

You might also like