Neil_Python_2018/9/4

今日学习总结如下:

1. 循环
while 条件:
代码块(循环体)
break 彻底停止当前本层循环
continue 停止当前本次循环。 继续执行下一次循环
else: # 当条件为假的时候会执行else
代码块
2. 格式化输出
%s 占位字符串 用这个
%d 占位数字
3. 运算符(难点)
and 并且。 左右两端同时为真。 结果才能是真
or 或者。 左右两端有一个是真。 结果就是真
not 非真即假, 非假即真

顺序: () => not => and => or

x or y
if x == 0: y else: x

and和or相反

4. 编码
最早的计算机编码是ASCII。 有英文+数字+特殊字符 8bit => 1byte 没有中文, 后面的编码必须兼容ASCII
GBK 把中国的文字进行编码. 对ASCII扩充。 16bit => 2byte 主要包含了中文, 日文, 韩文, 英文(兼容ASCII)
UNICODE 32bit 兼容各个国家的编码。万国码。 浪费空间 32bit => 4byte
UTF-8 可变长度的UNICODE。本质是UNICODE。
英文在UTF-8里面占用1个字节 8bit
欧洲文字。占用2个字节 16bit
中文. 占用3个字节 24bit
UTF-16

8bit => 1byte
1024byte => 1kb
1024kb => 1mb
1024mb => 1gb
1024gb => 1tb
1024tb = > 1

5. in
判断xxx是否出现在xxxx中

今日作业代码如下:

# Author: Neil

operation = """
1、判断下列逻辑语句的True,False.
1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 -- True
2)not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 -- False


2、求出下列逻辑语句的值。
1),8 or 3 and 4 or 2 and 0 or 9 and 7 --- 8
2),0 or 2 and 3 and 4 or 6 and 0 or 3 --- 4


3、下列结果是什么?
1)、6 or 2 > 1 --- 6
2)、3 or 2 > 1 --- 3
3)、0 or 5 < 4 --- False
4)、5 < 4 or 3 --- False
5)、2 > 1 or 6 --- True
6)、3 and 2 > 1 --- True
7)、0 and 3 > 1 --- 0
8)、2 > 1 and 3 --- 3
9)、3 > 1 and 0 --- 0
10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2 --- 2


4、while循环语句基本结构?
while 条件:
代码块
流程: 判断条件是否为真。 如果真, 执行代码块。 然后再次判断条件是否为真 。如果真继续执行代码块。。。。
直到条件变成了假。 循环退出
"""
print(operation)



print("5.猜数字")
print("------请猜一下隐藏的数字.范围(0-100) ^_^ ------")
digital = 66
count = 0
while count <3:
Guess_the_number = int(input("guess:"))
if Guess_the_number == digital:
print("Congratulations, you guessed it.")
break
elif Guess_the_number > digital:
print("Smaller than this.")
elif Guess_the_number < digital:
print("Bigger than this.")
count += 1



print("7.循环输入数字 1 2 3 4 5 6 8 9 10 ")
count = 0
while count <= 9:
count += 1
if count == 7:
continue
print(count)



print("8.数字 1-100 的和")
count = 0
sum = 0
while count <=100:
sum = sum + count
count += 1
print(sum)


print("9.输出 1-100 内所有的奇数")
x = 0
while True:
if x == 100:
break
x += 1
if 0 != x % 2:
print(x)



print("10.输出 1-100 内所有的偶数")
x = 0
while True:
if x == 100:
break
x += 1
if 0 == x % 2:
print(x)


print("11.求1-2+3-4+5 ... 99的所有数的和.")
x = 0
y = 0
while True:
if 0 == x % 2:
y = y - x
else:
y = y + x
if x ==100:
break
x += 1
print(y)



print("12.用户登录.")
_username = 'Neil'
_password = '1874'

count =0
while count <3:
username = input("Username:")
password = input("Password:")
if _username == username and _password == password:
print("Welcome user {name} login...".format(name = username))
break
else:
print("Wrong username or password! Opportunity remaining : 2 ")
count += 1
if count == 3:
print("Input error limit!")
break




print("13.判断是否为质数")

num = int(input("请输入一个数字: "))
if num > 1:
for i in range(2, num):
if (num % i) == 0:
print(num, "不是质数")
print(i, "乘于", num // i, "是", num)
break
else:
print(num, "是质数")
else:
print(num, "不是质数")



print("14.广告语判断.")
l = input("请输入广告语:")
if "第一" in l or "稀缺" in l or "国家级" in l or "最" in l:
print("输入的广告语是不合法的")
else:
print("合法的广告语")




print("15.判断输入的数字是几位数字")
a = int(input("您要输入的数字:"))
b = len(str(a))

print(b)

猜你喜欢

转载自www.cnblogs.com/wblilei/p/9588089.html