python-字串,字典,列表
阿新 • • 發佈:2020-09-08
0x01 字串
python單雙引號都可以
str = "hello world"
str_test = "yicunyiye"
print(str,str_test)
註釋
#單行註釋
"""
多行註釋
"""
input你輸入了任何東西都強轉成字串輸出
str = "hello world" str_test = "yicunyiye" print(str,str_test) print("hello \n world") print(str_test+"\n"+str) print("\t hello") print("'") print('"') input_test = input('>>>') print("你輸入了:",input_test)
也可以c語言風格
intTest = 1234
print("int is %d"%intTest)
%r原本替換
rtest = '"123'
print("your input is %r"%rtest)
輸出
your input is '"123'
使用terminal
from sys import argv
print('你輸入的引數是:%r'%argv)
from sys import argv
print('你輸入的引數是:%r'%argv[1])
在terminal中輸入
python StringTest.py yicunyiye
輸出
你輸入的引數是:['StringTest.py', 'yicunyiye']
你輸入的引數是:'yicunyiye'
0x02 列表
列表
split
序號切片
pop
append
len
remove
strTest = '1 2 3 4 5 6'
print(strTest.split(' '))
輸出
['1', '2', '3', '4', '5', '6']
增刪改查
1.新增
listTest.append("yicunyiye")
print(listTest)
輸出
[1, 2, 3, 4, 5, 'yicunyiye']
2.彈出
print(listTest.pop())
輸出
yicunyiye
原列表就沒有yicunyiye了,相當於刪除表尾元素
刪除,寫3就是刪除3寫'a'就是刪除a
listTest = [1,2,'a',4,5]
listTest.remove('a')
print(listTest)
輸出
[1, 2, 4, 5]
列表是從0開始的
print(listTest[0])
輸出1
listTest = [1,2,4,5]
print(listTest[1:3])
輸出[2, 4]
可以知道左閉右合
計算列表長度
print(len(listTest))
0x03 字典
增加
查詢
刪除
改變
取出所有
#鍵 值 對
dictTest = {"one":"yicunyiye","two":"wutang"}
print(dictTest)
輸出
{'one': 'yicunyiye', 'two': 'wutang'}
增加
#增加
dictTest["three"] = "keji"
print(dictTest)
輸出
{'one': 'yicunyiye', 'two': 'wutang', 'three': 'keji'}
刪除
#刪除
del dictTest["three"]
#dictTest.pop("two")
print(dictTest)
輸出
{'one': 'yicunyiye', 'two': 'wutang'}
改變
#改變
dictTest["two"] = "yicunyiye"
print(dictTest)
輸出
{'one': 'yicunyiye', 'two': 'yicunyiye'}
查詢
#查詢
print(dictTest["one"])
print(dictTest.get("two"))
輸出
yicunyiye
取出所有
#取出所有
print(dictTest.items())
輸出
dict_items([('one', 'yicunyiye'), ('two', 'yicunyiye')])
複製
#複製
newDict = dictTest.copy()
print(newDict)
輸出
{'one': 'yicunyiye', 'two': 'yicunyiye'}