python学习之路——day02

一 . while循环

基本循环

1 while 条件:
2     循环体
3 
4     #如果为真执行循环体
5     #如果为假循环体不执行

循环体的终止语句

break :彻底停止掉当前层次循环体

cocontinue: 停止当前本次循环,继续执行下一次循环

例如:

break

1 while True:
2     content = input("请猜一个100以内的数:")
3     if content == "99":
4         print("恭喜你猜中了")
5         break
6     else:
7         print("你猜错了")

continue

 1 content = 1
 2 while content < 11:
 3     if content == 4:
 4         content += 1
 5         continue
 6     print(content)
 7     content += 1
 8 
 9 #结果
10 1
11 2
12 3
13 5
14 6
15 7
16 8
17 9
18 10

while.....else...

与 if 类似 if 有个else  while 也有一个else

while  后面跟 else  作用是 当 while  循环正常执行完 中间没有被  break  中止 就会执行下面的  else   

重点  break 不会触发 else的执行   break 是跳出整个循环体

 没有 break  执行 else

 1 count = 1
 2 while count < 10:
 3     if count == 4:
 4         count += 1
 5         continue
 6     print(count)
 7     count += 1
 8 else:
 9     print("打印完毕")
10 
11 #结果
12 1
13 2
14 3
15 5
16 6
17 7
18 8
19 9
20 打印完毕

含有 break  不执行  else语句     直接条跳出整个循环

 1 count = 1
 2 while count < 10:
 3     if count == 4:
 4         break
 5     print(count)
 6     count += 1
 7 else:
 8     print("打印完毕")
 9 
10 #结果
11 1
12 2
13 3

二  .格式化输出

格式化

%s   : 占位   占位的是字符串,  什么都能接  全能的

%d   :  占位  占位的是数字, 只能接数字  其他都不能接

例如:

 1 #名片制作
 2 name = input("请输入你的名字:")
 3 age = input("请输入你的年龄:")
 4 job = input("请输入你的工作:")
 5 hobby = input("请输入你的兴趣爱好:")
 6 print("-----------%s的名片---------" % (name))
 7 print('''name: %s 
 8 age: %s
 9 job: %s
10 hobby: %s''' % (name,age,job,hobby))
11 print("----------------------------")
12 
13 #结果
14 请输入你的名字:王尼玛
15 请输入你的年龄:22
16 请输入你的工作:待业
17 请输入你的兴趣爱好:找你妹
18 -----------王尼玛的名片---------
19 name: 王尼玛 
20 age: 22
21 job: 待业
22 hobby: 找你妹
23 ----------------------------
View Code

如果将 年龄 的占位符换成%d  前面的是  age = int(input())  , 否则会报错  , input  接收的是字符串,  %d 只能占位数字  

转义

1 name = "alex"
2 print("我叫%s到现在已经活了30%%了" % (name))
3 
4 #结果
5 我叫alex到现在已经活了30%了

如果字符串中出现%s这样的格式化的内容,后面的%都会认为是格式化 ,想用%  就使用转义  :%%

三  .   运算符 

1- 简单运算符

a = 10 

b = 20

算术运算符

比较运算符

赋值运算符

逻辑运算符

and : 并且  的意思   左右两边同时为真 , 结果为真
or   : 或   的意思   左右两边有一个真结果就是真 , 两边都是假  结果才是假
not  :  非假既真   非真既假

运算顺序优先级
() => not => and => or

 1 x or y  如果x为0 返回y ,如果x非0  返回x
 2 print(3 or 5)#3
 3 print(0 or 5)#5
 4 print(1 or 9)#1
 5 print(4 or 0 or 1 or 0)#4
 6 print(8 or 4 or 9 or 0 or 1 or 0)#8
 7 print(0 or 1 or 5 or 0 or 0 or 3)#1
 8 
 9 x and y  与 or  相反
10 print(5 and 4 or 5 and 8 or 0 and 5)#4
11 print(0 and 5 or 7 and 0 and 0 or 5)#5
12 print(0 and 1 and 3 or 5 and 0 or 0)#0
13 print(0 and 5 or 1 and 0 or 0)#0
14 print(0 and 1 and 2 and 3 or 5 or 0)#5

Flase  为 0,  True 为 非0

成员运算符

x = 'alex123'

y = '123'

四  .  编码

1 . ASSII码  最早的编码 ,  至今还在使用,   8位一个字节(字符)

2 . GBK   国标码. 16位2个字节

3 . Unicode  万国码  32位 4个字节

4 . UTF-8 , 可变长度的Unicode

  英文 : 8位 一个字节

  欧洲文字 :16位. 2个字节

  汉子: 24位   3个字节

单位转换

8bit = 1byte
1024byte = 1KB
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB

猜你喜欢

转载自www.cnblogs.com/heshun/p/9379999.html
今日推荐