- 學(xué)習(xí)測試開發(fā)的Day70擅编,真棒攀细!
- 學(xué)習(xí)時間為1H15M
- 第七次全天課20190126(下午視頻3H32M-4H10M)
字典內(nèi)置的常用函數(shù)
使用has_key函數(shù)已經(jīng)從python3中取消,如何判斷某個key在字典中是否存在:
a={1:'a',2:"b",3:"c"}
print (1 in a.keys())
print (4 in a.keys())
使用update函數(shù)來更新字典的value值
a={1:'a',2:"b",3:"c"}
a.update({4:'c'}) #若不存在key,則增加key和value
a.update({1:'x'}) #若key存在爱态,則直接更新key對應(yīng)的value
print (a)
>>> d
{1: None, 2: None}
>>> d.update({1:100})
>>> d
{1: 100, 2: None}
>>> d.update({100:100})
>>> d
{1: 100, 2: None, 100: 100}
>>>
字典內(nèi)置的常用函數(shù)之pop數(shù)據(jù)
image.png
popitem()
>>> d.popitem()
(1, 100)
>>> d.popitem()
(2, None)
>>> d.popitem()
(100, 100)
>>> d.popitem()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'popitem(): dictionary is empty'
>>> d
{}
pop()
>>> help(dict.pop)
Help on method_descriptor:
pop(...)
D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised
>>> d={1:2,3:3,5:6}
>>> d
{1: 2, 3: 3, 5: 6}
>>> d.pop(1)
2
>>> d.pop(1,100)
100
>>> d
{3: 3, 5: 6}
>>>
查看字典的方法
>>> dir(dict)
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
>>>
序列切片
列表谭贪、元組和字符串都是序列。
序列的兩個主要特點是索引操作符和切片操作符肢藐。
切片操作符是序列名后跟一個方括號故河,方括號中有一對可選的數(shù)字,并且用冒號分隔吆豹。
切片操作符中的第一個數(shù)(冒號之前)表示切片開始的位置鱼的,第二個數(shù)(冒號之后)表示切片的結(jié)束位置,但不包括這個位置(如:list[2:5])痘煤。
如果切片時凑阶,不指定第一個數(shù),Python就會從序列的首位置開始(如list[:5])衷快。如果不指定結(jié)束位置宙橱,Python就會停止在序列尾(如:list[3:])
代碼示例1:字符串使用切片
>>> str1
'abdcdwesdfa'
>>> str1[1:5]
'bdcd'
>>> str1[:4]
'abdc'
>>> str1[3:]
'cdwesdfa'
>>> str1[-4:-1]
'sdf'
>>> str1[:20] #超出了序列的長度,一樣可以進
行切片蘸拔,返回整個序列
'abdcdwesdfa
# -*- coding: utf-8 -*-
bookList=['google test','yahaoo
test','sogou test','alibaba test']
print ("我的第一本書",bookList[0])
print ("我的第二本書",bookList[1])
print ("我的第三本書",bookList[2])
print ("我的第四本書",bookList[3])
#切片
print ("我的第二和第三本書
",bookList[1:3])
print ("我的倒數(shù)兩本書",bookList[2:])
print ("我的最后一本書",bookList[-
1])
print ("我的倒數(shù)第二本書",bookList[-
2])
print ("我的全部書",bookList[:])
string="gloryroad test"
#遍歷字符串:
for s in string:
print (s)
#字符串的切片操作
print ("字符串的前2個字母
",string[0:2])
print ("字符串的最后2個字母",string[-
2:])
print ("字符串的第3到第5個字母
",string[2:5])
print ("倒著打印最后字母",string[-1])
print ("輸出單數(shù)的字母",string[::2])
print ("輸出雙數(shù)的字母",string[1::2])
print ("倒著輸出所有字母",string[::-1])
print ("倒著輸出單數(shù)字母",string[::-2])
練習(xí):圖書館
注冊和登錄
users={"helloyetta":"123456"}
login_users={}
books={}
def register():
global users
while 1:
username = input("請輸入您要注冊的用戶名:")
if not len(username)>=7:
print("您輸入的用戶名少于7位师郑,請重新輸入")
continue
for i in username:
if not (i>='a' and i<='z') and i!="_":
print("您輸入的用戶名必須是小寫字母或者_!")
break
else:
if username in users:
print("您輸入的用戶名已經(jīng)存在调窍,請重新輸入宝冕!")
continue
break
while 1:
password=input("請輸入您要使用的密碼:")
if not len(password) >=7:
print("您輸入的密碼少于7位,請重新輸入")
continue
for i in password:
if not(i>='a' and i<='z') and i!='_' and not (i>='0' and i<='9'):
print("您輸入的密碼必須是小寫字母邓萨、_地梨、數(shù)字!")
break
else:
password2=input("請再次輸入您要使用的密碼:")
if password==password2:
users[username]=password
print("%s 注冊已經(jīng)成功!"%username)
break
else:
print("兩次密碼錄入不一致缔恳!請重新錄入宝剖!")
def login():
global users
while 1:
login_username = input("請輸入登錄的用戶名:")
login_password=input("請輸入登錄的密碼:")
if login_username in users:
if login_password==users[login_username]:
print("%s 用戶登錄成功!"%login_username )
login_users[login_username]=None
break
print("您輸入的用戶名和密碼有誤歉甚,請重新輸入万细!")
#login()
#register()
print("歡迎使用光榮之路圖書館系統(tǒng):")
print("""
命令清單:
注冊用戶請輸入:register
登錄請輸入:login
""")
while 1:
command=input("請您輸入要操作的命令:")
if not command in ["register","login"]:
print("您輸入的命令不存在,請重新輸入纸泄!")
continue
exec(command+"()")
結(jié)果:
PS D:\0grory\day7> python .\book.py
歡迎使用光榮之路圖書館系統(tǒng):
命令清單:
注冊用戶請輸入:register
登錄請輸入:login
請您輸入要操作的命令:login
請輸入登錄的用戶名:helloyetta
請輸入登錄的密碼:123456
helloyetta 用戶登錄成功雅镊!
請您輸入要操作的命令:register
請輸入您要注冊的用戶名:teacherwang
請輸入您要使用的密碼:12345678
請再次輸入您要使用的密碼:12345678
teacherwang 注冊已經(jīng)成功!
請您輸入要操作的命令:login
請輸入登錄的用戶名:teacherwang
請輸入登錄的密碼:12345678
teacherwang 用戶登錄成功刃滓!
請您輸入要操作的命令:
注冊還有個小bug,下段代碼有點問題仁烹,如果錄入不符合下面判斷的如yetta1234,不會讓重新輸入用戶名,而是給出提示信息后直接到輸入密碼了咧虎,還需要再想下.....
'''
for i in username:
if not (i>='a' and i<='z') and i!="":
print("您輸入的用戶名必須是小寫字母或者卓缰!")
break
'''