Python Programming manual (4)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

137 Government Polytechnic College, Gandharvakottai

Department of Computer Engineering


1052234440 Python Programming Manual

Designed By,
S.Karthiga, Lecturer,
Department of Computer Engineering,
137, Government Polytechnic College,
Gandharvakottai.
PART – A
Ex No 1: Greatest of three numbers

Question:
Write a python program to read three numbers and print the greatest of three numbers.
Aim:
To write a python program to read three numbers and print the greatest of three numbers.
Program:
print("Enter Three Numbers")
a=int(input())
b=int(input())
c=int(input())
if(a>b) and (a>c):
greatest=a
elif b>c:
greatest=b
else:
greatest=c
print("the greatest of three numbers")
print(greatest)

Output:
Enter Three Numbers
5
7
9
the greatest of three numbers
9

Result:
Thus the python program to read three numbers and print the greatest of three numbers is
successfully executed.
Ex No 2: Sum of N numbers

Question:
Write a python program to find the sum of N numbers using range () function in for loop.

Aim:
To write a python program to find the sum of N numbers using range () function in for loop.

Program:
print("Enter a number")
n=int(input())
sum=0
for i in range(n+1):
sum=sum+i
print("Sum of N numbers is ")
print(sum)

Output:
Enter a number
5
Sum of N numbers is
10

Result:
Thus the python program to find the sum of N numbers using range() function in for loop is
successfully executed.
Ex No 3: String Operations

Question:
Write a python program to demonstrate the string slicing, concatenation, replication and len()
method.

Aim:
To write a python program to demonstrate the string slicing, concatenation, replication and
len() method.

Program:
s1="GPTC"
s2="GKT"
print(s1[:3])
print(s1[1:])
print(s1+s2)
print(s1*3)
print(len(s1))

Output:
GPT
PTC
GPTCGKT
GPTCGPTCGPTC
4

Result:
Thus the python program demonstrates the string slicing, concatenation, replication and len()
method is successfully executed.
Ex No 4: Tuple and List
Question:
Write a python program to create a tuple and convert into a list and print the list in sorted order.

Aim:
To write a python program to create a tuple and convert into a list and print the list in sorted
order.

Program:
t=(5,2,3,1,8)
print("Created tuple is:")
print(t)
li=list(t)
print("Tuple converted to list :")
print(li)
print("Sorted list is ")
print(sorted(li))

Output:
Created tuple is:
(5, 2, 3, 1, 8)
Tuple converted to list :
[5, 2, 3, 1, 8]
Sorted list is
[1, 2, 3, 5, 8]

Result:
Thus the python program to create a tuple and convert into a list and print the list in sorted
order is successfully executed.
Ex No 5: Dictionary

Question:
Write a python program to create a dictionary and check whether a key or value exists in the
dictionary.

Aim:
To write a python program to create a dictionary and check whether a key or value exists in the
dictionary.

Program:
dic={"rollno":1,
"name":"Anbu",
"age": 12}
print("Enter Key to check")
k=input()
if k in dic.keys():
print("Yes "+ k + "is a key")
else:
print("No " + k + "is not a key")
print("Enter value to check")
v=input()
if v in dic.values():
print("Yes "+ v + "is a value")
else:
print("No " + v + "is not a value")

Output:
Enter Key to check
dob
No dob is not a key
Enter value to check
Anbu
Yes Anbu is a value

Result:
Thus the python program creates a dictionary and checks whether a key or value exists in the
dictionary and is successfully executed.
PART - 2

Ex No 6: NumPy Reshape and Slicing

Question:
Write a python program to create a one dimensional array and convert into a 2D-dimensional
array using reshape(), print the first two columns alone using slicing.

Aim:
To write a python program to create a one dimensional array and convert into a
2D-dimensional array using reshape(), print the first two columns alone using slicing.

Program:
import numpy as np
n=np.array([1,2,3,4,5,6,7,8,9])
print("One Dimensional array")
print(n)
m=n.reshape(3,3)
print("Two Dimensional array")
print(m)
print("First two columns")
print(m[:,:2])

Output:
One Dimensional array
[1 2 3 4 5 6 7 8 9]
Two Dimensional array
[[1 2 3]
[4 5 6]
[7 8 9]]
First two columns
[[1 2]
[4 5]
[7 8]]

Result:
Thus the python program to create a one dimensional array and convert into a 2D-dimensional
array using reshape(), print the first two columns alone using slicing is successfully executed.
Ex No 7: Numpy search

Question:
Write a python program to create two-dimensional array and search for an element using
where() function.
Aim:
To write a python program to create two-dimensional array and search for an element using
where() function.
Program:
import numpy as np
n=np.array([[5,6,7],
[9,7,8],
[7,3,2]])
print("Enter element to search")
e=int(input())
r=np.where(n==e)
if r[0].size >0:
print("Element found at positions")
print(r)
else:
print("Element not found")

Output:
Enter element to search
7
Element found at positions
(array([0, 1, 2], dtype=int64), array([2, 1, 0], dtype=int64))

Result:
Thus, the python program to create two-dimensional array and search for an element using
where() function is successfully executed.
Ex No 8: Numpy Aggregation Functions
Question:
Write a python program to create a 2D-dimensional array and demonstrate aggregation
functions sum (), min () and max () in the row and column wise.

Aim:
To Write a python program to create a 2D-dimensional array and demonstrate aggregation
functions sum (), min () and max () in the row and column wise.

Program:
import numpy as np
n=np.array([[1,2,3],
[4,5,6],
[7,8,9]])
print("Row Wise Aggregate Functions")
print("Sum ")
print(np.sum(n,axis=1))
print("Min")
print(np.min(n,axis=1))
print("Max")
print(np.max(n,axis=1))

print("\n")
print("Column wise Aggregate Functions")
print("Sum")
print(np.sum(n,axis=0))
print("Min")
print(np.min(n,axis=0))
print("Max")
print(np.max(n,axis=0))

Output:
Row Wise Aggregate Functions
Sum
[ 6 15 24]
Min
[1 4 7]
Max
[3 6 9]
Column wise Aggregate Functions
Sum
[12 15 18]
Min
[1 2 3]
Max
[7 8 9]

Result:
Thus the python program to create a 2D-dimensional array and demonstrate aggregation
functions sum (), min () and max () in the row and column wise is successfully executed.
Ex No 9 Text File Handling
Question:
Write a python program to read a text file and write the content in another file.

Aim:
To write a python program to read a text file and write the content in another file.

Program:
with open ('file1.txt','r') as input:
with open ('file2.txt','w') as output:
for line in input:
output.write(line)
print('Content written Successfully')

Output:
Content written Successfully

Result:
Thus, the python program to read a text file and write the content in another file is successfully
executed.
Ex No 10 Pandas-CSV File Handling

Question:
Write a python program to read a csv file using pandas and print the content.

Aim:
To write a python program to read a csv file using pandas and print the content.

Program:
import pandas as pd
data= pd.read_csv('sample.csv')
print(data)

Output:
Index User Id ... Date of birth Job Title
0 1 88F7B33d2bcf9f5 ... 1945-10-26 Games developer
1 2 f90cD3E76f1A9b9 ... 1910-03-24 Phytotherapist
2 3 DbeAb8CcdfeFC2c ... 1992-07-02 Homeopath
3 4 A31Bee3c201ef58 ... 2017-08-03 Market researcher
4 5 1bA7A3dc874da3c ... 1938-12-01 Veterinary surgeon
.. ... ... ... ... ...
95 96 5eFda7caAeB260E ... 1954-07-30 Software engineer
96 97 CCbFce93d3720bE ... 1932-04-29 Barrister
97 98 2fEc528aFAF0b69 ... 1994-12-28 Police officer
98 99 Adc7ad9B6e4A1Fe ... 2012-04-12 Broadcast journalist
99 100 b8D0aD3490FC7e1 ... 2016-11-15 IT sales professional
[100 rows x 9 columns]

Result:
Thus, the python program reads a csv file using pandas and prints the content is successfully
executed.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy