Python基础二(while else,格式化输出,逻辑运算,编码基础)

一、while else句型

 

while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句,Break后跳出循环,不执行else内容。

 

count = 0
while count <= 5 :
    count += 1
    print("Loop",count)

else:
    print("循环正常执行完啦")
print("-----out of while loop ------")

输出

Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
Loop 6
循环正常执行完啦
-----out of while loop ------

如果执行过程中被break啦,就不会执行else的语句了

count = 0
while count <= 5 :
    count += 1
    if count == 3:break
    print("Loop",count)

else:
    print("循环正常执行完啦")
print("-----out of while loop ------")

  输出

Loop 1
Loop 2
-----out of while loop ------

  练习题:

 

如何实现用户输入三次后,显示登录失败,想再次给三次机会

i = 1
name = 'guo'
password = '123'
while i <= 3:
    name1 = input('请输入姓名:')
    password1 = input('请输入密码:')
    if name == name1 and password == password1:
        print('登录成功!')
        break
    else:
        print('输入错误')
        i += 1
        if i == 4:
            answer = input('是否想再试一试/y')
            if answer  == 'y':
                i = 1
else:
    print('你傻不傻')

  在此需注意,if 条件下的缩进,如果if  answer == “y”不在上一个 if条件下吗,则会打印失败。

二、格式化输出  

1、第一种写法

name = input("姓名")
age = int(input("年龄"))
job = input("工作")
msg = " 我叫%s,今年%d, 从事%s"%(name,age,job)
print(msg)

%s:  占位符,表示字符串

%d:  表示数字

2、第二种写法(字典)

name1 = input('姓名')
age1 = input('年龄')
job1 = input('工作')
dic = {'name':name1,'age':age1,'job':job1}
msg = '我叫%(name)s,今年%(age)s,从事%(job)s工作'%dic
print(msg)

 

msg = '我叫%s,今年%s,目前Python学习进度2%%'%('郭晓彦',28)
print(msg)

 

注: 计算机认为是%,而不是占位符。

转译,%--->>%%。

三、逻辑运算符    not   、 and  、  or

原则:
优先级 ( ) > not > and > or
同一优先级别从左至右执行判断

1、前后比较都是条件对比----输出的内容为 True 或 False

print(2 > 1 and 3 > 4)   #False
print(2 > 1 or 3 > 4)   #True
print(not 2 > 1)   #False
a = 2 > 1 and 2 < 3 or 2 > 4 and 1 < 5 or 7 < 4
print(a)   #False

练习题

print(3 > 4 or 4 < 3 and 1==1)  # F
print(1 < 2 and 3 < 4 or 1>2 )  # T
print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)  # T
print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)  # F
print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F
print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F

2、前后比较的都是数字----输出数字

 

原则 :
x or y if x 为 True,则值是x ;else:值是y.
and 与or相反
ps: str ---> int int(str) 字符串必须是数字组成
int----> str str(int)
int ---> bool 非0 ----> True
0 -----> False

练习题:
print(1 or 2)     1
print(2 or 3)     2
print(0 or 2)     2
print(0 or 3)     3
print(1 and 2)    2
print(0 and 3)    0

 升级:

print(0 or 3 and 4 or 5 )   #先运算 3 and 4 
结果为  4

 

四、编码基础

计算机:

储存文件,或者是传输文件,实际上是010101010

计算机创建初期(由美国创立),二进制,密码本--对照表

python2解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill),而python3对内容进行编码的默认为utf-8。

 

ASCII(美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言,其最多只能用 8 位来表示(一个字节),即:2**8 = 256,所以,ASCII码最多只能表示 256 个符号。

因为全球语言很多,ascii不足以存储这么多对应关系,创建了一个超级密码本:万国码unicode

8 位 == 1个字节.
hello h一个字符,e一个字符,he就不是一个字符.
中国:中是一个字符,国是一个字符.(字符是一种语言的最小表示单位)
unicode :
创建之初,16位,2个字节,表示一个字符.
英文: a b c 六个字节 一个英文2个字节
中文 中国 四个字节 一个中文用2个字节
改成 32位,4个字节,表示一个字符.
a 01000001 01000010 01000011 00000001
b 01000001 01000010 01100011 00000001
中 01001001 01000010 01100011 00000001
缺点:浪费资源
对Unicode进行升级:  utf-8
utf-8 用最少用8位数,去表示一个字符.
英文: 8位,1个字节表示.
欧洲文字: 16位,两个字节表示一个字符.
中文,亚洲文字: 24位,三个字节表示.
utf-16 用最少用16位数.
GBK:
国标,只能中国人自己用, 一个中文用16位,两个字节表示.

单位转化:
01010101 8位bit
8bit == 1bytes

1024bytes == 1kB
1024KB == 1MB
1024MB == 1GB
1024GB == 1TB

五、in  , not in

可用于非法字验证:

s = 'fkdjsaalexgfdjlk'
print('alex' in s)
not in
comment = input('请输入你的评论')
s = '苍老师'
if s in comment:
    print('有非法字符,从新输入')
else:
    print('评论成功')

 

 六、作业题

作业及解答
1、判断下列逻辑语句的True,False.
1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6          # T
2)not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6     # F
2、求出下列逻辑语句的值。
1),8 or 3 and 4 or 2 and 0 or 9 and 7           # 8
2),0 or 2 and 3 and 4 or 6 and 0 or 3           # 4
3、下列结果是什么?
1)、6 or 2 > 1           # 6
2)、3 or 2 > 1           # 3
3)、0 or 5 < 4           # F
4)、5 < 4 or 3           # 3
5)、2 > 1 or 6           # T
6)、3 and 2 > 1          # 3
7)、0 and 3 > 1          # T
8)、2 > 1 and 3          # T
9)、3 > 1 and 0          # T
10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2       # 2
4. 简述变量命名规范
1、数字、字母、下划线的任意组合
2、不能以数字开头
3、变量的可描述性
4、变量不能使用英文
5、变量不能使用python中的关键字
6、变量不能过长
5. name = input(“>>>”) name变量是什么数据类型?
字符串
6. if条件语句的基本结构?
第一种
if 3 > 2:
    print('yes')
第二种
a = input('爱好')
if a == '玩游戏':
    print('你个败家子')
else:print('好好学习吧')
第三种猜分数
count = int(input('请输入分数'))
if count > 100:
    print('最大100分,重新猜')
elif count > 90:
    print('A')
elif count > 80:
    print('B')
elif count > 59:
    print('C')
elif count > 0:
    print('E')
else:print('不能为负数')
第四种 嵌套
sex = input('性别')
if  1 > 0:
    if name == 'f':
        print('一起来搞基')
    else:print('我喜欢女生')

7. while循环语句基本结构?
1、基本循环
while True:
    print('dead loop')
    循环体
    如果条件为真,那么循环体则执行
    如果条件为假,那么循环体不执行
2、 循环终止  break  continue
count = 1
while count <= 100:
    print(count)
    count += 1
    if count == 81:
        break
count = 1
while count <= 100:
    if count % 2 == 1 :
        print(count,'奇数')
        if count == 7:
           continue
    count += 1

3、 while  else

i = 1
name = 'guo'
password = '123'
while i <= 3:
    name1 = input('请输入姓名:')
    password1 = input('请输入密码:')
    if name == name1 and password == password1:
        print('登录成功!')
        break
    else:
        print('输入错误')
        i += 1
        if i == 4:
            answer = input('是否想再试一试/y')
            if answer  == 'y':
                i = 1
else:
    print('你傻不傻')

8. 写代码:计算 1 - 2 + 3 ... + 99 中除了88以外所有数的总和?
count = 1
sum = 0
while count < 100 :
    if count % 2 == 0:
        sum -= count
        if count == 88:
            sum = sum + 88
    else:
        sum += count
    count += 1
print(sum)

9. 用户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使字符串格式化)

count = 1
while count < 4:
    name = input('请输入姓名:')
    password = input('请输入密码:')
    if name == 'gxy'and password == '123':
        print('登录成功')
        break
    else:
        if count == 3:
            print('对不起,用户已锁定')
            break
        else:
            print('请重新输入,剩余',3 - count,'次')
    count += 1


10. 简述ascii、unicode、utf-8编码关系?
ASCII:只适用于英文,有8位,256种可能,不足以存储各国语言的对应关系
Unicode 万国码,32位,4个字节表示一个字符,比较浪费资源
在Unicode基础上升级为utf-8,包含2**32种可能,英文用1个字节表示,欧洲文字用2个字节表示,亚洲文字用3个字节表示
Python3目前使用utf-8

11. 简述位和字节的关系?
一个字节 == 8位,即 1 byte= 8bit

12. “老男孩”使用UTF-8编码占⽤⼏个字节?使用GBK编码占⼏个字节?
使用utf-8占用9个字节,使用GBK占用6个字节

13. 制作趣味模板程序需求:等待用户输入名字、地点、爱好,根据用户的名字和爱好进行任意实现
如:敬爱可亲的xxx,最喜欢在xxx地方,做xxx
方法一
name = input('请输入姓名:')
site = input('请输入地点:')
hobby = input('请输入爱好:')
msg = '敬爱可亲的%s,最喜欢在%s地方,做%s'%(name,site,hobby)
print(msg)
方法二
name1 = input('请输入姓名:')
site1 = input('请输入地点:')
hobby1 = input('请输入爱好:')
dic = {'name':name1,'site':site1,'hobby':hobby1}
mag = '敬爱的%(name)s,最喜欢在%(site)s地方,做%(hobby)s'%dic
print(mag)

14. 等待用户输入内容,检测用户输入内容中是否包含敏感字符?如果存在敏感字符提示“存在敏感字符请重新输入”,
并允许用户重新输入并打印。敏感字符:“⼩粉嫩”、“⼤铁锤”
content = input('请输入内容:')
a = '小粉嫩'
b = '大铁锤'
while True:
    if a in content or b in content:
        print('存在敏感字符请重新输入')
        content = input('请输入内容:')
    else:break

15. 单行注释以及多行注释?
单行注释 使用#
多行注释 使用'''

16. 简述你所知道的Python3和Python2的区别?
Python2 :ASCII,不支持中文,保留了其他语言的陋习,重复代码较多
Python3 : utf-8,支持中文,简洁,清晰,规范,符合Python宗旨

17. 看代码书写结果:
a = 1>2 or 4<7 and 8 == 8      print(a)   #结果True

18.continue和break区别?
continue 是结束本次循环,继续下次循环,见到continue相当于见底,不执行下面代码
break 是完全跳出while循环

Day3默写代码:
Bit,Bytes,Kb,Mb,Gb,Tb之间的转换关系。
8Bit = 1Bytes
1024Bytes = 1KB
1024kb = 1MB
1024MB = 1GB
1024GB = 1TB

Unicode,utf-8,gbk,每个编码英文,中文,分别用几个字节表示。
Unicode: 英文 6个字节、中文4个字节
utf-8:英文1个字节,中文3个字节
gbk:中文2个字节

  

  

 

 

  

 

posted @ 2018-01-22 17:33  GuoXY  阅读(684)  评论(1编辑  收藏  举报