ISF DP Computer Science

Intro #

This lab will introduce you to our coding environment and cover some coding basics:

  • navigating your terminal
  • using github
  • running files
  • variables
  • printing to the terminal
  • getting user input
  • selection statements (if/else if/else)
  • while loops
  • working with strings

[0] Navigating your Terminal #

        Command                                What it does
lsList what’s in the current directory.
cd ~Go to your home directory
cd folderGo to folder
cd ..Go up a level in your directory system.
code file.pyOpens file.py with in VS Code
code .Opens file.py with in VS Code
python file.pyRuns the Python program file.py

In addition, you can use these shortcuts to help you out:

        Key                                What it does
tabAutocomplete
^Cycle through your previous commands

[1] Using GitHub #

💻 Go to your dpcs/unit00_strings folder using the cd command.

cd ~/desktop/dpcs/unit00_strings/
💻 Clone your repo. This will copy it onto your computer.
git clone https://github.com/isf-dp-cs/lab_python_basics

💻 In the Terminal, use cd to enter the folder you just cloned.

cd lab_python_basics

💻 Enter the Poetry Shell to start the lab. We will run this command at the start of each lab, but only when we are inside a lab folder.

poetry shell

[2] Variables #

Variables are used to hold a values within the code. The value can change during the execution of a program.

Variable test 0 #

💻 First let's run variable_test0.py.

python variable_test0.py

👀 You should see the following words print out in the Terminal.

Hello
YOUR NAME

💻 Let's see how that is happening by opening the file in Visual Studio Code: code variable_test0.py

VSCode is the code editor we will be using throughout this course.

# variable_test0.py

name = "YOUR NAME"
print("Hello")
print(name)

💻 Start by replacing "YOUR NAME" with your name (but keep the ""). Now you have declared the name variable and assigned your name as its value.

💻 Save the file and run the program again. You should see an output similar to the one below:

Tip: Use the up arrow to cycle through your previous commands in Terminal

$ python variable_test0.py
Hello
Emma

What just happened? After storing your name in the name variable, print(name) prints out whatever is stored in the variable.

Let’s do another test.

💻 Add the following lines of code to your file.
name = "YOUR FRIEND'S NAME"
print("HELLO")
print(name)

💻 Replace "YOUR FRIEND'S NAME" with your friend’s name.

Now our program is printing the name variable twice but we’ve assigned different values to the variable at different places in the code. What do you think will happen?

💻 Run the code to find out! You should see an output similar to the one below:

$ python variable_test0.py
Hello
Emma
Hello
Britte

Variable test 1 #

💻 Let's continue explore variables by running a different variable test: python variable_test1.py

Hmm, something is wrong here.

💻 Open up the code and try to fix the bug: code variable_test1.py

Be sure to read the error message. Why does it say 'favorite_fruit' is not defined?

# variable_test1.py

favorite_color = "color"
print("Your favorite color is " + favorite_color)
print("Your favorite fruit is " + favorite_fruit)
favorite_fruit = "fruit"

Variable test 2 #

💻 Now, let's experiment with user input. Run the 3rd variable test: python variable_test2.py

# variable_test2.py

favorite_artist = input("What is your favorite artist? ")
print("Oh, I love " + favorite_artist + "!")
💻 Run your program multiple times and change up what artist you type.

This shows how your programs can be responsive to user input and how you can store information from the user in variables that may change every time your program runs.


User Input and Data Types #

You might notice that we are using input() to get text from the user. A piece of data that stores text is called a string.

However, sometimes you need numerical input from a user. In this case, you need to convert the input from a string to an int or integer.

age_string = input("How old are you? ")
age_num = int(age_string) #convert to an integer

💻 In variable_test3.py, write code to convert a given temperature from celcius to fahrenheit. Remember, how do you open a file to edit in Visual Studio Code?

The forumula for calculating fahrenheit is (temp x 1.8) + 32

Here is an example interaction:

Input a temperature in Celcius:
>>> 28
28 degrees in Celcius is 82.4 degrees in Fahrenheit

[3] Selection/Conditional Statements #

💻 Run the file selection_example.py to see this conditional statement in actions.

python selection_example.py

💻 Open the code:

code selection_example.py

💻 Experiment with changing the numbers in the selection statements. Then run your code to see how your changes affect what it prints.


Conditional Output #

💻 In the file generation_labeler.py write code to tell someone what generation they are in.

💻 Open the code:

code generation_labeler.py

You can determine someone’s generation using the following formula:

GenerationYears
Boomer1946–1964
Gen X1965-1980
Millenial1981-1996
Gen Z1996-2012
Gen Alpha2013–now

Here is an example interaction:

What year were you born?
>>> 1995
You are a Millenial. I bet you love avocado toast.

Nested Conditionals #

Indentation is important for python code. Conditional statements that are indented under eachother are calld nested. This can be an important tool for handling many branching scenarios.

💻 Run the file
python animal_indentifier.py

Experiment with the code till you understand how it works.

💻 Open the code
code animal_indentifier.py
💻 Add in more conditional statements into the code to include:
TypeCriteria
Bugmore than 4 legs, up to 8 legs
Human2 legs, can’t fly
Bird2 legs, can fly
Unknown Creatureeverything else

[4] While Loops #


Guessing Game #

Practice by using a while loop to create a number guessing game.

💻 Start by running the game file:
python guessing_game.py
----------------------------
Guess a number between 1-10!
----------------------------

Guess a number: 5
Guess a number: 10
Guess a number: 3
Correct
Guess a number: 8
Guess a number:

It works! But, even after you guess the correct number, the game continues. It’s up to you to fix the code!

👾 💬 Stopping a program

When you want to stop running a program, type ^C in the terminal.

💻 Open the file in Visual Studio Code
code guessing_game.py
💻 Fix the game so the loop ends once the user guesses the correct number. It should also tell the user if their guess is too high or too low.

👾 The final game should like something like this:

----------------------------
Guess a number between 1-10!
----------------------------

Guess a number: 5
Too high...
Guess a number: 3
Too high...
Guess a number: 2
Correct

[5] Indexing into Words #

Starts With A #

💻 Start by running the game file:
python starts_with_a.py
💻 Fix the game so it only increases the points if the word actually starts with 'a'

Looping through a Word #


[6] For Loops #

A for loop enabled you to easily repeat code.

This loop runs 5 times, repeating everything indented to the right of the for i in range(5): line. i is a variable that gets incremented by one every time the loop runs.

for i in range(5):
    print(i)

It will output:

0
1
2
3
4

Remember, in Computer Science counting starts at 0


You don’t necessarily need to use the i variable inside the loop. Depending on the situation, it can be useful.

for i in range(3):
    print("Hello World!")

Will output:

Hello World!
Hello World!
Hello World!

Loops are incredibly useful for increasing the readability and efficency of your code.