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

Experiment 4

The document discusses generating random values like colors, strings, integers and dates using Python functions. It also discusses generating a graph using Pandas and Matplotlib to visualize average grades of students in different age groups.

Uploaded by

Mahesh Kadambala
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)
15 views

Experiment 4

The document discusses generating random values like colors, strings, integers and dates using Python functions. It also discusses generating a graph using Pandas and Matplotlib to visualize average grades of students in different age groups.

Uploaded by

Mahesh Kadambala
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
You are on page 1/ 5

Name: K Mahesh

ID No:2200030757
Sub: Python Lab
Experiment-04
1. Generating a Random Color hex, Random Alphabetical String
and Random Multiple of 7 b/w 0 and 70.
import random
import string
def generate_random_color_hex():
return "{:06X}".format(random.randint(0, 0xFFFFFF))
def generate_random_alphabetical_string(length):
return ''.join(random.choice(string.ascii_letters) for _ in
range(length))
def generate_random_integer_between(start, end):
return random.randint(start, end)
def generate_random_multiple_of_seven():
return random.randint(0, 10) * 7
# Generate random color hex
random_color_hex = generate_random_color_hex()
print("Random Color Hex:", random_color_hex)

# Generate random alphabetical string of length 8


random_alphabetical_string =
generate_random_alphabetical_string(8)
print("Random Alphabetical String:", random_alphabetical_string)

# Generate random integer between 10 and 20 (inclusive)


random_integer = generate_random_integer_between(10, 20)
print("Random Integer between 10 and 20:", random_integer)

# Generate random multiple of 7 between 0 and 70


random_multiple_of_seven = generate_random_multiple_of_seven()

1
Name: K Mahesh
ID No:2200030757
Sub: Python Lab
print("Random Multiple of 7 between 0 and 70:",
random_multiple_of_seven)

Output

2. Generating a Random Integer


import random
from datetime import datetime, timedelta

def generate_random_integer_excluding_max(max_value):
return random.randint(0, max_value - 1)

def generate_random_integer_range_excluding_max(start, end):


return random.randint(start, end - 1)

def generate_random_integer_with_step(start, end, step):


return random.randrange(start, end, step)

2
Name: K Mahesh
ID No:2200030757
Sub: Python Lab

def generate_random_date(start_date, end_date):


time_delta = end_date - start_date
random_days = random.randint(0, time_delta.days)
return start_date + timedelta(days=random_days)

# Example usage
random_int_0_to_5 = generate_random_integer_excluding_max(6)
random_int_5_to_9 =
generate_random_integer_range_excluding_max(5, 10)
random_int_0_to_10_step_3 =
generate_random_integer_with_step(0, 11, 3)

start_date = datetime(2022, 1, 1)
end_date = datetime(2022, 12, 31)
random_date = generate_random_date(start_date, end_date)

print("Random Integer (0 to 5):", random_int_0_to_5)


print("Random Integer (5 to 9):", random_int_5_to_9)
print("Random Integer (0 to 10, step 3):",
random_int_0_to_10_step_3)
print("Random Date between {} and {}: {}".format(start_date,
end_date, random_date))
Output:

3
Name: K Mahesh
ID No:2200030757
Sub: Python Lab

3. Generating a graph using Pandas and Matplotlib


import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv(r'D:\python\Python Pratical\Exercise-
4\students.csv')
age_below_20=df[df['AGE']<20]
age_20_to_25=df[(df['AGE']<=25) & (df['AGE']>=20)]
age_above_25=df[df['AGE']>20]

avg_grade_ageb20=age_below_20['GRADE'].mean()
avg_grade_20_25=age_20_to_25['GRADE'].mean()
avg_grade_agea25=age_above_25['GRADE'].mean()

age_groups = ['Below 20', '20-25', 'Above 25']


average_grades=[avg_grade_ageb20,avg_grade_20_25,avg_grade_ag
ea25]
plt.bar(age_groups, average_grades, color='skyblue')
plt.xlabel('Age Groups')
plt.ylabel('Average Grade')
plt.title('Average Grade for Different Age Groups')
plt.show()

4
Name: K Mahesh
ID No:2200030757
Sub: Python Lab
Output:

You might also like