Professional Documents
Culture Documents
Dhruv Autosaved
Dhruv Autosaved
Output:-
2. Program to get factorial of a given number.
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
num = int(input("Enter a number: "))
if num < 0:
print("Factorial is not defined for negative numbers.")
elif num == 0:
print("The factorial of 0 is 1.")
else:
print("The factorial of", num, "is", factorial(num))
Output:-
3. Program to print the table of a given number.
Output:-
4. Program to check for a common element in a list.
L1=[]
L2=[]
result=False
n = int(input("Enter the list size: "))
i=0
while i<n:
a=input("Enter elements: ")
L1.append(a)
i+=1
m=int(input("Enter the list size: "))
c=0
while c<m:
a=input("Enter elements: ")
L2.append(a)
c+=1
for x in L1:
for y in L2:
if x==y:
result=True
print(result)
if result:
print("A common element exist")
else:
print("No common elements exist")
Output:-
5. Program to print the grades of a student.
Output:-
6. Program for Default argument
def change(P,Q=30):
P=P+Q
Q=P-Q
print(P,"#",Q)
return P
X=150
Y=100
X=change(X,Y)
print(X,"#",Y)
Y=change(X)
Output:-
7. Program for Positional Argument
grade('max',80)
Output:-
8. Program to find the sum of numbers from 1-9 using
variable length argument.
def sum(*numbers):
total = 0
for num in numbers:
total += num
return total
r=sum(1,2,3,4,5,6,7,8,9)
print(r)
Output:-
9. Program for Keyword argument
Output:-
10. Program to find circumference and area of a
circle .
def circle(radius):
circumference = 2 * 3.14159 * radius
area = 3.14159 * radius ** 2
return circumference, area
circumference,area=circle(3)
print(circumference)
print(area)
Output:-
11. Program to find total number of characters in a
file.
def countch(count):
with open(count, 'r') as file:
text = file.read()
return len(text)
filepath = 'textfile.txt'
charactercount = countch(filepath)
print(f"Character Count: {charactercount}")
Output:-
12. Program to find total number of words in a file.
def countw(textfile):
with open(textfile, 'r') as file:
text = file.read()
words = text.split()
return len(words)
filepath = 'textfile.txt'
wordc = countw(filepath)
print(f"Word Count: {wordc}")
Output:-
13. Program to check number of lines in a file.
def countl(textfile):
with open(textfile, 'r') as file:
lines = file.readlines()
return len(lines)
filepath = 'textfile.txt'
linecount = countl(filepath)
print(f"Line Count: {linecount}")
Output:-