Python- Day4
Randomization and Python Lists
Do check this Page: Python Random Module Methods
We will explore Generate Random Integers:
randint(a, b):
Returns a random integer between a and b (both inclusive). This also raises an ValueError
if a
> b
.
import random
random_int = random.randint(1,10)
print(random_int)
>>
PS \Python> python .\random_int.py
4
PS \Python> python .\random_int.py
10
PS \Python> python .\random_int.py
3
We can create our own module and import it.
#my_module.py
pi = 3.1415
#random_int.py
import random
import my_module
random_int = random.randint(1,10)
print(random_int)
print(my_module.pi)
>>
PS \Python> python .\random_int.py
8
3.1415
Generating Random floating point numbers:
Similar to generating integers, there are functions that generate random floating point sequences.
- random.random() -> Returns the next random floating point number between [0.0 to 1.0)
- random.uniform(a, b) -> Returns a random floating point
N
such that a <= N <= b if a <= b and b <= N <= a if b < a. - random.expovariate(lambda) -> Returns a number corresponding to an exponential distribution.
- random.gauss(mu, sigma) -> Returns a number corresponding to a gaussian distribution.
There are similar functions for other distributions, such as Normal Distribution, Gamma Distribution, etc.
import random
random_int = random.randint(1,10)
print(random_int)
random_flot = random.random()
print(random_flot)
>>
1
0.041635438063850505
Project 1: Love Score
import random
print("\n")
print("-------Welcome to Love Calculator-------\n")
name1 = input("What is your name?\n")
name2 = input("What is their name?\n")
love_score = random.randint(1, 100)
print(f"Love score of {name1} and {name2} is {love_score}")
>>
-------Welcome to Love Calculator-------
What is your name?
Lisa
What is their name?
Jack
Love score of Lisa and Jack is 34
Project 2: Heads or Tails
import random
random_side = random.randint(0, 1)
if random_side == 1:
print ("Heads")
else:
print("Tails")
>>
PS \Python> python.exe .\heads_tails.py
Tails
PS \Python> python.exe .\heads_tails.py
Heads
List :
Check this List : Data Structure
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
print(fruits[2])
print(fruits[-2])
>>
pear
apple
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
print(fruits[2])
print(fruits[-2])
fruits[2] = "Cheery" #CHNAGE NAME
print(fruits)
>>
pear
apple
['orange', 'apple', 'Cheery', 'banana', 'kiwi', 'apple', 'banana']
You can append it to the list :
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
fruits.append("Nid")
print(fruits)
>>
['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana', 'Nid']
Extend list:
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
fruits.extend(["AP", "gy"])
print(fruits)
>>
['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana', 'Nid', 'AP', 'gy']
To check the length of the list
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
print(len(fruits))
Project 3: Who Will Pay
names = input("Enter the names\n")
name_list = names.split(", ")
import random
num_item = len(names)
random_choice = random.randint(0, num_item - 1)
print(names[random_choice])
Nested List
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
vegetables = ['Beetroot', 'Carrots', 'Capsicum', 'Ginger', 'Cauliflower']
fridge = [fruits, vegetables]
print(fridge)
>>
[['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana'], ['Beetroot', 'Carrots', 'Capsicum', 'Ginger', 'Cauliflower']]
Project 4: Treasure Map
line1 = [" ", " "," "]
line2 = [" ", " "," "]
line3 = [" ", " "," "]
map = [line1, line2, line3]
print("\nHiding your treasure! X marks the spot.")
print("There are three rows and coloumes\n")
postion = input("Where do you want to put the treasure?")
letter = postion[0].lower()
abc = ["a", "b", "c"] #Possiable entry
letter_index = abc.index(letter)
#Checking if in "letter" (we entry), that is there in the list or not
#This will give us number
number_index = int(postion[1]) - 1
#Whatever position we entered, -1 because list stars from [0]
map[number_index][letter_index] = "X"
print(f"{line1}\n{line2}\n{line3}")
>>
Hiding your treasure! X marks the spot.
There are three rows and coloumes
Where do you want to put the treasure?B3
[' ', ' ', ' ']
[' ', ' ', ' ']
[' ', 'X', ' ']
Project 5: Rock-Paper-Scissors game
import random
rock = '''
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
'''
paper = '''
_______
---' ____)____
______)
_______)
_______)
---.__________)
'''
scissors = '''
_______
---' ____)____
______)
__________)
(____)
---.__(___)
'''
game_images = [rock, paper, scissors]
user_choice = int(input("What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors.\n"))
if user_choice >= 3 or user_choice < 0:
print("Number is Invalid, You lose!")
else:
print(game_images[user_choice])
computer_choice = random.randint(0, 2)
print("Compute Choose:")
print(game_images[computer_choice])
if user_choice == computer_choice:
print ("It's a tie!")
elif (user_choice == 0 and computer_choice == 2) or \
(user_choice == 1 and computer_choice == 0) or \
(user_choice == 2 and computer_choice == 1):
print ("You win!")
else:
print ("You lose!")
>>
What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors.
2
_______
---' ____)____
______)
__________)
(____)
---.__(___)
Compute Choose:
_______
---' ____)____
______)
__________)
(____)
---.__(___)
It's a tie!
If you found this guide helpful then do click on 👏 the button.
Follow for more Learning like this 😊
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!