This lab is just an introduction to tooling used in the course and to having fun with Python
. The original version was written by Dominique Thiebaut at Smith College, and it was adapted for this course in June 2018 by R. Jordan Crouser. It has been updated in January 2019 and 2020 by Alicia M. Grubb.
The purpose of this lab is to give you a first taste of programming in Python
, to give you space to explore the different systems that are available to you, and get a sense of how to program in an “intuitive” fashion. Try not to worry too much about the details. This is our first dive into programming, and we haven’t had time to fully understand what variables are, what strings are, or how the function print()
works. Today is just a day for you to explore the tools, run some Python
code in Thonny
, and submit a program using Moodle.
This lab is to be completed individually. All future labs will be completed in pairs. You may discuss and help your labmates in completing these steps, but each person must complete all steps.
It is very important that you stay up to date with announcemnets in this course. Think about whether you would prefer to receive updates on your mobile device or on your laptop. You are required to install the app on at least one device. Read and follow the steps in the Slack Tutorial Here.
Send a Direct Message to someone else in your lab section.
Many of us depend on cloud services to manage our life and rarely handle files directly. In this course you will work with many files both in class and in your self-study. For a refresher on files, read a mac or windows tutorial.
Note for Mac users: Recent versions of OSX have hidden users home folder. In Finder, click the Go
menu in the top tool bar and then select Home
. Your home folder will appear in the main window. Click the File
menu in the top tool bar and then select Add to Sidebar
. Now your home folder shows in the sidebar with an icon of a house. You can drag it up to sit above your Documents
icon.
In the past, many students have had issues managing their files in this course. We recommend you take a moment and create a folder on your computer for this course, and subfolders for labs and homework, such as
Also, determine if this folder lives on your local machine, in the cloud (aka on a remote server), or both. If you are not sure about this ask a TA or Instructor.
Thonny is the name of an Integrated Development Environment you can use to create Python programs on your computer. You should install Thonny on your own computer. (Mac Users: after downloading and opening the .dmg
file. Open readme.txt
for further instructions.)
Here’s a quick overview of how to use Thonny to program in Python. There are two distinct ways of typing in Python statements:
return
.At the top of the Untitled file in the editor window, please copy the following header information:
# ------------------------------------------------------
# Name:
# Section: L01, L02, L03, or L04
# Filename: lab0.py
# Peers:
# References:
# ------------------------------------------------------
Fill in the relevant information, such as your name, your lab section. Note how these lines each start with the special character #
: this tells python that these lines are comments
, rather than code. We will use comments
to make the code we write in this class easier to understand.
Type the following code in the Untitled file in the editor window (underneath your header):
age = 20
year = 2018
print( age )
print( year )
print( year - age )
Press the or F5 or select Run > Run current script to run the program.
When prompted to save the program, name it lab0.py and save it to the folder on your computer that you created above.
After running the program, look at the Shell window. Some numbers appear… Do they make sense?
age
and year
.In the program we just wrote, age
and year
are variables. In computer science, variables are used to hold information (in our case: numbers).
In Python, you can give variables any name you want, as long as you use letters or digits 0-9, and as long as the first character is a letter. SSN
is a valid name for a variable. So is alpha
or Lab3Students
.
Let’s modify your program slightly, as shown below:
age = 20
year = 2018
print( age, year )
Now run your program. Notice how the output is slightly different?
Now try this:
age = 20
year = 2018
year_born = year - age
print( "You are", age, "years old" )
print( "You were born in", year_born )
Aha! Now it’s much easier to see what the program is doing.
Now take a look at lab0-template.py and copy the lines below #### Introduction
to your lab0.py file. For each of the following steps, add your code below the corresponding comment lines.
Answer the question under Step #0. Be sure to start each line with #
to make it a comment in Python.
What if we wanted to add new variables that contain text instead of numbers?
In computer science we refer to text as strings of characters , or strings for short. In Python, strings are declared using quotation marks. Try this:
name = "Alex"
college = "Smith College"
age = 20
print( name, "goes to", college, "and is", age, "years old" )
Run your program a few times, each time changing the value of a variable. For example change name
to “Monique”, or age
to 18.
Write a new program that contains these variables:
name = "Noa"
college = "Amherst College"
age = 20
year = 2018
Figure out how to add new variables and write several print statements that will make your program output:
Noa goes to Amherst College and is 20 years old
Noa was born in 1998
If your program is well written, and if you change the variable name
to be “Jordan”, college to be “Smith College”, and age to be 33, then your program should automatically output:
Jordan goes to Smith College and is 33 years old
Jordan was born in 1985
Verify that your program works correctly.Let’s write another new program:
word1 = "hello"
word2 = "there"
print( word1 * 2 )
print( word1 * 5 )
print( word2 * 10 )
See how you can multiply a string?
Try adding this:
print( word1 * 2, word2 * 4 )
Run the program. Does the output make sense?
Modify your program to make it print the output shown below. Note the new exclamation point:
hello hello hello hello there!
hello there! there! there! there! there! hello
Hint: you’re allowed to change the contents of the varables.Write a program that prints the output below:
hellothere!hellothere!hellothere!hellothere!hellothere!
Add this piece of code:
for name in [ "Aleah", "Belle", "Chen" ]:
print( name )
Notice that the line that starts with print
is indented from the line that starts with for
. This is a very important detail: Python uses indentation to influence how statements depend on each other. Because the print statement is indented relative to the for statement, it is controlled by the for statement, and will be repeated for each string in the list.
Run the program and observe the output.
Play with these different code snippets. Make sure you understand how your program works before moving on to the next sections.
Remember: the purpose is not to go quickly through the lab, but to start acquiring an intuition for the logic of programming.
for name in [ "Aleah", "Belle", "Chen" ]:
print( name )
print( "---------------" )
and this one:
for name in [ "Aleah", "Belle", "Chen" ]:
print( name, len( name ) )
or:
for name in [ "Aleah", "Belle", "Chen" ]:
print( name )
print ( len( name ) )
Still some more:
for name in [ "Aleah", "Belle", "Chen" ]:
print( name, '-' * len( name ) )
Make your program display the names with a bar made of minus signs under each name. The length of the bar should be the same as the length of the name that is above it. For example:
Aleah
-----
Belle
-----
Chen
----
Similar question, but now like this:
-----
Aleah
-----
-----
Belle
-----
----
Chen
----
Create a program with all the statements below. They contain the solution for this challenge. You will need to figure out how to reorganize them and modify them to create the answer for this challenge. Run this program and study carefully its code and its output.
name = "Julia"
nameLen = len( name )
nameLen4 = len( name ) + 4
nameLen2 = len( name ) + 2
x = 10
bar1 = "-" * x
bar2 = bar1 + "|"
bar3 = "<" + bar1 + ">"
bar4 = "-" * nameLen
print( "name = ", name )
print( "nameLen = ", nameLen )
print( "nameLen4 = ", nameLen4 )
print( "nameLen2 = ", nameLen2 )
print( "x = ", x )
print( "bar1 = ", bar1 )
print( "bar2 = ", bar2 )
print( "bar3 = ", bar3 )
print( "bar4 = ", bar4 )
Modify your program as follows:
for name in [ "Julia Child", "Gloria Steinem" ]:
nameLen = len( name )
nameLen4 = len( name ) + 4
nameLen2 = len( name ) + 2
x = 10
bar1 = "-" * x
bar2 = bar1 + "|"
bar3 = "<" + bar1 + ">"
bar4 = "-" * nameLen
print( "nameLen = ", nameLen )
print( "nameLen4 = ", nameLen4 )
print( "nameLen2 = ", nameLen2 )
print( "x = ", x )
print( "name = ", name )
print( "bar1 = ", bar1 )
print( "bar2 = ", bar2 )
print( "bar3 = ", bar3 )
print( "bar4 = ", bar4 )
print()
Carefully look at the output of your program. Because all the print statements are indented relative to the for statement, they are repeated for every name in the list. See how bar1
and bar2
do not change in length, but bar4
changes with each string?
Here is your challenge for today: Make your program use a for-statement and a list of 3 names, and make it display a box around each name. Your goal is to make the box fit exactly the length of the name, without extra spaces around it. Your output on our original list would be
---------
| Aleah |
---------
---------
| Belle |
---------
--------
| Chen |
--------
#
to make it a comment in Python):
Identify one existing ethical/social/cultural issue within the tech world that you are concerned about. Write a short paragraph about the situation (where is it happening, what company, what is the issue, why is it a problem).
lab0.py
file.If there is still time left in lab, try making your program generate the output below:
+-------+
| Aleah |
+-------+
+-------+
| Belle |
+-------+
+------+
| Chen |
+------+
How about this one:
+--------+
| Ali |
+--------+
| Balwin |
+--------+
| Chen |
+--------+