Python笔记day02(基础)
1,作业
(1) 使用while循环输入 1 2 3 4 5 6 8 9 10
count =
while count < :
count += # count = count + 1
if count == :
print(' ')
else:
print(count)
count =
while count < :
count += # count = count + 1
if count == :
continue
print(count)
(2)输出 1-100 内的所有奇数
#方法一:
# count = 1
# while count < 101:
# print(count)
# count += 2
#方法二:
# count = 1
# while count < 101:
# if count % 2 == 1:
# 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 = sum + count
# count += 1
# print(sum)
(4)用户登陆(三次机会重试)
i =
while i < :
username = input('请输入账号:')
password = int(input('请输入密码:'))
if username == '咸鱼哥' and password == :
print('登录成功')
else:
print('登录失败请重新登录')
i +=
2,格式化输出
#格式化输出
# % s d
# name = input('请输入姓名')
# age = input('请输入年龄')
# height = input('请输入身高')
# msg = "我叫%s,今年%s 身高 %s" %(name,age,height)
# print(msg)
"""
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)
"""
name = input('请输入姓名')
age = input('请输入年龄')
height = input('请输入身高')
msg = "我叫%s,今年%s 身高 %s 学习进度为3%%s" %(name,age,height)
print(msg)
3,while,else
Python的for…else和while…else语法,这是Python中最不常用,最为误解的语法特性之一。
Python中的for、while循环都有一个可选的else分支(类似if语句和try语句那样),在循环迭代正常完成之后执行。换句话说,如果我们不是除正常以外的其他方式退出循环,那么else分支将被执行。也就是在循环体内没有break语句、没有return语句,或者没有异常出现。
count =
while count <= :
count +=
if count == :break
print("Loop",count)
else:
print("循环正常执行完啦")
print("-----out of while loop ------")
Loop 1
Loop 2
—–out of while loop ——
4,初始编码
最早的’密码本’ ascii 涵盖了英文字母大小写,特殊字符,数字。
ascii 只能表示256种可能,太少,创办了万国码 unicode
16表示一个字符不行,32位表示一个字符。
A 01000001010000010100000101000001
B 01000010010000100100001001000010
我 01000010010000100100001001000010
Unicode 升级 utf-8 utf-16 utf-32
8位 = 1字节bytes
utf-8 一个字符最少用8位去表示,英文用8位 一个字节
欧洲文字用16位去表示 两个字节
中文用24 位去表示 三个字节
utf-16 一个字符最少用16位去表示
gbk 中国人自己发明的,一个中文用两个字节 16位去表示。
1bit 8bit = 1bytes
1byte 1024byte = 1KB
1KB 1024kb = 1MB
1MB 1024MB = 1GB
1GB 1024GB = 1TB
5,逻辑运算
and or not
优先级,()> not > and > or
# print(> or < and ==) # F
# print( < and < or >) # T
# print( > and < or > and < ) # T
# print( > and < or > and > or < ) # F
# print( > and < or > and > and > or < ) # F
# print(not > and < or > and > and > or < ) # F
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
x or y x True,则返回x
# print( or ) #
# print( or ) #
# print( or ) #
# print( or ) #
# print( or or or ) #
# print( or and or )
x and y x True,则返回y
# print( and )
# print( and )
print( or < )
print( > or and )