100 Days Of Python - Day 4
Day 4
- Data Structures on Python Documentation
Module
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py
appended.
Importing Modules
We can import modules in Python using the import
keyword.
1
import math
We can also import specific functions from a module.
1
from math import sqrt
Random Module
The random
module provides access to functions that support many operations. Perhaps the most important thing is that it allows you to generate random numbers.
1
2
3
4
import random
random_integer = random.randint(1, 10)# generates a random integer between 1 and 10
print(random_integer)
Heads or Tails
We can use the random
module to simulate a coin toss. This program will print either Heads
or Tails
randomly.
1
2
3
4
5
6
7
8
import random
random_integer = random.randint(0, 1)
if random_integer == 0:
print("Heads")
else:
print("Tails")
Data Structures
- These are different ways of organizing and storing data in our computer’s memory so that we can use it efficiently.
- The data in a data structure have a relationship with each other (something in common).
- Data structures are used to organize data in a particular order.
- There are four built-in data structures in Python:
- List
- Dictionary
- Tuple
- Set
List
- similar to arrays in other programming languages
- can store multiple pieces of data in one place
1
2
3
4
fruits = ["Apple", "Peach", "Pear"] # list of strings
print(fruits[0]) # prints "Apple"
List Operations
The operators do not need to be memorized. You can always look them up in the Python Documentation.
Below are some of the operations that can be performed on lists:
append()
- adds an item to the end of the listextend()
- adds all elements of one list to anotherinsert()
- inserts an item at a given positionremove()
- removes an item from a listpop()
- removes and returns an element at a given positionclear()
- removes all items from a listindex()
- returns the index of an element in a listcount()
- returns the number of elements with the specified valuesort()
- sorts the listreverse()
- reverses the order of the listcopy()
- returns a copy of the listlen()
- returns the length of the listmax()
- returns the maximum value in the listmin()
- returns the minimum value in the list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
fruits = ["Apple", "Peach", "Pear"]
fruits.append("Banana") # adds "Banana" to the end of the list
print(fruits) # prints ["Apple", "Peach", "Pear", "Banana"]
fruits.extend(["Mango", "Grapes"]) # adds all elements of the list to the end of the list
print(fruits) # prints ["Apple", "Peach", "Pear", "Banana", "Mango", "Grapes"]
fruits.insert(0, "Orange") # inserts "Orange" at index 0
print(fruits) # prints ["Orange", "Apple", "Peach", "Pear", "Banana", "Mango", "Grapes"]
fruits.remove("Banana") # removes "Banana" from the list
print(fruits) # prints ["Orange", "Apple", "Peach", "Pear", "Mango", "Grapes"]
fruits.pop(0) # removes and returns the element at index 0
print(fruits) # prints ["Apple", "Peach", "Pear", "Mango", "Grapes"]
fruits.clear() # removes all elements from the list
print(fruits) # prints []
fruits = ["Apple", "Peach", "Pear"]
print(fruits.index("Pear")) # prints 2
print(fruits.count("Apple")) # prints 1
fruits.sort() # sorts the list
print(fruits) # prints ["Apple", "Peach", "Pear"]
fruits.reverse() # reverses the order of the list
print(fruits) # prints ["Pear", "Peach", "Apple"]
fruits_copy = fruits.copy() # returns a copy of the list
print(fruits_copy) # prints ["Pear", "Peach", "Apple"]
print(len(fruits)) # prints 3
print(max(fruits)) # prints Pear
print(min(fruits)) # prints Apple
Banker Roulette
This program will select a random name from a list of names.
1
2
3
4
5
6
7
8
import random
names_string = input("Give me everybody's names, separated by a comma. ")
names = names_string.split(", ")
random_name = random.choice(names)
print(f"{random_name} is going to buy the meal today!")
- Another way to select a random name from a list of names is to use the
randint()
function.
1
2
3
4
5
6
7
8
9
10
11
12
import random
names_string = input("Give me everybody's names, separated by a comma. ")
names = names_string.split(", ")
number_of_names = len(names)
random_index = random.randint(0, number_of_names - 1)
random_name = names[random_index]
print(f"{random_name} is going to buy the meal today!")
Index Errors and Working with Nested Lists
- Index Errors
- An
IndexError
occurs when you try to access an index that does not exist. For example, if you have a list with 3 elements, the only valid indices are 0, 1, and 2. If you try to access index 3, you will get anIndexError
.
1
2
fruits = ["Apple", "Peach", "Pear"]
print(fruits[3]) # IndexError: list index out of range
- To avoid
IndexError
, you can use thelen()
function to get the number of elements in a list, and then subtract 1 from it to get the last index.
1
2
3
fruits = ["Apple", "Peach", "Pear"]
number_of_fruits = len(fruits)
print(fruits[number_of_fruits - 1]) # prints Pear
- Nested Lists
- Nested lists are lists that contain other lists.
- It helps us to keep track of related data without changing their order.
1
2
3
4
5
6
fruits = ["Strawberries", "Nectarines", "Apples", "Grapes", "Peaches", "Cherries", "Pears"]
vegetables = ["Spinach", "Kale", "Tomatoes", "Celery", "Potatoes"]
dirty_dozen = [fruits, vegetables]
print(dirty_dozen) # prints [["Strawberries", "Nectarines", "Apples", "Grapes", "Peaches", "Cherries", "Pears"], ["Spinach", "Kale", "Tomatoes", "Celery", "Potatoes"]]
Treasure Map
This program will create a map using nested lists.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
row1 = ["⬜️", "⬜️", "⬜️"]
row2 = ["⬜️", "⬜️", "⬜️"]
row3 = ["⬜️", "⬜️", "⬜️"]
map = [row1, row2, row3]
print(f"{row1}\n{row2}\n{row3}") # prints ["⬜️", "⬜️", "⬜️"] ["⬜️", "⬜️", "⬜️"] ["⬜️", "⬜️", "⬜️"]
position = input("Where do you want to put the treasure? ")
horizontal = int(position[0])
vertical = int(position[1])
map[vertical - 1][horizontal - 1] = "X"
print(f"{row1}\n{row2}\n{row3}") # prints ["⬜️", "⬜️", "⬜️"] ["⬜️", "X", "⬜️"] ["⬜️", "⬜️", "⬜️"]
Rock Paper Scissors
This program will simulate a game of rock paper scissors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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"))
print(game_images[user_choice])
computer_choice = random.randint(0, 2)
print("Computer chose:")
print(game_images[computer_choice])
if user_choice >= 3 or user_choice < 0:
print("You typed an invalid number, you lose!")
elif user_choice == 0 and computer_choice == 2:
print("You win!")
elif computer_choice == 0 and user_choice == 2:
print("You lose")
elif computer_choice > user_choice:
print("You lose")
elif user_choice > computer_choice:
print("You win!")
elif computer_choice == user_choice:
print("It's a draw")