【Day2】Python学习笔记

1、流程控制语句 if

1.1 if … (else …)

只考虑一种情况 。
例:

print(111)
if 5>4 :
    print(222)
print(333)

考虑两种情况,且为互补关系。
例:

if 4 > 5:
    print('4>5')
else:
    print('5>4')
1.2 if … elif … else …

考虑多种情况。
例:

score=int(input('请输入你的成绩:'))
if   score==100:
    print('perfect')
elif score>90:
    print('excellent')
elif score>80:
    print('great')
elif score>70:
    print('good')
else:
    print('keep trying')
1.3 嵌套

一种情况包含两种或更多情况时,使用嵌套。
例:将三个不相等的数进行排序:

print("请输入三个不相等的数:")
a=int(input('a:'))
b=int(input('b:'))
c=int(input('c:'))
if a>b:
    if b>c:
        print('a>b>c')
    else:
        if a>c:
            print('a>c>b')
        else:
            print('c>a>b')
else:
    if  a>c:
        print('b>a>c')
    else:
        if  b>c:
            print('b>c>a')
        else:
            print('c>b>a')

2、循环语句 while

2.1 无限循环

没有设定结束循环的条件时,即为无限循环。
例:

while  1:
    print('根本停不下来')

注:"while 1:"的效率高于"while True:"。

2.2 条件循环

设有结束循环的条件时,为条件循环。
例:计算1-100的和:

count = 1
sum = 0
while count <= 100:
    sum += count
    print(count)
    count += 1
print("1-%d的和为:%d" %(count-1,sum))
2.3 break和continue

break为结束循环,continue为结束本次循环。
例:

count = 0
while count < 100:
    count += 1
    if  count%2 == 0:     
        continue
    if  count > 50:
        break
    print(count)

3、初识格式化输出

%:占位符,s:字符串,d:数字
%%:显示%
例:

name = input('请输入姓名:')
age = int(input('请输入年龄:'))
msg = '我是%s,今年%d岁。' % (name, age)
print('我是%s,今年%d岁。' % (name, age))

4、while … else …

当while不被break打断时,执行else的内容。
例:

i = 0
while i < 10:
    i += 1
    if i == 8:
        break
    print(i)
else:
    print("成功输出1-10。")

5、初始编码

电脑传输、储存实际上都是在使用二进制编码:01。

5.1 ASCII码

美国创建了ASCII码
0000 0001   8位(bit ) == 1字节(byte) 1024byte == 1kb 1024kb==1mb 之后还有G T P E Z

5.2 unicode码

为解决全球化文字问题,创建了万国码,unicode
最开始用1个字节表示所有英文,特殊字符,数字等等;用2个字节表示中文。由于2个字节不能表示9万多个中文,因此用4个字节表示中文。

5.3 utf - 8

utf - 8:unicode升级版。英文字母用1个字节表示,欧洲文字用2个字节表示,中文用3个字节表示。

5.4 gbk

gbk:在国内使用,英文用1个字节表示,中文用2个字节表示。
注:utf-8和gbk的转换要通过unicode。

6、运算符

6.1 and or not

优先级:() > not > and > or
例:

print(2 >1 and (3 > 1 or 2 >3))
print(2 > 1 and 3 > 4 and 2 > 5 or 2 < 4 and 1 > 3 or 2 > 3)
6.2 x or y 和 x and y的结果

x or y的结果:x为True返回x,x为False返回y
因为:or有一个为真则为真,一个为假则取决于另一个
x and y的结果: x为True返回y,x为False返回x
因为:and有一个为假则为假,一个为真则取决于另一个
例:

print(1 or 3)
print(0 or 3)
print(1 and 2)
print(0 and 2)
6.3 bool和int的转换

bool转换为int:True>1、False>0
int转换为bool:0>False、其他均为True

print(int(True))
print(bool(-3))
print(bool(0))
6.4 1 > 2 and 3 or 4 and 3 < 2的结果

比较运算的结果为bool值,遵循x or y和x and y的规律,注意x、y的格式为bool或int即可。
例:

print(3 > 4 or 0)
print(1 > 2 and 3 or 4 and 3 < 2)

附:参考运算符优先级

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值