0% found this document useful (0 votes)
2 views8 pages

Python File

The document is a Python lab manual containing various programs that demonstrate fundamental programming concepts such as checking for prime numbers, counting digits, generating random numbers, and basic operations on data structures like lists, tuples, and dictionaries. It also includes examples of exception handling and file operations. Each program is accompanied by sample input and output to illustrate its functionality.

Uploaded by

srshrdhr
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)
2 views8 pages

Python File

The document is a Python lab manual containing various programs that demonstrate fundamental programming concepts such as checking for prime numbers, counting digits, generating random numbers, and basic operations on data structures like lists, tuples, and dictionaries. It also includes examples of exception handling and file operations. Each program is accompanied by sample input and output to illustrate its functionality.

Uploaded by

srshrdhr
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

Python-lab manual

1. Program to Check if a Given Number is Prime

num = int(input("Enter a number: "))


if num > 1:
for i in range(2, num):
if num % i == 0:
print("Not a Prime Number")
break
else:
print("Prime Number")
else:
print("Not a Prime Number")

Output:
Enter a number: 7
Prime Number

2. Program to Check if Number is Positive, Negative or Zero

num = float(input("Enter a number: "))


if num > 0:
print("Positive Number")
elif num < 0:
print("Negative Number")
else:
print("Zero")

Output:
Enter a number: -5
Negative Number

Department of Computer science,GFGC Shimoga 1


Python-lab manual

3. Program to Count Digits in a Number (Using while Loop)

num = int(input("Enter a number: "))


count = 0
while num != 0:
num = num // 10
count = count + 1
print("Total digits:", count)

Output:
Enter a number: 12345
Total digits: 5

4. Program to Generate Random Numbers and Calculate Sum (Using for Loop)

import random

n = int(input("How many random numbers? "))


total = 0

for i in range(n):
num = [Link](1, 100)
print("Random Number:", num)
total = total + num

print("Sum of random numbers:", total)

Output:
How many random numbers? 3
Random Number: 45
Random Number: 12
Random Number: 78
Sum of random numbers: 135

Department of Computer science,GFGC Shimoga 2


Python-lab manual

5. Program to Remove Punctuations from a String

string = input("Enter a string: ")


punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
result = ""
for char in string:
if char not in punctuations:
result = result + char
print("String without punctuation:", result)

Output:
Enter a string: Hello!!! How are you?
String without punctuation: Hello How are you

6. Program to Find Factorial of a Number

num = int(input("Enter a number: "))


fact = 1
for i in range(1, num + 1):
fact = fact * i
print("Factorial:", fact)

Output:
Enter a number: 5
Factorial: 120

Department of Computer science,GFGC Shimoga 3


Python-lab manual

7. Calculator Program

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


num2 = float(input("Enter second number: "))
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
choice = int(input("Enter your choice (1-4): "))

if choice == 1:
print("Result:", num1 + num2)
elif choice == 2:
print("Result:", num1 - num2)
elif choice == 3:
print("Result:", num1 * num2)
elif choice == 4:
print("Result:", num1 / num2)
else:
print("Invalid Choice")

Output:
Enter first number: 10
Enter second number: 5
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter your choice (1-4): 1
Result: 15.0

Department of Computer science,GFGC Shimoga 4


Python-lab manual

Part -b
1. Demonstrate Basic Operations on List (sum(), max(), min(), sort())

numbers = [10, 5, 20, 8, 15]


print("List:", numbers)
print("Sum:", sum(numbers))
print("Maximum:", max(numbers))
print("Minimum:", min(numbers))
[Link]()
print("Sorted List:", numbers)

Output:

List: [10, 5, 20, 8, 15]


Sum: 58
Maximum: 20
Minimum: 5
Sorted List: [5, 8, 10, 15, 20]

2. Program to Demonstrate Use of Tuples

student = ("Ram", 20, "BCA")


print("Tuple:", student)
print("Name:", student[0])
print("Age:", student[1])
print("Course:", student[2])

Output:
Tuple: ('Ram', 20, 'BCA')
Name: Ram
Age: 20
Course: BCA

Department of Computer science,GFGC Shimoga 5


Python-lab manual

3. Program to Demonstrate Use of Dictionaries

student = {
"name": "Ram",
"age": 20,
"course": "BCA"
}

print("Dictionary:", student)
print("Name:", student["name"])
print("Age:", student["age"])

Output:
Dictionary: {'name': 'Ram', 'age': 20, 'course': 'BCA'}
Name: Ram
Age: 20

4. Program to Create Simple Class and Object

class Student:
def __init__(self, name, age):
[Link] = name
[Link] = age

def display(self):
print("Name:", [Link])
print("Age:", [Link])

s1 = Student("Ram", 20)
[Link]()

Output:
Name: Ram
Age: 20

Department of Computer science,GFGC Shimoga 6


Python-lab manual

5. Program to Count Number of Lines in a Text File

file = open("[Link]", "r")


count = 0

for line in file:


count = count + 1
print("Number of lines:", count)
[Link]()

Output:
Number of lines: 5

6. Program to Create DataFrame from Excel Sheet and Perform Simple Operations

import pandas as pd

df = pd.read_excel("[Link]")
print("DataFrame:")
print(df)
print("First 5 rows:")
print([Link]())
print("Summary:")
print([Link]())

Output:
DataFrame:
Name Marks
0 Ram 85
1 Sita 90

First 5 rows:
Name Marks
0 Ram 85
1 Sita 90

Summary:
Marks
count 2.0
mean 87.5

Department of Computer science,GFGC Shimoga 7


Python-lab manual

7. Program to Demonstrate Exception Handling

try:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
result = num1 / num2
print("Result:", result)
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Invalid input")
finally:
print("Program completed")

Output:

Enter first number: 10


Enter second number: 0
Cannot divide by zero
Program completed

Department of Computer science,GFGC Shimoga 8

You might also like