Your first program
Run a program, print text and numbers, and tell a string from a number.
14 min
A program is a list of instructions the computer follows from the top, one line at a time. Python reads your lines in order, does what each one says, and stops at the end.
The instruction you will use most is print(). It shows something on the screen:
print("Hello, world!")
print(2 + 3)The first line prints the text Hello, world!. The second prints 5: Python worked out the sum first, then printed the answer.
Everything in Python is a value, and every value has a type. Four types cover most of what you will do this month:
| Type | What it holds | Example |
|---|---|---|
int | whole numbers | 7, -3, 0 |
float | numbers with a decimal point | 2.5, 0.1 |
str | text (a string) | "hello", '42' |
bool | true or false | True, False |
Text goes inside quotes, single or double, and they must match. Without quotes Python thinks a word is a name and complains that it does not know it.
You can ask for a value's type with type(): print(type(3.0)) shows <class 'float'>.
A # starts a comment. Python ignores the rest of that line. Use comments to tell a human reader why, not to repeat what the code says.
“"5" and 5 are the same thing, because they look the same.”
On screen both print as a 5, and in everyday life a digit is a digit.
What is actually true
5 is an int you can do sums with. "5" is text that happens to contain the character 5. 5 + 5 is 10, but "5" + "5" is "55": for text, + glues pieces together.
How to catch yourself
When a result looks like glued digits ("55", "1010"), one of your values is a str.
Write a program that prints exactly these two lines, one under the other:
Hello, world!
I am learning Python.Use two print() calls.
Your code must satisfy
- prints both lines
# Your two print() calls go hereprint() can take several values separated by commas and puts a space between them. Print one line that says the type of the value 3.0, in this form:
3.0 is <class 'float'>Do not type the word float yourself: let type() produce it.
Your code must satisfy
- type printed
print(3.0, 'is', )- A program is a list of instructions Python follows from the top, in order.
print()shows a value;#starts a comment Python ignores.- The four everyday types are int, float, str and bool.
- Quotes make text:
"5"is a string and5is a number, and they behave differently.
Check the ideas before you move on.
What does print(2 + 3) show?
- A2 + 3
- B5
- C23
- DAn error
Which value is a str?
- A`42`
- B`4.2`
- C`"42"`
- D`True`
What does print("5" + "5") show?
- A10
- B55
- C5 5
- DAn error
A line that starts with is a comment, and Python ignores it.
Capitalisation, spacing and punctuation are ignored when grading.
Which of these are valid Python values?
- A`3.14`
- B`"hello"`
- C`hello`
- D`False`
Think of one small task you do every day, such as making tea. Write it as a list of exact steps a robot could follow. Where did you have to be more precise than you expected?
Practise this lesson, not just read it
In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of Python for Complete Beginners is planned around your week. Free.