Untitled
unknown
plain_text
2 months ago
10 kB
9
Indexable
QUESTION: Write a Python program to print your Name, Roll Number and Section.
ANSWER:
print("Name: Md Kaif")
print("Roll No: 21")
print("Section: B")
EXPERIMENT 4
QUESTION: Write a Python program to enter two numbers and display their sum.
ANSWER:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum = a + b
print("Sum is:", sum)
EXPERIMENT 5
QUESTION: Write a Python program to read data from CSV file.
ANSWER:
import csv
file = open("data.csv", "r")
data = csv.reader(file)
for row in data:
print(row)
file.close()
EXPERIMENT 6
QUESTION: Write a Python program to write data into CSV file.
ANSWER:
import csv
data = [["Name", "Age"],
["Kaif", 21],
["Rahul", 22]]
file = open("data.csv", "w", newline="")
writer = csv.writer(file)
writer.writerows(data)
file.close()
print("Data written successfully")
EXPERIMENT 7
QUESTION: Write a Python program to read data from JSON file.
ANSWER:
import json
file = open("data.json", "r")
data = json.load(file)
print(data)
file.close()
EXPERIMENT 8
QUESTION: Write a Python program to read data from Text file.
ANSWER:
file = open("sample.txt", "r")
data = file.read()
print(data)
file.close()
EXPERIMENT 9
QUESTION: Write a Python program to reduce memory usage using generator.
ANSWER:
def my_generator(n):
for i in range(n):
yield i
gen = my_generator(10)
for value in gen:
print(value)
EXPERIMENT 10
QUESTION: Write a Python program to split data into chunks.
ANSWER:
def split_list(data, size):
for i in range(0, len(data), size):
yield data[i:i + size]
my_list = [1,2,3,4,5,6,7,8,9]
for chunk in split_list(my_list, 3):
print(chunk)
QUESTION: Write a Python program to remove elements from a dictionary.
ANSWER:
student = {
"name": "Ravi",
"age": 20,
"course": "B.Tech"
}
student.pop("course")
del student["age"]
print(student)
EXPERIMENT 12
QUESTION: Write a Python program to create a dictionary with different data types.
ANSWER:
data = {
"int": 10,
"float": 5.5,
"list": [1,2,3],
"tuple": (4,5),
"dict": {"a":1,"b":2}
}
print(data)
EXPERIMENT 13
QUESTION: Write a Python program to create a nested dictionary and access elements.
ANSWER:
students = {
"S1": {"Name": "Aman", "Marks": 90},
"S2": {"Name": "Raj", "Marks": 85}
}
print(students["S1"]["Name"])
EXPERIMENT 14
QUESTION: Write a Python program to create dictionary using dictionary comprehension.
ANSWER:
squares = {x: x*x for x in range(1,6)}
print(squares)
EXPERIMENT 15
QUESTION: Write a Python program to display all elements in a tuple.
ANSWER:
data = ("I","am","Python",7)
print(data)
EXPERIMENT 16
QUESTION: Write a Python program to display elements of tuple using index.
ANSWER:
data = ("I","am","Kaif",7)
print(data[1])
print(data[3])
EXPERIMENT 17
QUESTION: Write a Python program to show that tuple is immutable.
ANSWER:
data = ("I","am","Kaif",7)
data[1] = "is"
print(data[1])
EXPERIMENT 18
QUESTION: Write a Python program to perform Bubble Sort.
ANSWER:
A = [56,58,28,33,71,96,23,51,56,82,41]
n = len(A)
for i in range(n):
for j in range(n-i-1):
if A[j] > A[j+1]:
A[j], A[j+1] = A[j+1], A[j]
print("Sorted Array:", A)
EXPERIMENT 19
QUESTION: Write a Python program to perform Insertion Sort.
ANSWER:
A = [56,6,8,28,33,71,96,23,51,56,82,41]
for i in range(1,len(A)):
key = A[i]
j = i-1
while j >= 0 and key < A[j]:
A[j+1] = A[j]
j = j-1
A[j+1] = key
print("Sorted Array:", A)
EXPERIMENT 20
QUESTION: Write a Python program to perform Quick Sort.
ANSWER:
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[0]
smaller = [x for x in arr[1:] if x <= pivot]
greater = [x for x in arr[1:] if x > pivot]
return quick_sort(smaller) + [pivot] + quick_sort(greater)
A = [56,5,8,28,33,71,96,23,51,56,82,41]
sorted_array = quick_sort(A)
print("Sorted Array:", sorted_array)
EXPERIMENT 21
QUESTION: Write a Python program to find the largest number in a list.
ANSWER:
arr = [56,3,8,28,37,71,96,23,51,56,82,41]
largest = arr[0]
for num in arr:
if num > largest:
largest = num
print("Largest number is", largest)
EXPERIMENT 22
QUESTION: Write a Python program to define and print a function.
ANSWER:
def fun():
print("Welcome to BIT Mesra")
fun()
EXPERIMENT 23
QUESTION: Write a Python program to call a function.
ANSWER:
def fun():
print("Welcome to BIT Mesra")
fun()
EXPERIMENT 24
QUESTION: Write a Python program to check even or odd using function argument.
ANSWER:
def even_odd(x):
if x % 2 == 0:
return "Even"
else:
return "Odd"
print(even_odd(16))
print(even_odd(7))
EXPERIMENT 25
QUESTION: Write a Python program using default function arguments.
ANSWER:
def my_fun(x, y=50):
print("x:", x)
print("y:", y)
my_fun(10)
EXPERIMENT 26
QUESTION: Write a Python program using keyword arguments.
ANSWER:
def student(fname, lname):
print(fname, lname)
student(fname="BIT Mesra", lname="Practice")
student(lname="Practice", fname="BIT Mesra")
EXPERIMENT 27
QUESTION: Write a Python program using positional arguments.
ANSWER:
def nameAge(name, age):
print("Hi I am", name)
print("My age is", age)
print("Case 1")
nameAge("Suraj", 27)
print("Case 2")
nameAge(27, "Suraj")
EXPERIMENT 28
QUESTION: Write a Python program using arbitrary arguments (*args and **kwargs).
ANSWER:
def my_fun(*args, **kwargs):
print("Non-keyword arguments")
for arg in args:
print(arg)
print("Keyword arguments")
for key, value in kwargs.items():
print(key, "=", value)
my_fun("Hey", "Welcome", first="Create", mid="for", last="Week")
EXPERIMENT 29
QUESTION: Write a Python program to create function within function.
ANSWER:
def f1():
s = "Love BIT Mesra"
def f2():
print(s)
f2()
f1()
EXPERIMENT 30
QUESTION: Write a Python program using anonymous (lambda) function.
ANSWER:
cube = lambda x: xxx
print(cube(7))
EXPERIMENT 31
QUESTION: Write a Python program using return statement in function.
ANSWER:
def square_value(num):
return num**2
print(square_value(2))
print(square_value(4))
EXPERIMENT 32
QUESTION: Write a Python program using recursive function to find factorial.
ANSWER:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(4))
EXPERIMENT 33
QUESTION: Write a Python program using lambda function to convert string to uppercase.
ANSWER:
a = "BIT Mesra"
upper = lambda x: x.upper()
print(upper(a))
EXPERIMENT 34
QUESTION: Write a Python program using lambda function with condition checking.
ANSWER:
check = lambda x: "Positive" if x > 0 else "Negative" if x < 0 else "Zero"
print(check(5))
print(check(-3))
print(check(0))
EXPERIMENT 35
QUESTION: Write a Python program using lambda function with list comprehension.
ANSWER:
func = [lambda arg=x: arg*10 for x in range(1,5)]
for i in func:
print(i())
EXPERIMENT 36
QUESTION: Write a Python program using lambda function returning multiple values.
ANSWER:
calc = lambda x, y: (x+y, x*y)
res = calc(3,4)
print(res)
EXPERIMENT 37
QUESTION: Write a Python program using lambda function with filter().
ANSWER:
C = [1,2,3,4,5,6]
even = filter(lambda x: x%2==0, C)
print(list(even))
EXPERIMENT 38
QUESTION: Write a Python program using lambda function with map().
ANSWER:
a = [1,2,3,4]
double = map(lambda x: x*2, a)
print(list(double))
EXPERIMENT 39
QUESTION: Write a Python program using lambda function with reduce().
ANSWER:
from functools import reduce
a = [1,2,3,4]
mul = reduce(lambda x,y: x*y, a)
print(mul)
EXPERIMENT 40
QUESTION: Write a Python program using lambda function with def keyword.
ANSWER:
def sq_def(x):
return x**2
print(sq_def(3))
EXPERIMENT 41
QUESTION: Write a Python program to find all numbers in a string using regex.
ANSWER:
import re
string = "My number is 12345 and my friend's number is 98765"
numbers = re.findall(r'\d+', string)
print(numbers)
EXPERIMENT 42
QUESTION: Write a Python program to split string using regex.
ANSWER:
import re
text = "On 12th Jan 2016 at 11:02 AM"
print(re.split(r'\s+', text))
print(re.split(r'\d+', text))
EXPERIMENT 43
QUESTION: Write a Python program to replace text using regex.
ANSWER:
import re
text = "Python is easy"
new_text = re.sub("easy", "powerful", text)
print(new_text)
EXPERIMENT 44
QUESTION: Write a Python program to create dictionary comprehension.
ANSWER:
square = {x: x*x for x in range(1,6)}
print(square)
EXPERIMENT 45
QUESTION: Write a Python program to display tuple elements.
ANSWER:
t = ("Python", "Java", "C++")
for i in t:
print(i)
EXPERIMENT 46
QUESTION: Write a Python program to access tuple elements using index.
ANSWER:
t = ("Python", "Java", "C++")
print(t[0])
print(t[1])
print(t[2])
EXPERIMENT 47
QUESTION: Write a Python program to demonstrate nested dictionary.
ANSWER:
student = {
"S1": {"Name":"Aman","Marks":90},
"S2": {"Name":"Raj","Marks":85}
}
print(student["S1"]["Name"])
EXPERIMENT 48
QUESTION: Write a Python program to remove dictionary elements.
ANSWER:
student = {
"name":"Ravi",
"age":20,
"course":"B.Tech"
}
student.pop("course")
print(student)
EXPERIMENT 49
QUESTION: Write a Python program to create generator function.
ANSWER:
def generator():
for i in range(5):
yield i
for value in generator():
print(value)
EXPERIMENT 50
QUESTION: Write a Python program to split list into equal chunks.
ANSWER:
def split_list(data, size):
for i in range(0, len(data), size):
yield data[i:i+size]
my_list = [1,2,3,4,5,6,7,8,9]
for chunk in split_list(my_list,3):
print(chunk)
Editor is loading...
Leave a Comment