Hello World
Simple Python I/O¶
The most common I/O functions in Python are input()
and print()
. These are not the only I/O functions in Python. The Python input()
function will print a prompt and then wait for text from the user. The function then returns the text. The Python print()
function will output text to the terminal. It is similar to the printf()
function in C or console.log()
in JavaScript.
Terminal Output¶
In Python, almost everything is printable by default, without conversion. Strings, Numbers, Sequences, Booleans, Dictionaries, Sets and many other classes of objects can be printed with no special syntax. Print does not have a return value, we simply call the function. A Class instance can be printed properly iff the Class has a __str__
and/or __repr__
method. More on Classes will be found in the Classes notebook. Expressions can be printed directly, iff it resolves to a printable value. Expression Example: print(2 + 4 * 8)
The following code prints Hello, Python!
to the terminal.
print("Hello, Python!")
Separator Examples:¶
The print function can take an unbounded number of arguments. We can separate these items with the sep
keyword argument. The sep
value must be a string. See the examples below…
print("Alpha", "Beta", "Gamma") # By default sep=' '
print("Alpha", "Beta", "Gamma", sep=", ") # Comma and space
print("Alpha", "Beta", "Gamma", sep="\t") # Tab
print("Alpha", "Beta", "Gamma", sep="\n") # New line
Terminal Input¶
Input from input()
is always read
as text (even numbers!), more specifically it’s a string, aka str
type.
The text from the user becomes the output (return value) of the input function.
The return value of any function can be assigned to a variable.
The follwing code prints What's your name?
and waits for input from the user. Then it stores the user input into the name
variable as a string.
name = input("What's your name? ")
The next line of code will print the greeting.
print("Hello,", name)
Run Your Python Script¶
First, navigate to the directory holding your script, then type the following:
$ python3 A01_hello_world.py
The $
above represents the shell prompt, yours may look different. You should only copy the code that follows it. In other words: you never need to type the $
prompt. If the terminal prompt instead looks like this…
>>>
Then you are currently in the Python REPL. First exit the REPL with the exit()
function, then run your script.
Your first script should resemble the following:¶
# File Name: A01_hello_world.py
"""
Author: <Your Name>
Team: <Your Team>
Date: <Today>
"""
name = input("What's your name? ")
print("Hello,", name)
The first few lines of this script are comments. The first line is a single-line comment – marked by the #
symbol. It is followed by a multi-line comment – indicated by the triple quotes before and after the text. Comments will be correctly ignored by Python. More info on comments to come later. For now, it is good enough to know that code comments are specifically for the human readers of your code, including future you.