有一段時間沒有更新博客了,一是因為工作實在比較忙,即使在年尾了,公司的產品還需要發版,第二個是在學習python語言,因為公司的App的打包都是自動化的,用python寫的,看著好像很牛的樣子。在網上查了一下python的整體介紹,python還可以做web開發以及很多事,若想做全棧工程師的話,python確實是一個不錯的選擇。好的。下面就開始來學習它的一些基礎語法吧!
數據類型
不管是何種語言,它都有自己的數據類型,那python的數據類型有哪些呢?
print(type("hello")) # 1 <class 'str'>
print(type(1)) # 2 <class 'int'>
print(type(1.5)) # 3 <class 'float'>
print(type(True)) # 4 <class 'bool'>
print(type([1, "2", 3, "你好"])) # 5 <class 'list'>
print(type((1, 2, 3, 4))) # 6 <class 'tuple'>
print(type({"name": "blain"})) # 7 <class 'dict'>
print(type({"name", "blain", 1})) # 8 <class 'set'>
print(type(None)) # 9 <class 'NoneType'>
type方法是用來判斷數據類型的
- str,int,float,bool
從上面的打印結果可以看出,1-4的類型是我們非常熟悉的,不過這里有一些不同,第一python是沒有double類型的,即使是1.554都是float類型,第二python中的布爾值首字母是大寫:True和False
-
list,tuple,dict,set,None
-
list:列表 它是一種有序的集合,我們可以對其進行增刪
在列表末尾添加元素:append()
test = ["hello"]
print("before test = ", test) # before test = ['hello']
test.append("world")
print("after test = ", test) # after test = ['hello', 'world']-
刪除列表末尾元素:pop()
test = ["hello", "World"]
deleteItem = test.pop()
print("after test = ", test) # after test = ['hello']
print("deleteItem = ", deleteItem) # deleteItem = Worldpop方法返回的是被刪除的元素
-
在列表指定位置添加元素:insert()
test = ["hello", "World", "China"] test.insert(2, "Love") print("after test = ", test) # after test = ['hello', 'World', 'Love', 'China']
-
刪除列表指定位置元素
test = ["hello", "World", "China"] deleteItem = test.pop(1) print("after test = ", test) # after test = ['hello', 'China'] print("deleteItem = ", deleteItem) # deleteItem = World
-
刪除列表任意元素
test = ["hello", "World", "China"] test.remove("hello") print("after test=", test) # after test= ['World', 'China']
-
獲取列表的長度:len()
test = ["hello", "World", "China"] print(len(test)) # length = 3
-
獲取列表中的元素
我們一般獲取列表中的元素是從索引0開始來獲取列表第一個元素,在python中也可以從最后一個元素開始獲取(當然不是借助列表的lenght來獲取), 最后一個元素的索引為-1,后面依此類推
test = ["hello", "World", "China"] print("test[-1] = ", test[-1]) # test[-1] = China print("test[-2] = ", test[-2]) # test[-2] = World print("test[-3] = ", test[-3]) # test[-3] = hello
-
tuple: 元組 它也是一種有序的列表,與list非常類似,不過tuple被初始化后就不能被修改了。也就意味著它沒有append,pop,insert等方法。
test = ("hello", "World", "China")
注意
當定義只有1個元素時的tuple,需要借助",";否則編譯器認為你只是在賦值。因為()既可以表示tuple,又可以表示數學公式中的小括號,這就產生了歧義
test1 = ("你好",) print("test1=", test1) # test1= ('你好',) test2 = ("你好") print("test2=", test2) # test2= 你好
-
dict:字典 也就是Java中的鍵值對(key-value),外形長得有點像json格式
test = {"name": "BlainPeng"} print(test) # {'name': 'BlainPeng'}
-
初始化dict(上面的也算一種),直接通過key來指定value
test = {} test["name"] = "BlainPeng" print(test) # {'name': 'BlainPeng'}
-
判斷key是否存在(通過in或get方法來判斷)
test = {"name": "BlainPeng"} # 方式一 print("Hello" in test) # False # 方式二 默認是返回None print(test.get("Hello")) # None # 也可以自定義返回值 print(test.get("Hello", "-2")) # -2
-
刪除key
test = {"name": "BlainPeng"} test.pop("name") print(test) # {}
-
與list的比較
- dict查找和插入的速度極快,不會隨著key的增加而變慢;(可以想像查字典)
- dict需要占用大量的內存,內存浪費多。
- list查找和插入的時間隨著元素的增加而增加;
- list占用空間小,浪費內存很少。
從上面幾條可以看出:
(1)dict是用空間來換取時間的一種方法。
(2)dict的key必須是不可變對象。這是因為dict根據key來計算value的存儲位置,如果每次計算相同的key得出的結果不同,那dict內部就完全混亂了。這個通過key計算位置的算法稱為哈希算法(Hash)。要保證hash的正確性,作為key的對象就不能變。在Python中,字符串、整數等都是不可變的,因此,可以放心地作為key。而list是可變的,就不能作為key。
-
-
set: 無序的、元素唯一的集合。與dict類似,也是一組key的集合,但不存儲value
test = {"name", "BlainPeng", "KuGou"} print(test) # {'name', 'KuGou', 'BlainPeng'}
-
通過set方法將一個iterable對象轉換成set集合
test = set("abc123") print(test) # {'2', 'c', '3', '1', 'b', 'a'}
iterable對象就是指可以用for循環的數據類型,如str,list,tuple
-
添加元素到key中:add()
test = set("abc123") print(test) test.add(1) print(test) # {1, '2', '1', 'a', '3', 'c', 'b'}
-
刪除key
test = set("abc123") print(test) # {'a', '2', '3', '1', 'b', 'c'} test.remove("a") print(test) # {'2', '3', '1', 'b', 'c'}
-
None 相當于Java中的null
-
總結
好了,Python學習就先學到這里,對于Python的數據類型,雖然與Java中有一些不同,不過還是比較簡單的,熟悉一些API就可以了。最后還說一下,python中的單行注釋是用"#",而多行注釋則用:
"""
注釋內容
"""