Cet article est le deuxième d'une série. Vous pouvez trouver le premier ici .
2.1 Les commentaires sont indiqués par # ou '' '(trois guillemets simples)
#
a = 1 #
'''
b = c
c = d
'''
2.2 Utilisation du typage dynamique (le type est déterminé automatiquement)
#
# =
i = 1 # (int)
f = 2.1 # (float)
s = "a" # (string)
b = True # (boolean)
l = [0,1,2] # , (array)
t = (0,1,2) # (tuple)
d = {"a":0, "b":1} # ,
print(i,f,s,b,l,t,d)
# 1 2.1 a True [0, 1, 2] (0, 1, 2) {'a': 0, 'b': 1}
# , type
print(type(i)) # <class 'int'>
# ,
# , , -,
# id
print(id(l)) # 00000000000000 ( )
l2 = l # , 2 , - 1.
print(id(l2)) # 00000000000000 ( , id(l))
# , , l, l2.
l2.append(1)
2.3 ( . , , )
a = 1 # 1
b = a # b 1
a = 2 # 2, b
a += 1 # 1 ( 3)
a -= 1 # 1 ( 2)
a *= 2 # ( 4)
a /= 3 # ⅓ ( 1.3333333333333333)
a = 1+2-3*4/5 # : , , , → a 0,6. (, , 0.6000000000000001)
2.4
a = 0
# , .
# , , ..
if a == 1:
# , , .
print(1)
print(2)
pass #
# if
print(3) # , if
# 3
2.4.1
# , ,
if :
# program group
#
# v
for v in
# program group
#
while :
# program group
#
# ( )
def ():
# program group
# , return
#
return
#
# print(2)
# , program group 2
if a == 1:
# program group 1 if a==1:
print(1)
# program group 2 , .
print(2)
2.4.2
a = 1
b = 2
c = 2
# a b , a c
if a==b and a!=c:
print(" ")
# b ( =b) >c ( =)
elif a>=b or a>c:
print(" b ")
# if elif
else:
print(" ")
# " "
2.4.2.1
a = 1
b = 2
# =b, v=0, , v=1
v = 0 if a==b else 1
print(v)
# 1
2.4.3 (for)
for v in [0,1,2,3]: # [0,1,2,3] .
# . v
print(v)
pass #
# 0 1 2 3
# enumerate, .
for i,v in enumerate([5,3,7,8]):
# i - , v -
print("(",i,v,")")
# ( 0 5 ) ( 1 3 ) ( 2 7 ) ( 3 8 )
# zip .
for v0,v1 in zip([1,2,3,4],[5,6,7,8]):
# v0 zip, v1 .
print("(",v0,v1,")")
# ( 1 5 ) ( 2 6 ) ( 3 7 ) ( 4 8 )
2.4.4 while
a = 3
# a 0
while a>=0:
print(a)
# a 1
a -= 1
# 3 2 1 0
2.4.5
# : - function_name, : param1 param2, param2 -
# , , 1 ( )
def function_name(param1,param2=1):
print("p1:",param1,"p2",param2)
# param1 param2
return param1 + param2
# ( function_name)
# param1 = 5, param2 ( )
v = function_name(5)
# ( print function_name) p1: 5 p2 1
print(" ",v)()
# " 6"
2.4.5.1 -,
# = lambda ( ) : ( ) - .
f = lambda x: x*x
# , def
v = f(2)
print(v) # 4
2.5
si = "1" #
sf = "2.3" #
i = 4 #
# i ,
print(str(i)+si)
# 41
# - si (int),
print(i+int(si))
# 5
# (float), ( float)
print(i+float(sf))
# 6.3
2.6 ,
#
a = [1,1,2,3,2] # a=[1,1,2,3,2]
b = [n for n in range(8)] # b=[0, 1, 2, 3, 4, 5, 6, 7]
#
# , -1
v = a[0] # v=1
v = a[-1] # v=2
#
a += [4] # a=[1, 1, 2, 3, 2, 4]
a.append(5) # a=[1, 1, 2, 3, 2, 4, 5]
# ( a=[1, 1, 2, 3, 2, 4, 5])
v = a.pop(0) # v=1, a=[1, 2, 3, 2, 4, 5]
# ( a=[1, 2, 3, 2, 4, 5])
# a[ : ]
# , a[:], 0, - )
c = a[1:3] # c=[2, 3]
# ・ ( = [1, 2, 3, 2, 4, 5])
mx,mi = max(a),min(a) # mx=5, mi=1
# (mean)、 (median)、 (mode)、 (stdev)、(variance)
# ( a=[1, 2, 3, 2, 4, 5])
from statistics import mean,median,mode,stdev,variance
v = mean(a) # v=2.8333333333333335
v = median(a) # v=2.5
v = mode(a) # v=2
v = stdev(a) # v=1.4719601443879744
v = variance(a) #v=2.1666666666666665
# ( a=[1, 2, 3, 2, 4, 5])
c = set(a) # c={1, 2, 3, 4, 5}
# Sorted( )( a=[1, 2, 3, 2, 4, 5])
c = sorted(a) # c=[1, 2, 2, 3, 4, 5] ( a )
# Sort( )( a=[1, 2, 3, 2, 4, 5])
a.sort() # a=[1, 2, 2, 3, 4, 5]
# (mapping) ( a=[1, 2, 2, 3, 4, 5])
# - x: x+2. x x+2.
# list map
c = list(map(lambda x:x+2,a)) #c=[3, 4, 4, 5, 6, 7]
# ( a=[1, 2, 2, 3, 4, 5])
# - x: x%2==0
# x、 .
c = list(filter(lambda x:x%2==0,a)) #c=[2, 2, 4]
#
# x - ( , - )
# xi -
from functools import reduce
# x+xi, ( a=[1, 2, 2, 3, 4, 5])
c = reduce(lambda x,xi:x+xi,a) #c=17
# max(xi,x), ( a=[1, 2, 2, 3, 4, 5])
c = reduce(lambda x,xi:max(x,xi),a) #c=5
2.7 (dictionary)、
# . - , , , -
d = {
" " : [81, 60, 97, 96],
"" : [80, 78, 75, 96],
" " : [76, 85, 65, 88],
}
print(d)
# {' ': [81, 60, 97, 96], '': [80, 78, 75, 96], ' ': [76, 85, 65, 88]}
# for
d1 = {name:[0,1] for name in [" ",""," "]}
# {' ': [0, 1], '': [0, 1], ' ': [0, 1]}
#
a = d[""] # a=[80, 78, 75, 96]
# ()
d[" "] = [77, 61, 91, 87] # d[" "]=[77, 61, 91, 87]
#
for k,v in d.items():
print(k,v)
# [81, 60, 97, 96] [80, 78, 75, 96] [77, 61, 91, 87]
# 、 ( “ ”)
jmx,jmi = max(d[" "]),min(d[" "])
#print(jmx,jmi)
#
from statistics import mean
# max , ,
# - k( )
kmx = max(d,key=lambda k:mean(d[k])) # kmx=" "
2.8 with open
# with,
# with , .
# , , open, close
# with open, with close .
# mode - r: 、w: 、a:
# , open, f
with open("09_memo.txt",mode="a") as f:
#
f.write("こんにちは\n")
#
with open("09_memo.txt","r") as r:
print(r.read())
2.9 random
#
import random
#
# .
# seed , .
random.seed(0)
# 0 1.
print(random.random()) # 0.8444218515250481
# .
print(random.randint(1,3)) # 1,2,3
#
print(random.choice([0,1,2])) # 0,1,2
#
#
print(random.sample([0,1,2],2)) # [1, 2]
#
# -
a = [0,1,2]
random.shuffle(a)
print(a) # [0, 2, 1]
print(random.sample(a,len(a))) # sample
!
, , , , ?