[Go to site: main page, start]

0% found this document useful (0 votes)
3 views14 pages

Pythonlab 1

Uploaded by

u.lingesh2007
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)
3 views14 pages

Pythonlab 1

Uploaded by

u.lingesh2007
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

1.

Largest of Three Numbers


PROGRAM:
a=int(input('enter the number'))
b=int(input('enter the number'))
c=int(input('enter the number'))
if a>b and a >= c:
print("a is greater")
elif b>c:
print("b is greater")
else:
print("c is greater")

Output:
enter the number 15
enter the number 2
enter the number 9
a is greater
>>>

enter the number 2


enter the number 5
enter the number 9
c is greater
-------------------
2. STUDENT RESULT

PROGRAM:

t=int(input("Enter your tamil mark:"))


e=int(input("Enter your English mark:"))
r=int(input("Enter your eco mark:"))
a=int(input("Enter your acc mark:"))
c=int(input("Enter your computer mark:"))
d=int(input("Enter your com mark :"))
total=t+e+r+a+c+d
print("Total:",total)
if((t>=35) and (e>=35) and (r>=35) and (a>=35) and (c>=35) and
(d>=35) and (total>=500)):
print("pass and first class")
elif((t>=35) and (e>=35) and (r>=35) and (a>=35) and (c>=35) and
(d>=35) and (total>=400)):
print("pass and second class")
elif((t>=35) and (e>=35) and (r>=35) and (a>=35) and (c>=35) and
(d>=35) and (total>=210)):
print("pass")
else:
print("fail")

OUTPUT:
Enter your tamil mark:99
Enter your English mark:88
Enter your eco mark:77
Enter your acc mark:55
Enter your computer mark:56
Enter your com mark :99
Total :474
pass and second class
3. String Operations
PROGRAM:
# 1. Create strings
str1 = "Hello"
str2 = "World"
# 2. Concatenate strings
result = str1 + " " + str2
print("Concatenated String:", result)
# 3. Access substrings
print("First 5 characters:", result[0:5]) # 'Hello'
print("Characters from index 6 to end:", result[6:]) # 'World'
print("Last 3 characters:", result[-3:]) # 'rld'
# 4. Other operations
print("Length of string:", len(result))
print("Uppercase:", [Link]())
print("Lowercase:", [Link]())

OUTPUT:
Concatenated String: Hello World
First 5 characters: Hello
Characters from index 6 to end: World
Last 3 characters: rld
Length of string: 11
Uppercase: HELLO WORLD
Lowercase: hello world
4. Area and perimeter of a circle using function
PROGRAM
import math
# Function to calculate area of a circle
def area_of_circle(radius):
return [Link] * radius * radius

# Function to calculate perimeter (circumference) of a circle


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

# Main program
r = float(input("Enter the radius of the circle: "))

area = area_of_circle(r)
perimeter = perimeter_of_circle(r)

print("Area of the circle:", area)


print("Perimeter (Circumference) of the circle:", perimeter)

OUTPUT:
Enter the radius of the circle: 5
Area of the circle: 78.53
Perimeter (Circumference) of the circle: 31.41
5. Array operations

PROGRAM:

import array as arr


# Create an integer array
a = [Link]('i', [1, 2, 3,42,4,30,12])
print(a[0])
# Append and insert
[Link](5)
[Link](1, 4)
print(a)
# Remove and pop
[Link](4)
[Link]()
print(a)

OUTPUT:

1
array('i', [1, 4, 2, 3, 42, 4, 30, 12, 5])
array('i', [1, 2, 3, 42, 4, 30, 12])
6. List Operations
PROGRAM:
# Create an empty list
numbers = []
# Input: number of elements
count = int(input("Enter how many numbers you want to add: "))
# Add elements to the list
for i in range(count):
while True:
num = float(input(f"Enter number {i+1}: "))
[Link](num)
break
# Display the list
print("\nOriginal List:", numbers)
# Sort the list
[Link]()
print("Sorted List:", numbers)
# Remove an element (if exists)
remove_val = float(input("\nEnter a number to remove: "))
if remove_val in numbers:
[Link](remove_val)
print("List after removal:", numbers)
else:
print(f"{remove_val} not found in the list.")
# Calculate sum and average
if numbers:
total = sum(numbers)
avg = total / len(numbers)
print(f"\nSum of numbers: {total}")
print(f"Average of numbers: {avg}")
else:
print("List is empty. Cannot calculate sum or average.")

OUTPUT:

Enter how many numbers you want to add: 3


Enter number 1: 30
Enter number 2: 40
Enter number 3: 50

Original List: [30.0, 40.0, 50.0]


Sorted List: [30.0, 40.0, 50.0]

Enter a number to remove: 40


List after removal: [30.0, 50.0]

Sum of numbers: 80.0


Average of numbers: 40.0
7. Tuple Demonstration
PROGRAM:
# Creating a tuple with student details
student = ("John", 21, "Computer Science", 8.5)
# Display the tuple
print("Student Tuple:", student)
print("Name:", student[0])
print("Age:", student[1])
print("Department:", student[2])
print("CGPA:", student[3])
# Slicing the tuple
print("First two elements:", student[:2])
print("Last two elements:", student[2:])
print("\nIterating through tuple:")
for item in student:
print(item)

name, age, dept, cgpa = student


print("\nUnpacked Values:")
print(f"Name: {name}, Age: {age}, Dept: {dept}, CGPA: {cgpa}")
# Demonstrating immutability
student[1] = 22 # This will raise an error
OUTPUT:
Student Tuple: ('John', 21, 'Computer Science', 8.5)
Name: John
Age: 21
Department: Computer Science
CGPA: 8.5
First two elements: ('John', 21)
Last two elements: ('Computer Science', 8.5)
Iterating through tuple:
John
21
Computer Science
8.5
Unpacked Values:
Name: John, Age: 21, Dept: Computer Science, CGPA: 8.5

Error: Tuples are immutable -> 'tuple' object does not support item
assignment
8. Module creation and usage
Module creation
# math_utils.py
def add(a, b):
return a + b
def multiply(a, b):
return a * b
def subtract(a, b):
return a – b

Module usage
# main_program.py
# Import only the 'multiply' function from math_utils module
from math_utils import multiply
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
print("Product of the numbers:", multiply(x, y))

OUTPUT:
Enter first number: 6
Enter second number: 7
Product of the numbers: 42
9. Exception handling for division by zero
PROGRAM:
def divide_numbers(a, b):
try:
result = a / b
print("Result of division:", result)
except ZeroDivisionError:
print("Error: Division by zero is not allowed!")

# Main program
num1 = int(input("Enter numerator: "))
num2 = int(input("Enter denominator: "))
divide_numbers(num1, num2)

OUTPUT:
Enter numerator: 10
Enter denominator: 2
Result of division: 5.0

Enter numerator: 10
Enter denominator: 0
Error: Division by zero is not allowed!
10. Simple calculator
PROGRAM:
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):
return a / b
# Main program
print("Simple Calculator")
print("Operations: + - * /")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
op = input("Enter operation (+, -, *, /): ")
if op == '+':
print("Result:", add(num1, num2))
elif op == '-':
print("Result:", subtract(num1, num2))
elif op == '*':
print("Result:", multiply(num1, num2))
elif op == '/':
print("Result:", divide(num1, num2))
else:
print("Invalid operation!")
OUTPUT:
Simple Calculator
Operations: + - * /
Enter first number: 10
Enter second number: 5
Enter operation (+, -, *, /): /
Result: 2.0

You might also like