3.19 DAY2

屏蔽敏感词汇 in not in 
n='oldboyalexwusir'
print('oldboy' not in n) False
print('oldboy' in n) True
comment = input('enter your comment')
if 'apple' in comment:
    print('wrong word')
WHILE ELSE
如果循环被break打断,程序不会走else
count = 1
while True:
    print(count)
    if count == 3: break
    count += 1
else:
    print('finished')
格式化输出
#元祖类型
name = input('enter your name') age = input('enter your age') hobby = input('enter your hobby') msg = 'my name is %s, my age is %d, my hobby is %s' % (name, int(age), hobby) print(msg) #字典类型替换
dic
= {'name':'oldboy','age':22,'hobby':'swmming'} msg ='I am %(name)s, i am %(age)d, my hobby is %(hobby)s' % dic print(msg)
想在格式化输出中显示%,学习进度为1%%

#面试必考
1.前后都是比较运算
优先级()> not > and > or
同一个优先级从左至右依次计算

2.前后都是赋值运算 0是false
x or y, if x is true,则return x 否则return y

数据类型转换
int - bool 非0即true,0为false
bool-int true为1 false为0
print(int(True)) 1
print(int(False)) 0
print(bool(100)) True
print(bool(0)) False
print ( 1 or 3) 1     print(1 and 3)  3
print (2 or 3) 2 print (2 and 3) 3
print (0 or 3) 3 print (0 and 3) 0

3.混合(面试题)
print(1 > 2 or 3 and 4) 4

#字符编码
ASCII码 8个bit组成一个字符 一个字节

1PB=1024TB=1024GB=1024MB=1024KB=1024B

UNICODE 两个字节 乱码问题消失 但多一倍的储存空间

utf-8

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


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


False


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


3


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. 简述变量命名规范


  1.变量名必须是数字,下划线,字母的任意组合


  2.不能以数字开头


  3.关键字不能为变量名


  4.变量具有可描述性,不宜过长


5. name = input(“>>>”) name变量是什么数据类型?


  字符串类型str


6. if条件语句的基本结构?


  


五种


1,if 条件:


    结果


2,if 条件:


    结果


  else:


    结果


3,if 条件:


    结果


  elif 条件:


    结果


4,if 条件:


    结果


  elif 条件:


    结果


  else:


    结果


5 if 条件:


  if 条件:


    结果


  else:


    结果


7. while循环语句基本结构?


1  while 条件:


    结果


2        while 条件:


    结果


   else:


    结果


8. 写代码:计算 1 - 2 + 3 ... + 99 中除了88以外所有数的总和?


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

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

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


count = 0
while count < 3:
    username = input('请输入您的用户名')
    password = input('请输入您的密码')
    if username == 'nero' and password == '123':
        print('成功登陆')
        break
    else:
        free_chance = 2 - count
        print('错误!,剩余机会%s' % free_chance)
        count= count + 1
如果三次用完了之后 问是否再试试 再给三次机会 如果不想试了说不要脸
name = 'xxx'
pwd = '123'
count = 0
while count < 3:
    username = input('请输入用户名')
    password = input('请输入密码')
    if username == name and password == pwd:
        print('登陆成功')
        break
    else:
        count = count + 1
        print('输入错误!剩余%d机会' % (3-count))
        if count == 3:
            answer = 'YES'
            choice = input('再试试?')
            if choice == answer:
                count = 0
else:
    print('不要脸')

10. 简述ascii、unicode、utf-8编码关系?


ascii是最初级的密码本,包含字母,数字,特殊符号,8位一个字节占一个字符,主要用于显示现代英语与其他西欧语音


unicode是万国码,32位四个字节占一个字符,所有国家的语言都包含,解决乱码问题, 比较浪费储存空间


utf-8是Unicode的升级版,utf-8编码把一个unicode字符根据不同的数字大小编码成1-6个字节,常用的英文字母被编码成1个字节,汉字通常是3个字节,只有很生僻的字符才能被编码4-6个字节,节省空间。 ASCII可以在utf-8继续工作。


11. 简述位和字节的关系?


8bit = 1byte


12. “⽼男孩”使⽤UTF-8编码占⽤⼏个字节?使⽤GBK编码占⼏个字节?


“⽼男孩”使⽤UTF-8编码占⽤9个字节,使用GBK编码占6个字节


13. 制作趣味模板程序需求:等待⽤户输⼊名字、地点、爱好,根据⽤户的名字和爱好进⾏任意现实 如:敬爱可亲的xxx,最喜欢在xxx地⽅⼲xxx


name = input('请输入您的名字')
position = input ('请输入地点')
hobby = input('请输入爱好')

msg = '敬爱可亲的%s,最喜欢在%s地方做%s' % (name,position,hobby)
print(msg)

14. 等待⽤户输⼊内容,检测⽤户输⼊内容中是否包含敏感字符?如果存在敏感字符提示“存在敏感字符请重新输⼊”,并允许⽤户重新输⼊并打印。敏感字符:“⼩粉嫩”、“⼤铁锤”


comment = input('请输入您的评论')
Flag = True
while Flag:
    if '大铁锤' in comment:
        print('存在敏感字符请重新输入')
    elif '小粉嫩' in comment:
        print('存在敏感字符请重新输入')
    else:
        print('成功输入')
    Flag = False

15. 单⾏注释以及多⾏注释?


单行注释用#,多行注释可以用三对双引号‘’‘ ’‘’




16. 简述你所知道的Python3和Python2的区别?


python 2:


1.源码混乱,重复代码冗余


2.print()或者print‘内容’


3.默认编码:ascii(英文字母,特殊字符)解决方式:在首行 #_*_encoding:utf-8_*_


4.raw_input( )


python 3:


1.优美清晰简单


2.print(‘内容’)


3.默认编码 utf-8 中文


4.input( )




17. 看代码书写结果:


a = 1>2 or 4<7 and 8 == 8


print(a)


True




18.continue和break区别?


continue只是终止本次循环,接着还执行后面的循环


break则完全终止循环




知识点总结


1bytes = 8 bit


1KB = 1024B


1MB = 1024KB


1GB = 1024MB


1TB = 1024GB


1PB = 1024TB




ASCII          表示英语及西欧语言                          8bit/1bytes


GB2312      国家简体中文字符集,兼容ascii        2bytes


Unicode     国际标准组织统一标准字符集            2bytes


GBK           支持繁体字                                         2bytes


UTF-8        不定长编码                                         1-3bytes







原文地址:https://www.cnblogs.com/kateli/p/8603222.html