考试用。
复习方式:跟着在idle里面输一遍。
1、计算机组成
2、程序设计语言
3、程序执行方式
规则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']
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
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)
数值类型 | 示例 |
---|---|
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
>>> '1'
'1'
>>> " '1' "
" '1' "
>>> '''
1
2
3
'''
'\n1\n2\n3\n'
>>> """
123
'123'
123
"""
"\n123\n'123'\n123\n"
索引与切片
>>> 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
>>> 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
双分支
if True:
pass
else:
pass
多分支
if True:
pass
elif True:
pass
else:
pass
嵌套
if True:
if True:
pass
else:
pass
else:
pass
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
>>> 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)