0% found this document useful (0 votes)
4 views6 pages

Python Programming Patterns and Loops

The document provides Python programs for printing various patterns, including stars and numbers, as well as even and odd numbers from 1 to 10. It explains loops in Python, including 'for' and 'while' loops, and introduces recursion with an example of calculating factorial. Additionally, it covers loop control statements: 'break', 'continue', and 'pass', with examples for each.

Uploaded by

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

Python Programming Patterns and Loops

The document provides Python programs for printing various patterns, including stars and numbers, as well as even and odd numbers from 1 to 10. It explains loops in Python, including 'for' and 'while' loops, and introduces recursion with an example of calculating factorial. Additionally, it covers loop control statements: 'break', 'continue', and 'pass', with examples for each.

Uploaded by

kyadadarshil6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. Write python programs toprint following patterns: 1.

Write
python programs toprint following patterns:
# Pattern 1
n=5
for i in range(n, 0, -1):
print('*' * i)
print()

# Pattern 2
for i in range(n, 0, -1):
for j in range(1, i + 1):
print(j, end="")
print()
print()

# Pattern 3
for i in range(1, n + 1):
print(" " * (n - i) + "* " * i)
print()

# Pattern 4
num = 1
for i in range(1, n + 1):
for j in range(i):
print(num, end=" ")
num += 1
print()

2. Write a python program to print even and odd numbers from 1


to 10.

# Printing even numbers


print("Even numbers:")
for i in range(1, 11):
if i % 2 == 0:
print(i, end=" ")

print("\n")

# Printing odd numbers


print("Odd numbers:")
for i in range(1, 11):
if i % 2 != 0:
print(i, end=" ")
or
# Printing even and odd numbers using one loop
for i in range(1, 11):
if i % 2 == 0:
print(f"Even: {i}")
else:
print(f"Odd: {i}")

3. Write a Python program to get the Fibonacci series between 0


to 30.
# Fibonacci series up to 30
a, b = 0, 1
print("Fibonacci series:")
while a <= 30:
print(a, end=" ")
a, b = b, a + b # Updating values

1. Explain various loops supported in Python with syntax and


suitable examples.
1. Various Loops Supported in Python
Python provides two types of loops:
• for loop
• while loop
1.1 for Loop
The for loop is used for iterating over a sequence (list, tuple,
string, range, etc.).
Syntax:
for variable in sequence:
# Code block
Example:
for i in range(5):
print(i) # Output: 0 1 2 3 4
1.2 while Loop
The while loop executes a block of code as long as a condition is
true.
Syntax:
while condition:
# Code block
Example:
i=1
while i <= 5:
print(i) # Output: 1 2 3 4 5
i += 1

2. What is Recursion? Explain with example.


Recursion is a programming technique where a function calls itself
repeatedly until a base condition is met.
Example (Factorial using Recursion):
def factorial(n):
if n == 0: # Base case
return 1
return n * factorial(n - 1) # Recursive call

print(factorial(5)) # Output: 120


Here, the function factorial() keeps calling itself with a smaller
value until n becomes 0.

3. Explain following loop control statements of python:


break, continue and pass
Loop control statements are used to control the flow of loops.
3.1 break Statement
The break statement terminates the loop when encountered.
Example:
for i in range(5):
if i == 3:
break
print(i) # Output: 0 1 2
3.2 continue Statement
The continue statement skips the current iteration and moves to
the next one.
Example:
for i in range(5):
if i == 3:
continue
print(i) # Output: 0 1 2 4
3.3 pass Statement
The pass statement does nothing and is used as a placeholder.
Example:
for i in range(5):
if i == 3:
pass # Does nothing
print(i) # Output: 0 1 2 3 4
These loop control statements help manage the flow of loops
effectively.

You might also like