1.
Working with function
1.1 Question 1.
Write a python using a function to print factorial of a number
‘n’.
def factorial(n):
fact=1
for i in range(1, n+1):
fact*=i
print(f"Factorial of {n} is {fact}")
num = int(input("Enter a number to find factorial: "))
factorial(num)
Enter a number to find factorial: 3
Factorial of 3 is 6
1.2 Question 2.
Write a python program to accept username “Admin” as dfault
argument and password 123 entered by user to allow login into
the system.
def user_pass(password,username="Admin"):
if password=="123":
print("You have logged into system")
else:
print("Password is incorrect!!!!!!")
password= input("Enter the password :")
user_pass(password)
Enter the password :123
You have logged into system
1.3 Question 3:
Write a python program to demonstrate e concept of variable
length argument to calculate product and power of the first 10
numbers.
def sum10(*n):
total=0
for i in n:
total=total+i
print("sum of first 10 Numbers:", total)
sum10(1,2,3,4,5,6,7,8,9,10)
def product10(*n):
pr=1
for i in n:
pr=pr*i
print("Product of first 10 numbers:",pr)
product10(1,2,3,4,5,6,7,8,9,10)
OUTPUT:
sum of first 10 Numbers: 55
Product of first 10 numbers: 3628800
2. FiLE HANDLING
2.1 Question 4:
Create a text file “[Link]” in python and ask the user to write
a single lie text by user input .
def program4():
f = open("[Link]","w")
text = input("Enter the text:")
[Link](text)
[Link]()
program4()
OUTPUT:
Enter the text: Python is a programming language.
Question 5