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

python_practicals

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)
4 views

python_practicals

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

Python Practicals - Random Functions and Data Structures

1. Program to Generate a Random Number

Aim: Write a program to generate a random number within a given range.

Code:

import random

random_number = random.randint(1, 100)

print(f'Random number between 1 and 100: {random_number}')

Output:

Random number between 1 and 100: 42

2. Program to Generate a Random Floating-Point Number

Aim: Write a program to generate a random floating-point number between 0 and 1.

Code:

import random

random_float = random.random()

print(f'Random floating-point number between 0 and 1: {random_float}')

Output:

Random floating-point number between 0 and 1: 0.876543

3. Program to Generate a Random Choice from a List

Aim: Write a program to randomly choose an item from a list.

Code:

import random
items = ['apple', 'banana', 'cherry', 'date']

random_item = random.choice(items)

print(f'Randomly selected item: {random_item}')

Output:

Randomly selected item: banana

4. Program to Shuffle a List Randomly

Aim: Write a program to shuffle the elements of a list randomly.

Code:

import random

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

random.shuffle(numbers)

print(f'Shuffled list: {numbers}')

Output:

Shuffled list: [2, 5, 4, 1, 3]

5. Program to Generate a Random Sample from a List

Aim: Write a program to generate a random sample of elements from a list.

Code:

import random

numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90]
random_sample = random.sample(numbers, 3)

print(f'Random sample of 3 elements: {random_sample}')

Output:

Random sample of 3 elements: [40, 80, 10]

6. Program to Generate a Random Number Within a Specific Range

Aim: Write a program to generate a random number within a specified range and with a given step.

Code:

import random

random_number_in_range = random.randrange(0, 51, 5)

print(f'Random number between 0 and 50 with step 5: {random_number_in_range}')

Output:

Random number between 0 and 50 with step 5: 35

7. Program to Generate a Random Integer Using `uniform()`

Aim: Write a program to generate a random floating-point number within a given range.

Code:

import random

random_float_in_range = random.uniform(1, 10)

print(f'Random floating-point number between 1 and 10: {random_float_in_range}')

Output:

Random floating-point number between 1 and 10: 7.324589


8. Program to Set a Random Seed

Aim: Write a program to set a random seed and generate reproducible random numbers.

Code:

import random

random.seed(10)

random_number = random.randint(1, 100)

print(f'Random number with seed 10: {random_number}')

Output:

Random number with seed 10: 74

You might also like