黑馬程序員python教程,8天python從入門(mén)到精通,學(xué)python看這套就

快捷鍵:
ctrl + d :復(fù)制當(dāng)前行代碼到下一行
shift + alt + ↑\↓ : 將當(dāng)前行代碼上移或下移
ctrl+/ :注釋
字面量:被寫(xiě)在代碼中固定的值。

字面量輸出:
print(10)
print(10.1)
print("nihao")

money = 40
money = money-10
print("錢(qián)包還剩余:", money, "元")
type()語(yǔ)句


小數(shù)部分被直接抹掉,不考慮四舍五入。





使用 + 拼接,注意無(wú)法和整數(shù)、浮點(diǎn)數(shù)直接拼接。

數(shù)字類(lèi)型被轉(zhuǎn)換成字符串類(lèi)型填入占位中:



這種格式不會(huì)理類(lèi)型,不控制精度。


name = "傳智播客"
stock_price = 19.99
stock_code = "003032"
growth_factor = 1.2
growth_days = 7
print(f"公司:{name}", f"股票代碼:{stock_code}", f"當(dāng)前股價(jià):{stock_price}")
print("每日增長(zhǎng)系數(shù)是:%.1f, 經(jīng)過(guò)%d天的增長(zhǎng),股價(jià)達(dá)到了%.2f" % (growth_factor, growth_days, stock_price * growth_factor ** growth_days))

input()語(yǔ)句,鍵盤(pán)輸入的數(shù)據(jù),input均看作字符串,如果想變成數(shù)字,int()或float()轉(zhuǎn)換。

布爾類(lèi)型,非True即False。



print("歡迎來(lái)到黑馬兒童游樂(lè)場(chǎng),兒童免費(fèi),成人收費(fèi)")
age =int(input("請(qǐng)輸入你的年齡:"))
if age>=18:
print("您已成年,游玩需要補(bǔ)票10元")
print("祝您游玩愉快")


print("歡迎來(lái)到黑馬動(dòng)物園")
height = int(input("請(qǐng)輸入你的身高(cm):"))
if height>120:
print("您的身高超出120cm,游玩需要補(bǔ)票10元")
else:
print("您的身高未超出120cm,可以免費(fèi)游玩")
print("祝您游玩愉快!")


簡(jiǎn)化:

right_num = 10
if int(input("請(qǐng)輸入第一次猜想的數(shù)字:")) == right_num:
print("恭喜第一次猜對(duì)") 這個(gè)條件實(shí)現(xiàn)后的步驟不寫(xiě)還不行?!
elif int(input("不對(duì),再猜一次:")) == right_num:
print("恭喜第二次猜對(duì)")
elif int(input("不對(duì),再猜最后一次:")) == right_num:
print("恭喜第三次猜對(duì)")
else:
print("sorry全部猜錯(cuò)啦,我想的是%d" % right_num)



i = 1
result = 0
while i <= 100:
result += i
i += 1
print(result)



i = 1
while i <= 9:
j = 1
while j <= i:
print(f"{j}*{i}={j*i}\t", end='')
j += 1
i += 1
print()




i = 0
name = "itheima is a brand of itcast"
for x in name:
if x == "a":
i += 1
print(f'"itheima is a brand of itcast"中共含有{i}個(gè)”a“')


range()語(yǔ)句


num = 100
i = 0
for x in range(1, num):
if x % 2 == 0:
i += 1
print(f'1到{num}中共含有{i}個(gè)偶數(shù)')

如果一定要在循環(huán)外部引用變量,在前面先定義好變量,形成臨時(shí)變量作用域


for i in range(1,10):
for j in range(1,i+1):
print(f"{j}*{i}={j*i}\t",end='')
print()


salary = 10000
for i in range(1,21):
if salary > 0:
import random
j = random.randint(1,10)
if j < 5:
print(f"員工{i}的績(jī)效為{j},不發(fā)工資")
continue
else:
print(f"員工{i}的績(jī)效為{j},發(fā)放1000元")
salary -=1000
print(f"總工資還剩{salary}")
else:
print("總工資余額不足,發(fā)放結(jié)束")
break




def temperature(t):
if t <= 37.5:
print(f"您的體溫是{t},體溫正常請(qǐng)進(jìn)")
else:
print(f"您的體溫是{t},需要隔離")
temperature(36)

return 返回的結(jié)果。上面的def temperature(t)函數(shù)中的print只是函數(shù)執(zhí)行的步驟,并不是返回的結(jié)果。








# 主菜單函數(shù)
def menu():
print("您選擇辦理的業(yè)務(wù):\n1.查詢(xún)余額\n2.存款\n3.取款\n4.退出")
# 查詢(xún)余額函數(shù)
def yu_e():
print(f"賬戶(hù)當(dāng)前的余額為{money}")
# 存款函數(shù)
def cunkuan():
global money
print(f"賬戶(hù)當(dāng)前的余額為{money}")
print("請(qǐng)輸入您的存款數(shù)量:")
cunru = int(input())
money += cunru
return money
# 取款函數(shù)
def qukuan():
global money
print(f"賬戶(hù)當(dāng)前的余額為{money}")
print("請(qǐng)輸入您的取款數(shù)量:")
quchu = int(input())
money -= quchu
return money
money = 500
print("請(qǐng)輸入你的姓名:")
name = input()
while money >= 0:
menu()
work = int(input())
if work == 1:
yu_e()
elif work == 2:
cunkuan()
yu_e()
elif work == 3:
qukuan()
if money >= 0:
yu_e()
else:
print("余額不足,無(wú)法取款")
elif work == 4:
print("本次服務(wù)結(jié)束,再見(jiàn)")
break
else:
print("輸入錯(cuò)誤,本次服務(wù)結(jié)束")
break








age_list = [21, 25, 21, 23, 22, 20]
age_list.append(31)
age_list.extend([29, 33, 30])
print(age_list)
print(age_list[0])
print(age_list[-1])
print(age_list.index(31))



第一種方法:
age_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_list = []
print(age_list)
i = 0
for x in age_list:
if x % 2 == 0:
new_list.append(x)
i += 1
print(new_list)
第二種方法:
age_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_list = []
print(age_list)
l = 0
while l < len(age_list):
if age_list[l] % 2 == 0:
new_list.append(age_list[l])
l += 1
print(new_list)




student = ("周杰輪", 11, ["football","music"])
print(student.index(11))
student[2].pop(0)
print(student)
student[2].insert(0,"coding")
print(student)



str1 = "itheima itcast boxuegu"
num = str1.count("it")
print(f"有{num}個(gè)‘it’字符")
str2= str1.replace(" ", "|")
print(str2)
str3 = str2.split("|")
print(str3)


str1 = "萬(wàn)過(guò)薪月,員序程馬黑來(lái),python學(xué)"
str2= str1[5: 10: 1][:: -1]
print(str2)





my_list = {'黑馬程序員', '傳智播客', '黑馬程序員', '傳智播客', 'itheima', 'itcast', 'itheima', 'itcast', 'best'}
new_list = set()
for x in my_list:
new_list.add(x)
print(new_list)



comployee_list = {
'王力鴻':
{"部門(mén)": "科技部",
"工資": 3000,
"級(jí)別": 1
},
'周杰倫':
{"部門(mén)": "市場(chǎng)部",
"工資": 5000,
"級(jí)別": 2
},
'林俊杰':
{"部門(mén)": "市場(chǎng)部",
"工資": 7000,
"級(jí)別": 3
}
}
for key in comployee_list:
print(f"{key}:{comployee_list[key]}")
for level in comployee_list.keys():
if comployee_list[level]["級(jí)別"] == 1:
comployee_list[level]["級(jí)別"] = 2
comployee_list[level]["工資"] += 1000
print(f"{level}:{comployee_list[level]}")
else:
print(f"{level}:{comployee_list[level]}")


列表變量 = [元素1, 元素2, 元素3]
空列表 = []或list()

元組變量 = (元素1, 元素2, 元素3)
空元組 = () 或 tuple()

字符串變量 = "元素"
空字符串 ="" 或 str()

集合變量 = {元素1, 元素2, 元素3}
空集合 = set()

字典變量 = {key1: value1, key2: value2, key3: value3}
空字典 = {} 或 dic()













utf-8通用語(yǔ)言
open()函數(shù)有7個(gè)參數(shù),這里只列舉了3個(gè),其他省略了,encoding實(shí)際是第4個(gè),注意不要用位置參數(shù),要用關(guān)鍵字參數(shù)。


read()
readlines()

readline()

close()

with open(()

with open("D:\python_files\words_Counts.txt", "r") as f:
content = f.readlines()
print(content)
one_content = []
one_content.extend(content)
str_con = str(one_content)
it = str_con.count("itheima")
print(f"該文檔中共有{it}個(gè)itheima")

f.read()生成的是str類(lèi)型



k = open("D:\python_files\words_pills.txt", "r", encoding="UTF-8")
f = open("D:\python_files\other_pills.txt", "w", encoding="UTF-8")
for words in k:
if words.count("測(cè)試") == 0:
f.write(words)
else:
continue
f.close()
k.close()

BUG






模塊寫(xiě)在代碼開(kāi)頭。只有import模塊,使用時(shí)要import.功能()調(diào)用,如果from 模塊 import功能,使用時(shí)直接功能()




調(diào)用同一個(gè)名稱(chēng)的模塊,后一個(gè)將把前一個(gè)覆蓋。

main,用于測(cè)試模塊且不會(huì)把值輸出給調(diào)用程序。

* 只調(diào)用all里面的功能。
如果不用 import *,直接 import test_B,是可以調(diào)用的,因?yàn)?只對(duì)all有效。







def str_reverse(s):
re_str = s[::-1]
return re_str
def sub_str(s, x, y):
cut_str = s[x:y:1]
return cut_str
def print_file_info(file_name):
try:
f = open(file_name, "r", encoding="utf-8")
content = f.read()
print(content)
except Exception as e:
print(f"程序異常原因:{e}")
finally:
f.close()

def append_to_file(file_name, data):
f = open(file_name, "a", encoding="utf-8")
f.write(data)
f.close()
主程序引用package包:
import my_utils.str_util
from my_utils import file_util
print(my_utils.str_util.str_reverse("黑馬程序員"))
print(my_utils.str_util.sub_str("itheima", 0, 4))
file_util.print_file_info("D:/python_files/my_utilsss.txt")
file_util.append_to_file("D:/python_files/my_utilsss.txt", "??")



官方畫(huà)廊(各種可視化圖表代碼):https://gallery.pyecharts.org/
安裝pyecharts包:win+r進(jìn)入命令窗口,輸入:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyecharts
安裝好后,輸入python進(jìn)入環(huán)境,再輸入import pyecharts,如果沒(méi)有報(bào)錯(cuò),說(shuō)明安裝成功。
標(biāo)粉位置必須一致。
from pyecharts.charts import Line
from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts
line = Line()
line.add_xaxis(["China", "USA", "UK"])
line.add_yaxis("GDP", [30, 20, 10])
line.set_global_opts(
title_opts=TitleOpts(title="GDP展示", pos_left="center", pos_bottom="1%"),
legend_opts=LegendOpts(is_show=True),
toolbox_opts=ToolboxOpts(is_show=True),
visualmap_opts=VisualMapOpts(is_show=True),
)
line.render()
查看圖像:

json格式化:

covid_charts.py


covid_map.py




?