1. 程式人生 > 其它 >Python 基礎知識 + 個人學習 + 實時更新

Python 基礎知識 + 個人學習 + 實時更新

技術標籤:基礎python程式語言正則表示式

1.輸出函式

print()

1.1 輸出數字

print(520)
print(98.5)

1.2 輸出字串

print('helloworld')
print("helloworld")

1.3 輸出含有運算子的表示式

print(3+1)

1.4 將資料輸出檔案中

將資料輸出檔案中,注意 !1.所指定的碟符的存在 2.使用file=fp

# a+是以讀寫的方式開啟檔案,
# 如果存在就在檔案內容的後邊繼續追加,不存在就建立
fp = open('D:/test.txt', 'a+')
print('helloworld'
,file=fp) fp.close()

1.5 不進行換行輸出

(輸出內容在一行當中)

print('hello','world','Python')

2. 轉義字元與原字元

常見的轉義字元

反斜槓 		\\
單引號 		\'
雙引號 		\"
換行  	 	\n
回車   		\r
水平製表位 	\t
退格			\b

2.1 \n

# 轉移功能的首字母 n->newline 的首字元表示換行
print('hello\nworld')  

在這裡插入圖片描述

2.2 \t

跳轉到下一個製表位 (4個位元組一個製表位)
例1:跳轉了3個位元組

print('hello\tworld')

在這裡插入圖片描述
例2:跳轉了4個位元組 (一個製表位)

print('helloooo\tworld')  

在這裡插入圖片描述

2.3 \r

\r 表示將游標的位置回退到本行的開頭位置

print('hello\rworld')  

在這裡插入圖片描述

2.4 \b

\b表示將游標的位置回退一位

print('hello\bworld')  

在這裡插入圖片描述

2.5 \\

print('http:\\www.baidu.com')  

在這裡插入圖片描述
解決方案:

print('http:\\\\www.baidu.com')  

在這裡插入圖片描述

2.6 \’

print('老師說:\'大家好\'')  

在這裡插入圖片描述

2.7 原字元

原字元,不希望字串中的轉義字元起作用,就使用原字元,就是在字串前加上r,或 R

print(r'hello\nworld')  

在這裡插入圖片描述
注意!
最後一個字元不能是反斜槓

# print(r'hello\nworld\')  錯誤
print(r'hello\nworld\\')  # 正確

3. 識別符號和保留字

3.1 保留字

'False','None','True', 'and', 'as', 'assert',
'async','await', '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'

3.2 標示符

規則

  • 字母,數字,下劃線
  • 不用數字開頭
  • 不用保留字
  • 嚴格區分大小寫

4. 變數

4.1 變數的定義與使用

name = '瑪利亞'
print(name)

變數由三部分組成

  • 標識:標示物件所儲存的記憶體地址,使用內建函式id(obj)來獲取
  • 型別:表示的是物件的資料型別,使用內建函式type(obj)來獲取
  • 值:表示物件所儲存的具體資料,使用print(obj)可以將值進行列印輸出
name = '瑪利亞'
print('標識', id(name))
print('型別', type(name))
print('值', name)

在這裡插入圖片描述

4.2 變數的多次賦值

  • 多次賦值後,變數會指向新的空間

5. 資料型別

5.1 常用的資料型別

  • 整形型別 -> int ->98
  • 浮點數型別 -> float -> 3.14159
  • 布林型別 -> bool -> True ,False
  • 字串型別 -> str -> 人生苦短,我用Python

5.2 整數型別

  • 英文為interger, 簡寫為int ,可以表示正數,負數和零
  • 整數的不同進製表示方式
    • 十進位制 -> 預設的進位制
    • 二進位制 -> 以0b開頭
    • 八進位制 -> 以0o開頭
    • 十六進位制 -> 0x開頭
n1 = 90
n2 = -76
n3 = 0

print(n1, type(n1))
print(n2, type(n2))
print(n3, type(n3))

# 整數可以表示為二進位制,十進位制,八進位制,十六進位制
print('十進位制', 118)
print('二進位制', 0b10101110101)
print('八進位制', 0o176)
print('十六進位制', 0x1EAF)

在這裡插入圖片描述

5.3 浮點型別

a = 3.14159
print(a, type(a))
n1 = 1.1
n2 = 2.2
print(n1+n2)

出現不精確問題(可能性)
在這裡插入圖片描述
解決辦法:

from decimal import Decimal
print(Decimal('1.1')+Decimal('2.2'))

在這裡插入圖片描述

5.4 bool型別

  • 用來表示真或假的值
  • True 表示真,False表示假
  • 布林值可以轉化為整數
    • True -> 1
    • False -> 0
f1 = True
f2 = False
print(f1, type(f1))
print(f2, type(f2))
# 布林值可以轉成整數計算
print(f1 + 1)  # 2  1+1的結果為2 True表示1
print(f2 + 1)  # 1  0+1結果為1,False表示0

在這裡插入圖片描述

5.5 字串型別

  • 字串又被稱為不可變的字元序列
  • 可以使用單引號’ ’ 雙引號" " 三引號’’’ ‘’'或""" “”"來定義
  • 單引號和雙引號定義的字串必須在一行
  • 三引號定義的字串可以分佈在連續的多行
str1 = '人生苦短,我用Python'
str2 = "人生苦短,我用Python"
str3 = """人生苦短,
我用Python"""
str4 = '''人生苦短,
我用Python'''
print(str1, type(str1))
print(str2, type(str2))
print(str3, type(str3))
print(str4, type(str4))

在這裡插入圖片描述

5.6 資料型別轉換

name = '張三'
age = 20
# 說明name與age的資料型別不同
print(type(name), type(age)) 
# print('我叫'+name+'今年,'+age+'歲')
#當將str型別與int型別連線時報錯,解決方案,型別轉換
print('我叫' + name + '今年,' + str(age) + '歲')

在這裡插入圖片描述
5.6.1 資料型別轉換的函式

在這裡插入圖片描述
5.6.2 str()函式

print('--------------------------------str()將其他型別轉換為str()型別---')
a = 10
b = 198.8
c = False
print(type(a), type(b), type(c))
print(str(a), str(b), str(c), type(str(a)), type(str(b)), type(str(c)))

在這裡插入圖片描述
5.6.3 int()函式

print('--------------------------------int()將其他型別轉換為int()型別---')
s1 = '128'
f1 = 98.7
s2 = '76.77'
ff = True
s3 = 'hello'
print(type(s1), type(f1), type(s2), type(ff), type(s3))
print(int(s1), type(int(s1)))  # 將str轉成int型別, 字串為 數字串
print(int(f1), type(int(f1)))  # 將float轉成int型別,擷取整數部分,舍掉小數部分
# print(int(s2), type(int(s2)))  # 將str轉成int型別,報錯,因為字串為小數串
print(int(ff), type(int(ff)))  # 轉換為0 或者 1
# print(int(s3), type(int(s3)))  # 將str轉成int型別時, 字串必須為數字串(整數),非數字串是不允許轉換的

在這裡插入圖片描述