首页 > 编程语言 > 详细

python基础

时间:2019-07-05 17:27:34      阅读:89      评论:0      收藏:0      [点我收藏+]

1.1while循环

1.1.1 数字非0的bool值

#while判断条件非0
while 1:
    print("你好")   #无限打印“你好”

#while判断条件为0
while 0:           #0的bool值为False
    print("哈哈")   
    
    

1.1.2while else

#人为打破的while循环,else不执行
i=1
while i<5:
    print(i)
    i+=1
else: 
    print("over")
#1,2,3,4,over

i=1
while i<5:
    print(i)
    i+=1
    if i==3:
        break      #人为打破while循环
else: 
    print("over")
#1,2

1.2 字符串格式化

1.2.1方法一:%

#一个%代表占位
s="天气好%s%"%("好")
print(s)    #ValueError: incomplete format。错误原因:%后没跟数据类型

#两个%代表转义
s="天气好%s%%"%("15")
print(s)    #天气好15%

1.2.2方法二: f

#f只有在python3.6之上才能使用
what="下雨"
a=f"今天{what}"
print(a)         #今天下雨

1.2.3方法三:format

a="我是{}{}".format("好","人")
print(a)         #我是好人

1.3 编码

'''
ascii   (美国)        不支持中文
gbk     (国标)        英文1字节   中文2字节
unicode (万国码)       英文2字节   中文4字节
utf-8   (可变长编码)   英文1字节   欧洲2字节  亚洲3字节
'''

'''  系统默认编码
linux   --utf-8
mac     --utf-8
windows --gbk
'''

''' 单位换算
1字节=8位
1Bytes=8bit
1024Bytes=1KB
1024KB=1MB
1024MB=1GB
1024GB=1TB
'''

1.4 运算符

1.4.1算数运算符

# + - * / ...
python2:
    -- 5/2=2
    -- 5.0/2=2.5
    -- ps:/除数和被除数都为整数,结果为得到商的整数部分;在除数或被除数加上小数点,得到正确值
python3:
    -- 5/2=2.5
    -- 5//2=2
    -- ps:/得到准确值,//得到商的整数部分

1.4.2比较运算符

> < == !=

1.4.3赋值运算符

= -= += *=  ...

1.4.4逻辑运算符

and 两边都为真取后面值 两边都为假取前面的
or  两边都为真取前面值,两边都为假取后面值
not
优先级: ()>not>and>or
#例子:1 and 2 or 3 not False and 4 or (1 and 2)

1.4.5成员运算符

in
not in

python基础

原文:https://www.cnblogs.com/gaoyukun/p/11139468.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!