Python Hello World Startup
Python Hello World Startup
1
Our First Program
A new programmer's first program by tradition: Hello, World
This Python program will print a message when executed.
"Python program" computer code written using the Python programming language
"print a message" display some text on the screen
"executed" run a program so that its instructions
are followed by the computer, line by line
2
Naming the File
To write a program, put all code in a single file. For our first
program, we'll choose the filename hello_world.py
Use descriptive names for files
We use snake_case to name things in Python
Words start with lowercase letters
Words separated by underscore (_) characters
End Python files with the extension .py
3
Hello, World!
Our file, hello_world.py:
print("Hello, world!")
4
Printing
Printing: displaying text on screen
print() function
Whatever you put inside of the parentheses gets displayed as output.
Examples:
print("Hello, world!") prints Hello, world!.
print("So long...") prints So long....
print(" ") prints .
5
Text and Strings
A string is a sequence of characters that is interpreted literally.
Surround text in quotes to create a string
Without quotes, text is interpreted as a statement
or an expression—that is, as another line of code.
6
Bug: Forgetting Quotes
If we remove the quotes from hello_world.py, we have an example of buggy code:
print(Hello, world!)
7
Statements & Order of Execution
Each line of code represents a statement, which
is a full instruction to be handled by the computer.
Default program execution: top to bottom, one at a time
8
Statements & Order of Execution
hello_everybody.py Output:
9
Comments
Use # for comments
Ignored by the computer
Doesn't influence the behavior of the program at all!
For humans: explanations, notes, TODOs
Example: Commenting hello_world.py
# This is a comment
print("Hello, world!") # Prints a message
10
Reading User Input
input() function for getting text interaction
Like print(), any string placed in the parentheses will be first displayed as a prompt.
Whatever the user types is saved for later inside of name
11
Reading User Input: Example
hello_input.py
print("Who would you like to say hello to?")
name = input(">")
print("Hello, ", name)
12
Running hello_input.py
First execution:
$ python hello_input.py
Who would you like to say hello to?
> Agustina
Hello, Agustina
13