point
/
) always returns a float//
operator if you want to perform a devaluation division to get an int type%
if you want to get the remainder _
", so it is easy to use with a calculator.j
or J
to represent imaginary parts, such as 3 + 5j
.
#addition
>>> 2+2
4
#Subtraction and multiplication
>>> 50 - 5 * 6
20
>>> (50 - 5) *6
270
#division
>>> 17 / 3
5.666666666666667
>>> 17 // 3
5
>>> 17 % 3
2
#Get both quotient and remainder
>>> divmod(17, 3)
(5, 2)
#radix
# 0b:Binary
# 0o:Octal
# 0x:Hexadecimal
>>> 0b10
2
>>> 0o10
8
>>> 0x10
16
#Exponentiation
>>> 5 ** 2
25
>>> 5 ** 3
125
#Use interactive mode as a calculator
>>> _ + 1
126
point
\
\
to be interpreted as a special character\
at the end of the line to escape+
operator and can be repeated with the *
#In the path name/Can be used, so the following is unnecessary
# raw name (Prefix the string with r)
>>> print('C:\some\name')
C:\some
ame
>>> print(r'C:\some\name')
C:\some\name
#Multiple lines
print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname
""")
#output
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname
#Repeat and concatenate strings
>>> 3 * "un" + "ium"
'unununium'
#Automatic concatenation of string literals(Cannot be used with variables)
>>> 'Py' 'thon'
'Python'
# index
>>> word[0]
'P'
>>> word[-1]
'n'
# slice
>>> word[:3]
'Pyt'
>>> word[3:]
'hon'
# len
>>> len(word)
6
point
.append
#list
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f']
# index
>>> letters[1]
'b'
# slicing
>>> letters[3:]
['d', 'e', 'f']
# append
>>> letters.append('g')
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
#Substitution to slicing
>>> letters[3:5] = ['D', 'E']
>>> letters
['a', 'b', 'c', 'D', 'E', 'f', 'g']
# len
>>> len(letters)
7
#Nested list
>>> array = [['a', 'b', 'c'], [1, 2, 3]]
#Fibonacci series
#Multiple assignment is possible in this way
a, b = 0, 1
while b < 10:
print(b)
a, b = b, a+b
name = 'Kawausomando'
#Specify multiple values(Space is given between)
print('My name is', name)
#output: My name is Kawausomando
#end keyword
print(name, end=' is my name')
#output: Kawausomando is my name
#sep keyword
print(1, 2, 3, sep=' and ')
#output: 1 and 2 and 3
Recommended Posts