002_python的in,while else,格式化输出,逻辑运算符,int与bool转换,编码

数据

1.什么是数据?

x=10,10是我们要存储的数据

2.为何数据要分不同的类型

数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示

3.数据类型

  • 数字
  • 字符串
  • 列表
  • 元组
  • 字典
  • 集合

数据类型

1.数字int

数字主要是用于计算用的,使用方法并不是很多,就记住一种就可以:#bit_length() 当十进制用二进制表示时,最少使用的位数

v = 11
data = v.bit_length()
print(data)

2.布尔值bool

布尔值就两种:True,False。就是反应条件的正确与否。 真 1 True。 假 0 False。 

3.字符串str

用 " " 、' ' 、''' '''或者""" """。中间包含的部分称之为字符串。

PS:即使里面写入的是数字,那么他的数据类型也是字符串 

1、字符串的索引与切片。

索引即下标,就是字符串组成的元素从第一个开始,初始索引为0以此类推。


a = 'ABCDEFGHIJK'
print(a[0])
print(a[3])
print(a[5])
print(a[7])

执行输出 A D F H

切片就是通过索引(索引:索引:步长)截取字符串的一段,形成新的字符串(原则就是顾头不顾腚)。


a = 'ABCDEFGHIJK'
print(a[0:3])
print(a[2:5])
print(a[0:]) #默认到最后
print(a[0:-1]) #-1就是最后一个
print(a[0:5:2]) #加步长
print(a[5:0:-2]) #反向加步长

执行输出 ABC CDE ABCDEFGHIJK ABCDEFGHIJ ACE FDB

2、字符串常用方法。


#captalize,swapcase,title
name = "ximenchunxue"
print(name.capitalize()) #首字母大写
print(name.swapcase()) #大小写翻转
msg='egon say hi'
print(msg.title()) #每个单词的首字母大写

执行输出 Ximenchunxue XIMENCHUNXUE Egon Say Hi


# 内容居中,总长度,空白处填充
name = "ximenchunxue"
ret2 = name.center(20,"*")
print(ret2)

执行输出 ****ximenchunxue****


#数字符串中的元素出现的个数。
name = "ximenchunxue"
print(name[0:10])
ret3 = name.count("n",0,10) # 可切片
print(ret3)

执行输出 ximenchunx 2 解释: 0,10表示从左至右取出10个字符串,那么结果为ximenchunx,字母n在里面出现了2次


a2 = "hqw\t1"
'''
expandtabs() 方法把字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是 8。
默认将一个tab键变成8个空格,如果tab前面的字符长度不足8个,则补全8个,
如果tab键前面的字符长度超过8个不足16个则补全16个,以此类推每次补全8个。
'''
ret4 = a2.expandtabs()
print(ret4)

执行输出 hqw     1

a4 = "dkfjdkfasf54"
#startswith 判断是否以...开头
#endswith 判断是否以...结尾
ret4 = a4.endswith('jdk',3,6) # 顾头不顾尾
print(ret4) # 返回的是布尔值
ret5 = a4.startswith("kfj",1,4)
print(ret5)

执行输出 True True


#寻找字符串中的元素是否存在
a4 = "dkfjdkfasf54"
ret6 = a4.find("fjdk",1,6)
print(ret6) # 返回的找到的元素的索引,如果找不到返回-1

执行输出 2


a4 = "dkfjdkfasf54"
ret61 = a4.index("fjdk",4,6)
print(ret61) # 返回的找到的元素的索引,找不到报错。

执行输出以下结果,报错了 Traceback (most recent call last): File "E:/python_script/day1/test.py", line 3, in <module> ret61 = a4.index("fjdk",4,6) ValueError: substring not found


#split 以什么分割,最终形成一个列表此列表不含有这个分割的元素。
ret9&nbsp;=&nbsp;'title,Tilte,atre,'.split('t')
print(ret9)
ret91&nbsp;=&nbsp;'title,Tilte,atre,'.rsplit('t',1)
print(ret91)

执行输出 ['', 'i', 'le,Til', 'e,a', 're,'] ['title,Tilte,a', 're,']


#format的三种玩法 格式化输出
res='{} {} {}'.format('egon',18,'male')
print(res)
res='{1} {0} {1}'.format('egon',18,'male')
print(res)
res='{name} {age} {sex}'.format(sex='male',name='egon',age=18)
print(res)

执行输出 egon 18 male 18 egon 18 egon 18 male


#strip
name='*egon**'
print(name.strip('*'))
print(name.lstrip('*'))
print(name.rstrip('*'))

执行输出

egon
egon**
*egon

#replace
name='alex say :i have one tesla,my name is alex'
print(name.replace('alex','SB',1))

执行输出 SB say :i have one tesla,my name is alex


#####is系列
name='jinxin123'
print(name.isalnum())&nbsp;#字符串由字母或数字组成
print(name.isalpha())&nbsp;#字符串只由字母组成
print(name.isdigit())&nbsp;#字符串只由数字组成

执行输出

True False False

元祖tupe 元组被称为只读列表,即数据可以被查询,但不能被修改,所以,字符串的切片操作同样适用于元组。例:(1,2,3)("a","b","c") 

列表list 列表是python中的基础数据类型之一,其他语言中也有类似于列表的数据类型,比如js中叫数组,他是以[]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如: li&nbsp;=&nbsp;['alex',123,True,(1,2,3,'wusir'),[1,2,3,'小明',],{'name':'alex'}]

列表相比于字符串,不仅可以储存不同的数据类型,而且可以储存大量数据,32位python的限制是 536870912 个元素,64位python的限制是 1152921504606846975 个元素。而且列表是有序的,有索引值,可切片,方便取值。


li&nbsp;=&nbsp;[1,'a','b',2,3,'a']
li.insert(0,55)&nbsp;#按照索引去增加
print(li)
&nbsp;
li.append('aaa')&nbsp;#增加到最后
li.append([1,2,3])&nbsp;#增加到最后
print(li)
&nbsp;
li.extend(['q,a,w'])&nbsp;#迭代的去增
li.extend(['q,a,w','aaa'])
li.extend('a')
li.extend('abc')
li.extend('a,b,c')
print(li)

执行输出 [55, 1, 'a', 'b', 2, 3, 'a'] [55, 1, 'a', 'b', 2, 3, 'a', 'aaa', [1, 2, 3]] [55, 1, 'a', 'b', 2, 3, 'a', 'aaa', [1, 2, 3], 'q,a,w', 'q,a,w', 'aaa', 'a', 'a', 'b', 'c', 'a', ',', 'b', ',', 'c']


li&nbsp;=&nbsp;[1,'a','b',2,3,'a']
l1&nbsp;=&nbsp;li.pop(1)&nbsp;#按照位置去删除,有返回值
print(l1)
&nbsp;
del&nbsp;li[1:3]&nbsp;#按照位置去删除,也可切片删除没有返回值。
print(li)
&nbsp;
li.remove('a')&nbsp;#按照元素去删除
print(li)
&nbsp;
li.clear()&nbsp;#清空列表

执行输出

a [1, 3, 'a'] [1, 3]


li&nbsp;=&nbsp;[1,'a','b',2,3,'a']
li[1]&nbsp;=&nbsp;'dfasdfas'
print(li)
li[1:3]&nbsp;=&nbsp;['a','b']
print(li)

执行输出

[1, 'dfasdfas', 'b', 2, 3, 'a'] [1, 'a', 'b', 2, 3, 'a']

切片去查,或者循环去查。

其他操作 count(数)(方法统计某个元素在列表中出现的次数)。


a&nbsp;=&nbsp;["q","w","q","r","t","y"]
print(a.count("q"))

执行输出 2

index(方法用于从列表中找出某个值第一个匹配项的索引位置)


a&nbsp;=&nbsp;["q","w","r","t","y"]
print(a.index("r"))

执行输出 2

sort (方法用于在原位置对列表进行排序)。 reverse (方法将列表中的元素反向存放)。


a&nbsp;=&nbsp;[2,1,3,4,5]
a.sort()# 他没有返回值,所以只能打印a
print(a)
a.reverse()#他也没有返回值,所以只能打印a
print(a)

执行输出

[1, 2, 3, 4, 5] [5, 4, 3, 2, 1]

字典dict 字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据。python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必须是可哈希的。可哈希表示key必须是不可变类型,如:数字、字符串、元组。

字典(dictionary)是除列表意外python之中最灵活的内置数据结构类型。列表是有序的对象结合,字典是无序的对象集合。两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。


dic&nbsp;=&nbsp;{'zhang':'k'}
dic['li']&nbsp;=&nbsp;["a","b","c"]
print(dic)
#setdefault 在字典中添加键值对,如果只有键那对应的值是none,但是如果原字典中存在设置的键值对,则他不会更改或者覆盖。
dic.setdefault('k','v')
print(dic)&nbsp;&nbsp;# {'age': 18, 'name': 'jin', 'sex': 'male', 'k': 'v'}
dic.setdefault('k','v1')&nbsp;&nbsp;# {'age': 18, 'name': 'jin', 'sex': 'male', 'k': 'v'}
print(dic)

执行输出 {'li': ['a', 'b', 'c'], 'zhang': 'k'} {'k': 'v', 'li': ['a', 'b', 'c'], 'zhang': 'k'} {'k': 'v', 'li': ['a', 'b', 'c'], 'zhang': 'k'}


dic&nbsp;=&nbsp;{'name':'zhang','age':23}
dic_pop&nbsp;=&nbsp;dic.pop("a",'无key默认返回值')&nbsp;# pop根据key删除键值对,并返回对应的值,如果没有key则返回默认返回值
print(dic_pop)
del&nbsp;dic["name"]&nbsp;&nbsp;# 没有返回值。
print(dic)
&nbsp;
dic_pop1&nbsp;=&nbsp;dic.popitem()&nbsp;&nbsp;# 随机删除字典中的某个键值对,将删除的键值对以元祖的形式返回
print(dic_pop1)&nbsp;&nbsp;# ('name','jin')
&nbsp;
dic_clear&nbsp;=&nbsp;dic.clear()&nbsp;&nbsp;# 清空字典
print(dic,dic_clear)&nbsp;&nbsp;# {} None

执行输出

无key默认返回值 {'age': 23} ('age', 23) {} None


dic&nbsp;=&nbsp;{"name":"jin","age":18,"sex":"male"}
dic2&nbsp;=&nbsp;{"name":"alex","weight":75}
dic2.update(dic)&nbsp;&nbsp;# 将dic所有的键值对覆盖添加(相同的覆盖,没有的添加)到dic2中
print(dic2)

执行输出 {'age': 18, 'name': 'jin', 'weight': 75, 'sex': 'male'}


dic&nbsp;=&nbsp;{"name":"jin","age":18,"sex":"male"}
value1&nbsp;=&nbsp;dic["name"]&nbsp;&nbsp;# 没有会报错
print(value1)
&nbsp;
value2&nbsp;=&nbsp;dic.get("djffdsafg","默认返回值")&nbsp;&nbsp;# 没有可以返回设定的返回值
print(value2)

执行输出

jin

默认返回值

其他操作


dic&nbsp;=&nbsp;{"name":"jin","age":18,"sex":"male"}
item&nbsp;=&nbsp;dic.items()
print(item,type(item))&nbsp;&nbsp;# dict_items([('name', 'jin'), ('sex', 'male'), ('age', 18)]) <class 'dict_items'>
#这个类型就是dict_items类型,可迭代的
&nbsp;
keys&nbsp;=&nbsp;dic.keys()
print(keys,type(keys))&nbsp;&nbsp;# dict_keys(['sex', 'age', 'name']) <class 'dict_keys'>
&nbsp;
values&nbsp;=&nbsp;dic.values()
print(values,type(values))&nbsp;&nbsp;# dict_values(['male', 18, 'jin']) <class 'dict_values'> 同上

执行输出

dict_items([('sex', 'male'), ('age', 18), ('name', 'jin')]) <class 'dict_items'> dict_keys(['sex', 'age', 'name']) <class 'dict_keys'> dict_values(['male', 18, 'jin']) <class 'dict_values'>

字典的循环


dic&nbsp;=&nbsp;{"name":"jin","age":18,"sex":"male"}
for&nbsp;key&nbsp;in&nbsp;dic:
&nbsp;&nbsp;&nbsp;&nbsp;print(key)
for&nbsp;item&nbsp;in&nbsp;dic.items():
&nbsp;&nbsp;&nbsp;&nbsp;print(item)
for&nbsp;key,value&nbsp;in&nbsp;dic.items():
&nbsp;&nbsp;&nbsp;&nbsp;print(key,value)

执行输出 age sex name ('age', 18) ('sex', 'male') ('name', 'jin') age 18 sex male name jin

三,其他(for,enumerate,range)

for循环:用户按照顺序循环可迭代对象的内容。


msg&nbsp;=&nbsp;'我是最好的'
for&nbsp;item&nbsp;in&nbsp;msg:
&nbsp;&nbsp;&nbsp;&nbsp;print(item)
&nbsp;
li&nbsp;=&nbsp;['alex','银角','女神','egon','太白']
for&nbsp;i&nbsp;in&nbsp;li:
&nbsp;&nbsp;&nbsp;&nbsp;print(i)
&nbsp;
dic&nbsp;=&nbsp;{'name':'太白','age':18,'sex':'man'}
for&nbsp;k,v&nbsp;in&nbsp;dic.items():
&nbsp;&nbsp;&nbsp;&nbsp;print(k,v)

执行输出: 我 是 最 好 的 alex 银角 女神 egon 太白 name 太白 age 18 sex man

enumerate:枚举,对于一个可迭代的(iterable)/可遍历的对象(如列表、字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值。


li&nbsp;=&nbsp;['alex','银角','女神','egon','太白']
for&nbsp;i&nbsp;in&nbsp;enumerate(li):
&nbsp;&nbsp;&nbsp;&nbsp;print(i)
for&nbsp;index,name&nbsp;in&nbsp;enumerate(li,1):
&nbsp;&nbsp;&nbsp;&nbsp;print(index,name)
for&nbsp;index, name&nbsp;in&nbsp;enumerate(li,&nbsp;100):&nbsp;&nbsp;# 起始位置默认是0,可更改
&nbsp;&nbsp;&nbsp;&nbsp;print(index, name)

执行输出: (0, 'alex') (1, '银角') (2, '女神') (3, 'egon') (4, '太白') 1 alex 2 银角 3 女神 4 egon 5 太白 100 alex 101 银角 102 女神 103 egon 104 太白

range:指定范围,生成指定数字


for&nbsp;i&nbsp;in&nbsp;range(1,10):
&nbsp;&nbsp;&nbsp;&nbsp;print(i)
print('======')
for&nbsp;i&nbsp;in&nbsp;range(1,10,2):&nbsp;&nbsp;# 步长
&nbsp;&nbsp;&nbsp;&nbsp;print(i)
print('++++++')
for&nbsp;i&nbsp;in&nbsp;range(10,1,-2):&nbsp;# 反向步长
&nbsp;&nbsp;&nbsp;&nbsp;print(i)

执行输出

1
2
3
4
5
6
7
8
9
======
1
3
5
7
9
++++++
10
8
6
4
2

in的使用

in 操作符用于判断关键字是否存在于变量中


a&nbsp;=&nbsp;'男孩wusir'
print('男孩'&nbsp;in&nbsp;a)

执行输出: True 

in是整体匹配,不会拆分匹配。


a&nbsp;=&nbsp;'男孩wusir'
print('男孩sir'&nbsp;in&nbsp;a)

执行输出:False

比如评论的敏感词汇,会用到in 和not in


comment&nbsp;=&nbsp;input('请输入你的评论:')&nbsp;&nbsp;
if&nbsp;'苍井空'&nbsp;in&nbsp;comment:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;print('您输入的有敏感词汇,请重新输入')

执行输出 > 请输入你的评论:苍井空 您输入的有敏感词汇,请重新输入

while else的使用

while else 如果循环被break打断,程序不会走else


count&nbsp;=&nbsp;1
while&nbsp;True:
&nbsp;&nbsp;&nbsp;&nbsp;print(count)
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;count&nbsp;==&nbsp;3:break
&nbsp;&nbsp;&nbsp;&nbsp;count&nbsp;+=&nbsp;1
else:
&nbsp;&nbsp;&nbsp;&nbsp;print('循环正常完毕')

执行输出 123


count&nbsp;=&nbsp;1
falg&nbsp;=&nbsp;True
while&nbsp;falg:
&nbsp;&nbsp;&nbsp;&nbsp;print(count)
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;count&nbsp;==&nbsp;3:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;falg&nbsp;=&nbsp;False
&nbsp;&nbsp;&nbsp;&nbsp;count&nbsp;+=&nbsp;1
else:
&nbsp;&nbsp;&nbsp;&nbsp;print('循环正常完毕')

执行输出: 123 循环正常完毕

格式化输出

比如用户输入信息后,输出一段话 msg&nbsp;=&nbsp;'我叫唐僧,今年25岁,爱好念经'

第一种替换方式


#% 占位符 s str 字符串 d digit 数字
name&nbsp;=&nbsp;input('请输入你的姓名:')
age&nbsp;=&nbsp;input('请输入你的年龄:')
hobby&nbsp;=&nbsp;input('请输入你的爱好:')
msg&nbsp;=&nbsp;'我叫%s,今年%d岁,爱好%s'&nbsp;%&nbsp;(name,int(age),hobby)
print(msg)

执行输出:

> 请输入你的姓名:唐僧 请输入你的年龄:25 请输入你的爱好:念经 我叫唐僧,今年25岁,爱好念经

第二种替换方式 定义一个字典 dic&nbsp;=&nbsp;{'name':'老男孩','age':51,'hobby':'无所谓'}

完整代码如下:


dic&nbsp;=&nbsp;{'name':'jack','age':51,'hobby':'无所谓'}
msg&nbsp;=&nbsp;'我叫%(name)s,今年%(age)d岁,爱好%(hobby)s'&nbsp;%&nbsp;dic
print(msg)

执行输出: > 我叫jack,今年51岁,爱好无所谓

格式化输出,就是做固定模板填充

第三种替换方式


name&nbsp;=&nbsp;input('请输入你的姓名:')
age&nbsp;=&nbsp;input('请输入你的年龄:')
msg&nbsp;=&nbsp;'我叫%s,今年%d岁,学习进度为1%'&nbsp;%&nbsp;(name,int(age))
print(msg)

执行报错

ValueError: invalid literal for int() with base 10: 'sf'
#在格式化输出中单纯的显示% 用%% 解决

name&nbsp;=&nbsp;input('请输入你的姓名:')
age&nbsp;=&nbsp;input('请输入你的年龄:')
msg&nbsp;=&nbsp;'我叫%s,今年%d岁,学习进度为1%%'&nbsp;%&nbsp;(name,int(age))
print(msg)

执行输出 > 请输入你的姓名:zhang 请输入你的年龄:21 我叫zhang,今年21岁,学习进度为1%

第四种替换方式


name&nbsp;=&nbsp;input('请输入你的姓名:')
age&nbsp;=&nbsp;input('请输入你的年龄:')
msg&nbsp;=&nbsp;'我叫{},今年{}岁,学习进度为1%'.format(name,int(age))
print(msg)

执行输出 > 请输入你的姓名:xiao 请输入你的年龄:24 我叫xiao,今年24岁,学习进度为1%

逻辑运算符

针对逻辑运算的进一步研究: 1,在没有()的情况下not 优先级高于 and,and优先级高于or,即优先级关系为**( )>not>and>or**,同一优先级从左往右计算。

and or not 第一种:前后都是比较运算 优先级: () > not > and > or 同一个优先级,从左至右依次计算

false and 任何条件,都是false true and false,结果为false

print(1&nbsp;&gt;&nbsp;2&nbsp;and&nbsp;3&nbsp;&lt;&nbsp;4)

执行输出: False print(1&nbsp;&gt;&nbsp;2&nbsp;and&nbsp;3&nbsp;&lt;&nbsp;4&nbsp;and&nbsp;3&nbsp;&gt;&nbsp;2&nbsp;or&nbsp;2&nbsp;&lt;&nbsp;3)

执行输出: True

在or中,只要有真,结果必定为真。 False or True 结果为True print(1&nbsp;and&nbsp;2)

执行输出: 2


print(1&nbsp;and&nbsp;2)
print(0&nbsp;and&nbsp;2)

执行输出: 2 0

第二种:前后都是数字运算 x or y , x为真,值就是x,x为假,值是y; x and y, x为真,值是y,x为假,值是x。 非0的数字,都是真


print(1&nbsp;or&nbsp;3)
print(2&nbsp;or&nbsp;3)
print(0&nbsp;or&nbsp;3)
print(-1&nbsp;or&nbsp;3)

执行输出: 1 2 3 -1

第三种,混合 print(1&nbsp;&gt;&nbsp;2&nbsp;or&nbsp;3&nbsp;and&nbsp;4)

执行输出: 4


print(1&nbsp;&gt;&nbsp;2&nbsp;or&nbsp;3&nbsp;and&nbsp;4)
print(2&nbsp;or&nbsp;2&nbsp;&gt;&nbsp;3&nbsp;and&nbsp;4)

执行输出: 4 2

数据类型的转换

int与布尔值的转换 int --> bool 非0即True,0为False bool --> int True 1 False 0


print(int(True))

执行输出:1


print(int(True))
print(int(False))
print(bool(100))
print(bool(0))

执行输出: 1 0 True False

编码

编码很重要,总会遇到编码的问题 1,发电报:滴滴滴滴 实际是高低电平。 密码本:


今&nbsp;0000&nbsp;0001
天&nbsp;0000&nbsp;0101
喝&nbsp;0000&nbsp;0011
酒&nbsp;0000&nbsp;1100
去&nbsp;0001&nbsp;1010
呀&nbsp;0001&nbsp;0001

0010010 1000011 1100101 010001

2,计算机在存储,和传输的时候, 01010101

初期密码本: asiic 包含数字,英文,特殊字符。八位 01000001 01000010 01000011 A B C

8位 = 1 byte 表示一个字符。

ascii最左1位都是0,为了拓展使用的。

ASCII码表里的字符总共有256个 前128个为常用的字符如运算符 后128个为特殊字符是键盘上找不到的字符

万国码unicode,将所有国家的语言包含在这个密码本。

初期:16位,两个字节,表示一个字符。 A : 00010000 00010010 中: 00010010 00010010 升级:32位,四个字节,表示一个字符。 A : 00010000 00010010 00010000 00010010 中: 00010010 00010010 00010010 00010010 32位资源浪费。

升级:utf-8。最少用8位(一个字节),表示一个字符。

英文:a :00010000 用8位表示一个字符。

欧洲:00010000 00010000 16位两个字节表示一个字符。

亚洲中 :00010000 00010000 00010000 24位,三个字节表示一个字符。

utf-16 不常用,最少用16位

gbk:国标。 只包含:英文中文。 英文:a :00010000 8位,一个字节表示一个字符。 中文:中:00010000 00010000 16位,两个字节表示一个字符。 gb2312 也是国标的一种

单位换算 8 bit = 1 byte 1024 byte = 1 kb 1024 kb = 1 MB 1024 MB = 1 GB 1024 GB = 1 TB

作业

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

1)1 &gt; 1 or 3 &lt; 4 or 4 &gt; 5 and 2 &gt; 1 and 9 &gt; 8 or 7 &lt; 6 2)not 2 &gt; 1 and 3 &lt; 4 or 4 &gt; 5 and 2 &gt; 1 and 9 &gt; 8 or 7 &lt; 6

1)执行输出:True 2)执行输出:False


求出下列逻辑语句的值。 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

1)执行输出:8 2)执行输出:4


下列结果是什么? 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


简述变量命名规范

  • 变量是由数字、字幕、下划线,任意组合。
  • 变量不能以数字开头。
  • 不能以python的关键字。
  • 不能太长,不能中文,要有描述性。
  • 下划线命名法、单词之间用下划线链接。

name = input(“>>>”) name变量是什么数据类型? 字符串类型

if条件语句的基本结构?


#第一种
if&nbsp;条件:
&nbsp;&nbsp;&nbsp;&nbsp;结果
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
#第二种
if&nbsp;条件:
&nbsp;&nbsp;&nbsp;&nbsp;结果
else:
&nbsp;&nbsp;&nbsp;&nbsp;结果
&nbsp;
#第三种
if&nbsp;条件:
&nbsp;&nbsp;&nbsp;&nbsp;结果
elif&nbsp;条件:
&nbsp;&nbsp;&nbsp;&nbsp;结果
...
&nbsp;
#第四种
if&nbsp;条件:
&nbsp;&nbsp;&nbsp;&nbsp;结果
elif&nbsp;条件:
&nbsp;&nbsp;&nbsp;&nbsp;结果
...
else:
&nbsp;&nbsp;&nbsp;&nbsp;结果
&nbsp;
#第五种
if&nbsp;条件:
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;条件:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;结果
&nbsp;&nbsp;&nbsp;&nbsp;elif&nbsp;条件:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;结果
elif&nbsp;条件:
&nbsp;&nbsp;&nbsp;&nbsp;结果
...
else:
&nbsp;&nbsp;&nbsp;&nbsp;结果


while循环语句基本结构?


while&nbsp;条件:
&nbsp;&nbsp;&nbsp;&nbsp;结果

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


count&nbsp;=&nbsp;0
the_sum&nbsp;=&nbsp;0
while&nbsp;count &lt;&nbsp;99:
&nbsp;&nbsp;&nbsp;&nbsp;count&nbsp;+=&nbsp;1
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;count&nbsp;==&nbsp;88:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(count&nbsp;%&nbsp;2)&nbsp;==&nbsp;0:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the_sum&nbsp;-=&nbsp;count
&nbsp;&nbsp;&nbsp;&nbsp;else:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the_sum&nbsp;+=&nbsp;count
&nbsp;
print(the_sum)


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

username = 'kevin'
passwrod = '123456'
max = 3
i = 0
while i &lt; max:
    i += 1
    name = input('请输入用户名:')
    pwd = input('请输入密码:')

    if name == username and pwd == passwrod:
        print('success!')
    else:
        print('用户名或密码错误,还剩余{}次机会!'.format(max - i))

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

ascii 是最早美国用的标准信息交换码,把所有的字母大小写,各种符号用 二进制来表示,共有256种,同时加入拉丁文等字符,1bytes代表一个字符。

Unicode是为了统一世界各国语言的不同,统一用2个bytes代表一个字符,可以表达2 ** 16=65556个,称为万国语言,特点:速度快,但浪费空间,可以用在内存处理中,兼容了utf-8,gbk,ASCII。

utf-8 为了改变Unicode的这种缺点,规定1个英文字符用1个字节表示,1个中文字符用3个字节表示,特点;节省空间,速度慢,用在硬盘数据传输,网络数据传输,相比硬盘和网络速度,体现不出来。


简述位和字节的关系?

位:二进制位(bit)是计算机存储信息的基本单位,代表1个二进制数位,其值为0或1。字节:8个连续的二进制位为一个字节,可以存放1个西文字符的编码。


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

在utf-8中,一个中文字符占用3个字节。在GBK中,一个中文字符占用2个字节所以答案为9和6


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

name = input('请输入你的姓名:')
addres = input('请输入你的地点:')
hobby = input('请输入你的爱好:')

msg = '''
敬爱可亲的{}
最喜欢在{}
地{}
'''.format(name,addres,hobby)

print(msg)

等待用户输入内容,检测用户输入内容中是否包含敏感字符?如果存在敏感字符提示“存在敏感字符请重新输入”,并允许用户重新输入并打印。敏感字符:"小粉嫩"、"大铁锤"


keyword&nbsp;=&nbsp;["⼩粉嫩","⼤铁锤"]
while&nbsp;True:
&nbsp;&nbsp;&nbsp;&nbsp;comment&nbsp;=&nbsp;input('请输入评论:')
&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;filter_word&nbsp;in&nbsp;keyword:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;filter_word&nbsp;in&nbsp;comment:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print('评论含有敏感词,请重新输入!')
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break
&nbsp;&nbsp;&nbsp;&nbsp;else:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print('提交成功!')
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break

单行注释以及多行注释? 单行注释:使用符号#多行注释:使用符号""" """ 和''' '''


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

源码风格不一样 python2 源码混乱,重复代码较多。 python3 源码规范,优美清新简单。 print方法有区别 python3的print方法,必须要加括号 默认编码不一样 python 3x 默认编码:utf-8 python 2x 默认编码: ascii input不同 python 2x: raw_input() python 3x: input()


看代码书写结果

a = 1&gt;2 or 4&lt;7 and 8 == 8&nbsp;
print(a)

先执行and部分4<7 and 8 == 8 ,4<7 结果为True 。8 == 8 结果为True 。那么and部分的结果为True那么就剩下1 > 2 or True,最终结果为 True


continue和break区别?

breck:结束循环continue:结束本次循环,继续下一次循环。


Unicode,utf-8,gbk,每个编码英文,中文,分别用几个字节表示。

Unicodeutf-8gbk
英文11
中文23

转载于:https://my.oschina.net/u/3486061/blog/3065771

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值