數據類型

基本的數據類型——數字app

       布爾型:spa

                      1. bool型只有兩個值:True和Falsecode

                      2. 之因此將布爾值歸爲數字,由於咱們習慣用1 表示True,0表示False。blog

      整型排序

      浮點型索引

      複數rem

基本的數據類型——字符串字符串

                     字符串是一個有序的字符的集合,用於存儲和表示基本的文本信息,' '或'' ''或''' '''中間包含的內容稱之爲字符串it

                     字符串的單引號和雙引號都沒法取消特殊字符的含義,若是想讓引號內全部字符均取消特殊意義,在引號前面加r,如name=r'l\thf'class

 

基本的數據類型——列表

                     定義:[]內以逗號分隔,按照索引,存放各類數據類型,每一個位置表明一個元素

                     

經常使用操做:

1. 索引

1 color = ["red","blue","green","white"]
2 索引:
3 print('color[0]')

2. 切片

1 color = ["red","blue","green","white"]
2 print("color[0:2]")
3 >>>red,blue
4 
5 print("color[1:3]")
6 >>>blue,green

倒着切
color = ["red","blue","green","white"]
color[-1:-5:-1]

字符串也能夠反轉
n = "進擊的小吳"
n[::-1]
>>>n = "吳小的擊進"

3. 追加

1 color = ["red","blue","green","white"]
2 color.append("black")
3 >>>color = ["red","blue","green","white","black"]

插入
color = ["red","blue","green","white"]
color.insert(2,"pink")
print(color.insert(2,"pink"))
>>>
color = ["red","blue","pink","green","white"]
 

4. 刪除

1 color = ["red","blue","green","white"]
2 color.remove("white")  #刪除從左到右的第一個值
3 
4 color = ["red","blue","green","white"]
5 color.pop()
>>>"white"
6 >>>color = ["red","blue","green"]

color = ["red","blue","green","white"]
print("del color[red]")
>>>color = ["blue","green","white"]

5. 長度

1 len(color)
2 >>>3

6. 包含和循環

>>> 'red' in color
True
>>> 'pink' in color
False

#循環:爲何是「i」?
>>> for i in color:
    print(i)

7. 排序

1 a = [2,3,4,9,7,15]
2 a.sort()
3 print(a.sort())
>>>a = [2,3,4,7,9,15]

 

 8. 查詢

1 color = ["red","blue","green","white"]
2 color.index("blue")
3 >>>print(color.index("blue"))
4 >>>1

color = ["red","blue","green","white"]color.count("red")>>>1
相關文章
相關標籤/搜索