python full document
python full document
BHARATHIAR UNIVERSITY
REGISTER NUMBER :
SEMESTER - I
24DS1C5
NOVEMBER 2024
DEPARTMENT OF COMPUTER SCIENCE
BHARATHIAR UNIVERSITY
CERTIFICATE
This is to certify that the bonafide record work PYTHON AND R PROGRAMMING LAB –
24DS1C5 was done and submitted by Mr. / Ms. with the Register Number
Data Science at the Department of Computer Science, Bharathiar University, Coimbatore – 641
OUTPUT
2. Conversion of ASCII to binary
To convert an ASCII character to its binary equivalent and display the result.
print("ASCII TO BINARY")
x = input("Enter a Character: ")
y = ord(x)
print("The ASCII value of the given character is", y)
print("The Binary value of ASCII is", bin(y))
OUTPUT
3. Printing the first n row of Pascal's triangle.
def print_pascals_triangle(n):
for i in range(n):
row = [1]
if i > 0:
for j in range(1, i):
row.append(prev_row[j-1] + prev_row[j])
row.append(1)
print(" " * (n - i), end="")
print(" ".join(map(str, row)))
prev_row = row
OUTPUT
4. Calculation of upper case and lower-case letters in a string
def count_case(s):
upper_count = sum(1 for char in s if char.isupper())
lower_count = sum(1 for char in s if char.islower())
OUTPUT
5. Programs using Tuple
tup = ('F', 'l', 'a', 'b', 'b', 'e', 'r', 'g', 'a', 's', 't', 'e', 'd')
tup = tup + ('!',)
print("Tuple after adding '!' at the end:", tup)
string_representation = ''.join(tup)
print("String representation of the tuple:", string_representation)
extracted = tup[3:5]
print("Extracted ('b', 'b') from the tuple:", extracted)
e_count = tup.count('e')
print("Number of occurrences of 'e' in the tuple:", e_count)
exists_r = 'r' in tup
print("Does 'r' exist in the tuple?", exists_r)
list_representation = list(tup)
print("List representation of the tuple:", list_representation)
tup = tuple(char for char in tup if char not in ('b', 'e', 'r'))
print("Tuple after deleting 'b', 'b', 'e', 'r':", tup)
OUTPUT
6. Programs using conditionals
OUTPUT
7. Programs using dictionaries
OUTPUT
8. Programs using Boolean operators
x = True
y = False
result_and = x and y
result_or = x or y
result_not_x = not x
print("Result of x and y:", result_and)
print("Result of x or y:", result_or)
print("Result of not x:", result_not_x)
OUTPUT
9. Implementation of functions
print("Sum:", sum_result)
print("Product:", product_result)
OUTPUT
B) Compute n + nn + nnn + nnnn
def compute(n):
result = n + (n * 11) + (n * 111) + (n * 1111)
return result
print("Result:", result)
OUTPUT
10. Programs using NumPy
To find the number of elements of a NumPy array, the length of one array element in bytes,
and the total bytes consumed by the elements.
import numpy as np
array = np.array([1, 2, 3, 4, 5])
num_elements = array.size
element_size = array.itemsize
total_bytes = array.nbytes
OUTPUT
11. Programs using Pandas
import pandas as pd
series1 = pd.Series([10, 20, 30, 40])
series2 = pd.Series([1, 2, 3, 4])
print("Series 1:")
print(series1)
print("\nSeries 2:")
print(series2)
print("\nAddition:")
print(addition)
print("\nSubtraction:")
print(subtraction)
print("\nMultiplication:")
print(multiplication)
print("\nDivision:")
print(division)
OUTPUT
12. Implementation of Maclaurin series
import math
def maclaurin_sine(x):
sine_approx = 0
for n in range(5):
sine_approx += ((-1)**n * (x**(2*n + 1))) / math.factorial(2*n + 1)
return sine_approx
OUTPUT
13. Programs using seaborn
tips = sns.load_dataset("tips")
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="day")
plt.title("Scatter Plot of Total Bill vs Tip")
plt.xlabel("Total Bill ($)")
plt.ylabel("Tip ($)")
plt.show()
OUTPUT
14. Programs using Matplotlib
To draw a line from position (0, 0) to position (6, 250) using Matplotlib.
plt.plot(xpoints, ypoints)
plt.title("Line from (0, 0) to (6, 250)")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
OUTPUT
15. Vector manipulations
OUTPUT
16. Matrix operations
print("Matrix 1:")
print(matrix1)
print("Matrix 2:")
print(matrix2)
print("Array:")
print(array1)
print(paste("Element at position (2, 3):", element))
print("Array after adding 5:")
print(array_sum)
print("Array after multiplying by 2:")
print(array_product)
OUTPUT
18. Operations using data frame
OUTPUT
19.Implementation of functions
To demonstrate how to define and use functions in R for performing basic calculations.
print(paste("Sum:", sum_product$Sum))
print(paste("Product:", sum_product$Product))
print(paste("Result of series for n=2:", series_result))
OUTPUT
20. Drawing scatter plot, box plot, violin plot, dot plot, bar plot, line plot
To demonstrate how to create different types of plots using the `ggplot2` library in R.
library(ggplot2)
data <- data.frame(
Category = c("A", "B", "C", "A", "B", "C"),
Value = c(5, 6, 7, 8, 9, 10)
)
# Scatter plot
scatter_plot <- ggplot(data, aes(x = Category, y = Value)) +
geom_point() +
ggtitle("Scatter Plot")
# Box plot
box_plot <- ggplot(data, aes(x = Category, y = Value)) +
geom_boxplot() +
ggtitle("Box Plot")
# Violin plot
violin_plot <- ggplot(data, aes(x = Category, y = Value)) +
geom_violin() +
ggtitle("Violin Plot")
# Dot plot
dot_plot <- ggplot(data, aes(x = Category, y = Value)) +
geom_dotplot(binaxis = 'y', stackdir = 'center') +
ggtitle("Dot Plot")
# Bar plot
bar_plot <- ggplot(data, aes(x = Category, y = Value)) +
geom_bar(stat = "identity") +
ggtitle("Bar Plot")
# Line plot
line_plot <- ggplot(data, aes(x = Category, y = Value)) +
geom_line() +
ggtitle("Line Plot")
print(scatter_plot)
print(box_plot)
print(violin_plot)
print(dot_plot)
print(bar_plot)
print(line_plot)
OUTPUT
21. Geometric Shapes
plot(1:10, 1:10, type = "n", xlab = "", ylab = "", xlim = c(0, 10), ylim = c(0, 10))
symbols(5, 5, circles = 2, inches = 0.5, add = TRUE, bg = "blue")
text(5, 5, "Circle", pos = 3)
rect(2, 3, 4, 5, col = "red", border = "black")
text(3, 5.5, "Rectangle")
polygon(c(6, 8, 7), c(3, 3, 5), col = "green")
text(7, 5.5, "Triangle")
title("Geometric Shapes")
OUTPUT
22. Data transformations
library(dplyr)
data <- data.frame(
Name = c("Reena", "Praba", "Abi", "Nandy", "Suguna", "Suba"),
Age = c(25, 30, 22, 28, 26, 24),
Score = c(80, 90, 75, 88, 85, 78),
Gender = c("Female", "Female", "Male", "Female", "Female", "Female")
)
print("Original Data:")
print(data)