Your first program: print & comments
Show output with print(), leave comments, and return a value to be checked.
Why the return value is the thing we check
In real code a function exists to hand a value back to whatever called it. print(...) is a side effect for a human watching a terminal. return is how one piece of code passes a result to another piece of code. Tests, callers, and data pipelines read the returned value and never look at the screen, so a function that prints the right answer but forgets to return it is still broken. That distinction is the whole point of this lesson.
Running top to bottom
Python executes a file one line at a time, from the top down. print(...) writes its argument to output, then moves to the next line.
print("Python runs top to bottom")
print("one line at a time")
# output:
# Python runs top to bottom
# one line at a time
A line starting with # is a comment. Python ignores everything after the # on that line, so comments are notes for humans, not instructions for the machine.
# this whole line is skipped
print("this runs") # a comment can also trail real code
Sort these lines by whether Python actually runs them.
Building strings with +
A string is text in quotes. The + operator on two strings joins them into one new string (this is called concatenation).
greeting = "Hello, " + "world" + "!"
print(greeting) # Hello, world!
Note the exact characters: "Hello, " already includes a comma and a trailing space, so you do not add spacing yourself. Getting that spacing right is exactly what the Apply and Practice exercises check.
Functions that return
A function packages code under a name so you can reuse it. def starts the definition, the name and parameters follow, and return sends a value back to the caller.
def greet(name):
return "Hello, " + name + "!" # hand the finished string back
print(greet("Ada")) # Hello, Ada!
Calling greet("Ada") substitutes "Ada" for name, builds "Hello, Ada!", and returns it. The same shape covers banner(name): wrap the name by returning "=== " + name + " ===".
Pitfall: + will not mix a string and a number
+ only concatenates string with string. If one side is a number you get a crash, not automatic conversion:
"Room " + 12
# TypeError: can only concatenate str (not "int") to str
The fix is to convert the number first with str(...): "Room " + str(12) gives "Room 12". In these exercises name is already a string, so plain + is safe.
Interview nuance: every Python function returns something. If you never write return, or you only print(...) inside it, the function hands back None, and print(...) itself evaluates to None. So return print("Hello") returns None, not the text. Interviewers use this to check that you separate a value (what return produces) from a side effect (what print does). The grader here calls your function and inspects the returned string, so always return the message rather than printing it.
# Comments start with # and are ignored.
print("Python runs top to bottom")
print("one line at a time")
greeting = "Hello, " + "world" + "!"
print(greeting)Apply
Your turn
The task this lesson builds to.
Implement greet(name): return a greeting for the given name.
For name = "World" it should return the string "Hello, World!". Build it by joining
"Hello, ", the name, and "!" with +. Return it (don't print it).
3 hints and 3 automated checks are waiting in the workspace.
Practice
Make it stick
A second problem on the same idea, so it survives past today.
Implement banner(name): wrap name in a simple banner.
For name = "Ada" it should return "=== Ada ===" (the name with "=== " before it and
" ===" after it).
2 hints and 3 automated checks are waiting in the workspace.