0% found this document useful (0 votes)
8 views6 pages

Python Programming Basics and Tasks

The document contains Python code examples for various programming tasks, including greeting users, calculating areas of shapes, computing gross salaries, and performing arithmetic operations. It also demonstrates managing a task list with lists and tuples, performing set operations for student enrollments in exams, and manipulating a dictionary of student records. Each section includes code snippets and outputs to illustrate the functionality.

Uploaded by

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

Python Programming Basics and Tasks

The document contains Python code examples for various programming tasks, including greeting users, calculating areas of shapes, computing gross salaries, and performing arithmetic operations. It also demonstrates managing a task list with lists and tuples, performing set operations for student enrollments in exams, and manipulating a dictionary of student records. Each section includes code snippets and outputs to illustrate the functionality.

Uploaded by

jaymala.chavan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Experiment 1

1.A)

print ("Hello, Welcome to Python!")

n = input("Enter your name : ")


print(f"Hello, {n}! Welcome to the Python Programming!")

2.B)

import math

# Predefined values
radius = 5
length = 10
width = 4
side = 6

# Function to calculate area of circle


def area_of_circle(radius):
return [Link] * radius ** 2

# Function to calculate area of rectangle


def area_of_rectangle(length, width):
return length * width

# Function to calculate area of square


def area_of_square(side):
return side ** 2

# Calculate and print areas


circle_area = area_of_circle(radius)
rectangle_area = area_of_rectangle(length, width)
square_area = area_of_square(side)

print(f"Area of circle with radius {radius} : {circle_area:.2f}")


print(f"Area of rectangle with length {length} and width {width} :
{rectangle_area:.2f}")
print(f"Area of square with side {side} : {square_area:.2f}")
1.C)

# Input the basic salary from the user


basic_salary = float(input("Enter the basic salary of the employee : "))

# Calculate allowances
DA = 0.70 * basic_salary # 70% of basic salary
TA = 0.30 * basic_salary # 30% of basic salary
HRA = 0.10 * basic_salary # 10% of basic salary

# Calculate the gross salary


gross_salary = basic_salary + DA + TA + HRA

# Display the gross salary


print("The gross salary of the employee is : ",gross_salary)

1.D)

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


num2 = float(input("Enter the second number: "))

# Basic arithmetic operations


addition = num1 + num2
subtraction = num1 - num2
multiplication = num1 * num2
division = num1 / num2 if num2 != 0 else "Undefined (division by zero)"
modulus = num1 % num2 if num2 != 0 else "Undefined (modulus by zero)"

# Results
print(f"The sum of {num1} and {num2} is: {addition}")
print(f"The difference of {num1} and {num2} is: {subtraction}")
print(f"The product of {num1} and {num2} is: {multiplication}")
print(f"The division of {num1} by {num2} is: {division}")
print(f"The modulus of {num1} and {num2} is: {modulus}")
Experiment 2

Q.1) Develop a Python program to manage a task list using lists and tuples,
including adding, removing, updating, and sorting tasks.

#List
task = ['task1', 'task2', 'task3']
print(task)

print("----------Add---------------")
[Link]('task10')
print(task)
[Link](0, 'task50')
print(task)
[Link](['task11', 'task12'])
print(task)
task[0] = 'task20'
print(task)

print("----------Delete---------------")
[Link]('task1')
print(task)
del_item = [Link](1)
print(del_item)
del task[0]
print(task)

print("----------Repetition-------------")
task = task * 3
print(task)

print("----------Select--------------")
print(task[0])
print(task[-1])
Tuple:

tup = (4, 1, 2, 3, 6)
tup_name = ( 'c','a', 'b', 'd')

print(tup)
print(tup_name)

print("----------Sort--------------")
sort = sorted(tup)
print(sort)
sort_var = sorted(tup_name)
print(sort_var)

print("----------Reverse--------------")
rev_sort_var = sorted(tup_name, reverse=True)
print(rev_sort_var)
rev_sort = sorted(tup, reverse=True)
print(rev_sort)

Q.2) Create a Python code to demonstrate the use of sets and perform set operations
(union, intersection, difference) to manage student enrollments in multiple courses /
appearing for multiple entrance exams like CET, JEE, NEET etc.

# Create sets of students enrolled in different entrance exams


cet_students = {"John", "Alice", "Bob", "David", "Eve"}
jee_students = {"Alice", "Bob", "Charlie", "David", "Grace"}
neet_students = {"Eve", "Grace", "Charlie", "Fay", "Hank"}

# 1. Union of sets: Students appearing for any of the exams (CET, JEE, or
NEET)
all_students = cet_students.union(jee_students, neet_students)
print("Students appearing for any exam (CET, JEE, NEET):")
print(all_students)

# 2. Intersection of sets: Students appearing for all three exams (CET,


JEE, NEET)
students_in_all_exams = cet_students.intersection(jee_students,
neet_students)
print("\nStudents appearing for all three exams (CET, JEE, NEET):")
print(students_in_all_exams)

# 3. Difference of sets: Students enrolled in CET but not in JEE or NEET


cet_only_students = cet_students.difference(jee_students, neet_students)
print("\nStudents enrolled only in CET:")
print(cet_only_students)

# 4. Symmetric Difference of sets: Students enrolled in either CET, JEE,


or NEET but not in both
exclusive_students =
cet_students.symmetric_difference(jee_students).symmetric_difference(neet_
students)
print("\nStudents appearing in only one of the exams (CET, JEE, or NEET,
but not both):")
print(exclusive_students)

Q.3) Write a Python program to create, update, and manipulate a dictionary of student
records, including their grades and attendance.

'''
Write a Python program to create, update, and manipulate a
dictionary of student records, including their grades and attendance.
'''
print("---------------dictionaries----------------------")
dictionaries = {"roll" : 1, "Name" : "A", "Year" : "SE", "Age": 20}
print(dictionaries)

print(type(dictionaries))
print("length of dict is : ", len(dictionaries))
print([Link]())
print([Link]())

print("-------------access an item-------------------")

print(dictionaries['roll'])
print([Link]("Name"))
print("-------------update/add-------------------")

dictionaries['grade'] = "A"
print("Add new element: ", dictionaries)

dictionaries['Attendance'] = "30%"
print("Update dictionary : ", dictionaries)

[Link](occupation = "software developer")


print(dictionaries)

print("-------------Update-------------------")
dictionaries["Age"]=25
print(dictionaries)

print("-------------delete-------------------")

[Link]("occupation")
print(dictionaries)

del dictionaries['Age']
print(dictionaries)

Common questions

Powered by AI

Exception handling is critical when performing arithmetic operations, like division, to manage cases such as division by zero, which result in runtime errors . By handling exceptions, programs can provide meaningful error messages and maintain stability, preventing crashes and ensuring consistent user experience during erroneous outputs .

Python programs can improve user experience in input and output interactions by implementing clear, user-friendly prompts, validating inputs to prevent errors, providing informative feedback, and structuring outputs in an organized and readable format . Using formatted strings for outputs enhances clarity, aiding users in understanding results and interactions effectively .

Immutability in tuples enhances reliability in Python programs by ensuring that data cannot be altered after creation, which prevents unintended side effects and promotes thread-safe operations . This characteristic is beneficial in scenarios where consistent data states are critical, making tuples ideal for fixed collections of related data .

Separation of concerns is crucial in structuring Python programs to maintain modularity, clarity, and reusability. By dividing tasks into distinct functions or modules, as seen in separating geometric calculations from input/output operations, the code becomes more organized and easier to debug, test, and maintain . This principle enhances collaboration and reduces complexity through well-defined responsibilities .

The challenges of using Python's set operations for identifying exclusive student enrollments include ensuring accurate input data and understanding set operations logic. However, the benefits include efficient computation, succinct code, and clarity in identifying students uniquely appearing for one exam . Set operations like symmetric_difference simplify these logical checks, providing clear enrollment differentiation .

Using functions to calculate areas of geometric shapes, such as a circle, rectangle, and square, promotes code reusability, encapsulation, and simplification of complex problems by breaking them into smaller, manageable parts . Functions allow the same logic to be reused with different inputs, improving maintainability and readability of the code .

Demonstrating arithmetic operations in Python helps beginners understand basic mathematics by providing an interactive platform to perform and visualize operations such as addition, subtraction, multiplication, division, and modulus . This hands-on approach simplifies learning and reinforces mathematical concepts through practical application .

A task list implemented using lists and tuples demonstrates basic list operations such as adding elements with append and insert, removing elements with remove, popping elements, and selecting items using indices . These operations highlight how lists can be modified, whereas tuples remain immutable, making them suitable for different scenarios within task management .

Set operations in Python, such as union, intersection, difference, and symmetric difference, allow efficient management of student enrollments in multiple exams by facilitating operations like checking students appearing for any exam, all exams, specific exams, or exclusive exams . This helps to quickly derive insights about shared and exclusive enrollments across different entrance exams .

Dictionary manipulation in Python is essential for efficiently managing student records as it allows dynamic access, updates, and management of data elements such as grades and attendance, using key-value pairs . Operations like adding new entries, updating existing data, and removing elements are straightforward, providing a flexible and efficient way to handle varied data about students .

You might also like