#100DaysOfCode: Python Day 4

Random Module & Lists

Subscribe to my newsletter and never miss my upcoming articles

๐Ÿค˜๐Ÿป๐Ÿงปโœ‚๏ธ I CREATED MY OWN ROCK PAPER SCISSORS GAME! โœ‚๏ธ๐Ÿงป๐Ÿค˜๐Ÿป
CLICK HERE and press "Run" to play.

Yesterday I learned how to import modules and use lists. Learning these skills have taken my simple game creation capabilities to a new level. I now know that I can make Tic Tac Toe using lists but have not done so yet ๐Ÿ˜‚. It feels awesome to know that's it's possible to make cool games with ease. I am loving Python!

Lessons

  1. Don't be hard on yourself for not remembering all the syntax. There is too much information to memorise. That's why we have documentation and google. We cannot possibly remember everything.
  2. Focus on knowing what's possible instead of trying to memorise syntax. That's where the value is!

TO THE CODE!!!

lists.py - lists & nested lists

# Lists
# A list is a data structure
# fruits = [item1, item2]

# A list stores order and the item, this index starts at 0
#              0         1       2
# fruits = ["Cherry", "Apple", "Pear"]
states_of_south_africa = ["Gauteng", "Northen Cape", "Eastern Cape", "Free State", "KwaZulu-Natal", "Limpopo", "Mpumalanga", "North West"]
print(states_of_south_africa[1])
# Output: Northen Cape
print(states_of_south_africa[-2])
# Output: Mpumalanga because we using a negative index value

# Update an item in the list
states_of_south_africa[0] = "The City of Gold"
# Add an item on the list
states_of_south_africa.append("RishalLand")
# Add a list of items on the list
states_of_south_africa.extend(["YourLand, MyLand, OurLand"])
# See how much more you can do here: https://docs.python.org/3/tutorial/datastructures.html

print(states_of_south_africa)

# You can get a lenth of a list by using the len()
print(len(states_of_south_africa))

# Nested Lists
ditry_dozen = ["Strawberries", "Spinach", "Kale", "Nectarines", "Apples", "Grapes", "Peaches", "Cherries", "Pears", "Tomatoes", "Celery", "Potatoes"]
# We want to Seperate them!
fruits = ["Strawberries", "Nectarines", "Apples", "Grapes", "Peaches", "Cherries", "Pears"]
vegetables = ["Spinach", "Kale", "Tomatoes", "Celery", "Potatoes"]

# Now we have a nested list
ditry_dozen = [fruits, vegetables]

randomModule.py - lists & nested lists

# Using the random module
# Modules are things that we can use in different files

import Random
# The Python team created this module to make it easier for us to generate random numbers
#Documentation: https://www.askpython.com/python-modules/python-random-module-generate-random-numbers-sequences

# You can also create your own module. 
# main.py is the file that will run by default
# Module created named my_module.py that stores the value of pi
# Once you import this you can use it in your main.py
# This is how you can seperate your code

# Create random whole numbers
random_integer = random.randint(1,10)
print(random_integer)

# Create random float numbers between 0.000000 & 0.99999999
random_float - random_random()
print(random_float)

# Create random float numbers between 0.000000 & 4.999999999
random_float * 5

It is tough balancing a full-time job, gymming, keeping up with social responsibilities and chores while taking on this challenge but this far, I am proud that I am sticking to it and excited for each day as I am learning and growing my skill sets.

No Comments Yet