Random, Lists
Use modules to better organize and simplify code
Today was about randomization. Computers aren’t random because they are built with circuits and switches and strict logic about them. But, we can make computers generate random numbers, alphabets, etc., with the help of pseudorandom number generators. The bad news is it is very complex. The good news is we don’t need to understand how it works to use this feature in Python.
We can directly import a “random” module in Python and use the functions built inside this module directly in our code without the need to understand how these numbers are generated. This module has a whole lot of functions we can use directly in our code. For e.g. to choose a random whole number from the range we assign. I wrote a small code to play heads and tails with my wife. The winner decided to pick dinner. Naturally, we got what my wife wanted.
Just like “random” is a module we can directly insert in our code without the need to program randomization, we can create our own modules, and import them in our code. It is a fairly simple process and should be used to better organize our code and to make it easier to debug.
Lists are fairly important to understand and use in Python. The basic concept is we assign a variable which contains a simple collection of ordered items. These ordered items can then be called in our code to access them, modify them, etc.,. Lists can also be created inside other lists, can be queued, can be stacked, etc.,. As you can notice, we have a wide range of things we can do with lists and hence they are frequently used.
For the project of the day, I created a simple rock, paper, scissors game. I first used the random module to help pick the computer either rock, paper or scissors and then used an input function to ask the user to pick one of the three. Based on the selection of the computer and the user, one of them won. To determine the winner, I used if else logic. That was it for the day and excited for tomorrow!