今天学习内容如下:
1.学习昨天练习题目的解题新方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #1、使用while循环输入 1 2 3 4 5 6 8 9 10 2 ''' 3 count = 0 4 while count < 10: 5 count += 1 # count = count + 1 6 if count == 7: 7 print(' ') 8 else: 9 print(count) 10 11 count = 0 12 while count < 10: 13 count += 1 # count = count + 1 14 if count == 7: 15 continue 16 print(count) 17 ''' 18 #3、输出 1-100 内的所有奇数 19 #方法一: 20 # count = 1 21 # while count < 101: 22 # print(count) 23 # count += 2 24 #方法二: 25 # count = 1 26 # while count < 101: 27 # if count % 2 == 1: 28 # print(count) 29 # count += 1 30 31 #5、求1-2+3-4+5 ... 99的所有数的和 32 # sum = 0 33 # count = 1 34 # while count < 100: 35 # if count % 2 == 0: 36 # sum = sum - count 37 # else: 38 # sum = sum + count 39 # count += 1 40 # print(sum) 41 42 #6、用户登陆(三次机会重试) 43 #input 心中有账号,密码 while 44 45 i = 0 46 while i < 3: 47 username = input('请输入账号:') 48 password = int(input('请输入密码:')) 49 if username == '咸鱼哥' and password == 123: 50 print('登录成功') 51 else: 52 print('登录失败请重新登录') 53 i += 1
2.下载python2.7.在pycharm中体验与Python3的不同,在pycharm中不断切换python2和python3
3.格式化输出%s和%d(在python2和python3下的不同)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# -*- encoding:utf8 -*- # print(123) # print("china") # print("总怪") # 比较以下两者的不同 name = input('请输入姓名:') year = input("请输入年龄:") msg = "我叫%s,我今年%d" %(name,year) # msg = "我叫%s,我今年%d" %(name,year) print(msg) # name = raw_input('请输入姓名:') # year = raw_input("请输入年龄:") # msg = "我叫%s,我今年%d" %(name,int(year)) # # msg = "我叫%s,我今年%d" %(name,year) # print(msg)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# print(123) # print("china") # print("总怪") # 比较以下两者的不同 name = input('请输入姓名:') year = input("请输入年龄:") msg = "我叫%s,我今年%d" %(name,year) # msg = "我叫%s,我今年%d" %(name,year) print(msg) # name = raw_input('请输入姓名:') # year = raw_input("请输入年龄:") # msg = "我叫%s,我今年%d" %(name,int(year)) # # msg = "我叫%s,我今年%d" %(name,year) # print(msg)
在Python2中input输入的字符串必须加上引号,python3则默认输入的为字符串,需要读者仔细体会不同。
4.while,else中break的用法,跳出循环不执行else中内容,若改成pass则执行else
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
count = 0 while count <= 5 : count += 1 if count == 3:break print("Loop",count) else: print("循环正常执行完啦") print("-----out of while loop ------") count = 0 while count <= 5 : count += 1 if count == 3:pass print("Loop",count) else: print("循环正常执行完啦") print("-----out of while loop ------")
5.字符串常用的方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# s = 16 # print(type(s)) # # # b = str(s) # print(b, type(b)) # print(1 > 2) # print(2 > 1) # s = 0 # m = bool(s) # print(m) # print(s[0]) # print(s[3]) # print(s[0:3]) # print(s[0:100]) # print(s[0:])#默认到最后 # print(s[5:-1])#-1就是最后一个 # print(s[0:-1:2])#加步长 # print(s[-1:0:-2]) #反向加步长 # s = 'dDg dg Dg' # print(s.capitalize())#首字母大写 # print(s.swapcase())#大小写翻转 # print(s.title())#每个单词的首字母大写 # s1 = s.center(20, '0')# 内容居中,总长度,空白处填充 # print(s1) # print(s.count('g'))#数字符串中的元素出现的个数。 # print(s.count('d', 0, 2))# 可切片 # s = 'ddg\tgdg' # # s2=s.expandtabs()#\t前面的补全 # # # 默认将一个tab键变成8个空格,如果tab前面的字符长度不足8个,则补全8个,如果tab键前面的字符长度超过8个不足16个则补全16个,以此类推每次补全8个。 # # print(s2) # s='gdkgljjldgj' # #startswith 判断是否以...开头 # #endswith 判断是否以...结尾 # print(s.startswith('g', 2, 4)) # print(s.startswith('g',)) # print(s.startswith('g', 0, 4)) # print(s.endswith('g', 0, 4)) # print(s.endswith('g', 0, 5)) # s='dggdhhfkfd' # print(s.find('sdg', 2, 5)) # print(s.find('dg', 0, 5))#寻找字符串中的元素是否存在 # print(s.find('g', 0, 5))# 返回的找到的元素的索引,如果找不到返回-1 # print(s.index('h', 0, 5)) # # print(s.index('nnb', 0, 5))# 返回的找到的元素的索引,找不到报错。 # s='dgs,gdh,ghf' # #split 以什么分割,最终形成一个列表此列表不含有这个分割的元素。 # print(s.split('s', 1)) # print(s.rsplit('g',1)) # print(s.split('g',2)) #format的三种玩法 格式化输出 # print('{},{},{}'.format(0,1,2)) # print('{} {} {}'.format(0,1,2)) # print('{2} {1} {0}'.format(0,1,3)) # print('{0} {2} {1}'.format(0,1,2)) # print('{0} {2} {2}'.format(0,1,3)) # print('{0} {2} {1}'.format('my','name','is')) # print('{name} {age} {sex}'.format(name='sd',age='s',sex='m')) # s=' %%%gdg%%dsjgldlj%%%% ' # print(s.strip('%')) # print(s.strip())#删掉字符串开头或结尾的字符 # print(s.rstrip()) # print(s.rstrip('%')) # # print(s.lstrip('%')) # 替换replace # name='tom say :i have one tesla,my name is tom' # print(name.replace('tom', 'sb',1)) #is系列 s='ddkjsdgj' print(s.isalnum())#字符串由字母或数字组成 print(s.isalpha())#字符串只由字母组成 print(s.isdigit())#字符串只由数字组成
6.列表的增删改查
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# 列表的元素增加 ''' li = [1,2,'a','s','g'] # li.insert(3,'w') # li.append('w') # li.extend('d,d,s') # li.extend(['d','d','s']) li.extend(['d','d','s','asd']) print(li) ''' # 列表的元素删除 # li=['s','g','d','g','s','s','h'] # print(li.pop(0)) #按照位置去删除,有返回值 # # print(li) # # del li[0:3] # # print(li) #按照位置去删除,也可切片删除没有返回值。 # print(li.remove('d')) #按照元素去删除 # print(li) # print(li.clear()) #清空列表 # 列表的元素修改 li = ['s','g','d','d','e'] li[1] = 'h' li[2:4] = ['s','g'] print(li)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
a = ['s','s','g','h','d'] print(a.count('s'))#count(数)(方法统计某个元素在列表中出现的次数)。 print(a.index('s')) # sort (方法用于在原位置对列表进行排序)。 # reverse (方法将列表中的元素反向存放)。 print(a.sort())# 他没有返回值,所以只能打印a print(a) print(a.reverse())# 他没有返回值,所以只能打印a print(a)
7.字典的增删改查
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# 字典的增加 # dic = {'name': 'shang', 'age': 18, 'year': 23, 'sex':'male'} # print(dic) # # setdefault 在字典中添加键值对,如果只有键那对应的值是none,但是如果原字典中存在设置的键值对,则他不会更改或者覆盖。 # print(dic.setdefault('name','dg')) # print(dic.setdefault('nam','dg')) # print(dic) # 字典的删除 # dic = {'name': 'shang', 'age': 18, 'year': 23, 'sex':'male'} # # print(dic.pop('name',23)) # print(dic)# pop根据key删除键值对,并返回对应的值,如果没有key则返回默认返回值 # print(dic.pop('ame',23)) # del dic['name'] # print(dic) #print(dic.popitem())# 随机删除字典中的某个键值对,将删除的键值对以元祖的形式返回 # print(dic.clear()) # 清空字典 # print(dic) # 字典的修改 # dic = {'name': 'shang', 'age': 18, 'year': 23, 'sex':'male'} # dic1={'name':'chun','age':'17','sggh':'ss'} # print(dic1.update(dic))# 将dic所有的键值对覆盖添加(相同的覆盖,没有的添加)到dic2中 # print(dic1) # 字典的查看 # dic1={'name':'chun','age':'17','sggh':'ss'} # print(dic1['name']) # #print(dic1['nae'])# 没有会报错 # print(dic.get('nae','方法'))# 没有可以返回设定的返回值 # 其它 # dic1={'name':'chun','age':'17','sggh':'ss'} # s = dic1.items() # print(s,type(s)) 这个类型就是dict_items类型,可迭代的 # keys = dic1.keys() # print(keys,type(keys) # for i in dic1: # print(i) # for item in dic1.items(): # print(item) # for key,value in s: # print(key, value) # for i,v in dic1.items(): # print(i,v)字典的遍历 # s='上当过sdgjslgegljsd' # for i in s: # print(i) # li = ['alex','银角','女神','egon','太白'] # # for i in li: # # print(i) # #enumerate:枚举,对于一个可迭代的(iterable)/可遍历的对象(如列表、字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值。 # for i in enumerate(li): # print(i) # # for index,name in enumerate(li): # print(index,name) # # for index,name in enumerate(li,1): # print(index,name) # for index,name in enumerate(li,100): # print(index,name) # s='上当过sdgjslgegljsd' # for i in enumerate(s): # print(i) # # for index,name in enumerate(s): # print(index,name) # # for index,name in enumerate(s,1): # print(index,name) # for index,name in enumerate(s,100): # print(index,name) # range:指定范围,生成指定数字。 for i in range(10): print(i) for i in range(1,10,2): print(i) for i in range(10, 1, -2): print(i)