python基礎之資料型別相關

2021-09-05 11:06:21 字數 4081 閱讀 9790

從0基礎學程式設計,選擇了python這門語言,記錄一下學習的歷程。

首先從接觸到的資料型別開始:

python中常用資料型別:整形、浮點、布林、字串、列表、元組、字典、集合等

整型(int):就是數學中的整數

浮點(float):就是數學中的小數

# 浮點

b = 3.14

print(b, type(b))

# 科學計數法

c = 3.1415926e-3

print(c, type(c))

d = 2 + 3j

print(d, type(d)

列表(list):通過進行定義,可以存放一系列的任意資料,是一種容器型別

lt = [1, 2, 'hello', 3.14, true]

print(lt, type(lt))

# 通過下標獲取元素,有越界問題

print(lt[1])

tp = (1, 2, [3, 4, 5])

print(tp, type(tp))

# 也是通過下標進行訪問

print(tp[2])

# 定義乙個元素的元組後面要新增乙個,

tp2 = (1,)

print(tp2, type(tp2))

s1 = 

s2 =

# print(s1, type(s1))

# 交集

print(s1 & s2)

# 並集

print(s1 | s2)

# 差集

print(s1 - s2)

print(s2 - s1)

# 定義空集合不能使用{},這是留給定義空字典使用的

# 應該使用set()

# s3 = {}

# print(type(s3))

s4 = set()

print(type(s4))

集合經常用於去重操作

d = 

print(d, type(d))

# 可以根據鍵獲取值

print(d['name'])

# 當鍵不存在時或報keyerror錯

# print(d['height'])

# 可以通過get方法根據鍵獲取值,

print(d.get('age'))

# 當鍵不存在時不會報錯,會返回none

print(d.get('height'))

# 可以設定預設值,有對應的鍵返回其值,沒有時返回設定的預設值

print(d.get('weight', 75))

# 統計元素個數,字典統計的是鍵值對個數

print(len(d))

print(len(s1))

print(len(lt))

print(len('helloworld'))

int:轉換為整型

float:轉換為浮點

str:轉換為字串

list:轉換為列表

tuple:轉換為元組

set:轉換為集合

dict:轉換為字典

# 轉換為整數

# a = int(3.14)

# 引數1:需要轉換的資料

# base:資料的進製型別,預設為十進位制

# a = int('123', base=8)

# a = int('abc', base=16)

# 浮點

# a = float(250)

# 字串

# a = str(123)

# 列表

# a = list('hello')

# a = list((1, 2, 3))

# a = list()

# 可以轉換不會報錯,但是只保留了鍵

# a = list()

# 元組

# a = tuple([1, 2, 3])

# 集合

# a = set([1, 2, 3])

# 字典

lt = [('name', 'dahua'), ('age', 18)]

a = dict(lt)

print(a, type(a))

s1 = 'hello'

s2 = 'world'

# 可以使用'+'將字串拼接在一起

s3 = s1 + s2

print(s3)

# '*'可以重複前面的字串若干次

s4 = 'abc' * 3

print(s4)

# len函式統計字串長度

print(len(s1))

s = 'abcdefg'

# 從開頭進行提取,下標從0開始

print(s[0])

# 從末尾進行提取,下標從-1開始

print(s[-1])

# 切片

# 格式:s[開始:結束:步進]

print(s[1:3])

# 當一邊的邊界省略,則提取到改側的邊緣

print(s[1:])

print(s[:4])

print(s[1:-2])

print(s[:-3])

print(s[-3:])

# 指定步進值,預設為1

print(s[::2])

# 逆序提取

print(s[::-1])

# 格式化

# %s:字串

# %d:整型

# %f:浮點

# %c:字元(乙個字元)

name = '二狗'

like = '大花'

age = 18

print('俺叫%s,暗戀%s,她今年%d歲' % (name, like, age))

# python中特有的解決方案

print('俺叫{},暗戀{},她今年{}歲'.format(name, like, age))

print('俺叫,暗戀,她今年歲'.format(name, like, age))

print('俺叫,暗戀,她今年歲'.format(n=name, l=like, a=age))

lt = [1, 2, 3, 4, 5]

print(lt[0])

print(lt[-1])

print(lt[1:3])

print(len(lt))

# 修改元素

lt[0] = 100

# 追加元素,將傳遞的元素作為乙個整體(乙個元素)追加到列表尾部

# 將可迭代物件的元素展開新增到列表中

lt.extend([300, 400])

# 插入元素,在指定位置插入元素

lt.insert(2, 250)

lt.insert(4, 250)

# 刪除元素

# 根據索引號刪除

del lt[0]

# 根據值進行刪除,只會刪除第乙個

lt.remove(250)

# 返回並刪除:返回並刪除指定下標的元素,預設是最後乙個

ele = lt.pop(2)

# print(ele)

# print(lt)

lt = [1, 2, 3, 4, 5, 3]

# 查詢元組在列表中的索引號

# 找到就會返回其索引號,即第乙個出現的位置

# 若不在列表中則會報錯

print(lt.index(3))

# 統計元素在列表中出現的次數

print(lt.count(3))

# 逆序

lt.reverse()

# 排序

# reverse=true表示降序排序

lt.sort(reverse=true)

print(lt)

Python基礎之資料型別

1.程式輸出 print print hello world hello world 2.程式輸入 input name input shuaishuai print hello name hello shuaishuai 3.注釋 這是乙個注釋 這是 乙個多行注釋 4,運算子 加,減,乘除,地板除...

python 基礎之資料型別

一.變數 1.目的 為了能讓計算機能像人一樣記憶 2.使用 先定義 後引用 定義 1.變數名 變數值 2.變數值 記錄事物的狀態 記憶體位址 id是通過記憶體位址算出來的 age 18 print id age 1374973952 型別type print type age is 判斷id是否相等...

Python基礎之資料型別

1.bool 在python裡面哪些值是false 0 none false t true print type t 2.int i 123 print type i 3.float 1e10 科學計數法也是float 字串是不可改變的,字串做了一些操作後,會生成乙個新的字串 只有乙個元素的tupl...