Course progress Course outline 24 of 24 lessons available
Python Foundations
Data and Collections
Building Reliable Programs
Modeling with Objects
Professional Python
Advanced Python
Python is a tiny helper
Imagine a small helper who is very fast, very patient, and very literal. It will follow your recipe exactly—but it cannot guess a missing step. That helper is Python.
- Write a tiny recipe
- Run give it to Python
- See look at the result
- Change try one new thing
Your first recipe is one line:
print("Hello, Python!")
print() means “show this on the screen.” Python replies:
Hello, Python!
By the end of this chapter, you will make a tiny program that asks your name and your dream, then prints a card just for you.
One big idea: a program is a recipe, and Python is the helper that follows it.
Three places, three jobs
You will see three kinds of text. They may look alike, but they have different jobs.
-
Terminal
python3 hello.py
asks the computer to run -
Python code
print("Hello")
is the recipe -
Output
Hello
is Python's answer
Open a terminal and check for Python 3:
python3 --version
You should see something beginning with Python 3. On Windows, try py --version instead. Use whichever command works on your computer for the rest of this chapter.
For one quick experiment, start Python:
python3
When you see >>>, type:
2 + 3
Python answers 5. Type exit() when you are done. This quick playground is called the REPL.
Terminal = start things. Python code = instructions. Output = the result.
Save a hello recipe
Quick experiments disappear. A file keeps your recipe.
- Make a plain-text file named
hello.py. Do not call itpython.py. - Put this inside and save it:
print("Hello, Python!")
- In the terminal, move to the folder containing the file and run:
python3 hello.py
You should see:
Hello, Python!
Now change the recipe:
print("I changed the recipe!")
Save, run, and look again. The new words appear because Python reads the saved file from top to bottom every time.
If you still see the old words, save the file before you run it again.
Let your program listen
print() is Python’s mouth. input() is Python’s ear: it asks a question and waits.
-
Ask
input()waits - Keep a name labels the answer
-
Show
print()speaks
Create dream_card.py with exactly these five lines:
print("My Python Dream Card")
name = input("Your name: ")
dream = input("What do you want to build? ")
print("Dreamer:", name)
print("Dream:", dream)
Run it:
python3 dream_card.py
One run might look like this:
My Python Dream Card
Your name: Maya
What do you want to build? a game
Dreamer: Maya
Dream: a game
name and dream are labels for the two answers. The next chapter will explain these labels precisely. For now, run the program again with different answers and watch the card change.
Source code stays the same; input can change on every run.
Errors are clues
Sometimes the recipe has a torn word. Python stops and leaves a clue called a traceback.
- Stop an error appears
- Read start at the last line
- Fix change one thing
- Run again check your idea
Try this broken line on purpose:
print("Hello)
The final line of the traceback says SyntaxError. Python is telling you that the quote was never closed. Add the missing ", save, and run again:
print("Hello")
If Python says it cannot open your file, check that the terminal is in the right folder. If it says NameError, check the spelling and make sure the name received an answer before you used it.
Read the last line, find the file and line number, fix one thing, then run again.
Three tiny missions
- Make it yours. Change the Dream Card heading and predict the new output before running it.
- Add one answer. Ask for a favorite color, save it as
color, and print it on the card. - Be an error detective. Remove one closing quote, find
SyntaxError, repair it, and confirm the program works.
Stuck? Check four small things: saved file, right folder, matching quotes, lowercase print.
You are ready for Chapter 2 when…
- you can explain terminal, Python code, and output;
- you can create, save, and run a
.pyfile; - you can use
print()andinput(); - you can change the Dream Card and see a new result;
- you can read the last line of an error and repair one mistake.
Next, you will open the labeled boxes and learn exactly how Python stores values, variables, and types.