for文
list = ['Alice', 'Bob', 'Charlie']
for name in list:
print(name)
for name in list:
if name == 'Bob':
break
print(name)
for name in list:
if name == 'Bob':
continue
print(name)
for i in range(3):
print(i)
for i in range(2,5):
print(i)
for i in range(0,10,3):
print(i)
names = ['Alice', 'Bob', 'Charlie']
ages = [24, 50, 18]
for name, age in zip(names, ages):
print(name, age)
if文
num = int(input())
if num == 100:
print('満点!')
elif num >= 90:
print('たいへんよく出来ました!')
elif num >= 80:
print('よく出来ました!')
else:
print('がんばりましょう')
リストの宣言
columns = []
リストへの要素の追加
for i in range(1,4):
columns.append("mean%d"%i)
print(columns)
<実行結果>
['mean1', 'mean2', 'mean3', 'mean4']
リストをpandas.DataFrame , pandas.Seriesに変換
list = [[2 , 3 , 5 , 4],[1 , 4 , 2 , 9],[8 , 7 , 5 , 3]]
Fd = pd.DataFrame(list , columns=columns)
type(Fd)
Fd
<実行結果>
pandas.core.frame.DataFrame
mean1 mean2 mean3 mean4
0 2 3 5 4
1 1 4 2 9
2 8 7 5 3
pandas.DataFrame , pandas.Seriesをリストに変換
Fl = Fd.values.tolist()
type(Fl)
Fl
<実行結果>
list
[[2, 3, 5, 4], [1, 4, 2, 9], [8, 7, 5, 3]]
小数第2位を四捨五入
list = [2 , 3 , 5]
print(np.mean(list),"\n")
print(np.round(np.mean(list),2))
<実行結果>
3.3333333333333335
3.33
対数関数
print('log2(4)=',np.log2(4),'\n')
print('log2(0.25)=',np.log2(0.25),'\n')
print('log10(100)=',np.log10(100),'\n')
rint('log10(0.001)=',np.log10(0.001),'\n')
<実行結果>
log2(4)= 2.0
log2()= -2.0
log10(100)= 2.0
log10(0.001)= -3.0
DataFrameのCSV出力
Fd.to_csv(path_or_buf="パスとファイル名.csv")
置換フォーマット
human = ['Tom','John','Emi']
for i in human:
print('Hello ,',i,'.')
<実行結果>
Hello , Tom .
Hello , John .
Hello , Emi .
human = ['Tom','John','Emi']
for i in range(3):
print('Hello , %s'%human[i],'.')
<実行結果>
Hello , Tom .
Hello , John .
Hello , Emi .
human = ['Tom','John','Emi']
age = [24 , 22 , 18]
for i in range(3):
print('%s is %d years old .'%(human[i],age[i]))
<実行結果>
Tom is 24 years old .
John is 22 years old .
Emi is 18 years old .