活動地址:CSDN21天學習挑戰賽
8.1參加了Python21day of learning activities
First of all thank you for your support
Get to know the first dayPython
The various climbs will be updated in the future as follows:
提示:以下是本篇文章正文內容
總結特點:Python是一種解釋型語言、Python是交互式語言,Python是面向對象語言
Python的發展歷史.Python由荷蘭人吉多·范羅蘇姆(Guido van Rossum)於1989年創造,並於1991年發布第一個公開發行版.自2004年以後,Pythonusage has grown significantly,Python2於2000年10月發布,穩定版本是Python 2.7.Python3於2008年12月發布,不完全兼容Python2.
pip說明:pip 是一個現代的,通用的Python包管理工具.提供了對 Python 包的查找、下載、安裝、卸載的功能,便於我們對Python的資源包進行管理.
:可能是因為在安裝python的過程中未勾選 Add Python 3.7 to PATH 選項,需要手動的配置pip的環境變量.
步驟:右鍵 此電腦 環境變量 找到並且雙擊 Path 在彈窗裡點擊新建 找到pip的安裝目錄,把路徑添加進去.
這裡新添加的路徑 E:\python\Scripts 是Python安裝好以後, pip.exe 這個可執行文件所在的目錄.
說明
(Unlike other languages, no reputation is required here)HH= "Hello Python!"
print(HH) # 注意,變量名不需要使用引號包裹
程序中: 在 Python 裡為了應對不同的業務需求,也把數據分為不同的類型. 如圖
練習:
number = 18
string = "字符串"
flo = 1.66
booleanT = True
booleanF = False
//列表
name_list = ['阿裡巴巴', '京東', '小']
//元組
name_tuple = (1, 2, 5, 4, 1, 4, 45, 4, 7, 7, 7, 7)
name_obj = {
"name": "郭先生!",
"age": 18,
"sex": "男",
}
print(number)
print(string)
print(flo)
print(booleanT)
print(booleanF)
print(name_list[0])
print(name_tuple[2])
print(name_obj)
//遍歷數組
for name_lists in name_list:
print(name_lists)
練習:
number = 18
string = "字符串"
flo = 1.66
booleanT = True
booleanF = False
name_list = ['阿裡巴巴', '京東', '小']
name_tuple = (1, 2, 5, 4, 1, 4, 45, 4, 7, 7, 7, 7)
name_obj = {
"name": "郭先生!",
"age": 18,
"sex": "男",
}
print(type(number))
print(type(string))
print(type(flo))
print(type(booleanT))
# 列表
print(type(name_list))
# 元組
print(type(name_tuple))
# 字典
print(type(name_obj))
練習:
name = "郭先生"
age = 18
obj = {
"name": "郭先生"
}
arr = [1, 1, 1, 1, 4]
name_tuple = (1, 1, 5, 4, 5)
name1 = ""
name2 = ''
age1 = 0
age2 = 0.0
obj1 = {
}
arr1 = []
name_tuple1 = ()
print(bool(name))
print(bool(age))
print(bool(obj))
print(bool(arr))
print(bool(name_tuple))
print('----------------')
print(bool(name1))
print(bool(name2))
print(bool(age1))
print(bool(age2))
print(bool(obj1))
print(bool(arr1))
print(bool(name_tuple1))
練習:
a = 4
b = 4
c = "55"
print(a + 3)
print(a - 3)
print(a * 3)
print(a / 3)
print(a // 3)
print(a % 3)
print(2 ** 3)
print((2 + 4) * 3)
# 特殊的類型
print(int(int(c) + 1) + 5)
練習:
a = 7
print(a)
a += 2
print(a)
a *= 2
print(a)
a /= 2
print(a)
a //= 2
print(a)
a %= 2
print(a)
a **= 2
print(a)
練習:
a = 10
b = 88
c = 99
print(a > b)
print(b < c)
print(a == b)
print(a != b)
print(b >= c)
print(b <= c)
練習:
a = False
b = 50
print(a > b or a < 10)
print(a > b and b < 10)
print(a < b or b < 10)
print(not a)
練習:
name = "阿裡巴巴"
age = 18
print("The student's name:%s\nThe student's age is :%d" % (name, age))
練習:
name = input("請輸入內容!")
if name == "阿裡巴巴":
print("Welcome to Jack Ma!")
else:
print("Please get out of my room!")
練習:
string = input("請輸入內容!")
print(len(string))
print(string.find('a'))
print(string.startswith("a"))
print(string.endswith("a"))
print(string.count("a"))
# 替換
print(string.replace("a", "h"))
# 將字符串切割為數組
print(string.split("a"))
print(string.upper())
print(string.lower())
# 去掉空格
print(string.strip())
print('--------------------')
# I think this design is very, very unfriendly
print(string.join('ccc'))
練習:
print('列表的增-------------------------')
append 在末尾添加元素
insert 在指定位置插入元素
extend 合並兩個列表
list_a = ['I am Li Ka-shing', '哈哈']
list_b = ['I am Li Ka-shing1', '哈哈1']
list_a.append("郭先生")
list_b.append("郭先生1")
list_a.insert(0, "Spurs shipping is not without")
print(list_a)
print(list_b)
print(list_a)
list_a.extend(list_b)
print(list_a)
print('列表的改-------------------------')
list_a = ['I am Li Ka-shing', '哈哈']
list_b = ['I am Li Ka-shing1', '哈哈1']
list_a[0] = "aa"
print(list_a)
print('列表的查-------------------------')
list_a = input("請輸入內容!")
list_b = ['李嘉誠', '郭先生', '馬雲']
if list_a in list_b:
print("我在")
else:
print("我不在")
list_a = input("請輸入內容!")
list_b = ['李嘉誠', '郭先生', '馬雲']
if list_a not in list_b:
print("我不在")
else:
print("我在")
print('列表的刪-------------------------')
del:根據下標進行刪除
pop:刪除最後一個元素
remove:根據元素的值進行刪除
list_b = ['李嘉誠', '郭先生', '馬雲']
# 刪除尾部
print(list_b.pop())
# Delete according to the table below
del list_b[0]
print(list_b)
# Delete the specified name
list_b.remove('郭先生')
print(list_b)
練習:
tupleS = (1, 1, 1, 2, 5, 4, 5, 4)
tupleS1 = (1,)
print(type(tupleS))
print(type(tupleS1))
print(tupleS)
print(tupleS1)
:注意:選取的區間從"起始"位開始,到"結束"位的前一位結束(不包含結束位本身),步長表示選取間隔.
練習:
string = "hello World"
print(string[0])
print(string[0:5])
print(string[:4])
print(string[0:])
print(string[0:4:2])
練習
# 字典的查------
arr = {'name': '郭先生', 'age': 181}
print(arr['name'])
print(arr.get('name'))
print(arr["name"])
print(arr.get("name"))
# 字典的改------
arr['name'] = "法外狂徒"
# 字典的增------
arr['hellow'] = "你好!"
# 字典的刪------
# del arr['name']
# del arr
# 清空字典內的內容------
arr.clear()
print(arr)
print('-------------')
# 字traversal of the canon------
traversal of the canon
arr1 = {'name': '郭先生', 'age': 181}
for key in arr1.keys():
print(key)
遍歷字典的value(值)
for key in arr1.values():
print(key)
遍歷字典的項(元素)
for key, value in arr1.items():
print(key, value)
遍歷字典的key-value(鍵值對)
for items in arr1.items():
print(items)
練習:
def fun(a, b):
c = a + b
d = 18
return d
print(fun(1, 5))
什麼是局部變量 如下:
1、 文件路徑
2.、The access mode is shown in Fig:
練習:
# 寫------
fs = open('text.txt', 'w')
fs.write("阿裡巴巴\n")
# Write in cumulative form------
fs = open('text.txt', 'a')
fs.write("阿裡巴巴\n")
# 讀------
fs = open('text.txt', 'r')
read = fs.read()
print(read)
# 讀text.txt取第一行------
read1 = fs.readline()
print(read1)
# 讀text.txt並返回數組------
read2 = fs.readlines()
print(read2)
# 關閉文件------
fs.close()
練習:
print('--------------Serialize the first method')
arr = ['a', 'a', 'a', 'a']
fs = open('text.txt', 'w')
import json
result = json.dumps(arr)
fs.write(result)
fs.close()
print('--------------Serialize the second method')
arr = ['a', 'a', 'a', 'a']
fs = open('text.txt', 'w')
import json
json.dump(arr, fs)
fs.close()
print('--------------Deserialize the first method')
fs = open('text.txt', 'r')
result = fs.read()
print(result)
print(type(result))
import json
result = json.loads(result)
print(result)
print(type(result))
print('--------------Deserialize the second method')
fs = open('text.txt', 'r')
import json
result = json.load(fs)
print(result)
print(type(result))
練習:
try:
fs = open('text1.txt', 'r')
result = fs.read()
print(result)
except FileNotFoundError:
print('網絡異常!')