python基礎知識総括試験用期末復習
62886 ワード
試験用.
復習方法:idleについて負けます.
python試験の基礎知識の総括第一章コンピュータ基礎及びpython概要 第2章簡単なプログラムを書く 1、識別子および命名規則 2、変数と付与文 3、入出力 4、数値 5、文字列 第三章プログラムフロー制御 1、条件式 2、選択構造 3、循環構造 4、random 第4章リストとメタグループ 第一章コンピュータ基礎及びpython概要
1、コンピュータ構成演算器:データ計算と論理判断 コントローラ:計算処理のフロー制御 メモリ:格納データおよび命令 2、プログラミング言語マシン言語:バイナリ アセンブリ言語:アシスタント 高度言語:自然言語 3、プログラム実行方式コンパイル:高度なソースコードをマシンターゲットコードに変換し、一度に翻訳し、コンパイラ は不要解釈:逐条変換、逐条実行 第二章簡単なプログラムの作成
1、識別子及び命名規則
ルール1:アルファベットまたは''先頭、後ろは英数字または下線のみのルール2:大文字と小文字の区別ルール3:キーワードは変数名として使用できません
変数名:アルファベット/アンダースコア+数値/アルファベット/アンダースコア
名前を付ける
例
合法的
x, num, _x ,aA
不法
2a, a-b, a b
キーワード
2、変数と代入文
1、type():データ型を判断する
2、変数は先に値を割り当ててからアクセスする必要があります.そうしないと、エラーが発生します.
3、賦課方式
3、入力と出力
1、入力文input()
2、出力文print()
パラメータ
意味
value
出力内容
sep
間隔モード、デフォルトのスペース
end
終了、デフォルトの改行
file
出力先、デフォルトコンソール
flush
更新を強制するかどうか、デフォルトでは更新しません
例
保存するには閉じる
ダイレクトストレージ
4、数値
数値タイプ
例
int
1
float
1.0
きほんえんざん
ビルトイン演算
mathライブラリ定数
関数#カンスウ#
5、文字列
索引とスライス
文字列演算
組み込み関数
処理方法の検索
分割処理方法その他
判断方法
レイアウト方法
formatフォーマット
きょうせいへんかん
第三章プログラムフロー制御
1、条件式
2、構造の選択
ダブルブランチ
マルチブランチ
ネスト
3、循環構造
for
while
break&continue
ループネスト
4、random
第四章リストとメタグループ
復習方法:idleについて負けます.
python試験の基礎知識の総括
1、コンピュータ構成
1、識別子及び命名規則
ルール1:アルファベットまたは''先頭、後ろは英数字または下線のみのルール2:大文字と小文字の区別ルール3:キーワードは変数名として使用できません
変数名:アルファベット/アンダースコア+数値/アルファベット/アンダースコア
名前を付ける
例
合法的
x, num, _x ,aA
不法
2a, a-b, a b
キーワード
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
2、変数と代入文
1、type():データ型を判断する
>>> m1 = 'a'
>>> type(m1)
<class 'str'>
2、変数は先に値を割り当ててからアクセスする必要があります.そうしないと、エラーが発生します.
>>> w
Traceback (most recent call last):
File "" , line 1, in <module>
w
NameError: name 'w' is not defined
>>> w = 100
>>> w
100
3、賦課方式
#
>>> a = 100
>>> b, c = 200, 300
>>> d = e = f = 100
>>> b, c = c, b
#
>>> a, b = 100
>
Traceback (most recent call last):
File "" , line 1, in <module>
a, b = 100
TypeError: cannot unpack non-iterable int object
3、入力と出力
1、入力文input()
>>> input()
3
'3'
>>> input(' :')
:3
'3'
2、出力文print()
パラメータ
意味
value
出力内容
sep
間隔モード、デフォルトのスペース
end
終了、デフォルトの改行
file
出力先、デフォルトコンソール
flush
更新を強制するかどうか、デフォルトでは更新しません
例
>>> print(3)
3
>>> print(3, 4, sep = '-')
3-4
>>> print(3, 4, end= '--')
3 4--
>>> print(3, 4, end= '--');print(4)
3 4--4
>>> print(3, 4);print(4)
3 4
4
保存するには閉じる
>>> file = open('test.txt', 'w')
>>> print(1, file=file)
>>> file.close()
ダイレクトストレージ
>>> file = open('test.txt', 'w')
>>> print(1, file=file, flush=True)
4、数値
数値タイプ
例
int
1
float
1.0
きほんえんざん
>>> 1+2
3
>>> 3-2
1
>>> 3*2
6
>>> 10/4
2.5
>>> 10//4
2
>>> 10%4
2
>>> 2**2
4
ビルトイン演算
>>> abs(-3)
3
>>> divmod(5, 2)
(2, 1)
>>> pow(2, 3)
8
>>> round(3.444, 2)
3.44
>>> max(1,2,3,4)
4
>>> min(1,2,3,4)
1
mathライブラリ定数
>>> math.inf
inf
>>> math.nan
nan
>>> math.pi
3.141592653589793
>>> math.e
2.718281828459045
関数#カンスウ#
>>> from math import *
>>> fabs(-3)
3.0
>>> fmod(6,4)
2.0
>>> fsum([1,2,2,3,4])
12.0
>>> gcd(232, 1298)
2
>>> trunc(3.332)
3
>>> modf(3.332)
(0.33199999999999985, 3.0)
>>> ceil(3.112)
4
>>> floor(3.121)
3
>>> factorial(4)
24
>>> pow(3,4)
81.0
>>> exp(2)
7.38905609893065
>>> sqrt(9)
3.0
>>> log(e)
1.0
>>> log2(4)
2.0
>>> log10(100)
2.0
>>> degrees(pi)
180.0
>>> radians(180)
3.141592653589793
>>> hypot(1,1)
1.4142135623730951
>>> sin(pi)
1.2246467991473532e-16
>>> cos(pi)
-1.0
>>> tan(pi)
-1.2246467991473532e-16
>>> asin(pi)
>>> asin(1)
1.5707963267948966
>>> acos(1)
0.0
>>> atan(1)
0.7853981633974483
5、文字列
>>> '1'
'1'
>>> " '1' "
" '1' "
>>> '''
1
2
3
'''
'
1
2
3
'
>>> """
123
'123'
123
"""
"
123
'123'
123
"
索引とスライス
>>> a = '12345'
>>> a[1:]
'2345'
>>> a[-3:]
'345'
>>> a[3]
'4'
>>> a[:]
'12345'
>>> a[1:3]
'23'
>>> a[::-1]
'54321'
>>> a[::-2]
'531'
>>> a[::2]
'135'
>>> a[1::2]
'24'
>>> a[1:3:2]
'2'
文字列演算
>>> a = '123'
>>> b = 'abc'
>>> a+b
'123abc'
>>> b*2
'abcabc'
>>> '1' in a
True
>>> 'd' in b
False
組み込み関数
>>> a = '12345'
>>>> b = 12
>>> len(a)
5
>>> str(b)
'12'
>>> chr(65)
'A'
>>> ord('3')
51
>>> hex(11)
'0xb'
>>> oct(3)
'0o3'
処理方法の検索
>>> w = 'abcda'
>>> w.find('a')
0
>>> w.rfind('a')
4
>>> w.find('1')
-1
>>> w.index('a')
0
>>> w.rindex('a')
4
>>> w.index('1')
Traceback (most recent call last):
File "" , line 1, in <module>
w.index('1')
ValueError: substring not found
>>> w.count('a')
2
分割処理方法その他
>>> w = 'a,b,c,d,e'
>>> w.split(',')
['a', 'b', 'c', 'd', 'e']
>>> w.split(',',1)
['a', 'b,c,d,e']
>>> w.rsplit(',',1)
['a,b,c,d', 'e']
>>> w = ['a', 'b', 'c']
>>> ''.join(w)
'abc'
>>> '-'.join(w)
'a-b-c'
>>> w = 'a A Aaa AAA aaa'
>>> w.lower()
'a a aaa aaa aaa'
>>> w.upper()
'A A AAA AAA AAA'
>>> w.capitalize()
'A a aaa aaa aaa'
>>> w.title()
'A A Aaa Aaa Aaa'
>>> w.swapcase()
'A a aAA aaa AAA'
>>> w = 'acccddd'
>>> w.replace('a', 'd')
'dcccddd'
>>> w = '===abc==='
>>> w.strip('=')
'abc'
>>> w.rstrip('=')
'===abc'
>>> w.lstrip('=')
'abc==='
判断方法
>>> w = 'abc'
>>>> w.startswith('a')
True
>>> w.endswith('c')
True
>>> 'a'.islower()
True
>>> 'A'.isupper()
True
>>> '2'.isdigit()
True
>>> 'Aa1'.isalnum()
True
>>> 'Aa'.isalpha()
True
レイアウト方法
>>> 'AAA'.center(5, '-')
'-AAA-'
>>> 'AAA'.ljust(5, '-')
'AAA--'
>>> 'AAA'.rjust(5, '-')
'--AAA'
>>> 'AAA'.zfill(5)
'00AAA'
formatフォーマット
>>> '{}-{}'.format(1, 2)
'1-2'
>>> '{1}-{0}'.format(1, 2)
'2-1'
>>> '{:=^10}'.format('a')
'====a====='
>>> '{:=<10}'.format('a')
'a========='
>>> '{:=>10}'.format('a')
'=========a'
>>> '{:.2f}'.format(3.333)
'3.33'
きょうせいへんかん
>>> int(3.3)
3
>>> float(2)
2.0
>>> int('3')
3
第三章プログラムフロー制御
1、条件式
>>> a = 10
>>> b = 20
>>> a==b
False
>>> a!=b
True
>>> a>b
False
>>> a<b
True
>>> a>=b
False
>>> a<=b
True
>>> 0<a<b
True
>>> ord('a')
97
>>> ord('A')
65
>>> 'a'>'A'
True
>>> a<15 and b<15
False
>>> a<15 or b<15
True
>>> not(a<15 or b<15)
False
2、構造の選択
ダブルブランチ
if True:
pass
else:
pass
マルチブランチ
if True:
pass
elif True:
pass
else:
pass
ネスト
if True:
if True:
pass
else:
pass
else:
pass
3、循環構造
for
for i in range(3):
print(i)
0
1
2
for i in 'abc':
print(i)
a
b
c
while
while True:
break
break&continue
for i in range(10):
if i>5:
break
else:
continue
ループネスト
for i in range(5):
for j in range(i):
j
0
0
1
0
1
2
0
1
2
3
4、random
>>> random()
0.6479211824993433
>>> randrange(3)
1
>>> randrange(3,7)
3
>>> randrange(3,7,2)
5
>>> randint(3,5)
5
>>> choice([1,2,3])
1
>>> uniform(3,4)
3.3340976283674633
>>> sample([1,2,3,4,5], 4)
[5, 2, 3, 1]
>>> a = [1,2,3,4,5,6]
>>> shuffle(a)
>>> a
[5, 4, 1, 3, 2, 6]
seed(123)
第四章リストとメタグループ