Module #8 - Python Playpen (Required)
In this module you will being and introduction to the programming language called Python.
What is Python?
You'd be correct in guessing a snake, but is also an object-oriented programming language that emphasizes readability. It was invented by Guido van Rossum and was named after Monty Python's Flying Circus, not after the snake.
Up and running with Python
Python needs to be run by an interpreter from your command line. So fire up bash or your terminal, enter python
and hit enter
. This will bring you into your Python interpreter.
You can now run all sorts of Python code! To do so, just enter the command and hit enter. Try the following below and see what happens.
# 1.
print("Welcome to Python!")
# 2.
4+4
# 3.
name = "Gordon Bombay"
# 4.
print(name)
From text editor to your command line
You might be wondering if we have to type all our Python code into the terminal, which does not save our code. What happens when the code becomes lengthy? Well, we can save our Python code in a text file, then run it from the command line, otherwise known as the terminal.
First, create a folder on your desktop called pythonFiles, where we will store all the files for this module.
Next, open up VS Code (your pre installed text editor for this class) and save a file titled python_starter.py and save it to your pythonFiles folder on your desktop.
After you have saved it, copy the following code into that file and save it. You don't need to worry about what each part of the code does what yet; soon this will all be clear.
Note: make sure you manually type exactly as it is. Python uses white spaces or blanks to delimit code blocks. In other words, it knows what each part of the code does by the spacing and indentation. If you encounter an error, this could be why. Your VS Code editor should help you with your with the spacing.
teams = ["Giants", "Redskins", "Cowboys", "Eagles"]
print("The following teams are part of the NFC EAST")
for team in teams:
if team == "Giants":
print(f"Go {team}!")
else:
print(team)
Save your code to the file python_starter.py and open up bash or terminal and run each code to get to our desktop.
# 1. This changes directory in our terminal to work on our pythonFiles folder on the desktop
cd Desktop/pythonFiles
# 2. This code lets us see the files are on our desktop
ls
# The result should then display our python file
python_starter.py
# 3. Finally we can run our Python code by entering the following
python python_starter.py
If the result looks as follows, congratulations. You just ran a Python script!
So what does all this Python mean?
Let's break this code down line by line so you get an understanding of what means what.
teams = ["Giants", "Redskins", "Cowboys", "Eagles"]
A variable is kind of like a person's name. Just as a name, such as Peyton, represents a person but isn't the real flesh-and-blood human being, a Python variable holds, or represents, a value. What are some types of values that Python can hold? A variable can hold an integer (whole number), e.g. my_int = 3. This means that to Python, the variable my_int represents the integer 3. As you can see from my_pi = 3.14, a variable can hold floats (decimals) as well. A variable can also hold a string. A string is a series of characters (and blank spaces), such as "Giants," "RedskinsFan88", or "%$27!". For example, you can store the string "I L0V3 data science so much!!!" under a variable, say, my_likes
: my_likes = "I L0V3 data science so much!!!"
. We should remind you here that numbers don't have to be surrounded by quotes, but strings do.
Our variable here is teams
and it holds a list. Just as a shopping list can contain multiple items, a Python list can also contain a single item, multiple items, or no items at all. Notice the format. A Python list is enclosed by as pair of square brackets: []
, and the list's imtems go inside them. So what can a Python list contain? It can contain, among other things, numbers and strings. A Python list can even contain other Python lists. Some examples: my_integers = [3, 15, 29, 2]
and my_lists =[["a", "b", "c"], [3, 15, 29, 2]]
print("The following teams are part of the NFC EAST")
This is a print statement, tells the to program print, or display, whatever is inside the parentheses.
for team in teams:
This is a for loop, which creates a new variable called team and goes through everyone of the values stored in teams. For loops let us iterate over endless amounts of data.
if team == "Giants":
print(f"Go {team}!")
This is the start of a conditional statement that instructs going over each team in our list. If we find a team named "Giants" then statement "Go Giants!" is printed on the screen. A few things bear mentioning here. First, in if team == "Giants":
, notice that thre are two equality signs. Remember that when we assigned a value to as variable, we used a single equality sign: teams = ["Giants", "Redskins", "Cowboys", "Eagles"]
. This double equality sign means something different. It is a test of whether something is true. So in this case, it is testing whether the variable team
refers to the string "Giants". If it is true, then it prints the statement.
else:
print(team)
On the other side of the conditional, else
means that, if the previous code doesn't apply, then we print the name of the team.