1 |
型、変数、型変換 |
主な型として下記がある。これ以外にも沢山あるようだ。
型 |
import |
説明 |
数値型ー整数 |
プリミティブ型 |
a = 10 + int(3.1) +int('2') ➡ 15
|
数値型ー浮動小数点 |
プリミティブ型 |
b = 3.14159
|
数値型ー複素数 |
プリミティブ型 |
C = 3+4j ← complex(3,4)と同じ。学校ではi=√-1と習ったが、電流iとかぶるので、jらしい
|
数値型ーbool |
プリミティブ型 |
blFlag = False; OnFlag = True ➡実は0,1
|
文字、文字列 |
プリミティブ型 |
str1 = 'yasryu\r\n';(;で区切りOK) str2 = "yas"+str(99) ➡ 'yas99'
|
シーケンス型-リスト[] list |
プリミティブ型 |
配列。
dim = [
1,10,100]
dim[2]=1000
print(dim[2]) ➡ 1000
num = [1,3,6,10,22] ←添え字は0始まり
for i in num ← i=1,3,6,10,22のループ
print(i)
print('終了') |
シーケンス型-タプル() tuple |
プリミティブ型 |
読み取り専用の配列。[]ではなく()
tup = (
2,20,200)
print(tup[2]) ➡ 200
|
マッピング型-辞書{} Dictionary |
プリミティブ型 |
キーで検索可能。map
dic = {
'mon':'月曜日','sun':'日曜日'
}
print(dic['sun']) ➡ 日曜日
|
集合型-集合 set |
プリミティブ型 |
重複を省いてくれる配列
a_set = set('アジ','ブリ','タコ','のり')
b_set = set('タコ','アジ','イカ','やす')
print(a_set & b_set) ➡アジ タコ
print(a_set | b_set) ➡アジ ブリ タコ のり イカ やす
|
日付
|
datetime |
now = datetime.datetime.now()
|
| |
5 |
制御文 |
書き方は他にもあるが、ここでは代表的なものだけ。「:」は、ブロックの始まりの合図。
if,elif,else |
aaa = input("数値を入力してください:")
if (float)aaa % 10 == 11:
out = "L1"
elif aaa >= 20 or bbb == True:
out = "L20"
else:
out = "other"
|
for,in |
num = [1,2,10]
for i
in num ←numをrange(3)とするとi=0,1,2となる
if i == 3:
continue
if i >= 8:
break
print(i)
else:
print('End')
|
while,else |
flag=False;cnt=1
while flag == False:
print(cnt)
cnt++
if cnt > 5:
flag = True
else:
print('End')
|
while in |
num = [1,2,10]
while i
in num
print(i)
print('End')
|
match ※C言語のswitch文 |
match num:
case 100:
print(100)
case 200:
print(200)
case _:
print('c言語のdefualt')
|
|
|
6 |
関数 |
自前関数 |
def AddNum(a,b):
return a+b
|
標準関数 |
【Ver3.8.1のもの】
abs(),
delattr(),
hash(),
memoryview(),
set(),
all(),
dict(),
help(),
min(),
setattr(),
any(),
dir(),
hex(),
next(),
slice(),
ascii(),
divmod(),
id(),
object(),
sorted(),
bin(),
enumerate(),
input(),
oct(),
staticmethod(),
bool(),
eval(),
int(),
open(),
str(),
breakpoint(),
exec(),
isinstance(),
ord(),
sum(),
bytearray(),
filter(),
issubclass(),
pow(),
super(),
bytes(),
float(),
iter(),
print(),
tuple(),
callable(),
format(),
len(),
property(),
type(),
chr(),
frozenset(),
list(),
range(),
vars(),
classmethod(),
getattr(),
locals(),
repr(),
zip(),
compile(),
globals(),
map(),
reversed(),
__import__(),
complex(),
hasattr(),
max(),
round()
|
外部関数 |
import outlib
import os
os.system('ls')
|
| |