0% found this document useful (0 votes)
11 views12 pages

CS Practical File

Uploaded by

zapverse2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views12 pages

CS Practical File

Uploaded by

zapverse2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

CS PRACTICAL FILE

Q1. Write the python program to print your name using a function.
Ans.
def print_name():
name = "Atharva"
print("My name is ", name)
Q2. Write the python program using functions to add and subtract
two values and return.
Ans.
def add(a, b):
return a + b

def subtract(a, b):


return a - b

num1 = float(input("Enter the first number: "))


num2 = float(input("Enter the second number: "))
sum_result = add(num1, num2)
difference_result = subtract(num1, num2)

print("The sum of" , num1," and ", num2 , "is:" ,sum_result)


print("The sum of" , num1," and ", num2 , "is:" , difference_result)
Q3. Write a python menu driven program to perform arithmetic
operations by using functions.
Ans.
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b != 0:
return a / b
else:
return "Error! Division by zero."
def menu():
print("Welcome to the Arithmetic Operations Program!")
print("Please choose an operation:") print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Exit")
while True:
menu() choice = input("Enter your choice (1-5): ")
if choice == '5':
print("Exiting the program. Goodbye!")
break
if choice in ['1', '2', '3', '4']:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
if choice == '1':
print(f"The result of {num1} + {num2} is: {add(num1, num2)}")
elif choice == '2':
print(f"The result of {num1} - {num2} is: {subtract(num1, num2)}")
elif choice == '3':
print(f"The result of {num1} * {num2} is: {multiply(num1, num2)}")
elif choice == '4':
result = divide(num1, num2)
print(f"The result of {num1} / {num2} is: {result}")
else:
print("Invalid choice! Please choose a valid option.")
Q4. Write the python code to show the working of default
arguments.
Ans.
def greet(name, message="Hello"):
print(message, name)

greet(‘bob’, ’ hi’)

greet(‘daniel’)
Q5. Write the python code to show the working of keyword
arguments.
Ans.
def describe_pet(animal_type, pet_name):
print(f"\nI have a {animal_type}.")
print(f"My {animal_type}'s name is {pet_name.title()}.")

describe_pet(animal_type='dog', pet_name='willie')
describe_pet(pet_name='whiskers', animal_type='cat')
Q6. Write the python code to show the global and local scope of a
variable.
Ans.
x = 10
def my_function():
y=5
print("Inside the function, x (global):", x)
print("Inside the function, y (local):", y)
print("Outside the function, x (global):", x)
try:
print("Outside the function, y (local):", y)
except NameError as e:
print("Error:", e)
Q7. Write the MySQL command to create the database db.
Ans.
CREATE DATABASE db;
Q8. Write the MySQL command to create a student table with the
student_id, class, section, gender, name, dob, and marks as
attributes where the student id is the primary key.
Ans.
CREATE TABLE student (
student_id INT PRIMARY KEY,
class varchar(10),
section CHAR(1),
gender CHAR(1),
name VARCHAR(100),
dob DATE,
marks INT
);
Q9. Write the MySQL command to insert the details of at least 5
students in the above table.
Ans.
INSERT INTO student (student_id, class, section, gender, name, dob, marks) VALUES
 (1, '10', 'A', 'M', 'John Doe', '2005-05-15', 85),
 (2, '10', 'B', 'F', 'Jane Smith', '2005-08-22', 90),
 (3, '9', 'A', 'M', 'Mike Johnson', '2006-11-30', 78),
 (4, '9', 'B', 'F', 'Emily Davis', '2006-03-12', 88),
 (5, '8', 'C', 'M', 'Chris Brown', '2007-07-19', 92);
Q10. Write the MySQL command to display the entire content of the
table.
Ans.
SELECT * FROM students;
Q11. Write the MySQL command to add an email column in the
above student table and also show the structure of the student
table after addition of the column
Ans.
ALTER TABLE student
ADD COLUMN email VARCHAR(100);

DESCRIBE student;

You might also like