while循环、格式化输出、运算符和编码初识

while循环

1. while循环的结构

while 条件:

    执行语句1

    执行语句2

1 i = 0
2 while i < 10:
3     print(i)
4     i += 1

运行结果

0
1
2
3
4
5
6
7
8
9

Process finished with exit code 0

 while循环可以使用break来终止循环

1 # 打印1到100
2 i = 1
3 while True:
4     print(i)
5     if i == 10:
6         break
7     i += 1

运行结果

1
2
3
4
5
6
7
8
9
10

Process finished with exit code 0

终止循环还可以使用quit(), exit(),不过是调用系统命令退出整个程序,不建议使用。

while还可以与continue配合使用,continue表示结束此次循环,进入下一次循环,来看代码

1 # 打印1到10里面除6以外的数
2 i = 1
3 while i < 11:
4     if i == 6:
5         i += 1
6         continue
7     print(i)
8     i += 1

看看执行结果

1
2
3
4
5
7
8
9

Process finished with exit code 0

再看一个例子:计算1+2+3+...+100的值

1 sum1 = 0
2 i = 1
3 while i < 101:
4     sum1 += i
5     i += 1
6 print(sum1)

运行结果

5050

Process finished with exit code 0

 

来看另一种写法

sum1 = 0
i = 1
while True:
    sum1 += i
    if i == 100:
        break
    i += 1
print(sum1)

运行结果

5050

Process finished with exit code 0

既然if可以与else一起使用,那么while可不可以呢?一样是可以的,来看代码

 

1 i = 0
2 while i < 10:
3     # if i == 5:
4     #     break
5     print(i)
6     i += 1
7 else:
8     print("程序正常执行完了"

 运行结果

0
1
2
3
4
5
6
7
8
9
程序正常执行完了

如果把第3,4行的注释去掉,看看运行结果

0
1
2
3
4

Process finished with exit code 0

看到这里应该就知道了明白了while...else...的意思了,else里面的语句只在while语句正常执行(没有被break、return等打断时)完时才执行

格式化输出

 1 name = input("请输入姓名:")
 2 age = input("请输入年龄:")
 3 job = input("请输入职业:")
 4 hobby = input("请输入爱好:")
 5 
 6 msg = """------------info of %s-------------
 7 Name: %s
 8 Age: %s
 9 Job: %s
10 Hobby: %s
11 -------------end-------------
12 """ % (name, name, age, job, hobby)
13 print(msg)

运行结果

请输入姓名:张无忌
请输入年龄:18
请输入职业:明教教主
请输入爱好:赵敏
------------info of 张无忌-------------
Name: 张无忌
Age: 18
Job: 明教教主
Hobby: 赵敏
-------------end-------------

这里的%表示占位符,意思就是在这里占个坑,s表示占这个坑的是字符串类型,整数类型用d,浮点型用f。那么这里就有一个问题,如果我们的输出内容里有1%这样的百分数怎么办呢,有办法!

再加一个百分号,对后面的百分号进行转义,也就是告诉python解释器这里是一个单纯的百分号。来看代码

1 # 格式化输出“我叫**,今年**岁,人生进度%1”
2 name = input("请输入姓名:")
3 age = input("请输入年龄:")
4 msg = """---------info of %s----------
5 name: %s
6 age: %s
7 人生进度:%%1 
8 """ % (name, name, age)
9 print(msg)

运行结果

请输入姓名:灭霸
请输入年龄:2
---------info of 灭霸----------
name: 灭霸
age: 2
人生进度:%1 


Process finished with exit code 0

如果我们想在百分号里面再插入一个占位符该怎么做呢,来看代码

1 name = input("请输入姓名:")
2 age = input("请输入年龄:")
3 msg = """---------info of %s----------
4 name: %s
5 age: %s
6 人生进度:%%%s 
7 """ % (name, name, age, int(age))
8 print(msg)

运行结果

请输入姓名:擎天柱
请输入年龄:3
---------info of 擎天柱----------
name: 擎天柱
age: 3
人生进度:%3 


Process finished with exit code 0

 

运算符

算术运算

+ : 加

-  : 减

*  :乘

/   :除

**   :幂

比较运算

>    大于

<     小于

!=  不等于

>=    大于等于

<=   小于等于

逻辑运算

and   与,两个都为真结果为真

  3 and 4   返回4

  0 and 2   返回2

  0 and 0   返回0

or      或,有一个为真即为真,注意前面为真时语句就不会往后执行了

  3 or 4   返回3

  0 or 2   返回2

  0 or 0   返回0

not    非,真假转换

 

 混合运算(数字与不等式以逻辑运算符连接)

 规则:(1)先算不等式

(2)结果是数字那部分时,返回值就是数字的值,结果是不等式那部分时,返回结果就是不等式的bool值

2 or 3 > 2
2

5 and 1 > 0
True

3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2
2

 

总结

优先级:不等式   >   not   >   and   >  or

混合运算:结果是数字那部分时,返回值就是数字的值,结果是不等式那部分时,返回结果就是不等式的bool值

 

编码初识

ASCII

8位(1个字节)表示一个字符,可以表示英文字母,数字和特殊字符

Unicode

每个字符用四个字节表示

utf-8

英文字符:一个字节

欧洲文字:两个字节

东亚文字:三个字节

GBK

英文字符:一个字节

中文:两个字节

 

posted @ 2018-12-20 17:37  乘月归  阅读(212)  评论(0编辑  收藏  举报