Sivangi Ass
Sivangi Ass
Sivangi Ass
OUTPUT:-
OUTPUT:-
Enterthe
Enter thenumber
number47
47
47 is a Prime number
47 is a Prime number
OUTPUT:-
OUTPUT:-
OUTPUT:-
Entera anumber:
Enter number:14541
14541
14541 is a palindrome
14541 is a palindrome
x, y = swap_values(x, y)
OUTPUT:-
OUTPUT:-
Before
Before swapping
swapping thethe value
value of Xofand
X and
Y: Y:
x =x55
= 55
y =y100
= 100
After swapping
After thethe
swapping value of Xofand
value Y ::Y ::
X and
x = 100
y =x55
= 100
y = 55
Q4 Write a python program to Read a file line by line and print it.
def file_name(file):
file = open(file,’r’)
while True:
line = file.read()
if not line :
break
print(line)
OUTPUT
OUTPUT
Enter
Enter thethe filename:
filename: p11.txt
p11.txt
mymy name is shivangi
live in vidisha
live in vidisha
Q5 Write a program to display the number of lines in the file and size of a file in bytes.
def file_info(filename):
try:
with open(filename, ’r’) as file:
num_lines = sum(1 for line in file)
file.seek(0, 2) # Move the file pointer to the end
file_size = file.tell() # Get the size of the file
print("Number of lines:", num_lines)
print("Size of the file (in bytes):", file_size)
except FileNotFoundError:
print("File not found.")
# Displaying the number of lines and size of a file
filename = input("Enter the filename: ")
file_info(filename)
OUTPUT
OUTPUT
Enter thethe
Enter filename: pramid.c
filename: pramid.c
Number of lines: 55
Number
Size of lines:
of the file 55 1040
(in bytes):
Size of the file (in bytes): 1040
def factorial(a):
if(a<0):
print("you enter the negative value")
elif (a==0):
return 1
else :
return a*factorial(a-1)
try:
n= int(input("Enter your number "))
print("The factorial of the number is ",factorial(n))
except (ValueError):
print("The input value is not valide ")
OUTPUT
OUTPUT
EnterEnter your number
your number 6 6
The factorial
The factorial of the of the number
number is 720 is 720
def fib_series(terms):
if terms <= 0:
print("Please enter a positive integer.")
else:
print("Fibonacci Series:")
for i in range(terms):
print(fibonacci(i), end=" ")
OUTPUT
OUTPUT
Enter
Enter the the number
number of terms
of terms for Fibonacci
for Fibonacci series:series:
8 8
Fibonacci
Fibonacci Series:
Series:
0 1 01 12132538513
8 13
Q8 Write a program for binary search.
def binary_search(arr, target):
low = 0
high = len(arr) - 1
if result != -1:
print(f"Element {target} is present at index {result}.")
else:
print(f"Element {target} is not present in the array.")
OUTPUT:-
OUTPUT:-
Enter the your target value 19
Enter the your target value 19
Element1919isispresent
Element presentat at index
index 7. 7.
def sum_of_squares(n):
if n < 0:
return "Please enter a non-negative integer."
else:
return n * (n + 1) * (2 * n + 1) // 6
OUTPUT:-
OUTPUT:-
Enter
Enter aa positive
positive integer
integer n: 9n: 9
The sum of squares of the
The sum of squares of the firstfirst
9 natural numbers
9 natural is: 285
numbers is: 285
Q10 Python Program to find sum of array.
arr = [1, 2, 3, 4, 5]
array_sum = sum(arr)
print("The sum of the array elements is:", array_sum)
OUTPUT:-
OUTPUT:-
The
The sum
sum of the
of the array
array elements
elements is: 15is: 15
OUTPUT:-
OUTPUT:-
enter
enter your
your sentence
sentence
my name is shivangi
1325 14011305 0919 19080922011409
Q13 Python program to print even length words in a string.
def print_even_length_words(string):
words = string.split()
even_length_words = [word for word in words if len(word) % 2 == 0]
print("Even length words in the string:")
for word in even_length_words:
print(word)
OUTPUT:-
OUTPUT:-
Enter
Enter a character:
a character: J J
The
The ASCII value ’J’
ASCII value of of is: 74 74
'J' is:
Q16 Python program to find smallest and largest number in a list.
def find(numbers):
if not numbers:
return None, None
smallest = largest = numbers[0]
for num in numbers:
if num < smallest:
smallest = num
elif num > largest:
largest = num
return smallest, largest
OUTPUT:-
OUTPUT:-
Smallest
Smallest number:
number: 2 2
Largest number: 90
Largest number: 90
Q17 Python program to find the size of a Tuple.
import sys
def tuple_size(t):
return sys.getsizeof(t)
OUTPUT:-
OUTPUT:-
Size of the tuple: 80
Size of the tuple: 80