首先聲明一下,我寫的這個僅僅就是個人的Python3學習總結,不是什麼所謂教程(才疏學淺 我還差了不知道多少個十萬八千里!!!)
這塊兒吶 就寫了一些Python3基本數據類型的使用
話不多說 直接上代碼:
# -*- coding: utf-8 -*-
# 輸出
print("hello world!!!") # 自動換行輸出
print("hello python!!!", end="這裏面想寫啥寫啥 空格什麼的 怎麼喜歡怎麼來 ") # 不換行輸出
# 輸入
demo = input("請輸入您的姓名,按enter鍵結束: ")
print(demo, ",歡迎來到Python的世界!")
# 數字類型 不可變
# 1. 整數 1
# 2. 浮點數 1.1 1.6e9(1.6 * 10的9次方)
# 3. 布爾型 True(1) False(0)
# 4. 複數 eg: 1 + 1j
# 加減乘除算法時,除法和java是不一樣的 還有一個乘方 eg: 2 ** 3 結果就爲8 是不是很爽
demo = 10 / 3 # 精確除法,得到的是一個浮點數
print(demo) # 3.3333333333333335
demo = 10 // 3 # 取整
print(demo) # 3
print(type(demo)) # <class 'int'> 這個是用來判斷變量類型的
# 字符串 不可變
# 1. 拼接時可以用','(自帶一個空格) 或者用'+'(還是習慣這個) 再或者格式化(下面專門說 有點兒意思)
# 注意: 用+拼接時,如果是數字類型的需要用str()函數,eg: str(2) + "22"
demo = 'demo'
print(ord('p'), ord('y'), ord('t'), ord('h'), ord('o'), ord('n')) # 112 121 116 104 111 110 字符->編碼
print(chr(112), chr(121), chr(116), chr(104), chr(111), chr(110)) # p y t h o n 編碼->字符
print(len(demo)) # 4 字符串長度
print(str(True) + "555") # True555 字符串拼接
print(1, "de") # 1 de
print(demo * 2) # demodemo
print(demo[0:2]) # de 截取字符串
print(demo[1]) # e 獲取指定下標的字符
print(demo[1:]) # emo
print(r'demo\nceshi') # demo\nceshi r''的作用是轉義字符不發生轉義
# 2. 字符串格式化(我是不常用這個,感覺很是麻煩,哪有+來得爽,只是感覺java上沒有這個,拿來說說,只當瞭解)
# %s(字符串) %d(整數) %f(浮點數) %x(十六進制整數) 其實一個%s就成 貌似all->字符串
print('我今天(%s)心情還好,但是這個天氣(%s)卻不怎麼滴!' % ('2019-7-4', '陰天'))
print('%03d' % 1) # 001 這個不錯 那個3代表字符長度轉爲>=3,不夠的補0(只能寫0)
print('%.1f' % 6.84888888) # 6.8 f前的數代表幾位小數(四捨五入)
print('我今天({0})心情還好,但是這個天氣({1})卻不怎麼滴!'.format('2019-7-4', '陰天')) # 這個字符串的格式化方法賊麻煩
print(type(demo)) # <class 'str'>
# 列表 list 這個在java開發時我是經常用 高頻
# 1. 截取的方式和字符串都一樣 能拼接 能用 * 複製 都一樣
# 2. 有序 元素能改變
# 3. [] 表示空列表
demoList = [6, 6.6, True, 'demo', [6, 6.6, True]]
print(demoList[1:3]) # [6.6, True]
demoList.append('ceshi')
print(demoList) # 在末尾追加元素 [6, 6.6, True, 'demo', [6, 6.6, True], 'ceshi']
demoList.insert(1, "demo")
print(demoList) # 插入元素 [6, 'demo', 6.6, True, 'demo', [6, 6.6, True], 'ceshi']
demo = demoList.pop(2) # 刪除指定元素並返回該元素 爲空時 默認刪除末尾元素
print(demo) # 6.6
demoList[0] = 8
print(demoList) # 修改指定元素
print(type(demoList)) # <class 'list'>
# 元組 tuple 基本和list一樣 不可變
# 1. 截取的方式和字符串都一樣 能拼接 能用 * 複製 都一樣
# 2. 有序 元素不能改變
# 3. () 表示空元組 比較特殊的是 只有一個元素的元組應該這樣表示 eg: (1,)
demoTuple = (6, 6.6, True, 'demo', [6, 6.6, True])
print(type(demoTuple)) # <class 'tuple'>
# 集合 set
# 1.創建方式比較特殊 可以用 demoSet = {6, 6.6, True, 'demo', [6, 6.6, True]},也可以用 demoSet = set(list)
# 2.空集合必須用set()
# 3.無序不重複
# 4.很像字典中的key
# 5.綜上原因 set集合可以進行並集和交集操作
demoSet = {6, 6.66, True, 'demo'}
print(demoSet) # {'demo', True, 6, 6.66}
demoSet = set([6, 6.666, True, 'demo'])
print(demoSet) # {'demo', True, 6, 6.666}
# demoSet = set('222') # 一般不這麼用 沒什麼意義
demoSet.add("ceshi") # 添加元素
print(demoSet) # {'demo', True, 6, 6.666, 'ceshi'}
demoSet.remove(6)
print(demoSet) # {'demo', True, 6.666, 'ceshi'}
demoSet1 = {6, 6.66, True, 'demo'}
print(demoSet & demoSet1) # 交集 {True, 'demo'}
print(demoSet | demoSet1) # 並集 {True, 'demo', 6.666, 6, 6.66, 'ceshi'}
print(type(demoSet)) # <class 'set'>
# 字典 Dictionary 真心和java中的map很像
# 1. 空字典 {}
# 2. 判斷是否有某個key值 用 (key in demoDictionary) 根據返回的True和False判斷
demoDictionary = {"name":"muyou", "age":26, 'gender':'男'}
print(demoDictionary["name"]) # 根據key值獲取value
print(demoDictionary.keys()) # 獲取所有的key值 dict_keys(['name', 'age', 'gender'])
print(demoDictionary.values()) # 獲取所有的value值 dict_values(['muyou', 26, '男'])
print(demoDictionary.pop("gender")) # 刪除元素
print(type(demoDictionary)) # <class 'dict'>
print(demoDictionary.items()) # dict_items([('name', 'muyou'), ('age', 26)]) 這個返回的爲元組數組
# 對於它的遍歷 也是Python3和java很不同的地方,不過這樣確實很Python,隨心所欲的寫,來 上代碼
for i, j in demoDictionary.items():
print("key:", i, ",value:", j)
print(type(demoDictionary.items())) # <class 'dict_items'>
# 最後 說一個Python中比較有意思的--列表生成式
demoList = [i for i in range(100) if i % 2 == 0]
print(demoList) # 直接獲取一個一百以內的偶數列表
輸出結果:
F:\Python\Python37\python.exe F:/Python/workspaces/learnPython/learnOne.py
hello world!!!
hello python!!!這裏面想寫啥寫啥 空格什麼的 怎麼喜歡怎麼來 請輸入您的姓名,按enter鍵結束: muyou
muyou ,歡迎來到Python的世界!
3.3333333333333335
3
<class 'int'>
112 121 116 104 111 110
p y t h o n
4
True555
1 de
demodemo
de
e
emo
demo\nceshi
我今天(2019-7-4)心情還好,但是這個天氣(陰天)卻不怎麼滴!
001
6.8
我今天(2019-7-4)心情還好,但是這個天氣(陰天)卻不怎麼滴!
<class 'str'>
[6.6, True]
[6, 6.6, True, 'demo', [6, 6.6, True], 'ceshi']
[6, 'demo', 6.6, True, 'demo', [6, 6.6, True], 'ceshi']
6.6
[8, 'demo', True, 'demo', [6, 6.6, True], 'ceshi']
<class 'list'>
<class 'tuple'>
{True, 'demo', 6, 6.66}
{True, 'demo', 6, 6.666}
{True, 6, 6.666, 'ceshi', 'demo'}
{True, 6.666, 'ceshi', 'demo'}
{True, 'demo'}
{True, 6.666, 6, 6.66, 'ceshi', 'demo'}
<class 'set'>
muyou
dict_keys(['name', 'age', 'gender'])
dict_values(['muyou', 26, '男'])
男
<class 'dict'>
dict_items([('name', 'muyou'), ('age', 26)])
key: name ,value: muyou
key: age ,value: 26
<class 'dict_items'>
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]
Process finished with exit code 0
代碼下載地址: https://github.com/rwq9866/learnPython
注: 新手上路,僅供參考!!!