Briefing
Easily to learn
- Efficient high level data structures
- Simple approach for OO programming
- Elegant syntax & Dynamic typing
Python interpreter
- Easily to be extended by C/C++
- Vast library of source or binary from major Python platform
Using the Python Interpreter
- Normally install at c:\python37
- Python command line sample:
. python -c command [arg]…
. python -m module [arg]… - Argument passing
. Access the arguments list by import sys. - Interactive mode
. When the command is read from tty, the interpreter will be in interactive mode.
. In this mode, it promotes the next command with >>> - Source code encoding
. Python source files are treated as UTF-8, so that all most all language char can be stored in string
. The standard lib is using ASCII
A simple approach to Python
Using Python as calculator
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5 # division always returns a floating point number
1.6
>>> 5 ** 2 # 5 squared
25
>>> 2 ** 7 # 2 to the power of 7
128
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
Strings
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> print('"Isn\'t," they said.')
"Isn't," they said.
>>> s = 'First line.\nSecond line.' # \n means newline
>>> s # without print(), \n is included in the output
'First line.\nSecond line.'
>>> print(s) # with print(), \n produces a new line
First line.
Second line.
>>> # 3 times 'un', followed by 'ium'
>>> 3 * 'un' + 'ium'
'unununium'
>>> word = 'Python'
>>> word[0] # character in position 0
'P'
>>> word[5] # character in position 5
'n'
>>> word[-1] # last character
'n'
>>> word[-2] # second-last character
'o'
>>> word[-6]
'P'
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
List
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
>>> squares[0] # indexing returns the item
1
>>> squares[-1]
25
>>> squares[-3:] # slicing returns a new list
[9, 16, 25]
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]