python成长之路二,成长之路大师二星,python的pri


python的print格式化输出,以及使用format来控制。

1,打印字符串(str),利用%s。

>>> print (‘My name is %s‘ % (‘TaoXiao‘))My name is TaoXiao

2,打印整数,浮点数。

>>> print ("He is %d years old" % (23))         # 整数  %dHe is 23 years old>>> print ("His height is %f m" % (1.73))       # 浮点数  %fHis height is 1.730000 m>>> print ("His height is %.2f m" %(1.73))          # 浮点数(指定保留小数点位数)  %.2fHis height is 1.73 m>>> print ("His height is %.4f m" %(1.73))          # 浮点数(强制保留4位)  %.4fHis height is 1.7300 m

3,利用format。这是官方推荐用的方式,%方式将可能在后面的版本被淘汰。

>>> print(‘{1},{0},{1}‘.format(‘TaoXiao‘,18))  # 通过位置传递,相当方便,可以重复,可以换位置。18,TaoXiao,18>>> print(‘{name}: {age}‘.format(age=24,name=‘TaoXiao‘))   # 通过关键字传递TaoXiao: 24

bug 点 在格式化输出中,只想单纯的表示一个%时,应该用%% 表示

>>> print(‘我叫%s,今年%d,我的学习进度1%%‘ % (‘关亮和‘,28))
我叫关亮和,今年28,我的学习进度1%

运算符

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

1,前后条件为比较运算

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

2,前后两边的条件为数值

x or y if x is True,return x0 对应的bool值为False,非0 都是True.True 1 ,False 0
print(bool(100))print(bool(-1))print(bool(0))print(int(True))print(int(False))

不同编码方式对应所占字节数

ASCII码中,一个英文字母(不分大小写)占一个字节的空间,一个中文汉字占两个字节的空间。一个二进制数字序列,在计算机中作为一个数字单元,一般为8位二进制数,换算为十进制。最小值0,最大值255。UTF-8编码中,一个英文字符等于一个字节,一个中文(含繁体)等于三个字节。Unicode编码中,一个英文等于两个字节,一个中文(含繁体)等于两个字节。符号:英文标点占一个字节,中文标点占两个字节。举例:英文句号“.”占1个字节的大小,中文句号“。”占2个字节的大小。UTF-16编码中,一个英文字母字符或一个汉字字符存储都需要2个字节(Unicode扩展区的一些汉字存储需要4个字节)。UTF-32编码中,世界上任何字符的存储都需要4个字节。

while循环

1,基本循环

while 条件:         # 循环体     # 如果条件为真,那么循环体则执行    # 如果条件为假,那么循环体不执行

2,循环终止语句

break
num = 0while True:    print(num)    num += 1    if num == 100:        break    
continue
count = 0while count < 10:    count += 1    if count == 7:        continue    print(count)
while ... else ..

与其它语言else 一般只与if 搭配不同,在Python 中还有个while ...else 语句

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

while flag < 3:    flag +=1    num = int(input("请输入一个数字:"))    if num > 66:        print("你输入的数字太大了")    elif num < 66:        print("你输入的数字太小了")    else:        print("恭喜你,猜对了")        breakelse:    print("太笨了你...")

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

count = 0while count <= 5 :    count += 1    if count == 3:break    print("Loop",count)else:    print("循环正常执行完啦")print("-----out of while loop ------")

课后作业

1、判断下列逻辑语句的True,False.

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

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


2、求出下列逻辑语句的值。

1),8 or 3 and 4 or 2 and 0 or 9 and 7

2),0 or 2 and 3 and 4 or 6 and 0 or 3

3、下列结果是什么?

1)、6 or 2 > 1

2)、3 or 2 > 1

3)、0 or 5 < 4

4)、5 < 4 or 3

5)、2 > 1 or 6

6)、3 and 2 > 1

7)、0 and 3 > 1

8)、2 > 1 and 3

9)、3 > 1 and 0

10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2

4、while循环语句基本结构?

5、利用if语句写出猜大小的游戏:

设定一个理想数字比如:66,让用户输入数字,如果比66大,则显示猜测的结果大了;如果比66小,则显示猜测的结果小了;只有等于66,显示猜测结果正确,然后退出循环。

6、在5题的基础上进行升级:

给用户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循环,如果三次之内没有猜测正确,则自动退出循环,并显示‘太笨了你....’。

7、使用while循环输出 1 2 3 4 5 6 8 9 10

8、求1-100的所有数的和(三种方法)

9、输出 1-100 内的所有奇数(两种方法)

10、输出 1-100 内的所有偶数(两种方法)

11、求1-2+3-4+5 ... 99的所有数的和

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

13、简述ASCII、Unicode、utf-8编码关系?

14、简述位和字节的关系?

15、“?男孩”使?UTF-8编码占??个字节?使?GBK编码占?个字节?

python成长之路二

评论关闭