This lab is just an introduction 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 2020 by R. Jordan Crouser.
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 Idle
, and submit a program using Moodle.
Repl.it is a full-featured integrated development environment (IDE) that supports live, interactive coding right in your web browser. There's nothing to install, and it works on any web-enabled device. We will use it as our primary coding environment for this course. (Note: please reach out to Jordan if you don't have reliable internet access, there are other options available.)
If you haven't done so already, you will need to sign up for a free account. Go to repl.it/signup and click the google icon to register with your Smith account:
If you get stuck, let an instructor or TA know and we'll help.
To the right you'll find an embedded repl.it window. You can edit it directly if you like, or click the "open in repl.it" button in the upper right corner if you prefer using a different window. Either way, when you start typing, your own personal copy of this repl (a "fork") will be created, and everything you type will be synced to your account.
There are two distinct ways of typing in Python statements in most IDEs:
You can use the editor, which allows you to write a collection of statements (a program), save the collection in a file, and then have Python execute the contents of the program. The editor window appears at the top, and each line of code is automatically numbered to make it easier to reference specific lines. To run your code, you'll click the green triangle button at the top of the window. We'll primarily use this approach in this class.
return
. In repl.it, the shell is the black window at the bottom. Try typing print("Hello!")
and hitting return
.
At the top of the main.py file in the editor window below, please copy the following header information:
# ------------------------------------------------------
# Name: <YOUR NAME HERE>
# Course Info: CSC111 - Fall 2020
# Description: Submission for Lab 1, pt. 0
# Date: <TODAY'S DATE HERE>
# ------------------------------------------------------
Fill in the relevant information, such as your name and the date. 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 F5 (or select Run > Run Module) to run the program.
When prompted to save the program, name it lab1.py and save it to a folder on your computer that will be easy to find.
After running the program, look at the console 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. R2D2
is a valid name for a variable. So is alpha
or c3po
.
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
yearBorn = year - age
print( "You are", age, "years old" )
print( "You were born in", yearBorn )
Aha! Now it's much easier to see what the program is doing.
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 a 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!
Create a new program in Idle with this new 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 |
--------
lab1.py
file.This is not required today, but if you want some extra challenging coding to do, try making your program generate the output below:
+-------+
| Aleah |
+-------+
+-------+
| Belle |
+-------+
+------+
| Chen |
+------+