yum install git -y
yum -y install gcc make patch gdbm-devel openssl-devel sqlite-devel readline readline-devel readline-static
zlib-devel bzip2-devel
useradd python
[root@PanQi 15:31:10 ~]#su - python
[python@PanQi 15:37:34 ~]$curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
结果网不行..NND
方法二:
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
在python用户的~/.bash_profile中追加
export PYENV_ROOT=/home/python/.pyenv
export PATH="/home/python/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
[python@PanQi 16:27:36 ~]$pyenv
pyenv 1.2.5-4-gc057a80
Usage: pyenv []
Some useful pyenv commands are:
commands List all available pyenv commands
local Set or show the local application-specific Python version
global Set or show the global Python version
shell Set or show the shell-specific Python version
install Install a Python version using python-build
uninstall Uninstall a specific Python version
rehash Rehash pyenv shims (run this after installing executables)
version Show the current Python version and its origin
versions List all Python versions available to pyenv
which Display the full path to an executable
whence List all Python versions that contain the given executable
See `pyenv help ' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
[python@PanQi 16:30:49 ~]$python -V
Python 2.6.6
[python@PanQi 16:33:35 ~]$pyenv versions
* system (set by /home/python/.pyenv/version)
[python@PanQi 17:23:39 ~]$pyenv install 3.5.3 -v
[python@PanQi 17:28:12 ~]$pyenv versions
* system (set by /home/python/.pyenv/version)
3.5.3
[python@PanQi 17:28:21 ~]$pyenv global 3.5.3
[python@PanQi 17:29:10 ~]$pyenv versions
system
* 3.5.3 (set by /home/python/.pyenv/version) 多个CRT标签页上都变为3.5.3,因为这个定义的global全局的
[python@PanQi 17:29:15 ~]$python -V
Python 3.5.3 python自己也显示为3.5.3
global 3.5.3
[python@PanQi 17:29:10 ~]$pyenv versions
system
* 3.5.3 (set by /home/python/.pyenv/version) 多个CRT标签页上都变为3.5.3,因为这个定义的global全局的
[python@PanQi 17:29:15 ~]$python -V
Python 3.5.3 python自己也显示为3.5.3
这样不好,影响全局。
[python@PanQi 17:30:56 ~]$pyenv global system 还变成系统默认的
[python@PanQi 17:34:07 ~]$pyenv versions
* system (set by /home/python/.pyenv/version)
3.5.3
[python@PanQi 17:34:10 ~]$pyenv shell 3.5.3 会话级别的,也不好用
[python@PanQi 12:53:07 ~]$mkdir panqi/projects -p
[python@PanQi 12:53:32 ~]$cd panqi/projects/
[python@PanQi 12:53:39 projects]$pwd
/home/python/panqi/projects
[python@PanQi 12:53:42 projects]$pyenv local 3.5.3
[python@PanQi 12:53:55 projects]$python -V
Python 3.5.3
[python@PanQi 12:53:59 projects]$pyenv version
3.5.3 (set by /home/python/panqi/projects/.python-version)
[python@PanQi 12:54:15 projects]$cd
[python@PanQi 12:54:19 ~]$python -V
Python 2.6.6
[python@PanQi 12:54:22 ~]$pyenv version
system (set by /home/python/.pyenv/version) local选项适用
如果多人开发,在一台机上,都在这个目录下,可能有些包版本会冲突
Virtualenv创建一个独立空间,做包隔离
[python@PanQi 13:01:35 ~]$pyenv install -v 3.6.1
[python@PanQi 13:01:19 ~]$pyenv versions
* system (set by /home/python/.pyenv/version)
3.5.3
3.6.1
[python@PanQi 13:04:52 cmdb]$pwd
/home/python/panqi/projects/cmdb
[python@PanQi 13:04:53 cmdb]$pyenv version
system (set by /home/python/panqi/projects/.python-version)
[python@PanQi 13:04:59 cmdb]$pyenv virtualenv 3.5.3 panqi353
Requirement already satisfied: setuptools in /home/python/.pyenv/versions/3.5.3/envs/panqi353/lib/python3.5/site-packages
Requirement already satisfied: pip in /home/python/.pyenv/versions/3.5.3/envs/panqi353/lib/python3.5/site-packages
[python@PanQi 13:05:50 cmdb]$pyenv versions
* system (set by /home/python/panqi/projects/.python-version)
3.5.3
3.5.3/envs/panqi353
3.6.1
panqi353
[python@PanQi 13:06:40 cmdb]$pyenv local panqi353
(panqi353) [python@PanQi 13:07:12 cmdb]$pyenv version
panqi353 (set by /home/python/panqi/projects/cmdb/.python-version)
(panqi353) [python@PanQi 13:07:46 cmdb]$cd
[python@PanQi 13:08:02 ~]$ 真正环境应该这样搭建
pyenv virtualenv 3.5.3 panqi353
Requirement already satisfied: setuptools in /home/python/.pyenv/versions/3.5.3/envs/panqi353/lib/python3.5/site-packages
Requirement already satisfied: pip in /home/python/.pyenv/versions/3.5.3/envs/panqi353/lib/python3.5/site-packages
[python@PanQi 13:05:50 cmdb]$pyenv versions
* system (set by /home/python/panqi/projects/.python-version)
3.5.3
3.5.3/envs/panqi353
3.6.1
panqi353
[python@PanQi 13:06:40 cmdb]$pyenv local panqi353
(panqi353) [python@PanQi 13:07:12 cmdb]$pyenv version
panqi353 (set by /home/python/panqi/projects/cmdb/.python-version)
(panqi353) [python@PanQi 13:07:46 cmdb]$cd
[python@PanQi 13:08:02 ~]$ 真正环境应该这样搭建
[python@PanQi 13:18:47 ~]$mkdir .pip
[python@PanQi 13:19:02 ~]$cd .pip/
[python@PanQi 13:19:08 .pip]$vim pip.conf
[global]
index-url=https://mirrors.aliyun.com/pypi/simple/
trusted-host=mirrors.aliyun.com 修改下pip配置文件 国内镜像
(panqi353) [python@PanQi 13:14:40 cmdb]$pip -V
pip 9.0.1 from /home/python/.pyenv/versions/3.5.3/envs/panqi353/lib/python3.5/site-packages
[python@PanQi 13:27:50 site-packages]$ls
easy_install.py pip-9.0.1.dist-info __pycache__ redis-2.10.6.dist-info setuptools-28.8.0.dist-info
pip pkg_resources redis setuptools
/home/python/.pyenv/versions/3.5.3/envs/panqi353/lib/python3.5/site-packages
[python@PanQi 13:27:50 site-packages]$ls
easy_install.py pip-9.0.1.dist-info __pycache__ redis-2.10.6.dist-info setuptools-28.8.0.dist-info
pip pkg_resources redis setuptools
这样安装的包,就在这个目录下,个人独立的目录,不会产生包版本冲突
(panqi353) [python@PanQi 13:32:20 cmdb]$pip install ipython
(panqi353) [python@PanQi 13:32:55 cmdb]$ipython
Python 3.5.3 (default, Jul 8 2018, 17:19:15)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]:
(panqi353) [python@PanQi 13:34:47 cmdb]$pip install jupyter
用ipython网页版:jupyter
pip install jupyter
jupyter notebook --help
jupyter notebook password
(panqi353) [python@PanQi 13:39:34 cmdb]$jupyter notebook --ip=0.0.0.0 --no-browser
[I 13:41:42.663 NotebookApp] Writing notebook server cookie secret to /home/python/.local/share/jupyter/runtime/notebook_cookie_secret
[I 13:41:42.931 NotebookApp] Serving notebooks from local directory: /home/python/panqi/projects/cmdb
[I 13:41:42.931 NotebookApp] 0 active kernels
[I 13:41:42.931 NotebookApp] The Jupyter Notebook is running at:
[I 13:41:42.931 NotebookApp] http://PanQi:8888/
[I 13:41:42.931 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
pip freeze > requirement
pip install -r requirement 可以导出,导入 相当于部署
windows下安装python
C:\Users\77>pip install ipython
C:\Users\77>pip jupyter notebook
再装ipython、jupyter
给定一个不超过5位的正整数,判断其有几位:
var = int(input('>>>'))
if var >= 10000:
print ("五位")
elif var >= 1000:
print ("四位")
elif var >= 100:
print ("三位")
elif var >= 10:
print ("两位")
else:
print ("一位")
var = int(input('>>>'))
if var >= 1000:
if var >= 10000:
print (5)
else:
print (4)
else:
if var >= 100:
print (3)
elif var >= 10:
print (2)
else:
print (1)
计算1000以内的被7整除的前20个数
count = 0
for i in range(0,1000,7):
print (i)
count += 1
if count == 20:
break
count = 0
for i in range(0,1000):
if i%7 == 0:
count += 1
print (i)
if count == 20:
break
count = 0
num = 0
while True:
if num%7 != 0:
num += 1
continue
else:
print (num)
count += 1
num += 1
if count == 20:
break
count = 0
num = 0
while True:
if num%7 == 0:
print (num)
num += 7
count += 1
if count == 20:
break
给定一个不超过5位正整数,判断其有几位,依次打出个位,十位。。。万位
var = int(input('>>>'))
count = 0
while True:
if var%10 != 0:
print (var%10)
var = var//10
count += 1
continue
else:
break
print (count)
如果需求是从万位到个位打印
var = int(input("请输入一个数字:"))
print ("输入数字位数为:",len(str(var)))
for i in (str(int(var))):
print (i)
请输入一个数字:9876543
输入数字位数为: 7
9
8
7
6
5
4
3
打印一个边长为N的正方形
a=int(input('>>>'))
for i in range(1,a+1):
if i == a or i == 1:
print ('#'*a)
else:
print ('#'+' '*(a-2)+'#')
>>>10
##########
# #
# #
# #
# #
# #
# #
# #
# #
##########
求100以内所有奇数和
sum = 0
for i in range(1,100,2):
sum += i
print (sum)
sum = 0
for i in range(1,100):
if i%2 != 0:
sum += i
print (sum)
输入一个数,求1到此数的阶乘之和
var=int(input("请输入一个数:"))
sum = 0
for i in range(var,0,-1):
jc=1
for n in range(i,0,-1):
jc*=n
sum += jc
print ("阶乘之和为:",sum)
var=int(input("请输入一个数:"))
jc=1
sum=0
for n in range(1,var+1):
jc *= n
sum += jc
print (sum)
判定一个质数
var=int(input("请输入一个数:"))
for i in range(2,var):
if var%i == 0:
print ("这不是质数")
break
else:
print ("这是个质数")
10000以内质数
print ("质数:",2)
for i in range(3,10001):
for j in range(2,i):
if i%j == 0:
break
else:
print ("质数:",i)
99乘法表
for i in range(1,10):
for j in range(1,i+1):
print (j,"*",i,"=",i*j,'\t',end="")
if j == i:
print ()
婓波拉切数列
i = 1
j = 1
t = 2
print (i,j,t,end=" ")
while True:
i = j
j = t
t = int(i+j)
if t > 100:
break
print (t,end=" ")
取素数
优化一:2到自己平方根的数不能整除
import math
count = int(0)
for i in range(2,100):
count += 1
for j in range(2,math.ceil(math.sqrt(i))):
count += 1
if i % j == 0:
break
else:
print (i)
print (count)
优化二:不能被质数整除
import math
import datetime
lst1 = []
count = int(0)
start = datetime.datetime.now()
for i in range(2,100000):
count += 1
for j in lst1:
count += 1
if i % j == 0:
break
else:
lst1.append(i)
delta = (datetime.datetime.now() - start).total_seconds()
print (len(lst1))
print (count)
print (delta)
优化三:不能被质数整除且只到自己平方根的
import math
import datetime
lst1 = []
flag = False
count = 0
start = datetime.datetime.now()
for i in range(2,100000):
for j in lst1:
count += 1
if i % j == 0:
flag = True
break
if j >= math.ceil(math.sqrt(i)):
flag = False
break
if not flag:
lst1.append(i)
print (len(lst1))
delta = (datetime.datetime.now() - start).total_seconds()
print (count)
print (delta)
杨辉三角
方法一:
lst1 = [[1],[1,1]]
for i in range(2,6):
cur = [1]
for j in range(i-1):
cur.append(lst1[i-1][j]+lst1[i-1][j+1])
cur.append(1)
lst1.append(cur)
print (lst1)
方法二:
lst1 = []
for i in range(6):
row = [1]
lst1.append(row)
if i == 0:
continue
for j in range(i-1):
row.append(lst1[i-1][j]+lst1[i-1][j+1])
row.append(1)
print (lst1)
方法三:
oldline = []
newline = [1]
print (newline)
for i in range(1,6):
oldline = newline.copy()
newline.clear()
oldline.append(0)
for j in range(i+1):
newline.append(oldline[j-1] + oldline[j])
print (newline)
方法四:
triangle = []
for i in range(6):
row = [1]*(i+1)
triangle.append(row)
if i == 0:
continue
for j in range(1,i//2+1):
val = triangle[i-1][j-1]+triangle[i-1][j]
row[j] = val
if j != i - j:
row[-j-1] = val
print (triangle)
随机输入三个数比大小
方法一:
lista=[]
out=None
for i in range(0,3):
lista.append(int(input('>>>')))
if lista[0] > lista[1]:
if lista[0] > lista[2]:
if lista[1] > lista[2]:
out=[2,1,0]
else:
out=[1,2,0]
else:
out=[1,0,2]
else:
if lista[0] > lista[2]:
out=[2,0,1]
else:
if lista[1] > lista[2]:
out=[0,2,1]
else:
out=[0,1,2]
for j in out:
print (lista[j])
方法二:
lista=[]
out=None
for i in range(0,3):
lista.append(int(input('>>>')))
lista.sort()
for j in range(0,3):
print (lista[j])
方法三:
import math
lista=[]
out=None
for i in range(0,3):
lista.append(int(input('>>>')))
while True:
cur = min(lista)
print (cur)
lista.remove(cur)
if len(lista) == 0:
break
方法四:
lisa = [1,2,3,4,5,6,99] # 优化版
count = 0
count_swap = 0
for i in range(length): #[0,6]
flag=False
for j in range(length-i-1): #7-i-1 两个length意思不一样
count += 1
if lisa[j] > lisa[j+1]:
tmp = lisa[j]
lisa[j] = lisa[j+1]
lisa[j+1] = tmp
flag=True
count_swap+=1
if not flag: #如果一趟下来,没有发生调换,说明本来便是从小到大的排序,就不用再循环了
break
print (lisa,count_swap,count)