# 基本类型

### 1. Number类型

• 整数: int
• 浮点数: float(双精度)
• 其他语言: 单精度(float) 双精度(double)
• 其他语言: short int long
``````type(1)    // int
type(1.1)    // float
type(1*1)    // int
type(1+1.0)    // float
type(1*1.0)    // float
type(2/2)    // float
type(2//2)    // int
``````
``````2/2    // 1.0
2//2    // 1
1//2    // 0
``````

`/`表示将自动转化为浮点数,而`//`将进行整除,只保留整数部分

##### 10,2,8,16进制
``````十进制: 0,1,2,3...9

``````
##### 各进制的表示与转换
• 0b 表示二进制
• 0o 表示八进制
• 0x 表示十六进制
``````0b10    //2
0b11    //3
0o10    //8
0o11    //9
0x10    //16
0x1F    //31
``````
• bin 转换成2进制
• int 转换成10进制
• hex 转换成16进制
• oct 转换成8进制
``````10 -> 2
bin(10)    //0b1010
8 -> 2
bin(0o7)    //0b111
16 - > 2
bin(0xE)    //0b1110
2 -> 10
int(0b111)    //7
8 -> 10
int(0o77)    //63
10 -> 16
hex(888)    //0x378
2 -> 16
hex(0o7777)    //0xfff
2 -> 8
oct(0b111)    //0o7
``````
##### 布尔类型

T/F必须要大写

``````type(True)    // bool
int(True)    // 1
int(False)    // 0
bool(2.2)    // True
bool('abc')    // True
bool('')    // False
bool(None)    // False
``````

### 2. 字符串

``````'''
hello world
hello world
hello world
'''
``````

``````'\nhello world\nhello world\nhello world '
``````

``````print(r'c:\northwind\northwest')    // c:\northwind\northwest
``````
##### 字符串运算
``````'hello' * 3     // hellohellohello
``````
``````'hello world'[3]    //l
'hello world'[-1]    //d
``````
``````'hello world'[0:4]    //'hell'
'hello world'[0:5]    //'hello'
'hello world'[6:10]    //'worl'
'hello world'[6:11]    //'world'
'hello world'[6:]    //'world'
``````

### 列表

``````type([1,2,3,4,5,6,7])    //list
type([[1,2],True,1,2])    //list
``````
##### 列表的基本操作
``````['num1','num2','num3','num4'][0]    // num1
['num1','num2','num3','num4'][0:2]    // ['num1','num2']
['num1','num2','num3','num4'][-1:]    // ['num4']
``````

##### 合并列表
``````['num1','num2','num3','num4']+['num5','num6']    // ['num1','num2','num3','num4','num5','num6']
['num1','num2']*3    // ['num1','num2','num1','num2','num1','num2']
``````

### 元组

``````(1,2,3,4,5)
(1,'-1',True)
(1,2,3,4)[0]    // 1
type((1,2,3))    // tuple
``````

``````type((1))    // int
type(('hello'))    // str
``````

``````(1,)    // (1,)
type((1,))    // tuple
``````

### 总结

str list tuple统称为序列,他们都有序号

##### 切片
``````['num1','num2','num3','num4'][0:2]    //['num1','num2']
``````

``````3 in [1,2,3,4,5]    //True
3 not in [1,2,3,4,5]    //False
``````
##### 长度
``````len([1,2,3,4,5])     //5
len('hello world')    //11
``````
##### 大小
``````max([1,2,3,4,5])    //6
min([1,2,3,4,5])    //1
max('hello world')    //w
min('hello world')    //' '
min('helloworld')    //d
``````

``````ord('w')    //119
``````

### 集合

``````type({1,2,3,4,5,6})    //set
``````

``````{1,1,2,2,3,3,4,4}    //{1,2,3,4}
``````
##### 常用操作
``````len({1,2,3})    //3
1 in {1,2,3}    //True
1 not in {1,2,3}    //False
{1,2,3,4,5,6} - {3,4}    //{1,2,5,6}
{1,2,3,4,5,6} & {3,4}    //{3,4}
{1,2,3,4,5,6} | {3,4,7}    //{1,2,3,4,5,6,7}
``````
##### 定义空集合
``````type({})    // dict
type(set())    // set
``````

### 字典

``````type({1:1,2:2,3:3,4:4})    // dict
``````

``````{'q':'翻滚','w':'被动','e':'钉','r':'大招'}['q']    // '翻滚'
``````

• value: str int float list set dict
• key: 必须是不可变的类型 int str tuple
image.png

# 变量

### 变量命名规则

``````a = '1'
a = 1
``````
``````a = 1
b = a
a = 3
print(b)    //1
``````
``````a = [1,2,3,4,5]
b = a
a[0] = '1'
print(a)    //['1',2,3,4,5]
print(b)    //['1',2,3,4,5]
``````

### 值类型和引用类型

``````type = 1
type(1)    //报错 'int' object is not callable
``````
``````a = [1,2,3,4,5]
b = a
a[0] = '1'
print(a)    //['1',2,3,4,5]
print(b)    //['1',2,3,4,5]
``````

int str tuple 值类型(不可改变) list set dict 引用类型(可变)

int 值类型 list 引用类型.那么当a = 1 b = a,b也被赋值为1, a = 3,那么a将变为3,但是b仍然是1,值类型是不可变的,如果变量发生了改变,将会赋值为一个新的值类型.引用类型则不同,当引用类型改变的时候,a和b将同时发生变化.

``````a = 'hello'
id(a)    //55299200 内存地址
a = a + 'python'
id(a)    //5536696 新的内存地址
``````
``````'python'[0] = 'a'    //报错,字符串是不可改变的类型
``````

### 列表的可变与元组的不可变

``````>>> a = (1,2,3)
>>> a[0]
1
>>> a[0] = 22
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
a[0] = 22
TypeError: 'tuple' object does not support item assignment
>>> b = [1,2,3]
>>> b[0] = 444
>>> b
[444, 2, 3]
>>>
``````

# 运算符

image.png

image.png

+ - * / // % **

### 赋值运算符

= += *= /= **= //=

### 比较(关系)运算符

== != > < >= <=

``````b = 1
b+=b>=1    //2
``````

``````b = b + b>=1
b = b + True
b = b + 1
b += 1
``````
##### 不只是数字才能作比较运算
``````'a' > 'b'    //False    97 > 98
'abc' < 'abd'   //True
``````

### 逻辑运算符

and or not 主要用来操作bool类型

``````>>> False and True
False
>>> False or True
True
>>> not False
True
>>> not not True
True
>>> 1 and 1
1
>>> 'a' and 'b'
'b'
>>> 'a' or 'b'
'a'
>>> not 'a'
False
``````

0被认为是False, 非0表示True

``````not 0.1    //False
``````

``````not ''    //True
``````

``````not []    //True
``````

and 和 or 如何返回出来结果,要看能否把这个逻辑运算的结果计算出来

``````1 and 0    //0
0 and 1    //0
1 and 2    //2
0 or 1    //1
1 or 0    //1
``````

### 成员运算符

in 和 not in 判断一个元素是否在一个组里,返回的是bool

``````a = 1
a in [1,2,3,4,5]    //True
b = 6
b not in [1,2,3,4,5]    //True
b not in (1,2,3,4,5)    //True
b not in {1,2,3,4,5}    //True
``````

``````b = 'a'
b in {'c' : 1}    //False
b = 1
b in {'c' : 1}    //False
b = 'c'
b in {'c' : 1}    //True
``````

### 身份运算符

is 和 is not 同样是返回bool

``````a = 1
b = 2
a is b    //False
``````
``````a = 1
b = 1
a is b    //True
``````
``````a = 1
b = 1.0
a == b    //True
a is b    //False
``````

is 是比较这个内存地址是否相等,而==比较的是否值是否相等

``````a = {1,2,3}
b = {2,1,3}
a == b    //集合是无序 True
a is b    //比较的是内存地址 False
``````
``````c = (1,2,3)
d = (2,1,3)
c == d    //元组是序列有序的,不可变 False
c is d    //False
``````
##### 判断类型
``````a = 'hello'
isinstance(a,str)    //True
isinstance(a,(str,int,float))    //True
``````

### 位运算符

& 按位与 | 按位或 ^ 按位异或 ~ 按位取反 << 左移动 >> 右移动

##### 按位与
``````a = 2
b = 3
a & b    //2
``````

1 0
1 1
1 0

##### 按位或
``````a = 2
b = 3
a | b    //3
``````

1 0
1 1
1 1

image.png

# 开发环境和插件安装

### VSCode 插件

Python Terminal View In Browser Vim vscode-icons

# 条件控制语句

### if

``````mood = False
if mood:
print('go to left')
else:
print('go to right')
``````

### elif

``````a = input()
if a == 1:
print('one')
elif a == 2:
print('two')
elif a == 3:
print('three')
elif a == 4:
print('four')
else:
print('other')
``````

### while

``````counter = 1
while counter <= 10:
counter += 1
print(counter)
else:
print('EOF')
``````

### for与for-else

``````a = ['apple','orange','banana','grape']

for x in a:
print(x)
``````

else在循环遍历完成之后执行

``````a = ['apple','orange','banana','grape']

for x in a:
print(x)
else:
print('fruit is gone')
``````

break跳出循环

``````b = [1, 2, 3]

for x in b:
if x == 2:
break
print(x)
else:
print('EOF')
``````

``````for x in range(0, 10):
print(x)    //0,1,2,3,4,5,6,7,8,9
``````

``````for x in range(0, 10, 2):
print(x, end=' | ')
``````

``````for x in range(10, 0, -2):
print(x, end=' | ')
``````

``````a = [1, 2, 3, 4, 5, 6, 7, 8]

for i in range(0, len(a), 2):
print(a[i], end=' | ')
``````

``````a = [1, 2, 3, 4, 5, 6, 7, 8]

b = a[0:len(a):2]
print(b)
``````

# 包,模块,类

### 包

__init__.py

##### 包的导入
``````import t.c7
print(t.c7.a)
``````

``````import t.c7 as m
``````
##### 导入具体的变量
``````from t.c7 import a
``````

``````from t.c7 import *
``````

``````__all__ = ['a','b']    //全部导入时只导入a,b两个变量
``````

``````from c9 import a, b, c
``````

``````from c9 import a, b,\
c
``````

``````from c9 import (a, b,
c)
``````

### __init__.py的用法

``````__all__ = ['c7']
``````

### 模块内置变量

##### dir()

``````a = 2
b = 3

infos = dir()
print(infos)
``````

``````'''
hello
'''
print('name:' + __name__)
print('package:' + __package__)
print('doc' + __doc__)
print('file:' + __file__)
``````
``````wangyukundeMacBook-Air:python wangyukun\$ python3 a1.py
name:c.c4
package:c
doc
hello

file:/Users/wangyukun/Desktop/python/c/c4.py
``````

### __name__的经典应用

``````if __name__ == '__main__':
print('this is app')
print('this is a module')
``````

### 相对导入和绝对导入

##### 绝对路径导入
``````// main.py
import package2/package4.m2
``````
##### 相对路径导入
``````// m2.py
from .m3 import m
``````

# 函数

### 推荐阅读更多精彩内容

• 〇、前言 本文共108张图，流量党请慎重！ 历时1个半月，我把自己学习Python基础知识的框架详细梳理了一遍。 ...
Raxxie阅读 12,679评论 15 381
• 一、Java 简介 Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计...
子非鱼_t_阅读 2,779评论 1 44
• 第六章：数字 第七章：序列，列表，元组 1.序列 序列类型有着相同的访问模式：它的每一个元素可以通过指定一个偏移量...
m风满楼阅读 220评论 0 2
• 沿途中漫山遍野的油菜花 一片片闪过火车的尾巴 这个季节油菜都开花了 而我还是一个人…… 看着沿途的风景…… ...
蘇欠阅读 25评论 0 1
• 为什么天天这么晚都睡不着，是想的太多，还是白天睡多了，一直想改都改不了熬夜的习惯
笑笑香心阅读 22评论 0 0