The goals of this lab are to help you to understand:
This program asks for your name as input, then says hello to you as output. Most often it's the first program you write when learning a new programming language.
TO RUN THIS CODE: Click in the cell below and click the run cell button.
NOTE: After the code executes, you will see a sequence number next to the code and output below the code itself. This is your indication the code in the cell has run. You must run all code cells in the notebook for full credit.
In [ ]:
your_name = input("What is your name? ")
print('Hello there',your_name)
Believe it or not there's a lot going on in this simple two-line program, so let's break it down.
What is your Name?
your_name
Hello there
your_name
At this point you might have a few questions. What is a variable? Why do I need it? Why is this two lines? Etc... All will be revealed in time.
Variables are names in our code which store values. I think of variables as cardboard boxes. Boxes hold things. Variables hold things. The name of the variable is on the ouside of the box (that way you know which box it is), and value of the variable represents the contents of the box.
Assignment is an operation where we store data in our variable. It's like packing something up in the box.
In this example we assign the value "USA" to the variable country
In [ ]:
# Here's an example of variable assignment.
country = 'USA'
In [ ]:
country # Run this cell. Itshould say 'USA'
At this point you might be thinking: Can I overwrite a variable? The answer, of course, is yes! Just re-assign it a different value:
In [ ]:
country = 'Canada'
You can also access a variable multiple times. Each time it simply gives you its value:
In [ ]:
country, country, country
Variables play an vital role in programming. Computer instructions have no memory of each other. That is one line of code has no idea what is happening in the other lines of code. The only way we can "connect" what happens from one line to the next is through variables.
For example, if we re-write the Hello, World program at the top of the page without variables, we get the following:
In [ ]:
input("What is your name? ")
print('Hello there')
When you execute this program, notice there is no longer a connection between the input and the output. In fact, the input on line 1 doesn't matter because the output on line 2 doesn't know about it. It cannot because we never stored the results of the input into a variable!
In [ ]:
# TODO: Write your code here
Computer code serves two equally important purposes:
If our code does something useful, like land a rocket, predict the weather, or calculate month-end account balances then the chances are 100% certain that someone else will need to read and understand our code.
Therefore it's just as important we develop code that is easily understood by both the computer and our colleagues.
This starts with the names we choose for our variables. Consider the following program:
In [ ]:
y = input("Enter your city: ")
x = input("Enter your state: ")
print(x,y,'is a nice place to live')
What do x
and y
represent? Is there a semantic (design) error in this program?
You might find it easy to figure out the answers to these questions, but consider this more human-friendly version:
In [ ]:
state = input("Enter your city: ")
city = input("Enter your state: ")
print(city,state,'is a nice place to live')
Do the aptly-named variables make it easier to find the semantic errors in this second version?
Write a program to input your name and your age and the print name and age on a single line.
Example:
Enter your name: Mike
Enter your age: 25
Mike is 25
In the above example Mike
was the entered name, and 25
was the entered age.
In [ ]:
# TODO:Write code here.
Now try to write a program which asks for two separate inputs: your first name and your last name. The program should then output Hello
with your first name and last name.
For example if you enter Mike
for the first name and Fudge
for the last name the program should output Hello Mike Fudge
HINTS
two_words
In [ ]:
# TODO: write your code here
In [ ]:
prefix = "re"
suffix = "ment"
root = input("Enter a root word, like 'ship': ")
print( prefix + root + suffix)
Write a program to prompt for three colors as input, then outputs those three colors as a lis, informing me which one was the middle (2nd entered) color. For example if you were to enter red
then green
then blue
the program would output: Your colors were: red, green, and blue. The middle was is green.
HINTS
and
.
In [ ]:
# TODO: write your code here
In Python 3.7, f-strings were introduced to make it easier to format string literals in the print()
statement.
Here's how it works:
f
in front of the string literal{curly braces}
{curly braces}
is replaced with its value!For example:
In [ ]:
name = "Mary"
major = "Data Science"
gpa = "4.0"
print(f"{name} is a {major} major. Her gpa is {gpa}")
In [ ]:
# TODO: write your code here
Please answer the following questions. This should be a personal narrative, in your own voice. Answer the questions by double clicking on the question and placing your answer next to the Answer: prompt.
Answer:
Answer:
Answer:
1 ==> I can do this on my own and explain how to do it.
2 ==> I can do this on my own without any help.
3 ==> I can do this with help or guidance from others. If you choose this level, please indicate HOW this person helped you.
4 ==> I don't understand this at all yet and need extra help. If you choose this please try to articulate that which you do not understand to the best of your ability.
Answer:
FIRST AND FOREMOST: Save Your work! Yes, it auto-saves, but you should get in the habit of saving before submitting. From the menu, choose File --> Save Notebook. Or you can use the shortcut keys CTRL+S
Handing in your Homework and Labs is easy! All you need to do is run the code cell below and follow the directions. This code sends your assignment to a private cloud where your instructor can download a copy of it at the time of submission.
Once the assignment is graded, you will see a grade and feedback / comments in Blackboard.
In [ ]:
# run this code to turn in your work!
from ist256.submission import Submission
Submission().submit()