Getting Started with Python: Modules & Lists
I am studying Python with @skillcrush, in this article we cover modules and lists
I am learning with Skillcrush. Please check them out and sign up if you're looking to break into tech!
Receive $250 off your course by using the link above. Use the coupon code: TWIXMIXY
NOTE: this discount is for Break Into Tech + Get Hired package exclusively.
It's the same program I am participating in!
🐍 Greetings travelers! We are studying Python with Skillcrush. In this article we cover modules and lists.
🟩🟩🟩🟩⬜⬜⬜⬜⬜⬜⬜⬜ 33% DONE
I’ve wrapped up my Front End Developer portfolio at Skillcrush, read here:
I am now enrolled at UC Berkeley! 🏫 I will be taking their Full Stack MERN Coding Bootcamp through the remainder of the year.
You can subscribe without signing up for a substack account! Just select “None” when subscribing. All the content from my learning journey is free.
Modules
→ MODULES
Files of useful code that can be easily used in your projects and give you additional tools to work with.
Three Helpful Modules:
→ THE RANDOM MODULE
Provides you with the code to instantly generate random numbers.
→ THE DATETIME MODULE
Displays the date and time in your apps in a variety of different formats.
→ THE TURTLE MODULE
A fun drawing module that lets you program a pen to draw on a canvas.
To use a module, you need to import it.
→ IMPORTING
The process of pulling a module into a file you’re working with.
import datetime
import random
import turtle
Random Module Functions:
randint()
: Generates a random integer between two specified values.random()
: Generates a random float between 0 and 1.uniform()
: Generates a random float between two specified values.shuffle()
: Shuffles the elements of a sequence.choice()
: Chooses a random element from a sequence.
DateTime Module Functions:
datetime.now()
: Returns the current date and time.datetime.fromtimestamp()
: Converts a timestamp to a datetime object.datetime.strptime()
: Parses a string into a datetime object.datetime.date()
: Creates a datetime object from a date.datetime.time()
: Creates a datetime object from a time.datetime.timedelta()
: Creates a timedelta object.datetime.timezone()
: Creates a timezone object.
Turtle Module Functions:
forward(distance):
Move the turtle forward by the specified distance.backward(distance):
Move the turtle backward by the specified distance.left(angle):
Turn the turtle left by the specified angle.right(angle):
Turn the turtle right by the specified angle.penup():
Lift the turtle's pen so that it does not draw anything.pendown():
Put the turtle's pen down so that it draws again.dot(size):
Draw a dot of the specified size.circle(radius):
Draw a circle of the specified radius.square(side_length):
Draw a square of the specified side length.triangle(side_length):
Draw a triangle of the specified side length.
Drawing App Challenge
I definitely played around with this some. I had to do some googling to find out what some things were doing since it seems the Replit’s base Turtle module had some extra stuff that wasn’t covered in the lesson. I made a few shapes, check them out:
Shift Time Challenge
Spent some time researching for this one as well, referring back to my notes and looking up examples of how to accomplish date and time formats. See here:
Guessing Game Challenge
A huge thing that I’m finding with Python is that there are less syntax markers and the code functions more based off of nesting and indentation.
I couldn’t figure out the try:
and except:
in how to nest the if statement inside of it, so I did look at the solution, but that appears to be the only thing I missed besides indenting the code properly.
The other aspect that I missed was that in the try/if statement I created a variable that changed the user input into an integer, so I needed to reference that when comparing it to the random number going forward.
Lists
→ LISTS
A collection of different values - each value stored in a list is called an element
How lists can be used:
Write lists
Add a new element to a list
Remove an element from a list
Select specific elements from a list
Write lists:
superheroes = ["Scarlet Witch", "Captain Marvel", "Black Widow", "Jean Grey"]
To me they look just like an array in JS.
Add a new element to a list:
superheroes.append("Jessica Jones")
append()
means to add to a list.
superheroes.insert(1, "Jennifer Walters")
insert()
adds the new element at a specific position.
Remove an element from a list:
superheroes.pop()
superheroes.pop(1)
pop()
removes the last element from the list. If you add an integer value to the argument, it will remove the name at that position.
Select specific elements from a list:
→ SLICING
Selecting a section of a list by specifying two index positions.
print(superheroes[0:2])
[0:2]
includes the first position on the index (0), but doesn’t include the end position (2)
List Cleanup Challenge
This was pretty simple, just used pop, insert, and append. Check it out:
Updating List Challenge
This challenge was all about slicing. Definitely great practice.
That’s it for today!
🟩🟩🟩🟩🟩🟩⬜⬜⬜⬜⬜⬜ 50% DONE
I’ve loved beginning to learn Python! Now that I have done work in both JavaScript and PHP I could see how once you learn one language it can be easier to adapt it to others. In the next article about Python we will cover loops in depth and dictionaries.
What did you think? Leave me a comment and share!