0% found this document useful (0 votes)
15 views4 pages

PF 7

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 views4 pages

PF 7

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/ 4

Course:pfsd

Course_id:22DC101A

Section:S-33

Id:2200090067

Name: p aakash
Pre-Lab:

a) Program to generate a random color hex, a random alphabetical string, random value between two
integers (inclusive) and a random multiple of 7 between 0 and 70. Use random.randint()import random

def random_color_hex():

color_hex = "#{:02X}{:02X}{:02X}".format(random.randint(0, 255), random.randint(0, 255),


random.randint(0, 255))

return color_hex

def random_alphabetical_string(length):

letters = 'abcdefghijklmnopqrstuvwxyz'

result = ''.join(random.choice(letters) for _ in range(length))

return result

def random_value_between(min_val, max_val):

return random.randint(min_val, max_val)

def random_multiple_of_7():

return random.randint(0, 10) * 7

print("Random Color Hex:", random_color_hex())

print("Random Alphabetical String:", random_alphabetical_string(10)) # Adjust the length as needed

print("Random Value Between 1 and 100 (inclusive):", random_value_between(1, 100))

print("Random Multiple of 7 Between 0 and 70:", random_multiple_of_7())


output:

Random Color Hex: #82D7B0

Random Alphabetical String: ruqimcoony

Random Value Between 1 and 100 (inclusive): 83

Random Multiple of 7 Between 0 and 70: 56

In-lab:

a) Program to generate a random integer between 0 and 6-excluding 6, random integer between 5
and 10-excluding 10, random integer between 0 and 10, with a step of 3 and random date
between two dates, Use random.randrange()

import random
from datetime import datetime, timedelta
random_int_1 = random.randrange(0, 6)
print("Random Integer between 0 and 5 (excluding 6):", random_int_1)
random_int_2 = random.randrange(5, 10)
print("Random Integer between 5 and 9 (excluding 10):", random_int_2)
random_int_3 = random.randrange(0, 11, 3)
print("Random Integer between 0 and 10 with a step of 3:", random_int_3)
start_date = datetime(2023, 1, 1)
end_date = datetime(2023, 12, 31)

random_days = random.randrange((end_date - start_date).days)

random_date = start_date + timedelta(days=random_days)

print("Random Date between {} and {}: {}".format(start_date.strftime("%Y-%m-%d"),


end_date.strftime("%Y-%m-%d"), random_date.strftime("%Y-%m-%d")))

output:

Random Integer between 0 and 5 (excluding 6): 1

Random Integer between 5 and 9 (excluding 10): 6

Random Integer between 0 and 10 with a step of 3: 0

Random Date between 2023-01-01 and 2023-12-31: 2023-05-27


Post-Lab:

a)Program to create a shallow copy of a given list. Use copy.copy.

import copy

original_list = [1, 2, [3, 4], 5]

shallow_copy = copy.copy(original_list)

shallow_copy[0] = 10

original_list[2][0] = 30

print("Original List:", original_list)

print("Shallow Copy:", shallow_copy)

output:

Original List: [1, 2, [30, 4], 5]

Shallow Copy: [10, 2, [30, 4], 5]

You might also like