人生苦短,learnpythonfromzero
python ---来自bbc的喜剧 ,创始人荷兰人Guido van Rossum
-
pip -v 看版本
https://pypi.org/ :搜索你想要的库 pip freeze > req 把以前包的版本限制住 pip intall -r req 包按照以前版本安装
-
jupyter: a添加行,shift+enter运行,m--markdown 格式
-
计算机5大部件:cpu(运算器和存储器组成)运算器 控制器 存储器 输入输出设备
cpu运算只与内存交互,并不是直接来自硬盘,cpu中有寄存器和多级缓存(1,2级(共享),3级缓存(自己用))Cache
类型type
type('1') 类型
type('1')('123')==> '123'
type(obj) 返回类型,而不是字符串
type(1 + True) ==> int
type(1 + False + 2.0) ==> float
isinstance(obj, str) instance返回bool值
isinstance(obj, (str, bool, int))
a = 123 s5 = f'{a}' #格式字符串,插值字符串
In [1]: a = 123
In [2]: s5 = f'{a}'
In [3]: s5
Out[3]: '123'
^(相同取0不同取1)与和或 逻辑/算数运算符 8421了解一下
In [31]: 10^-7
Out[31]: -13
01010 10
0111 7
11001 -7补码(取反+1)
10011 和10^ 计算机看
11101 取补码给人看
1&奇数等于1,1&偶数等于0,奇数二进制数尾数为1,偶数为0,&相当于乘法,|相当于加法 1055 & 1 ==> 1 1204 & 1 ==> 0 and:两真则真,返回true or:一真则真,如果前面真,不用看后面,反之看后面
12 and 3
3
9 > 10 and 1
False
9 < 10 and 1
1
-1 or 100
-1
9 > 10 or 1
1
9 < 10 or 1
True
-r -R 转义字符 eg: s1 = r'abc\n'
In [5]: s1 = r'abc\n'
In [6]: s1
Out[6]: 'abc\\n'
In [7]: print(s1)
abc\n
s2 = R"ABC"
In [8]: s2 = R"ABC"
In [9]: s2
Out[9]: 'ABC'
In [10]: print(s2)
ABC
\t制表符 \续行符 \n换行符 eg: print('1234567\t4\t56\t0')
In [11]: print('1234567\t4\t56\t0')
1234567 4 56 0
print('1234567\t4\n56\t0')
In [12]: print('1234567\t4\n56\t0')
1234567 4
56 0
print('1234567\t4\n56\t0
\n123\t5678')
正常除法 eg: 1 / 2
In [17]: 1 / 2
Out[17]: 0.5
取整 eg: 1 // 2
In [18]: 4 // 3
Out[18]: 1
取模数(余数)eg: 5 % 2 2 % 4
In [21]: 5 % 2
Out[21]: 1
In [22]: 2 % 4
Out[22]: 2
平方 eg: 2**3
In [23]: 2**3
Out[23]: 8
开方 eg: 2**0.5
In [24]: 2**0.5
Out[24]: 1.4142135623730951
开三次方 a**(1/r)
需要开a的r次方则pow(a, 1/r) eg: 8**(1/3)
In [26]: 8**(1/3)
Out[26]: 2.0
转换成2进制函数 eg: bin(32)
In [27]: bin(32)
Out[27]: '0b100000'
移位运算 eg: 32 >> 3
In [28]: 32 >> 3
Out[28]: 4
2**3==2 << 2
In [29]: 2**3
Out[29]: 8
In [30]: 2 << 2
Out[30]: 8
- 安装python3、pyenv、ipython、jupyter
安装在普通用户下0-1在root下,其他一律在普通用户下
0 yum install git
1 yum -y install gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel
2 wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz(下载)
3 tar xf Python-3.6.8.tgz(解压)------------解压后cd Python-3.6.8
4 ./configure --prefix=/usr/local/python3.6.8(执行编译检查)
5 make(编译)//make && make install (-v)
6 make install(安装)
7 vi /etc/profile.d/python.sh(创建配置文件来更改使用哪个python)
#!/bin/bash(禁用默认)
export PATH=/usr/local/python3.6.8/bin:$PATH(添加当前路径,保留默认路径)
8 source /etc/profile.d/python.sh(重新执行环境变量文件使配置生效)
pyenv
9 git clone https://github.com/pyenv/pyenv.git ~/.pyenv
10 cd ~/.bash_profile(~表示当前目录下.pyenv是自定义文件夹名字,总体意思是下载到当前目录下的.pyenv文件夹)
11 echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile(变量写入.bash_profile)
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile(同上)
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile(同上)
12 source ~/.bash_profile
13 pyenv 看一下版本(新开一个用户,已登录用户不可以,已登录用户source一下就可以了)
14 pyenv install -l 看可安装的python版本
15 pyenv install 3.6.8 -v/pyenv install 3.7.2 -v
16 pyenv versions(显示系统上所有版本
16.1 mkdir py368/cd py368/pyenv local 3.6.8/python -V
pyenv shell 版本名(改变当前窗口的版本,system是默认版本的意思)
pyenv global 版本名(全局改变版本,不建议)
pyenv local 版本名(改变当前路径的版本)
17 git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv(下载虚拟隔离工具,不加文件夹地址就下载到当前文件夹)
18 echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile(变量写入.bash_profile)
19 source ~/.bash_profile(重新执行环境变量文件使配置生效)
20 pyenv virtualenv 3.6.8 mz368(起一个新名字) 然后看一下版本pyenv versions
21 pyenv local ma368
22 pip install ipython(安装ipython)
pip install jupyter(安装jupyter)
iptables -F(暂时关闭防火墙)
jupyter notebook --ip=0.0.0.0 --no-browser --allow-root(启动jupyter)
输入随机生成的密码
while 循环一般用于不知道有多少次循环
range用法
range: 可迭代对象,range(1, 10)----[0, 10),前包后不包
for i in range(10):
print(i + 1, end=' ')
for i in range(10):
print('j = ', i)
range(1, 5, 2) 2为步长
for i in range(1, 5, 2):
print(i)
3
5
range(5, 1, -2)
for i in range(5, 1, -2):
print(i)
5
3
倒序打印
for i in range(9, -1, -1):
print(i)
9
8
7
6
5
4
3
2
1
0
range?----------ipython中显示详细用法
Init signature: range(self, /, *args, **kwargs)
Docstring:
range(stop) -> range object
range(start, stop[, step]) -> range object
Return an object that produces a sequence of integers from start (inclusive)
to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1.
start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3.
These are exactly the valid indices for a list of 4 elements.
When step is given, it specifies the increment (or decrement).
Type: type
continue、break用法 continue影响当前循环,break终止这一次循环 for i in range(n): #break else: pass for else只要for循环中没有breakelse就执行
# 计算1-10偶数
for i in range(10):
if not i % 2: #if not 0
print(i)
for i in range(10, 2):
print(i)
for i in range(10):
if not i & 0x1:
print(i)
# 使用while
s = 1
while s:
s += 1
if s % 2:
continue
print(s)
if s >= 8:
break
内存管理
内存是线性编制的, 为什么要内存清理,就是为了有大量连续的内存供给我们使用,回收引用计数为零的内存,减少碎片(强制改变已住数据)和空洞(非连续的内存);使用计算来记录所有对象的引用数,有时候会有a记住b,b记住a,这时需要GC循环引用检测来处里。
(1)对象引用计数为0的被垃圾回收GC (2)计算增加:1.赋值给变量增加引用数 eg: x=3; y=x; z=[x, 1] 2.实参传参 foo(y) (3)计算减少:1.函数运行结束时局部变量减少,对象引用计数减少 2.变量被赋值给其他对象x=3;y=x;x=4