一、 數(shù)據(jù)類型
變量的定義與聲明
#變量a為空 在引用變量前一定要聲明或者復(fù)制 不然會報錯
#聲明 :
a = ' ' b =3
#引用
a = b
print(a)
常用的基礎(chǔ)數(shù)據(jù)類型
#整型 ( int) 整數(shù)
a = 1
#浮點型(float) 精確到小數(shù)點后兩位
a = 0.01
#布爾值(Booleans) 包含Ture 和 False 一般用于判斷
#字符串(str) 用雙引號 或者單引號 引用的稱之為字符串
a = ' hello word ~~ today 2021-09-03'
字符串的常見操作
字符串的截取
# 獲取第五個字符
str1 = str[4]
print(str1)
#返回結(jié)果
h
# 獲取全部字符
str2 = str[:]
print(str2)
#返回結(jié)果
rss hahaha ~~
# 獲取1-3個字符
str3 = str[0:3]
print(str3)
#返回結(jié)果
rss
字符串的運算
# 字符串的拼接
str_1 = 'hello'
str_2 = 'rss'
print(str_1 +" "+ str_2)
#返回結(jié)果
hello rss
# 重復(fù)輸出
print(str_1 * 2)
#返回結(jié)果
hellohello
字符串的常用函數(shù)
# 分割 split(分割符,次數(shù)) 根據(jù)傳入的參數(shù)進行分割 次數(shù)就是分割幾次 返回的是一個列表
str_1 = 'hello:python:today:is good day'
print(str_1.split(':',2))
# 返回結(jié)果
['hello', 'python', 'today:is good day']
# strip 去掉頭部 和尾部 指定字符
str_1 = 'hello python today is good day2222'
print(str_1.strip('2'))
# 返回結(jié)果
hello python today is good day
元組的數(shù)據(jù)類型
# 元組的特點:
# 1、()包含起來的
# 2、是有序的數(shù)據(jù)類型
# 3、元組的元素 包含不同的數(shù)據(jù)類型
# 4、元組的元素不可更改 但是字典和列表可以更改
# 5、如果只有一個元素 后邊加一個, a = (1,)
tuple_1 = (1,[2,5,3],(0,1),{'today':'2021-09-05'},'哈哈哈哈')
a = (1,)
print(tuple_1)
print(a)
列表的數(shù)據(jù)類型
# 、定義:
list_1 = [1,2,9,'hhh','和']
# 取值單個元素
print(list_1[0]) #1 下標從0開始
# 取值 范圍
print(list_1[0:3]) #[1, 2, 9] 取值范圍是m~n-1(例如:0:3 取到的就是前三位)
# 取值 for循環(huán)遍歷
for i in list_1:
print(i)
# 常用方法
# append() 只能在列表的末尾添加數(shù)據(jù) 每次只能添加一個值
list_1.append('add')
print(list_1)
# insert() 可在指定位置添加值
list_1.insert(0,'first')
print(list_1)
字典的數(shù)據(jù)類型
# 1.字典'{ }'括號括起來的
# 2.字符是無序的
# 3。字典的元素可以是不同的數(shù)據(jù)類型
# 4。字典的元素可以更改 但是如果包含元組 里邊的元素不可以更改 只能整體替換
# 5。字典的元素 按照 key value 健值對存在
# 定義一個字典
dict1 = {'username':'rhh','phone':'123456'}
# 字典取值
# 取單個元素
name = dict1['username']
print('name:',name)
# 結(jié)果:
# name: rhh
# for循環(huán)獲取元素
for i in dict1.values():
print(i)
# 結(jié)果:
# rhh
# 123456
# 常用方法如下:
# 新增元素
dict1['age'] = 19
print('dict1',dict1)
# 刪除元素
dict1.pop('age')
print('dict1',dict1)
# 修改元素
dict1['username'] = 'user'
print('dict1',dict1)