1. 程式人生 > 實用技巧 >python基礎--基礎入門

python基礎--基礎入門

一, 註釋

1,註釋的作用:通過⽤自己熟悉的語言,在程式中對某些程式碼進⾏標註說明,這就是註釋的作用,能夠增強程式的可讀,方便後期維護。

直譯器不執行註釋內容

2,註釋的分類及語法

註釋分兩類:單行註釋和多行註釋

單行註釋:

# 輸出hello world
print('hello world')
print('hello Python') # 輸出(簡單的說明可以放到⼀一⾏行行程式碼的後⾯面,⼀一般習慣程式碼後⾯面新增
兩個空格再書寫註釋⽂文字)
View Code

多行註釋

"""
下⾯面三⾏行行都是輸出的作⽤用,輸出內容分別是:
hello Python
hello itcast
hello itheima
""" print('hello Python') print('hello itcast') print('hello itheima') ''' 下⾯面三⾏行行都是輸出的作⽤用,輸出內容分別是: hello Python hello itcast hello itheima ''' print('hello Python') print('hello itcast') print('hello itheima')
View Code

快捷鍵ctrl + /

二,變數

變數作用:是一個儲存資料的的時候當前資料所在的記憶體地址的名字而已。

定義變數:變數名 = 值

識別符號:

識別符號命名規則是Python中定義各種名字的時候的統⼀一規範,具體如下:

1,由數字、字母、下劃線組成

2,不能數字開頭

3,不能使用內建關鍵字

4,嚴格區分⼤小寫

False None True and as assert break class
continue def del elif else except finally for
from global if import in is lambda nonlocal
not or pass raise return try while with
yield
View Code

命名習慣:

1,見名知義

2,大駝峰:即每個單詞首字母都大寫,例如:MyName

3, 小駝峰:第二個(含)以後的單詞首字母大寫,例如:myName

4, 下劃線:例如:my_name

使用變數:

#定義變數:儲存資料TOM
my_name = 'TOM'
print(my_name)
schoolName = '程式設計師'
print(schoolName)

資料型別:在 Python 里為了應對不同的業務需求,也把資料分為不同的型別

檢測資料型別的方法:type()

a = 1
print(type(a)) # <class 'int'> -- 整型
b = 1.1
print(type(b)) # <class 'float'> -- 浮點型
c = True
print(type(c)) # <class 'bool'> -- 布林型
d = '12345'
print(type(d)) # <class 'str'> -- 字串串
e = [10, 20, 30]
print(type(e)) # <class 'list'> -- 列列表
f = (10, 20, 30)
print(type(f)) # <class 'tuple'> -- 元組
h = {10, 20, 30}
print(type(h)) # <class 'set'> -- 集合
g = {'name': 'TOM', 'age': 20}
print(type(g)) # <class 'dict'> -- 字典
View Code

三,格式化輸出

技巧:

%06d,表示輸出的整數顯示位數,不足以0補全,超出當前位數則原樣輸出
%.2f,表示⼩數點後顯示的⼩數位數。

格式化字元串除了了%s,還可以寫為f'{表示式}'

測試程式碼

轉義字元:

\n :換行

\t :製表符,一個tab鍵(4個空格)的距離

結束符:

print('輸出的內容', end="\n")

四,輸入

語法:

input("提示資訊")

特點:

1,當程式執⾏行行到input ,等待⽤使用者輸入,輸⼊完成之後才繼續向下執⾏。
2,在Python中, input 接收⽤使用者輸入後,一般儲存到變數量,⽅便使用。
3,在Python中, input 會把接收到的任意⽤使用者輸入的資料都當做字串處理。

password = input('請輸⼊入您的密碼:')
print(f'您輸入的密碼是{password}')
# <class 'str'>
print(type(password))

五,運算子

運算子的分類:

1,算數運算子
2,賦值運算子
3,符合賦值運算子
4,比較運算子
5,邏輯運算子

1,算數運算子:

注意:

混合運算子優先順序順序:()高於**高於* / // % 高於 + -

2,賦值運算子:

單個變數賦值:

num = 1
print(num)

多個變數賦值:

num1, float1, str1 = 10, 0.5, 'hello world'
print(num1)
print(float1)
print(str1)

結果如下:

多變數賦相同值:

a = b = 10
print(a)
print(b)

結果如下:

3,複合賦值運算子:

a = 100
a += 1
#輸出101 a = a + 1 ,最終a = 100 + 1
print(a)

b = 2
b *= 3
#輸出6  b = b * 3,最終b = 2 * 3
print(b)

c = 10
c += 1 + 2
#輸出13 ,先算運算子右側1+2=3,c += 3,推匯出c = 10 + 3
print(c)

4,比較運算子

比較運算子也叫關係運算符,通常用來判斷

a = 7
b = 5
print(a == b) # False
print(a != b) # True
print(a < b) # False
print(a > b) # True
print(a <= b) # False
print(a >= b) # True

5,邏輯運算子

a = 1
b = 2
c = 3
print((a < b) and (b < c)) # True
print((a > b) and (b < c)) # False
print((a > b) or (b < c)) # True
print(not (a > b)) # True

數字間的邏輯運算

a = 0
b = 1
c = 2
# and運算子,只要有一個值為0,則結果為0,否則結果為最後一個非0數字
print(a and b)   # 0
print(b and a)   # 0
print(a and c)   # 0
print(c and a)   # 0
print(b and c)   # 2
print(c and b)   # 1

# or運算子,只有所有值為0結果才為0,否則結果為第一個⾮0數字
print(a or b)  # 1
print(a or c)  # 2
print(b or c)  # 1