1.python介绍
python的介绍
编程语言类型
编译型:C、c++
动静态:对数据类型做检查的时间,运行时检查为动态,不需指明数据类型;编译期检查为静态
Python:动态解释强类型
IDE:集成开发环境 Integrated Development Environment
2.python 基础
2.1 print() 输入
print("a")
2.2 注释
-
单行注释
#井号
-
多行注释
''' 三单引号 ''' """ 三双引号 """
2.3 input()输出
a=input()
2.4 type() 查看数据类型
type(a)
#内存地址
id(a)
2.5 python常见数据类型
-
数字(number)
- 整型 int
- 浮点型 float
- 布尔型 bool
- 复数 complex
-
字符串 string
-
列表 list
-
元组 tuple
-
字典 dict
-
集合 set
2.6 变量和常量
1.变量
-
概念
- 程序可操作的存储区名称
- 程序运行时存储区能改变的数据
-
作用
- 数据存储到内存
-
命名
-
必须是字母、下划线、数字组合
-
不能以数字开头
-
不可以是python中的关键字
-
注意:
- 见名知意
- 区分大小写
- 单双下划线开头的变量有特殊的通途
-
定义
- 关键字
>>> 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.常量
不变的量 : 通常用大写
#例如
PI=3.1415926
#互换
x=123
y=456
x,y=y,x
2.7数字函数
1.种类
-
int
-
32bit 范围 -2^31 2^31-1
-
64bit 范围 -2^63 2^63-1
现在不分长整型 短整型
-
# ** 代表幂
-
float
带小数点的数字
-
复数(complex)
>>> c=complex(1,2) >>> c (1+2j) #返回实部 >>> c.real 1.0 #返回虚部 >>> c.imag 2.0 #共轭复数 >>> c.conjugate() (1-2j)
2.常见数学函数
函数名 | 描述 |
---|---|
abs(x) | 绝对值 |
pow(x,y) | x^y |
round(x,[n]) | 返回四舍五入值,n表小数点位数 |
math.exp(x) | 返回e的x次幂 |
math.fabs(x) | 返回绝对值,浮点类型 |
math.ceil(x) | 返回上入整数 |
math.floor(x) | 返回下舍整数 |
math.log(x,[y]) | 单参数,默认以e为底;双参数以[y]为底 |
math.sqrt(x) | 平方根 |
max(x1,x2,x3…) | 返回最大值 |
min(x1,x2,x3…) | 返回最小值 |
2.8表达式与运算符
-
表达式
由变量,常量,运算符组成的式子
-
运算符
-
算术运算符
-
比较运算符
-
逻辑运算符
-
赋值运算符
-
成员运算符
-
身份运算符
-
位运算符
-
python 算术运算符
运算符 | 描述 | 实例 |
---|---|---|
** | 幂运算 | a**b |
% | 取余 | a%b |
// | 整除 | a//b |
divmod(x,y)
取商和余
>>> divmod(23,5) (4, 3)
运算符 | 描述 |
---|---|
& | 与 同为1才是1 否则为0 |
| | 或 有1即是1 |
^ | 异或 不同则为1 |
~ | 取反 ~x得到-x-1 |
<< | 左移运算符 |
>> | 右移运算符 |
>>> a,b=15,30
>>> bin(a)
'0b1111'
>>> bin(b)
'0b11110'
>>> a&b
14
>>> a|b
31
>>> a^b
17
>>> ~a
-16
#a<<x a向左移x位(二进制)
成员运算符
运算符 | 描述 |
---|---|
in | 存在返回ture 否则返回false |
not in | 不存在返回ture 否则返回false |
>>> a="inlix"
>>> "i" in a
True
身份运算符
运算符 | 描述 |
---|---|
is | 判断两个标识符是不是引用自同一个对象 |
not is | 判断两个标识符是不是引用自同一个对象 |
>>> id(a)
20329536
>>> id(b)
263743024
>>> a is b
False
逻辑运算符
and or not
2.9强制转换
语法 | 描述 |
---|---|
int(x,[base]) | 强转为整型,如果x为字符串,要base基数 |
float() | 强转为浮点类型 |
str() | 强转为字符串 |
repr() | 将对象转换为表达式字符串 |
list() | |
tuple() | |
dict() | |
set() | |
chr() | 将整数转化为字符 |
ord() | 将字符转化为数值 |
bin() | 转二进制 |
oct() | 转八进制 |
hex() | 转十六进制 |
>>> sr="123"
>>> int(sr)
123
>>> int(sr,8)
83
>>> 8**2+2*8+3
83
2.10 随机函数
import random
常见功能
-
random.random()
import random for i in range(100): a=random.random() a=random.uniform(6,8)#浮点范围 print(a,end=" ") #6.236724369468372 7.072374050015673
-
random.randint()
import random for i in range(100): a=random.random() a=random.randint(6,8) #整数范围 print(a,end=" ") #6 7 6 8 8 7 8 8 6 8 8
-
random.randrange()
#从指定范围内,按照指定基数递增或递减的集合中,随机选
import random for i in range(100): a=random.randrange(1,16,2) #从指定范围内,按照指定基数递增或递减的集合中,随机选 print(a,end=" ") #15 7 5 3 9 1 9 7 9 1 9 13 1 13 3 15 9 7 3 5 7
-
random.choice()
从序列中随机获取一个元素
import random for i in range(100): a=random.choice(range(1,16,2)) print(a,end=" ")
-
random.shuffle()
将列表元素打乱
import random for i in range(100): li=["a","b","c"] random.shuffle(li) print(li,end=" ")
-
random.sample()
从指定序列中随机获取指定长度的片段并随机排序
import random li = ["a", "b", "c"] for i in range(3): print(random.sample(li,3))