Pin-Jiun / Python

Python Document
0 stars 0 forks source link

1-pip, Variables and Datatypes #1

Open Pin-Jiun opened 1 year ago

Pin-Jiun commented 1 year ago

Install

1.到 pip document 官方網站 建議的載點下載 pip 安裝檔 curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py 2.啟動剛剛下載好的 get-pip.py 檔案,就安裝完成囉! py get-pip.py

如何查看 pip 目前版本? py -m pip --version

如何更新 pip 到最新版本? py -m pip install -U pip

常用library安裝 pip install numpy; pip install opencv-python; pip install matplotlib; pip install pandas; pip install python-pptx

# 資料︰程式的基本單位
# 數字
3456 # 整數
4.5 # 浮點數(小數)
# 字串
"測試中文"
"Hello World"
# 布林值
True
False
# 有順序、可動的列表 List
[3,4,5]
["Hello","World"]
# 有順序、不可動的列表 Tuple
(3,4,5)
("Hello","World")
# 集合 Set
{3,4,5}
{"Hello World"}
# 字典 Dictionary
{"apple":"蘋果","data":"資料"}
# 變數︰用來儲存資料的自訂名稱
# 變數名稱=資料
x=3
# print(資料)
print(x)
x=True # 取代舊的資料
print(x)
x="Hello"
print(x)
x={3,5,6} # 集合 Set
print(x)

數字,字串的基本處理

# 數字運算
#   x = 2+3
#   print(x)
17 / 3  # classic division returns a float
#>>>5.666666666666667
17 // 3  # floor division discards the fractional part
#>>>5
#   print(x)

# 字串運算
#   s = "Hell\"o"  # \ = 跳脫 = Hell"o
#   print(s)
#   s = "hello" "world"
#   print(s)
#   s = "Hello\nworld"
#   print(s)
#   s = "Hello"*3+"world"
#   print(s)
# 字串會對內部的字元編號(索引),從 0 開始算起
s = "hello"
print(s[1:4])   # 包含開頭不包含結尾,得 ell
s = "hello"
print(s[1:])    # 包含開頭的全部到結尾,得 ello
s = "hello"
print(s[:4])    # 開頭的全部到結尾前一個,得 hell
Pin-Jiun commented 1 year ago

字串常用method

str.strip()從兩邊,lstrip()從左邊,rstrip()從右邊

Pin-Jiun commented 1 year ago

python程式碼過長, 就是每行後面加個 \

time = "2017"
print "one" + "," \
+ "two" \
+ ",three" + \
"," + time
Pin-Jiun commented 8 months ago

在Python中,使用 + 运算符和 f-string 都可以用来连接字符串。这两种方法有一些不同的语法和用法:

使用 + 运算符: python Copy code string1 = "Hello" string2 = "World" result = string1 + " " + string2 print(result) 这将输出:

Copy code Hello World 使用 f-string: python Copy code string1 = "Hello" string2 = "World" result = f"{string1} {string2}" print(result) 这将输出相同的结果:

Copy code Hello World f-string 是从 Python 3.6 版本开始引入的,它提供了一种更简洁和直观的方式来在字符串中插入变量值。你可以在 f-string 中使用大括号 {} 包含变量或表达式,它们将在运行时被替换为相应的值。