Numbers, Booleans & Expressions
Strings are not the only thing print() accepts. Give it a number and it prints the number. Give it a sum and something interesting happens: Python works out the answer first, and prints only the answer.
1Numbers need no quotes
Numbers go straight inside the parentheses, with no quotes. If you put quotes around a number, it is not a number any more. It becomes text that looks like a number.
A whole number, with no decimal point: 7, 0, -15, 1000000.
A number with a decimal point: 3.14, -0.5, 2.0. Even 2.0 is a float, because it has the point.
3 + 4j). It is not in your syllabus. It is mentioned only so that you are not surprised if you ever see it.2Booleans: only two values
A boolean has only two possible values: True and False. Python uses them to answer yes-or-no questions. Later they will control the decisions your programs make. Two rules about them are strict:
True · FalseLowercase true is not a boolean. Python looks for something called true, does not find it, and gives a NameError.
'True' is a string, not a booleanIt prints the same way, which is why it is easy to get wrong. But with quotes it is just a word, not a yes/no value.
3Expressions are worked out first
If you give print() a sum, it does not print the sum. Python works out the answer first, and gives only that answer to print(). The maths happens before anything is printed.
Python never showed 2 + 3. It showed 5. The sum was worked out first, and only the answer reached print(). Notice also that there are no quotes around the sum — quotes would have turned it into text, and then Python would have shown 2 + 3 just as you typed it.
print() is worked out first. Only the answer is printed.Try a few of your own at the prompt:
print('2 + 3') has quotes, so it is text. Python shows it exactly as written: 2 + 3. No maths happens at all.4Try them yourself
Every value below can go inside print() — except one, which is there to break. Click through them and see what comes back.
A string: text inside quotes. The quotes only wrap it. They are not printed.
5Mixing types in one print()
Commas do not mind at all. When you separate arguments with commas, you can mix strings, numbers and booleans freely. Each one is printed on its own.
One line, and it holds a string, a number and a boolean together. The commas keep them apart. Try it, and put your own name and marks in:
6Recap
print() accepts strings (in quotes), integers and floats (no quotes), booleans (True / False, capital letter, no quotes) and expressions — sums, which Python works out first, printing only the answer.What does print(2 + 3) show?
What does print('2 + 3') show — with the quotes?
Which one is a real boolean?