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

Functions Programs

The document contains a series of Python functions demonstrating various programming concepts, including pattern printing, handling variable arguments, generating a list of squares, checking for prime numbers, and calculating factorials. Each function is accompanied by example outputs to illustrate their functionality. The document serves as a practical guide for implementing basic programming tasks in Python.

Uploaded by

ece
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)
2 views2 pages

Functions Programs

The document contains a series of Python functions demonstrating various programming concepts, including pattern printing, handling variable arguments, generating a list of squares, checking for prime numbers, and calculating factorials. Each function is accompanied by example outputs to illustrate their functionality. The document serves as a practical guide for implementing basic programming tasks in Python.

Uploaded by

ece
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

8/13/25, 12:17 PM Functions programs.

ipynb - Colab

# 1. Pattern printing program

# Number of rows
rows = 5

# Outer loop for each row


for i in range(1, rows + 1):
# Inner loop to print numbers from 1 to i
for j in range(1, i + 1):
print(j, end=" ")
print() # Move to the next line after each row

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

# 2. Function that accepts a variable number of arguments


def print_numbers(*args):
print("You passed the following numbers:")
for number in args:
print(number)

# Calling the function with different number of arguments


print_numbers(10, 20, 30)
print()
print_numbers(5, 15, 25, 35, 45)

You passed the following numbers:


10
20
30

You passed the following numbers:


5
15
25
35
45

[Link] print_square_list():
# Create list of squares using list comprehension
square_list = [x**2 for x in range(1, 31)]

# Print the list


print("Squares from 1 to 30:")
print(square_list)

# Call the function


print_square_list()

[Link] 1/3
8/13/25, 12:17 PM Functions [Link] - Colab

Squares from 1 to 30:


[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441

#4. Function to check whether a number is prime


def is_prime(num):
if num < 2:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True

# Function to print the first 20 prime numbers


def print_first_20_primes():
count = 0 # To count prime numbers
number = 2 # Starting from the first prime number

print("First 20 prime numbers are:")


while count < 20:
if is_prime(number):
print(number, end=" ")
count += 1
number += 1

# Call the function


print_first_20_primes()

First 20 prime numbers are:


2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71

#5. Recursive function to find factorial


def factorial(n):
if n == 0 or n == 1:
return 1 # Base case
else:
return n * factorial(n - 1) # Recursive call

# Main program
num = int(input("Enter a number to find its factorial: "))

if num < 0:
print("Factorial is not defined for negative numbers.")
else:
result = factorial(num)
print(f"The factorial of {num} is: {result}")

Enter a number to find its factorial: 5


The factorial of 5 is: 120

[Link] 2/3

You might also like