Python資料型別之基礎記。。。
阿新 • • 發佈:2020-08-21
資料型別
1、什麼是資料?為何要有資料?
==> x=10,10是我們要儲存的資料 。資料是用來表示狀態的,不同的狀態就應該用不同的型別的資料去表示
2、資料型別
A*數字(整型,長整型,浮點型,複數)
B*字串
C*列表
D*元組
E*字典
F*集合
3、資料的使用一般是按照以下幾點展開
#======================================基本使用======================================
#1、用途
#2、定義方式
#3、常用操作+內建的方法
#======================================該型別總結====================================
#存一個值or存多個值
#有序or無序
#可變or不可變(1、可變:值變,id不變。可變==不可hash 2、不可變:值變,id就變。不可變==可hash)
#AAA* 一:整型int
# ======================================基本使用======================================
# 1、用途:年齡、個數、號碼、出生年等
# 2、定義方式
# age = 18 # age = int(18)
# int功能可以把純數字的字串轉換成int型別
# res = int("18")
# # res = int("1.8")
# print(type(res))
# 瞭解(***)
# print(bin(11)) # 0b1011
# print(oct(11)) # 0o13
# print(hex(11)) # 0xb
# 3、常用操作+內建的方法
# 算數運算子與比較運算
# 二:浮點型float
# ======================================基本使用======================================
# 1、用途:薪資、身高、體重等
# 2、定義方式
salary = 3.1 # salary = float(3.1)
# float功能可以把浮點陣列成的字串轉換成float型別
# res = float("1.8")
# print(type(res))
# 3、常用操作+內建的方法
# 算數運算子與比較運算
# ======================================該型別總結====================================
# 存一個值
# 不可變
# ======================================基本使用======================================
#BBB 1、用途:記錄描述性質的狀態,例如名字、性別、國籍等
# 2、定義方式:在引號('',"",'''''',""""""")內包含一串字串
s = "hello" # s = str("hello")
# str功能可以把任意型別轉換成str型別
# res=str([1,2,3]) # "[1,2,3]"
# print(type(res))
# 3、常用操作+內建的方法
# =======================>優先掌握的操作:
# 1、按索引取值(正向取+反向取) :只能取
s = "hello world"
# print(s[0],type(s[0])) # "h"
# print(s[-1])
# s[1] = "E" # 不能修改
# 非法操作
# s[2222]
# s[11] = "A"
# 2、切片(顧頭不顧尾,步長)=>屬於拷貝操作
s = "hello world"
# new_s=s[1:7]
# print(new_s)
# print(s)
# new_s=s[1:7:2] #1 3 5
# print(new_s)
# print(s)
# new_s=s[:7:2]
# new_s=s[::2] # 0 2 4 6 8 10
# h l o w r d
# print(new_s)
# new_s=s[::] # 完整拷貝字串,只留一個冒號就可以new_s=s[:]
# print(new_s)
# 3、長度len
# s = "hello world"
# print(len(s))
# res=print("sfd")
# print(res)
# 4、成員運算in和not in
# s = "hello world"
# # print("hel" in s)
# print("egon" not in s) # 語義明確,推薦使用
# # print(not "egon" in s)
# 5、移除空白strip
# s = " \n hel lo \t "
# new_s = s.strip()
# print(new_s)
# print(s) # 沒有改變原字串
# 應用案列:
# name = input("your name>>> ").strip() # name = "egon "
# pwd = input("your pwd>>> ").strip()
#
# if name == "egon" and pwd == "123":
# print('login successful')
# else:
# print('user or password error')
# 去除左右兩邊的非空白字元
# print("**+=-%^#****he**llo**%^#**+=**".strip("*+=-%^#"))
# 6、切分split:把字串按照某個分隔符切成一個列表
# userinfo = "egon_dsb:123:18:3.1"
# res = userinfo.split(":")
# # print(res[0])
# print(res)
# print("-".join(res))
# 純字串組成的列表
# l = ["aaaa", "bbb", "ccc"]
#
# # res=l[0]+":"+l[1]+":"+l[2]
# res = ":".join(l)
# print(res, type(res))
# 7、迴圈
# for i in "hello":
# print(i)
# =======================>需要掌握的操作:
# 1、strip,lstrip,rstrip
# print("***hello***".strip("*"))
# print("***hello***".lstrip("*"))
# print("***hello***".rstrip("*"))
# 2、lower,upper
# msg = "AbCDEFGhigklmn"
# res = msg.lower()
# res = msg.upper()