3. Python
"Copy"
Python (comments) hash # (string literal) hash hash Python
#
spam = 1 #
# ...
text = "# "
3.1. Python
Python >>>
3.1.1. (Number)
expressionExpression +-* / ()
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5 #
1.6
(/) float floor division // %
>>> 17 / 3 #
5.666666666666667
>>>
>>> 17 // 3 #
5
>>> 17 % 3 # %
2
>>> 5 * 3 + 2 # * +
17
Python (powers) ** [1]
>>> 5 ** 2 # 5
25
>>> 2 ** 7 # 2 7
128
(=)
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
(defined)
>>> n #
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
(operator) (operand)
>>> 4 * 3.75 - 1
14.0
_ Python
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
---
int floatPython Decimal FractionPython (complex numbers) j J 3+5j
3.1.2.
Python str (strings) "!" "rabbit" "Paris" "Got your back." "Yay! :)" ('...') ("...") [2]
>>> 'spam eggs' #
'spam eggs'
>>> "Paris rabbit got your back :)! Yay!" #
'Paris rabbit got your back :)! Yay!'
>>> '1975' #
'1975'
\
>>> 'doesn\'t' # \' ...
"doesn't"
>>> "doesn't" # ...
"doesn't"
>>> '"Yes," they said.'
'"Yes," they said.'
>>> "\"Yes,\" they said."
'"Yes," they said.'
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
Python shell print()
>>> s = 'First line.\nSecond line.' # \n
>>> s # print()
'First line.\nSecond line.'
>>> print(s) # print() \n
First line.
Second line.
\ raw string r
>>> print('C:\this\name') # \t tab\n
C: his
ame
>>> print(r'C:\this\name') # r
C:\this\name
"""...""" '''...''' \
>>> print("""\
... Usage: thingy [OPTIONS]
... -h Display this usage message
... -H hostname Hostname to connect to
... """)
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
>>>
+ (concatenate) *
>>> # 3 'un' 'ium'
>>> 3 * 'un' + 'ium'
'unununium'
string literal
>>> 'Py' 'thon'
'Python'
>>> text = ('Put several strings within parentheses '
... 'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'
>>> prefix = 'Py'
>>> prefix 'thon' #
File "<stdin>", line 1
prefix 'thon'
^^^^^^
SyntaxError: invalid syntax
>>> ('un' * 3) 'ium'
File "<stdin>", line 1
('un' * 3) 'ium'
^^^^^
SyntaxError: invalid syntax
+
>>> prefix + 'thon'
'Python'
indexed( subscripted) 0 1
>>> word = 'Python'
>>> word[0] # 0
'P'
>>> word[5] # 5
'n'
>>> word[-1] #
'n'
>>> word[-2] #
'o'
>>> word[-6]
'P'
-0 0 -1
slicing (substring)
>>> word[0:2] # 0 () 2 ()
'Py'
>>> word[2:5] # 2 () 5 ()
'tho'
(slice indices) 0 slice
>>> word[:2] # 2 ()
'Py'
>>> word[4:] # 4 ()
'on'
>>> word[-2:] # ()
'on'
s[:i] + s[i:] s
>>> word[:2] + word[2:]
'Python'
>>> word[:4] + word[4:]
'Python'
slice slice 0 n n
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
0...6 i j slice i j
slice word[1:3] 2
>>> word[42] #
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
slice
>>> word[4:42]
'on'
>>> word[42:]
''
Python --- immutable
>>> word[0] = 'J'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> word[2:] = 'py'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> 'J' + word[1:]
'Jython'
>>> word[:2] + 'py'
'Pypy'
>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34
- Text Sequence Type --- str
sequence
method
- f-stringf
str.format()(string formatting)- printf-style String Formatting
%
3.1.3. List
Python list itemList
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
sequence list (slice)
>>> squares[0] #
1
>>> squares[-1]
25
>>> squares[-3:] #
[9, 16, 25]
List (concatenation)
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> cubes = [1, 8, 27, 65, 125] #
>>> 4 ** 3 # 4 64 65
64
>>> cubes[3] = 64 #
>>> cubes
[1, 8, 27, 64, 125]
list list.append()
>>> cubes.append(216) # 6
>>> cubes.append(7 ** 3) # 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
Python list list list
>>> rgb = ["Red", "Green", "Blue"]
>>> rgba = rgb
>>> id(rgb) == id(rgba) #
True
>>> rgba.append("Alph")
>>> rgb
["Red", "Green", "Blue", "Alph"]
>>> correct_rgba = rgba[:]
>>> correct_rgba[-1] = "Alpha"
>>> correct_rgba
["Red", "Green", "Blue", "Alpha"]
>>> rgba
["Red", "Green", "Blue", "Alph"]
slice list list
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> #
>>> letters[:] = []
>>> letters
[]
len() list
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4
list list list
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'
3.2.
>>> #
>>> #
>>> a, b = 0, 1
>>> while a < 10:
... print(a)
... a, b = b, a+b
...
0
1
1
2
3
5
8
ab0 1 (expression) (evaluate)whilea < 10Python C (true) (false)list (comparison operators) C<>==<=>=!=Python tab
-
>>> i = 256*256 >>> print('The value of i is', i) The value of i is 65536
end
>>> a, b = 0, 1 >>> while a < 1000: ... print(a, end=',') ... a, b = b, a+b ... 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
** - -3**2 -(3**2) -9 9 (-3)**2
\n ('...') ("...") " \'