开始学习python的第二天

一、练习题

1.使用while循环输入 1 2 3 4 5 6 8 9 10

#第一种方法
count = 0
while count < 10:
    count += 1  # count = count + 1
    if count == 7:
        print(' ')
    else:
        print(count)
#第二种
count = 0
while count < 10:
    count += 1  # count = count + 1
    if count == 7:
        continue
    print(count)

2.输出 1-100 内的所有奇数

#方法一:
count = 1
while count < 101:
    print(count)
    count += 2
#方法二:
count =1
while count <101:
    if count%2 == 1:#求偶数余0即可
        print(count)
    count +=1

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

sum =0
count =1
while count <100:
    if count%2 == 0:
        sum = sum - count
    else:
        sum +=count
        count += 1
print(sum)

4.用户登陆(三次机会重试)

i = 0
while i < 3:
    username = input('请输入账号:')
    password = int(input('请输入密码:'))
    if username == '咸鱼哥' and password == 123:
        print('登录成功')
    else:
        print('登录失败请重新登录')
    i += 1

二、格式化输出(%s / %d,%—占位符,s—字符串,d—数字)

name = input('请输入姓名:')
age = input('请输入年龄:')
job = input('请输入工作:')
hobbie = input('你的爱好:')

msg = '''------------ info of %s -----------
Name  : %s
Age   : %d
job   : %s
Hobbie: %s
------------- end -----------------''' %(name,name,int(age),job,hobbie)
print(msg)

* 年龄age将其转换为int型

name = input('请输入姓名')
age = input('请输入年龄')
height = input('请输入身高')
msg = "我叫%s,今年%s 身高 %s 学习进度为3%%" %(name,age,height)
print(msg)

* 想要在格式化输出中单纯的表示一个%就在其后在加一个%

三、while else(当while语句执行时被break打断就不会在执行else内的语句)

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

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

* 将break置换成pass就会明白while else的用法

四、初始编码

1.电报,电脑的传输,存储都是01010101

2.最早的'密码本' ascii 涵盖了英文字母大小写,特殊字符,数字。

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

3.ascii 只能表示256种可能,太少。

美国:ASCII码为了解决全球化的文字问题,创办了万国码 unicode

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

              2个字节,16位表示一个中文不够,Unicode一个中文用四个字节表示

Unicode 升级 utf-8 utf-16 utf-32
8位 = 1字节bytes
utf-8 一个字符最少用8位去表示,英文用8位 一个字节
欧洲文字用16位去表示 两个字节
中文用24 位去表示 三个字节
utf-16 一个字符最少用16位去表示

gbk 中国人自己发明的,一个中文用两个字节 16位去表示。

五、运算符

1.算数运算符

2.比较运算符

3.赋值运算符

4.位运算符

5.逻辑运算符

6.运算符优先级

#and or not
#优先级,()> not > and > or
# print(2 > 1 and 1 < 4)
# print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2)
# T or T or F
#T or F
# 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

7.ps  int  ----> bool       非零转换成bool True             0 转换成bool 是False

print(bool(2))
print(bool(-2))
print(bool(0))
#bool --->int
print(int(True))   # 1
print(int(False))  # 0

8.'''x or y x True,则返回x'''

print(1 or 2) # 1
print(3 or 2) # 3
print(0 or 2) # 2
print(0 or 100) # 100

9.'''x and y x True,则返回y'''

print(1 and 2)
print(0 and 2)
print(2 or 1 < 3)
print(3 > 1 or 2 and 2)

猜你喜欢

转载自www.cnblogs.com/zhouanfu/p/10809267.html