python(一)


1 python安装

先安装python,之后安装pycharm 创建工程时需要关联解释器路径;

2 python运行

先编译后解释

.py--.pyc文件---解析----结果

.pyc的目的是减少重复编译的过程,提高速度,同时可以保护源代码

3 手动编译

单个编译:import py_ccompile  py_compile.compile(r "filepath") 或者 python -m py_compile filepath

批量编译:import compileall ccomplileall.compile_dir(r "filepath") 或者 python -m compileall filepath

 4 编程规范

缩进对齐 注意不同编辑器的制表位不同,推荐使用同一编辑器,防止空格与制表符的混用导致缩进失效。

不同 ----

跨行:            三重引号 :注释跨行使用,定义字符串,按照原有字符串格式输出

注释: #  三重引号

5 编码格式

ASCII 1个字符   

GB2312  2个字节  处理中文 国标编码

Unicode 统一编码标准 UTF-16/32

UTF-8 可变长编码 将unicode字符根据不同的数字大小编码成1-6个字节 英文字母被编码成一个字节,汉字通常是3个字节;

6 乱码的本质:编码格式不统一。

解决:

# coding=utf-8 建议中间不要有空格

# -*-coding: utf-8 -* 


弱类型语言变量使用之前不需要定义,不需要指定类型,因为其内部存储均为引用关系,都是存着一块地址,指向一块内存区域,因此不需要区分;

可变数据类型

不可变数据类型:数字/字符串/布尔值/元组/None

数字:

整数 4字节

长整数 不限长度

浮点数 双精度

自动类型转换

强制类型转换 chr(90) ord('A')

转义 还原特殊含义字符的原始含义 换行  

r“”前面加r,代表原始子符串,路径 正则表达式

print("Bob said 
i'm ok")
str = """BOb said
i'm ok
"""
print(str)

Bob said 
i'm ok
BOb said
i'm ok

None

None表示一个空对象,没有任何的方法和属性;

None有自己的数据类型NoneType

不代表0 False “ ” 

None和任何其他的数据类型比较永远返回False

如果函数无retrun,则默认返回None

s = None
print(s == "")
print(s == 0)
print(s == False)

False
False
False

 运算符:

/ 除数不能为0

// 整除

** 幂

% 取余 判断奇偶数  a%2==0 偶数   a%2==1 奇数 

比较运算符:返回布尔值

== 等于 比较对象的值是否相等

is 两个引用是否相同

!=或< >

赋值运算符:

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

python中将两个变量的值进行交换:不需要借助中间变量你,连续位异或2次,以前的另一种实现,在python中直接赋值就可以实现。

a = 1
b = 2
c = a,b = b,a  实际上是定义了元组。
print(a)
print(b)
print(type(c))

2
1
<class 'tuple'>  实现原理:通过元组实现,元组的括号可以省略

函数return回一个元组,误以为是两个值

def sum():
    a = 0
    b = 1
    return a,b  # return (a,b)

逻辑运算符: and or not

1 短路运算 2 if条件为False的值 3 函数没有返回值时默认为None

if 条件为False : 0 False None [] 

def a():
    print("A")
    return 0
def b():
    print('B') #无返回值时默认为None
def c():
    print("C")
    return []
def d():
    print("D")
    return False
if a() or b() or c() or d():
    print("OK")

A
B
C
D
def a():
    print("A")
    return 0
def b():
    print('B')
def c():
    print("C")
    return []
def d():
    print("D")
    return False
if a() and b() and c() and d(): // 连续and,短路操作
    print("OK")

A

成员运算符: in /not in

in 如果在指定的序列中找到一个变量的值,则返回ture,否则返回false

not in 如果在指定的序列中找不到变量的值,则返回ture,否则返回false

应用:查看字符串中是否包含某个值

print("e" in "hello")
print("w" in "hello")
print("w" not in "hello")

True
False
True
a = 1
b = (1,2,3)
print(a in b)
print(a not in b)

Ture
False

身份运算符 is /is not

三目运算符   为真时的结果 if 判定条件 else 为假时的结果

print(1 if 5>3 else 0)  
1

 ++ -- python中不支持


字符串--赋值

单引号/双引号混用,可以避免使用转义字符。

str() 将数值转换为字符串

repr() 默认在字符串前面加r

z = "aa
c"
print(str(z))
print(repr(z))  #相当与在字符串前面加r

aa
c
'aa
c'
a = [1,"a","c",10,23]
flag = 0
for n in a:
    try:
        int(n)
    except:
        flag+=1
print(flag)  #统计自动化用例是失败数

2

 字符串格式化:

%% 本身包含%

%s 字符串

%d 整数

%f 浮点型

%x 16进制

%r 原始字符串

i = 0
b = "aaaaa"
print(i + b)
print(str(i)+b)

print(i + b)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

0aaaaa
name = "赵玉"
age = 18
print("可爱的%s,%d岁了" % (name,age))
print('''可爱的 %s
%d岁了
''' % (name,age))

可爱的赵玉,18岁了
可爱的 赵玉
18岁了
name = "小明"
mouth = 10
huafei = 50
yue = 29.99
info = "亲爱的%s,你的%d月份的话费是%.2f,余额是%.2f" % 
       (name,
        mouth,
        huafei,
        yue)
print(info)

亲爱的小明,你的10月份的话费是50.00,余额是29.99
info = "亲爱的{name},你的{mouth}月份的话费是{huafei},余额是{yue}"
info = info.format(name="小明",mouth=10,huafei=50.0,yue=29.99)
print(info)

亲爱的小明,你的10月份的话费是50.0,余额是29.99
info = "亲爱的{0},你的{1}月份的话费是{2},余额是{3}"
info = info.format("小明",10,50.0,29.99)
print(info)
亲爱的小明,你的10月份的话费是50.0,余额是29.99
protocol = "http"
domain = "192.168.2.111"
url = "huice/event/api/add"
data = "tittle=python大会&time=2018-01-06"

str = "{protocol}://{domain}/{url}?{data}"
str = str.format(protocol=protocol,
           domain=domain,
           url=url,
           data=data)
print(str)

http://192.168.2.111/huice/event/api/add?tittle=python大会&time=2018-01-06

拼接一个动态函数

method = '''def test_{case} (self):
        "{desc}"
        execute_case({data})
'''
case = "case01"
desc = "测试用例一"
data = "id=1"

method = method.format(case=case,
                       desc=desc,
                       data=data)
print(method)


def test_case01 (self):
        "测试用例一"
        execute_case(id=1)

 

  

 

原文地址:https://www.cnblogs.com/zhaoyujiao/p/8413709.html