0% found this document useful (0 votes)
13 views

Python Ex

Uploaded by

anantsharma99262
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Python Ex

Uploaded by

anantsharma99262
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Institute of Technology & Management, Gwalior

Department of Computer Science & Engineering


Continuous Assessment Sheet (CAS)
Academic Year: Jan – June 2024 Course Name: Python Lab (CS-406)

Name of Student: _________________ Class : B.Tech (CSE) IV Sem (C) Batch : __ Roll No.: 0905CS22____

Exp Date Concept Program Oral and Total Date of Signature


. Underst Executio Report (30) Submis of Staff
Title of Experiment anding n and Writing sion
No.
(10) (10) (10)
Write a program to demonstrate different
1
number data types in Python.
Write a program to perform different
2
Arithmetic Operations on numbers in Python.
Write a program to create, concatenate and
3 print a string and accessing sub-string from a
given string.
Write a program to create, append, and
4
remove lists in python.
Write a program to demonstrate working
5
with tuples in python.
Write a program to demonstrate working
6
with dictionaries in python.
Write a python program to find largest of
7
three numbers.
Write a Python program to convert
8 temperatures to and from Celsius,
Fahrenheit. [Formula: c/5 = f-32/9]
Write a Python program to construct the
9
stars(*) pattern, using a nested for loop.
10 Write a Python script that prints prime
numbers less than 20.
Write a python program to find factorial of a
11
number using Recursion.
Using a numpy module create an array and
check the following: 1. Type of array 2. Axes
12
of array 3. Shape of array 4. Type of elements
in array
13 Write a python program Using a numpy
module create array and check the following:
1. List with type float 2. 3*4 array with all
zeros 3. From tuple 4. Random values
14 Write a python code to read a csv file using
pandas module and print the first and last
five lines of a file.
Total Marks

Signature Sign Signature


Head of Department Course Teacher Student
Institute of Technology & Management, Gwalior

Rubrics for Lab Evaluation


Course: PYTHON Lab (CS 406)

Criteria Level 1 Level 2 Level 3 Level 4


Poor Average Good Very Good

Range(Marks) 0-2 3-5 6-8 9-10


Student is able to Student is able to
Concept Student is not able Student is able to
understand given demonstrate knowledge
Understandings to understand given understand given
problem statement and about given problem
(10M) problem statement. problem statement.
interpret it as well. statement convincingly.
Range(Marks) 0-2 3-5 6-8 9-10
Program Not able to write, Able to write but not Able to write, apply
Able to write, apply and
Execution apply and apply and implement but not implement
implement concepts.
(10M) implement concepts. concepts. concepts.
Range(Marks) 0-2 3-5 6-8 9-10
Not able to submit Able to submit file in
Able to submit file in Able to submit file in
File submission file in given given deadline but
given deadline as well given deadline with
and Oral deadline and poor incomplete and
as good performance exceptional performance
(10M) performance in oral average performance
in oral. in oral.
as well. in oral as well.

Signature Signature Signature


Course I/C Course coordinator Head of Department
Experiment No. 1
Write a program to demonstrate different number data types in
Python.
Input.
# Integer
num_int = 10
print("Integer: ", num_int)

# Floating point
num_float = 10.5
print("Float: ", num_float)

# Complex number
num_complex = 3 + 4j
print("Complex: ", num_complex)

# Boolean
num_bool = True
print("Boolean: ", num_bool)

Output.
Integer: 10
Float: 10.5
Complex: (3+4j)
Boolean: True
=== Code Execution Successful ===
Experiment No. 2
Write a program to perform different Arithmetic Operations on
numbers in Python.
Input.
# Define the numbers
num1 = 10
num2 = 5

# Addition
print("Addition: ", num1 + num2)

# Subtraction
print("Subtraction: ", num1 - num2)

# Multiplication
print("Multiplication: ", num1 * num2)

# Division
print("Division: ", num1 / num2)

# Modulus
print("Modulus: ", num1 % num2)
# Exponentiation
print("Exponentiation: ", num1 ** num2)

# Floor division
print("Floor division: ", num1 // num2)

Output.
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
Modulus: 0
Exponentiation: 100000
Floor division: 2

=== Code Execution Successful ===


Experiment No. 3
Write a program to create, concatenate and print a string
and accessing sub-string from a given string.
Input.
# Create strings
str1 = "Hello"
str2 = "World"

# Concatenate strings
str3 = str1 + " " + str2
print("Concatenated String: ", str3)

# Access substring
substr = str3[0:5]
print("Substring: ", substr)

Output.

Concatenated String: Hello World


Substring: Hello
=== Code Execution Successful ===
Experiment No. 4
Write a program to create, append, and remove lists in
python.
Input.
# Create a list
list1 = [1, 2, 3]
print("Original List: ", list1)

# Append an element
list1.append(4)
print("List After Appending: ", list1)

# Remove an element
list1.remove(2)
print("List After Removing: ", list1)
Output.
Original List: [1, 2, 3]
List After Appending: [1, 2, 3, 4]
List After Removing: [1, 3, 4]

=== Code Execution Successful ===


Experiment No. 5
Write a program to demonstrate working with tuples in
python.
Input.

# Create a tuple
tuple1 = (1, 2, 3)
print("Original Tuple: ", tuple1)

# Access elements from a tuple


print("First Element: ", tuple1[0])

# Length of a tuple
print("Length of Tuple: ", len(tuple1))

# Concatenation of tuples
tuple2 = (4, 5, 6)
tuple3 = tuple1 + tuple2
print("Concatenated Tuple: ", tuple3)
# Repetition in tuple
tuple4 = tuple1 * 3
print("Repeated Tuple: ", tuple4)

# Check if an element exists


print("Is 2 in tuple1? ", 2 in tuple1)

Output.

Original Tuple: (1, 2, 3)


First Element: 1
Length of Tuple: 3
Concatenated Tuple: (1, 2, 3, 4, 5, 6)
Repeated Tuple: (1, 2, 3, 1, 2, 3, 1, 2, 3)
Is 2 in tuple1? True

=== Code Execution Successful ===


Experiment No. 6
Write a program to demonstrate working with dictionaries in
python.

Input.

# Create a dictionary
dict1 = {'name': 'John', 'age': 25, 'job': 'Engineer'}
print("Original Dictionary: ", dict1)

# Access elements from a dictionary


print("Name: ", dict1['name'])

# Update a value in the dictionary


dict1['age'] = 26
print("Dictionary After Updating: ", dict1)

# Add a new key-value pair


dict1['city'] = 'New York'
print("Dictionary After Adding: ", dict1)

# Remove a key-value pair


del dict1['job']
print("Dictionary After Removing: ", dict1)

# Check if a key exists


print("Is 'name' in dict1? ", 'name' in dict1)

Output.

Original Dictionary: {'name': 'John', 'age': 25, 'job': 'Engineer'}


Name: John
Dictionary After Updating: {'name': 'John', 'age': 26, 'job':
'Engineer'}
Dictionary After Adding: {'name': 'John', 'age': 26, 'job':
'Engineer', 'city': 'New York'}
Dictionary After Removing: {'name': 'John', 'age': 26, 'city':
'New York'}
Is 'name' in dict1? True

=== Code Execution Successful ===


Experiment No. 7
Write a python program to find largest of three numbers.
Input.

# Define the three numbers


num1 = 10
num2 = 20
num3 = 15
# Find the largest number
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3

print("The largest number is", largest)

Output.

The largest number is 20


=== Code Execution Successful ===
Experiment No. 8

Write a Python program to convert temperatures to and


from Celsius, Fahrenheit. [Formula: c/5 = f-32/9]

Input.

# Function to convert Celsius to Fahrenheit


def celsius_to_fahrenheit(c):
return (9/5) * c + 32

# Function to convert Fahrenheit to Celsius


def fahrenheit_to_celsius(f):
return (5/9) * (f - 32)

# Test the functions


celsius = 20
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"{celsius} degrees Celsius is equal to {fahrenheit}
degrees Fahrenheit")
fahrenheit = 68
celsius = fahrenheit_to_celsius(fahrenheit)
print(f"{fahrenheit} degrees Fahrenheit is equal to
{celsius} degrees Celsius")

Output.

20 degrees Celsius is equal to 68.0 degrees Fahrenheit


68 degrees Fahrenheit is equal to 20.0 degrees Celsius

=== Code Execution Successful ===


Experiment No. 9
Write a Python program to construct the stars(*) pattern, using
a nested for loop.

Input.

# Number of rows
rows = 5

# Nested for loop to print the star pattern


for i in range(rows):
for j in range(i+1):
print("*", end="")
print()

Output.
*
**
***
****
*****
=== Code Execution Successful ===
Experiment No. 10
Write a Python script that prints prime numbers less
than 20.

Input.

# Function to check if a number is prime


def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i=5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
# Print prime numbers less than 20
for i in range(20):
if is_prime(i):
print(i)

Output.

2
3
5
7
11
13
17
19

=== Code Execution Successful ===


Experiment No. 11
Write a python program to find factorial of a number using
Recursion.

Input.

# Function to calculate factorial


def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)

# Test the function


num = 5
print(f"The factorial of {num} is {factorial(num)}")

Output.

The factorial of 5 is 120

=== Code Execution Successful ===


Experiment No. 12
Using a numpy module create an array and check the
following: 1. Type of array 2. Axes of array 3. Shape of
array 4. Type of elements in array.

Input.

import numpy as np

# Create a numpy array


arr = np.array([[1, 2, 3], [4, 5, 6]])

# Check type of array


print("Type of array: ", type(arr))

# Check axes (dimensions) of array


print("Number of axes (dimensions): ", arr.ndim)

# Check shape of array


print("Shape of array: ", arr.shape)
# Check type of elements in array
print("Type of elements: ", arr.dtype)

Output.

Type of array: <class 'numpy.ndarray'>


Number of axes (dimensions): 2
Shape of array: (2, 3)
Type of elements: int64

=== Code Execution Successful ===


Experiment No. 13
Write a python program Using a numpy module create
array and check the following: 1. List with type float 2.
3*4 array with all zeros 3. From tuple 4. Random values.

Input.

import numpy as np

# 1. Create a numpy array from a list with type float


list1 = [1.0, 2.0, 3.0]
arr1 = np.array(list1, dtype=float)
print("Array from list with type float: ", arr1)

# 2. Create a 3x4 array with all zeros


arr2 = np.zeros((3, 4))
print("3x4 array with all zeros: \n", arr2)

# 3. Create a numpy array from a tuple


tuple1 = (1, 2, 3)
arr3 = np.array(tuple1)
print("Array from tuple: ", arr3)

# 4. Create a numpy array with random values


arr4 = np.random.rand(3, 2)
print("Array with random values: \n", arr4)

Output.

Array from list with type float: [1. 2. 3.]


3x4 array with all zeros:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
Array from tuple: [1 2 3]
Array with random values:
[[0.17134878 0.56279447]
[0.93081204 0.52168756]
[0.03521853 0.96406569]]

=== Code Execution Successful ===

You might also like