Simple Python Values
Learn about simple types of values in Python.
Text
A value that is text is called a string. Here are some examples of strings and what they evaluate to in the Python Interactive Interpreter:
>>> 'hello'
'hello'
>>> "hello"
'hello'
>>> 'I\'m a string'
"I'm a string"
>>> "I'm a string"
"I'm a string"
You can see that, in Python, it doesn't matter if you use single- or double-quotes to enclose a string. You can use whichever is more convenient, taking into account that if you need to put the same sort of quotation mark within the string you need to escape it with a backslash (like in the third statement). When the Python Interactive Interpreter tells you what a string evaluates to, it chooses whichever quotation method is more convenient based on that, too, because it makes it more readable without backslashes.
Here's another way to quote strings:
>>> '''I'm a string'''
"I'm a string"
>>> """I'm
... a
... string"""
"I'm\na\nstring"
>>> print "I'm\na\nstring"
I'm
a
string
You can put three of whichever quote style you use, which allows you to make the string have multiple lines. Also, you don't need to escape a single- or double-quotation mark since Python knows that it needs three of them to match the opening of the quotation.
Notice that the Python Interactive Interpreter tells you that, where there is a line break in the string, there is a \n. Also, when you explicitly tell it to print out the value that it just told you that evaluated to (with the \n's), it then displays the line breaks properly. This is because the Python Interactive Interpreter tells you what expressions evaluate to in Python code, so that it's very clear exactly what they evaluate to, and when you use the print statement it is trying to display exactly what the string represents.
Numbers
A Python value that is a number has different features from a value that is a string. You can put a number in a string, like this:
>>> '5'
'5'
...but then it's not really a number to Python. A value that is a literal number is typed in python code as the number itself:
>>> 5
5
Arithmetic can be done with numbers to form expressions.
>>> 3 + 2
5
>>> jim_age = 20
>>> john_age = 30
>>> average_age = (jim_age + john_age) / 2
>>> average_age
25
In Python, integers are different from decimal numbers. If arithmetic is done with a combination of the two, the expression will result in a decimal number. Otherwise, it will result in whatever sort the original numbers were, rounding down if a decimal results and the expression needs to evaluate to an integer.
>>> 5 / 2
2
>>> 5.0 / 2
2.5
>>> 5.0 / 2.0
2.5
A decimal number is called a float, as in floating-point integer.