2018 .8.8

一:python 代码编写
1.基本语句

if 条件表达式:
满足表达式所执行的内容
else:
不满足表达式所执行的内容

2.变式

1)有多个条件表达式

if xxxx:
pass
elif xxxx:
pass
elif xxxx:
pass
else:
pass

2)三目运算符

if间接实现三元运算符: value1 if 条件 else value2

>>> a=14
>>> b=2
>>> max = a>b?a:b
  File "<stdin>", line 1
    max = a>b?a:b
             ^
SyntaxError: invalid syntax
>>> a if a>b else b
14
>>> max = a if a>b else b

print(a if a>b else b)

if a>b:

print(a)

else:

print(b)

阅读:11
[原]Python之运算符
08/08/2018 03:37 PM

1.算术运算符

+,-,,/,*,//,%

“`

** /:

python2:

5/2
2
100/300
0
5/2.0
2.5
100/300.0
0.3333333333333333
from future import division
5/2
2.5
100/300
0.3333333333333333

python3:

5/2
2.5
100/300
0.33333333333333

“`#乘方表示方式

2.赋值运算符

=,+=,-=,/=,*=,%=

3.关系运算符

,>=,<,<=,!=,==
4.逻辑运算符

and,or,not

  1. python中支持的数值类型:

1(整形)

>>> aComplex = 2j+3
>>> type(aComplex)
<type 'complex'>


***案例:
>>> aComplex.conjugate()
(3-2j)
>>> aComplex.imag
2.0
>>> aComplex.real
3.0
```# eFloat = 33e   # 不是浮点数, ae+b(a,b为常量, 代表a*10^b)





<div class="se-preview-section-delimiter"></div>

# 字符串数据类型





<div class="se-preview-section-delimiter"></div>

aString = “hello”
type(aString)

>>> aString = "hello"
>>> type(aString)
<type 'str'>

>>> dir(aString)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> help(aString.center)

>>> aString.center(40)
'                 hello                  '
>>> aString.center(40, '*')
'*****************hello******************'
>>> print("学生管理系统".center(50, '-'))
----------------学生管理系统----------------
>>> print("学生管理系统".center(50, '*'))
****************学生管理系统****************

# 布尔值: True(1), False(0)



2. 数据类型的转换:

在python中, 所有的数据类型,都可以作为内置函数, 转换数据类型;

str(1)
‘1’
int(2e-10)
0
complex(2)
(2+0j)


3. 如何删除内存中的变量;

aFloat
1.2e-09
del aFloat
aFloat
Traceback (most recent call last):
File “”, line 1, in
NameError: name ‘aFloat’ is not defined

***查看帮助: 可以使用什么方法, 实现什么功能?
>>> help(aComplex)

>>> dir(aComplex)
['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__pow__', '__radd__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', 'conjugate', 'imag', 'real']
1. 输出:

python2: print "要打印的字符串"

python3: print("要打印的字符串")
# %s:代表字符串, %d: 整形, %f: 浮点型
>>> print("%s的年龄为%s" %(name, age))
westos的年龄为19


# .2f: 保留小数点后两位
>>> money = 7800.7812345660
>>> print("%s本月的公资为%f" %(name, money))
westos本月的公资为7800.781235
>>> print("%s本月的工资为%.2f" %(name, money))
westos本月的工资为7800.78



#.3d: 整形总占位数, 不够的前面补0
>>> sid = 1
>>> print("%s的学号为130%d" %(name, sid))
westos的学号为1301
>>> print("%s的学号为130%.3d" %(name, sid))
westos的学号为130001
>>> sid = 10
>>> print("%s的学号为130%.3d" %(name, sid))
westos的学号为130010



#输出中%是占位符, 输出%需要转义为 '%%'

举例:

print "内存占有率:%.2f%" %(1.23324546456)
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2882, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-21-5446e13955af>", line 1, in <module>
    print "内存占有率:%.2f%" %(1.23324546456)
ValueError: incomplete format
print "内存占有率:%.2f%%" %(1.23324546456)
内存占有率:1.23%
print "内存占有率:%10.2f%%" %(1.23324546456)
内存占有率:      1.23%
print "内存占有率:%-10.2f%%" %(1.23324546456)
内存占有率:1.23      %
举例:

````````````
print "%s" %(001)
1
print "%d" %(001)
1
print "%d" %(0011)
9
print "%o" %(9)
11
print "%o" %(10)
12
print "%o" %(11)
13
print "%x" %(16)
10
print "%x" %(17)
11
print "%x" %(11)
b
print "%f" %(11)
11.000000
print "%.3f" %(11)
11.000
print "%.2f" %(11)
11.00

```````````

2. 输入:





<div class="se-preview-section-delimiter"></div>

# 输入:

*** python2:
- input:(只接受数值类型)




<div class="se-preview-section-delimiter"></div>

help(input)

input()
1
1
num = input()
1
num
1
num = input(“请输入密码:”)
请输入密码:1234567
import getpass
num = getpass.getpass(“请输入密码:”)
请输入密码:
print(num)
12345678
num = input(“请输入密码:”)
请输入密码:westos123
Traceback (most recent call last):
File “”, line 1, in
File “”, line 1, in
NameError: name ‘westos123’ is not defined



- raw_input(接收字符串类型)





<div class="se-preview-section-delimiter"></div>

name = raw_input(“请输入用户名:”)
请输入用户名:westos

如果接收的值要进行数值比较时, 一定要转化为同种类型比较;

age = raw_input(“请输入年龄:”)
请输入年龄:19
type(age)

>>> num = input()
12
>>> name = input()
westos
>>> type(num)
<class 'str'>
>>> type(name )    
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'nam' is not defined
>>> type(name)
<class 'str'>

四、Python3安装

  1. 下载源码包

  2. 解包

    • 把源码包解压到/opt目录, /opt一般存放安装的第三方软件;
    • tar xf Python-3.6.4.tgz -C /opt/

      1. 编译
    • 准备工作: 安装gcc(C语言的编译器),因为python解释器是C语言编写的

    • yum install gcc -y

    • 进行编译,注意: 先切换到/opt/Python-3.6.4

    • ./configure –prefix=/ur/local/python –with-ssl

–with-ssl 添加ssl加密

–prefix指定安装目录:

4.安装

  • 准备工作: 安装zlib-devel
  • yum install zlib-devel -y
  • 安装
  • make && make install

    1. 使用

(方法1)创建软链接

  • ln -s /usr/local/python3/bin/python3 /usr/bin/python3

(方法2)修改环境变量

echo $PATH ##环境变量

临时添加

export PATH=”python3命令所在的路径:$PATH”

永久添加

echo export PATH=”python3命令所在的路径:$PATH” >> ~/.bashrc

重新读取配置文件:

source ~/.bashrc

  1. 检测是否成功?

    • 在shell中输入python3, 看是否会进入python3的交互式环境中;
  2. 如何将python3加入到pycharm的IDE工具中;

    • Ctrl+alt+s —-> 进入设置界面 —> Project

五、Python脚本

1.引用环境变量

1)#!/usr/bin/python ##直接引用系统默认Python版本

2)#!/usr/bin/env python ##引用环境变量里自定义的Python脚本,具有较强可移植性

2.指定编码格式

1)#coding:utf-8

2)#coding=utf-8

3)#encoding:utf-8

4)#encoding=utf-8

1.LVM 相关概念
物理存储介质   ##系统的物理存储设备                pv   ##物理卷 ,LVM 的基本存储逻辑块   

pe   ##物理块,LVM最小寻址单元,默认4M      vg   ##卷组,类似非lvm系统磁盘      

lv    ##逻辑卷,类似非lvm系统的逻辑分区           le    ##逻辑块,lv也可当作最小的寻址单元
2.设置lvm分区挂载

“`用watch -n 1 ‘pvs;vgs;lvs;df -h /mnt’ 命令监控lvm系统

猜你喜欢

转载自blog.csdn.net/qq_42839679/article/details/81540640