Python_變數和資料型別
1.注釋
(1)單行注釋:#(快捷鍵:Ctrl+/)
# print("hello")
(2)多行注釋:六個單引號或者六個雙引號(英文狀態下,三個一組)
"""
print("hello")
print("hello")
print("hello")
print("hello")
"""
'''
print("hello")
print("hello")
print("hello")
print("hello")
'''
2.變數
(1)變數:存盤資料時當前資料所存在的記憶體地址的名字
(2)語法:變數名=值
(3)識別符號命名規則:
- 由數字、字母、下劃線組成
- 不能由數字開頭
- 不能使用內置關鍵字
- 嚴格區分大小寫
(4)先定義變數,然后再使用變數
(5)當定義的變數發生變化時,使用的變數也會相應的發生變化
#定義變數和使用變數
my_name='liMing' #定義變數
print(my_name) #使用變數
sex='男'
print(sex)
3.Debug工具
(1)用途:查看程式的執行細節和流程或者調解bug
(2)Debug工具使用步驟:
- 打斷點
- Debug除錯
(3)斷點位置:要除錯代碼塊的第一行代碼;一個斷點即可
(4)打斷點方法:單擊目標代碼的行號右側空白位置
4.資料型別 - 數值:int(整型)、float(浮點型)
- 布爾型:True(真)、False(假)
- str(字串)
- list(串列)
- tuple(元組)
- set(集合)
- dict(字典)
#資料型別
#type用于檢測資料型別
#int--整型
a=1
print(type(a))
#float--浮點型,小數
b=1.3
print(type(b))
#str--字串,資料一般需要帶引號
c='hello'
print(type(c))
#bool--布爾型別,通常判斷使用,有True和False
d=True
print(type(d))
# list--串列
e =[10,20,30]
print(type(e))
# tuple--元組
f=(10,20,30)
print(type(f))
# set--集合
g={10,20,30}
print(type(g))
# dict--字典(鍵值對)
h={'name':'LiMing','sex':'男'}
print(type(h))
5.格式化輸出
(1)格式化符號
- %s:字串
- %d:有符號的十進制整數(%03d:不足三位數的時候用0補齊,超出的原樣輸出)
- %f:浮點數(%.2f:保留兩位小數)
# 格式化輸出
name='LiMing'
age=12
stu_id1=2
stu_id2=2000
weight=43.2
#我的名字是X
print('我的名字是%s' % name) #%s--字串
#我的年齡是X
print('我的年齡是%d' % age) #%d--有符號的十進制整數
#我的體重是X
print('我的體重是%.1f' % weight) #%f--浮點數(%.2f:保留兩位小數)
#我的學號是00X
print('我的學號是%03d' % stu_id1) #(%03d:不足三位數的時候用0補齊)
print('我的學號是%03d' % stu_id2) #(超出規格的原樣輸出)
#我的名字是X,今年X歲
print('我的名字是%s,今年%d歲' %(name,age))
#我的名字是X,明年X歲
print('我的名字是%s,明年%d歲' %(name,age+1))
#我的名字是X,今年X歲,學號是X,體重是X
print('#我的名字是%s,今年%d歲,學號是%03d,體重是%.1f' %(name,age,stu_id1,weight))
#我的名字是X,今年X歲,學號是X,體重是X
print('#我的名字是%s,今年%s歲,學號是%s,體重是%s' %(name,age,stu_id1,weight)) #%s功能強大

(2)格式化字串除了%s,還可以寫為f’{運算式}’(是Python3.6中新增的格式化方法)
#我的名字是X,今年X歲
print('我的名字是%s,今年%d歲' %(name,age))
# 語法:f'{運算式}'
print(f'我的名字是{name},今年{age}歲')
#我的名字是X,明年X歲
print('我的名字是%s,明年%d歲' %(name,age+1))
# 語法:f'{運算式}'
print(f'我的名字是{name},明年{age+1}歲')
6.轉義字符
(1)\n:換行
(2)\t:制表符,一個tab(4個空格)鍵的距離
# 轉義字符
print('hello')
print('world')
print('hello\nworld')
print('hello\tworld')

7.結束符
(1)語法:print(‘輸出的內容’,end="\n")
(2)在Python中,print()默認自帶end="\n"這個換行結束符,所以導致每兩個print直接會換行展示,用戶可以按照需求更改結束符
# 結束符
print('hello')
print('world')
print('hello', end="\n")
print('world')
print('hello', end="\t")
print('world')
print('hello', end='...')
print('world')

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/259235.html
標籤:python
上一篇:Mybatis的動態SQL陳述句
