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

Python

The document outlines a series of programming experiments conducted by a student named Puranpreet Singh, focusing on various Python applications such as array operations using NumPy, socket programming for connecting to Google, file information retrieval, and data serialization. Each experiment includes an aim, theoretical background, code examples, outputs, and learning outcomes. The practical exercises aim to enhance understanding of Python programming in data science and related fields.

Uploaded by

Harsh
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)
3 views13 pages

Python

The document outlines a series of programming experiments conducted by a student named Puranpreet Singh, focusing on various Python applications such as array operations using NumPy, socket programming for connecting to Google, file information retrieval, and data serialization. Each experiment includes an aim, theoretical background, code examples, outputs, and learning outcomes. The practical exercises aim to enhance understanding of Python programming in data science and related fields.

Uploaded by

Harsh
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 No.

5
Student Name: Puranpreet Singh UID: 24BEC10030
Branch: Electronic And Communication Section/Group: 24BEC307
Date of Performance: 24/02/26 Subject Name: Programming for Data Science

1. Aim of the practical:


Python program to perform Array operations using Numpy package.

2. Tool Used: Google Colab

3. Theory:

NumPy (Numerical Python) is a powerful open-source Python library used for


numerical computing. It provides support for large, multi-dimensional arrays and
matrices along with a collection of mathematical functions to operate on them
efficiently. The core object in NumPy is the ndarray (N-dimensional array), which
allows fast and memory-efficient storage of homogeneous data. Array operations in
NumPy are faster than Python lists because they are implemented in C and use
vectorization, eliminating the need for explicit loops. Common array operations include
creation of arrays, reshaping, indexing, slicing, mathematical operations (addition,
subtraction, multiplication, division), statistical functions (mean, sum, standard
deviation), and matrix operations (transpose, dot product). NumPy also supports
broadcasting, which allows arithmetic operations between arrays of different shapes.
Due to its speed and efficiency, NumPy is widely used in data science, machine learning,
scientific computing, and engineering applications. It forms the foundation for libraries
like Pandas and SciPy.

4. Code:
import numpy as np
Ar1 = [Link]([1, 2, 3])
Ar2 = [Link]([4, 5, 6])
print("Array 1:", Ar1)
print("Array 2:", Ar2)
print("Addition:", [Link](Ar1, Ar2))
print("Subtraction:", [Link](Ar1, Ar2))
print("Multiplication:", [Link](Ar1, Ar2))
print("Division:", [Link](Ar1, Ar2))
print("Square root of Array 1:", [Link](Ar1))
print("Square root of Array 2:", [Link](Ar2))
print("Minimum of Array 1:", [Link](Ar1))
print("Minimum of Array 2:", [Link](Ar2))
print("Maximum of Array 1:", [Link](Ar1))
print("Maximum of Array 2:", [Link](Ar2))
print("Mean of Array 1:", [Link](Ar1))
print("Mean of Array 2:", [Link](Ar2))
print("Median of Array 1:", [Link](Ar1))
print("Median of Array 2:", [Link](Ar2))
print("Sum of Array 1:", [Link](Ar1))
print("Sum of Array 2:", [Link](Ar2))
print("Standard Deviation of Array 1:", [Link](Ar1))
print("Standard Deviation of Array 2:", [Link](Ar2))
Ar3 = [Link]([[1, 2, 3, 10],
[4, 5, 6, 11],
[7, 8, 9, 12]])

print("Array Ar3:", Ar3)


print("Reshaped array Ar3 (2 rows, 6 columns):", [Link](Ar3, (2, 6)))
Ar4 = [Link]([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

print("Array Ar4:", Ar4)


print("Transpose of Array Ar4:", [Link](Ar4))

5. OUTPUT

FIG1. Arithmetic Operation on Array

FIG2. Aggregation Operation on Array


6. LEARNING OUTCOMES:
 Understand how to create and display one-dimensional and two-dimensional
arrays using NumPy.
 Perform basic arithmetic operations like addition, subtraction, multiplication,
division, and square root on arrays.
 Calculate aggregation values such as minimum, maximum, mean, median, sum,
and standard deviation.
 Reshape arrays into different dimensions without changing the data.
 Transpose a matrix to interchange rows and columns easily using NumPy
functions.

Grid Evaluation:
EXPERIMENT NO. 4
Student Name: Puranpreet Singh UID: 24BEC10030
Branch: Electronics And Communication Section/Group:24BEC307
Date of Performance:16/02/26 Subject Name: Programming For Data Science

AIM OF THE PRACTICAL:


Python program to connect to Google using socket programming.

TOOL USED: Google Collab

THEORY:
This Python program demonstrates socket programming to establish a connection with Google’s
web server. A socket is an endpoint used for communication between two computers over a
network. The program imports the socket module and defines a function connect_to_google().
Inside the function, the host is set to "[Link]" and the port number is set to 80, which is
the standard port for HTTP communication.
A TCP socket is created using socket.AF_INET (IPv4 addressing) and socket.SOCK_STREAM
(TCP protocol). The connect() method attempts to establish a connection with Google’s server. If
the connection is successful, a confirmation message is printed. After connecting, the socket is
closed to release network resources. If any error occurs during the connection process, it is
handled using a try-except block, which prints an appropriate error message.

CODE:
import socket

def connect_to_google():
host = "[Link]"
port = 80
try:
# Create a socket object
client_socket = [Link](socket.AF_INET, socket.SOCK_STREAM)
# Connect to the Google server
client_socket.connect((host, port))
print("Connected to Google successfully.")
# Close the socket connection
client_socket.close()
except [Link] as e:
print("Failed to connect to Google. Error:", e)
if name == " main ":
connect_to_google()
OUTPUT:

Fig1. Program to Connect Google

Fig 2. Program To Connect Wikipedia


LEARNING OUTCOME
1. Understand the basic concept of socket programming in Python.
2. Learn how to create a TCP socket using socket.AF_INET and socket.SOCK_STREAM.
3. Understand how to establish a client-server connection using the connect() method.
4. Learn the importance of proper resource management by closing the socket after use.
5. Understand how to handle network errors using the try-except exception handling
mechanism.

Grid Evaluation:
EXPERIMENT NO. 3
Student Name: Puranpreet Singh UID: 24BEC10030
Branch: Electronics And Communication Section/Group:24BEC307
Date of Performance:03/02/26 Subject Name: Programming For Data Science

AIM OF THE PRACTICAL:


Python program to get information about the file pertaining to the file mode and to get time values
with components using local time and gm time.

TOOL USED: Google Collab

THEORY:
This Python program is used to obtain detailed information about a file, specifically its access
mode and time-related attributes. File mode represents the permissions and status of a file in the
operating system, indicating how the file can be accessed or modified. Python provides built-in
modules such as os and time to interact with the file system efficiently. Using these modules, the
program retrieves the file’s last modification time and converts it into human-readable
components like year, month, day, hour, minute, and second. The time values are displayed using
both local time and GMT (UTC), enabling comparison between system time and universal time.
This approach is useful in file management, system monitoring, auditing, and logging
applications, where accurate time tracking and file permission details are essential for reliable
operation and security.

CODE:
import os
import time

# Get file information


def get_file_info(file_path):

# Check if file exists


if not [Link](file_path):
print("File does not exist.")
return

# Get file mode


file_mode = [Link](file_path).st_mode
print("File Mode:", file_mode)

# Get time values using local time


local_time = [Link](file_path)
local_time_components = [Link](local_time)

print("\nLocal Time:")
print("Year:", local_time_components.tm_year)
print("Month:", local_time_components.tm_mon)
print("Day:", local_time_components.tm_mday)
print("Hour:", local_time_components.tm_hour)
print("Minute:", local_time_components.tm_min)
print("Second:", local_time_components.tm_sec)

# Get time values using GMT (UTC)


gmt_time_components = [Link](local_time)

print("\nGMT (UTC) Time:")


print("Year:", gmt_time_components.tm_year)
print("Month:", gmt_time_components.tm_mon)
print("Day:", gmt_time_components.tm_mday)
print("Hour:", gmt_time_components.tm_hour)
print("Minute:", gmt_time_components.tm_min)
print("Second:", gmt_time_components.tm_sec)

# Example usage
file_path = r"/content/[Link]"
get_file_info(file_path)

OUTPUT:
LEARNING OUTCOME
1. Understand how to access and interpret file mode and permission details using Python.
2. Learn to retrieve file time information and break it into individual time components.
3. Differentiate between local time and GMT (UTC) representations of file timestamps.
4. Gain practical experience in using Python’s os and time modules for file handling.
5. Apply file information retrieval techniques in real-world applications such as system
monitoring and logging.

Grid Evaluation:
EXPERIMENT NO. 2
Student Name: Puranpreet Singh UID: 24BEC10030
Branch: Electronics And Communication Section/Group:24BEC307
Date of Performance:20/01/26 Subject Name: Programming For Data Science

AIM OF THE PRACTICAL:


Python Programme to convert an array to an byte of machine values and vice versa

TOOL USED: Google Collab

THEORY:
In Python, the process of converting an array into bytes and then reconstructing it back into its
original form is based on the concept of serialization and deserialization. Serialization refers to
transforming structured data, such as an array, into a continuous stream of bytes that can be easily
stored in files, transmitted across networks, or processed by machines. Each element of the array
is represented in its binary form according to the system’s architecture, making the data compact
and machine-readable. Deserialization is the reverse process, where the byte stream is interpreted
back into the original structured format, restoring the values exactly as they were. This
mechanism ensures that data can move seamlessly between human-readable structures and
machine-level representations without loss of information. It is widely used in applications like
data storage, cryptography, networking, and system programming, where efficiency and accuracy
in handling raw machine values are critical. By understanding this theory, learners gain insight
into how programming languages bridge the gap between high-level abstractions and low-level
binary operations, which is fundamental for working with memory management, file handling,
and secure data transmission.

CODE:
import struct

def array_to_bytes(array):
format_string = f"{len(array)}b"
packet_data = [Link](format_string, *array)
return packet_data

def bytes_to_array(bytes_data):
num_elements = len(bytes_data)
ormat_string = f"{num_elements}b"
unpacked_data = [Link](format_string, bytes_data)
return list(unpacked_data)
# Input Example usage
input_array = [10, 20, 30, 40, 50]

# Convert Array_to_bytes
bytes_data = array_to_bytes(input_array)
print("Array as bytes:", bytes_data)

# Convert bytes_to_array
output_array = bytes_to_array(bytes_data)
print("Bytes as array:", output_array)

OUTPUT:

Conceptual Clarity on Data Representation


Learners understand how arrays, which are structured collections of elements, can be transformed
into raw binary values that computers process internally.
1. Mastery of Serialization and Deserialization
Students gain practical knowledge of converting arrays into byte streams (serialization) and
reconstructing them back into arrays (deserialization), ensuring data integrity.
2. Efficiency in Storage and Transmission
By working with bytes, learners appreciate how data can be stored compactly and
transmitted quickly across networks, which is vital in system programming and networking.
3. Application in Real-World Scenarios
The exercise highlights how these concepts are applied in cryptography, databases, and file
handling, bridging theoretical knowledge with practical use cases.
4. Strengthened Programming Skills
Learners develop confidence in using Python’s built-in methods (tobytes() and
frombytes()), enhancing their ability to handle low-level operations and preparing them for
advanced topics like memory management and secure data exchange.

Grid Evaluation:

You might also like