Summary - Python basics
- Python course
- Beginner
- Lesson 6/116
- 5 min
Combine print, comments and indentation to write your first Python script.
Summary - Python basics
In this group you learned what Python is, the REPL, print, comments, indentation and running scripts.
- Python is an interpreted, general-purpose language.
- You print strings to the screen with
print. - Comments start with
#. - Indentation defines blocks; the standard is four spaces.
- Script files use the
.pyextension and run withpython3 file.py.
Now two small tasks combine these concepts.
Practice Write a greeting script
As if writing a hello.py script, add a # comment and below it print("Welcome"). When run, the script should print Welcome.
Starter code
# Greeting scriptSample solution
# Greeting script
print("Welcome")Expected output
Welcome
Practice Multi-line script
Write a script with two print calls: first print Start, then End. Add a # comment at the top.
Starter code
# Two-line scriptSample solution
# Two-line script
print("Start")
print("End")Expected output
Start End
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which is wrong?
- print writes to the screen
- A script uses the .txt extension Correct answer
- Indentation defines blocks
- # starts a comment
Explanation: Scripts use .py, not .txt.
2 Complete the indentation
if True:
____("Blocks work")
if True:
print("Blocks work")
Explanation: print is inside the if block.