有一段时间没有更新博客了,一是因为工作实在比较忙,即使在年尾了,公司的产品还需要发版,第二个是在学习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中的单行注释是用"#",而多行注释则用:
"""
注释内容
"""