Day 04

Photo by Billy Huynh on Unsplash

Day 04

Lists and Randomization

Once your code gets bigger and bigger you do not want it all sitting in a single file. Break it up into files or in python language, into modules.

Each module contains code for one specific purpose. More modular code your code, more readability, better the developer you are.

If you were to built a robot, you wouldn't have just one person working on it head to toe. One would be responsible for the arms, another for the legs, head, brain, actuators, sensor, wiring. Each broken up into sub-tasks. Once each sub-group is well tested and ready to go, you then proceed to finally assemble it.

image.png

Similarly when working with code, it's broken up into multiple sub-groups.

Random

For almost any application you create, you would require a certain level of randomness in your code. Be it a game, an app or a simple application you would require random numbers.

Python comes with certain in-built modules which you can simply import and use. You could even create your own modules.

import random

#generates a random no. between 1 and 10 - [1, 10)
random_int = random.randint(1, 10)

#generates a random decimal value between 0 and 1 - [0, 1)
random_float = random.random()

Now how would you generate a random decimal value that's greater than 1?

import random

#generates random decimal values between 1 and 5
(random.random())*(random.randint(1, 5))

Lists

Variable were for storing single pieces of data. Lists are a Data Structure which help to store multiple pieces of data.

Python gives us the freedom to store multiple types of data in the same list.


my_nums = [1, 2, 3, 4, 5, 6, 7]
lucky_draw = ['dog', 4, 5, 'a', '!', 32]

List methods

To manipulate lists, python has some in-built function.

image.png

Accessing values in a list

let my_list = [1, 2, 3, 4, 5, 6, 7]
my_list[0]  #1
my_list[-1]  #7

Slice

To get a subset of values.

let my_list = [1, 2, 3, 4, 5, 6, 7]
my_list[0:2]  #[1, 2]
my_list[:3]  #[1, 2, 3]
my_list[2:]  #[3, 4, 5, 6, 7]

Update items in list

Python lists are mutable and therefore you can change the values in it at any time.

let my_list = [1, 2, 3, 4, 5, 6, 7]
my_list[1] = "ha-ha"  #[[1, "ha-ha", 3, 4, 5, 6, 7]]

Index

To get the index of a value in a list. Returns the index of the first occurrence. You can also specify starting index of your search. list.index(value, search_start)

let my_list = [1, 2, 3, 4, 5, 6, 7]
my_list.index(3)  #2

Sort

The sort methods, sorts and changes the existing the array.

let my_list = [4, 2, 8, 5, 0, 1]
my_list.sort()  #[0, 1, 2, 4, 5, 8]

>## Count

Count the number of times a value occurs in a list.

```python
let my_list = [3, 2, 3, 4, 5, 6, 3]
my_list.count(3)  #3

Reverse

Can be specified in .sort itself.

let my_list = [1, 2, 3, 4, 5, 6, 7]
my_list.sort(reverse = True)  #[7, 6, 5, 4, 3, 2, 1]

Append

Adds an element to the end of the list. Is an inplace operation i.e. modifies the original array.

let my_list = [1, 2, 3, 4, 5, 6, 7]
my_list.append(8)  #[1, 2, 3, 4, 5, 6, 7, 8]

Remove

Removes the first occurrence of the value from the list.

let my_list = [1, 2, 3, 4, 5, 6, 7]
my_list.remove(5)  #[1, 2, 3, 4, 6, 7, 8]

Pop

Traditionally 'pop' removes the last element of the array. In python 'pop' will remove the value of the index provided. If no index is provided, it'll remove the last.

let my_list = [1, 2, 3, 4, 5, 6, 7]
my_list.pop()  #[1, 2, 3, 4, 5, 6]
my_list.pop(3)  #[1, 2, 3, 5, 6]

Extend

Using this methods, you can add values or even another list to the existing array. It'll be added to the end of the array.

let my_list = [1, 2, 3, 4]
my_list.extend([5, 6])  #[1, 2, 3, 4, 5, 6]

Can also use the '+' operator

[1, 2] + [2, 1] #[1, 2, 2, 1]

Insert

Inserts a value or an list at the specified index. Inserts it before the index i.e. the preexisting value at the index will now be at 'index+1'.

let my_list = [1, 2, 3, 4]
my_list.insert(2, [5, 6])  #[1, 2, [5, 6], 3, 4]

Random and lists

To get a random value from a list in python, there are a couple of methods.

random.choice()

Most common and widely used as it has been designed for this specific purpose.

import random

my_list = [1, 2, 3, 4, 5]
lucky_num = random.choice(my_list)

random.randrange()

Gives you a random number is a specified range. So first you generate a random value which will be used as the random index.

my_list = [1, 2, 3, 4, 5]
rand_index = random.randrange(len(my_list))
lucky_num = my_list[rand_index]

random.randint()

Generates a random number between 2 specified values. Like randrange, we can first generate the random number which will then be used as the index.

my_list = [1, 2, 3, 4, 5]
rand_index = random.randint(0, len(my_list)-1)
lucky_num = my_list[rand_index]

Project - Rock, Paper, Scissors!

04-op.png

Getting the information and manipulating to our way

04-ip.png

If-else hell 04-ip2.png