>>> print("Hello, world!")
Hello, world!
>>> 31234 * (762 + 181)
29453662
.py..pyc)..py files containing the source code for the program.python <file>in a shell, where
<file> is a file containing the program.
Note that if you have multiple versions of Python running on your computer, you may have to specify which
one:
python3.2 <file>
<ctrl>-p to go up, <ctrl>-n to go down)
+, -, /, //, *, **, %.
>>> 5+20 25 >>> 50.7 / 2.3 22.04347826086957 >>> 8**3 512 >>> 9 / 2 4.5 >>> 21 % 4 1
=. This operator has a completely different meaning in Python.
>>> 21 + 3 = SyntaxError: invalid syntax >>> 21 + 3 = 24 SyntaxError: can't assign to operator
>>> 3 * 4 + 5 17 >>> 3 * (4 + 5) 27
int) and floating-point numbers (float).
>>> type(3) <class 'int'> >>> type(3.0) <class 'float'>
>>> type(int) <class 'type'> >>> type(type) <class 'type'>
str),
which begin and end with single or double quotes.
>>> 'Hello, world!'
'Hello, world!'
>>> type('Hello, world!')
<class 'str'>
>>> type("12")
<class 'str'>
>>> type('type')
<class 'str'>
+ and * also work with strings.
>>> 'Hello, world!' + " This is me."
'Hello, world! This is me.'
>>> "no" * 3
'nonono'
>>> '1' + '2'
'12'
>>> '1' + 2
Traceback (most recent call last):
File "<pyshell#260>", line 1, in <module>
'1' + 2
TypeError: Can't convert 'int' object to str implicitly
+ and * work with lists too.
>>> ['a', 'list', 'of', 'strings'] ['a', 'list', 'of', 'strings'] >>> >>> ['a', 'list', 'of', 'strings'] + [';', 'another', 'list'] ['a', 'list', 'of', 'strings', ';', 'another', 'list'] >>> [['x', 4], ['y', 5], ['heading', 0]] [['x', 4], ['y', 5], ['heading', 0]] >>> [+, -] SyntaxError: invalid syntax
'hello', 'أهلاً', 'ሰላም', 'こんにちは'
['hello', 'world'], ['ለዓለም', 'ሁሉ', 'ሰላም']