一,引子。
1 什么是数据?
x=10,10是我们要存储的数据
2 为何数据要分不同的类型
数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示
3 数据类型
数字
字符串
列表
元组
字典
集合
二基础数据类型。
2.1数字int。
数字主要是用于计算用的,使用方法并不是很多,就记住一种就可以:
#bit_length() 当十进制用二进制表示时,最少使用的位数 v = 11 data = v.bit_length() print(data)
2.2布尔值bool。
布尔值就两种:True,False。就是反应条件的正确与否。
真 1 True。
假 0 False。
2.3字符串str。
2.3.1、字符串的索引与切片。
索引即下标,就是字符串组成的元素从第一个开始,初始索引为0以此类推。
a = 'ABCDEFGHIJK' print(a[0]) print(a[3]) print(a[5]) print(a[7])
切片就是通过索引(索引:索引:步长)截取字符串的一段,形成新的字符串(原则就是顾头不顾腚)。
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]) #反向加步长
2.3.2、字符串常用方法。
2.4元祖tupe。
元组被称为只读列表,即数据可以被查询,但不能被修改,所以,字符串的切片操作同样适用于元组。例:(1,2,3)("a","b","c")
2.5列表list。
列表是python中的基础数据类型之一,其他语言中也有类似于列表的数据类型,比如js中叫数组,他是以[]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如:
li = [‘alex’,123,Ture,(1,2,3,’wusir’),[1,2,3,’小明’,],{‘name’:’alex’}]
列表相比于字符串,不仅可以储存不同的数据类型,而且可以储存大量数据,32位python的限制是 536870912 个元素,64位python的限制是 1152921504606846975 个元素。而且列表是有序的,有索引值,可切片,方便取值。
2.5.1、增。
2.5.2、删。
2.5.3、改。
2.5.4、查。
切片去查,或者循环去查。
2.5.5、其他操作。
count(数)(方法统计某个元素在列表中出现的次数)。
1 a = ["q","w","q","r","t","y"] 2 print(a.count("q"))
index(方法用于从列表中找出某个值第一个匹配项的索引位置)
1 a = ["q","w","r","t","y"] 2 print(a.index("r"))
sort (方法用于在原位置对列表进行排序)。
reverse (方法将列表中的元素反向存放)。
1 a = [2,1,3,4,5] 2 a.sort()# 他没有返回值,所以只能打印a 3 print(a) 4 a.reverse()#他也没有返回值,所以只能打印a 5 print(a)
2.6字典dict。
字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据。python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必须是可哈希的。可哈希表示key必须是不可变类型,如:数字、字符串、元组。
字典(dictionary)是除列表意外python之中最灵活的内置数据结构类型。列表是有序的对象结合,字典是无序的对象集合。两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。
2.6.1、增。
2.6.2、删。
2.6.3、改。
2.6.4、查。
2.6.5、其他操作。
字典的循环。
# dic = {"name":"jin","age":18,"sex":"male"} # for key in dic: # print(key) # for item in dic.items(): # print(item) # for key,value in dic.items(): # print(key,value)
三,其他(for,enumerate,range)。
for循环:用户按照顺序循环可迭代对象的内容。
msg = '老男孩python是全国范围内最好的python培训机构' for item in msg: print(item) li = ['alex','银角','女神','egon','太白'] for i in li: print(i) dic = {'name':'太白','age':18,'sex':'man'} for k,v in dic.items(): print(k,v)
enumerate:枚举,对于一个可迭代的(iterable)/可遍历的对象(如列表、字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值。
li = ['alex','银角','女神','egon','太白'] for i in enumerate(li): print(i) for index,name in enumerate(li,1): print(index,name) for index, name in enumerate(li, 100): # 起始位置默认是0,可更改 print(index, name)
range:指定范围,生成指定数字。
for i in range(1,10): print(i) for i in range(1,10,2): # 步长 print(i) for i in range(10,1,-2): # 反向步长 print(i)
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
3) 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8 and 4 > 6 or 3 < 2
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 9 or 3
4
3) 5 and 9 or 10 and 2 or 3 and 5 or 4 or 5
9
3、下列结果是什么?
1) 6 or 2 > 1
6,因为'x' or 'y' 取x
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,因为'x' and 'y' x为真时,取y
7) 0 and 3 > 1
0 因为'x' and 'y' x为0时,取x
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
解:
3 > 1 and 2 :3>1 为真,所以取2
2 < 3 and 3 and 4 :2<3 为真,取3,3 and 4,取4
2 or 4 or 3>2
2
4、求出1-2+3-4+5...+99中,除去88以外的所有数的和?
i=1 sum=0 while i < 100: if i=88: i += i if i%2!=0: sum=sum+i continue else: sum=sum-1 i += 1 print(sum)
5、求出1-2+3-4+5...-99中,除去88以外的所有数的和?
i=0 j=-1 sum=0 while i < 99: i = i + 1 if i==88: continue else: j = -j sum=sum+i*j print(sum)
6、用户登录(三次机会)并且每次输错时显示剩余登录机会(用到字符串格式化)
1 while count < 3: 2 username = input("请输入你的用户名:") 3 password = input("请输入你的密码:") 4 if username == "xiaoj" and password == "123456": 5 print("登录成功!!!") 6 break 7 if 2-count != 0: 8 msg = '''你还剩余 %s 次登录机会 9 ''' % (2-count) 10 print(msg) 11 else: 12 print("你已经没有机会了!") 13 14 count += 1 15 if 3-count == 0: 16 try1 = input("是否还想在重新试一试???是,请输入yes:") 17 if try1 == "yes": 18 count = 0 19 else: 20 print("滚!!!")