python入门_老男孩_格式化输出_while-else_初始编码_优先级_运算符_day2

关键词:

  • 格式化输出
  • while-else
  • % and while-else
  • 初始编码

格式化输出

  %d  %s  %r  %占位符

  %%s  转义

 1 name = input('请输入姓名: ')
 2 age = input('请输入年龄: ')
 3 sex = input('请输入性别: ')
 4 
 5 # 转义
 6 msg ="我是%s,今年%d,%s,学习进度为3%%" %(name, age, sex)
 7 # 如果是 %%s呢
 8 
 9 print(msg) 
10 print("3%%")

while-else

 1 # 循环被打断,不执行else部分
 2 # 未被打断,则执行else部分
 3 
 4 # 未被打断
 5 count = 0
 6 
 7 while count < 5:
 8     print('loop')
 9     count += 1
10 else:
11     print('This is the sixth loop')
12 
13 # 被打断
14 i = 0
15 
16 while i < 5:
17     print('loop')
18     break
19     i += 1
20 else:
21     print('This is the sixth loop')

% 和 while-else的综合案例

 1 '''
 2 登陆尝试程序,设定尝试上限为三次,高于三次,询问是否继续尝试,如得到yes,再给三次机会;如得到no,输出机会难得,失不再来
 3 '''
 4 
 5 usr = 'larry'
 6 pwd= '123'
 7 
 8 i = 0
 9 while i < 3:
10     username = input('请输入名字: ')
11     password = input('请输入密码: ')
12 
13     if username == usr and password == pwd:
14         print('登陆成功')
15         break
16     else:
17         print('你还有%d次机会' %(2-i))
18         if (2-i) == 0:
19             result = input('是否还想试试,yes, no: ')
20             if result == 'yes':
21                 i = 0
22                 continue
23     i += 1
24 else:
25         print("机会难能可贵,失不再来")

初始编码

  方便电脑的传输,00000001

  最开始

    美国: ascii码  7+1  最左为0  可以表示所有的英文字母,数字,特殊字符

    换算方式

    00000001  8bit  ==  1byte

    1byte  1024byte == 1 kb

    1kb    1024kb == 1MB

    1MB   1024MB == 1GB

    1GB   1024GB == 1TB

      PB  EB ....

  后来

    美国:  万国码, unicode

      最开始

        1个字节  表示所有的英文,特殊字符,数字等等

        2个字节  16位表示一个中文(中文共九万多汉字),不够,unicode用四个字节表示中文,32位

        约计地球上的所有文字,21位就可以完全涵盖,四个资源浪费空间。

      升级版

        utf-8  最少用一个字节,8位表示一个英文

            欧洲16位  两个字节

            一个中文  3个字节表示    

        gbk  国内使用  一个中文  2个字节表示

     python2默认ascii码,中文报错..utf-8

优先级

# 优先级, () > not > and > or

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

#False

运算符

1 # x or y, x为非零,则返回x
2 
3 print(1 or 2)
4 print(3 or 2)
5 print(0 or 2)
6 print(0 or 3)
# x and y, 若x为真,返回y

print(1 and 2) #2
print(0 and 2) #0
# or / and 综合案例

print(0 or 4 and 3 or 2) #3
# 面试最强案例

print(2 or 1<3 and 2) # 2
print(3>1 or 2 and 2) # True
print(1>2 and 3 or 4 and 3< 2) #False

原文地址:https://www.cnblogs.com/dignity/p/9723278.html