黑马程序员python教程,8天python从入门到精通,学python看这套就

快捷键:
ctrl + d :复制当前行代码到下一行
shift + alt + ↑\↓ : 将当前行代码上移或下移
ctrl+/ :注释
字面量:被写在代码中固定的值。

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

money = 40
money = money-10
print("钱包还剩余:", money, "元")
type()语句


小数部分被直接抹掉,不考虑四舍五入。





使用 + 拼接,注意无法和整数、浮点数直接拼接。

数字类型被转换成字符串类型填入占位中:



这种格式不会理类型,不控制精度。


name = "传智播客"
stock_price = 19.99
stock_code = "003032"
growth_factor = 1.2
growth_days = 7
print(f"公司:{name}", f"股票代码:{stock_code}", f"当前股价:{stock_price}")
print("每日增长系数是:%.1f, 经过%d天的增长,股价达到了%.2f" % (growth_factor, growth_days, stock_price * growth_factor ** growth_days))

input()语句,键盘输入的数据,input均看作字符串,如果想变成数字,int()或float()转换。

布尔类型,非True即False。



print("欢迎来到黑马儿童游乐场,儿童免费,成人收费")
age =int(input("请输入你的年龄:"))
if age>=18:
print("您已成年,游玩需要补票10元")
print("祝您游玩愉快")


print("欢迎来到黑马动物园")
height = int(input("请输入你的身高(cm):"))
if height>120:
print("您的身高超出120cm,游玩需要补票10元")
else:
print("您的身高未超出120cm,可以免费游玩")
print("祝您游玩愉快!")


简化:

right_num = 10
if int(input("请输入第一次猜想的数字:")) == right_num:
print("恭喜第一次猜对") 这个条件实现后的步骤不写还不行?!
elif int(input("不对,再猜一次:")) == right_num:
print("恭喜第二次猜对")
elif int(input("不对,再猜最后一次:")) == right_num:
print("恭喜第三次猜对")
else:
print("sorry全部猜错啦,我想的是%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}个”a“')


range()语句


num = 100
i = 0
for x in range(1, num):
if x % 2 == 0:
i += 1
print(f'1到{num}中共含有{i}个偶数')

如果一定要在循环外部引用变量,在前面先定义好变量,形成临时变量作用域


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},不发工资")
continue
else:
print(f"员工{i}的绩效为{j},发放1000元")
salary -=1000
print(f"总工资还剩{salary}")
else:
print("总工资余额不足,发放结束")
break




def temperature(t):
if t <= 37.5:
print(f"您的体温是{t},体温正常请进")
else:
print(f"您的体温是{t},需要隔离")
temperature(36)

return 返回的结果。上面的def temperature(t)函数中的print只是函数执行的步骤,并不是返回的结果。








# 主菜单函数
def menu():
print("您选择办理的业务:\n1.查询余额\n2.存款\n3.取款\n4.退出")
# 查询余额函数
def yu_e():
print(f"账户当前的余额为{money}")
# 存款函数
def cunkuan():
global money
print(f"账户当前的余额为{money}")
print("请输入您的存款数量:")
cunru = int(input())
money += cunru
return money
# 取款函数
def qukuan():
global money
print(f"账户当前的余额为{money}")
print("请输入您的取款数量:")
quchu = int(input())
money -= quchu
return money
money = 500
print("请输入你的姓名:")
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("余额不足,无法取款")
elif work == 4:
print("本次服务结束,再见")
break
else:
print("输入错误,本次服务结束")
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}个‘it’字符")
str2= str1.replace(" ", "|")
print(str2)
str3 = str2.split("|")
print(str3)


str1 = "万过薪月,员序程马黑来,python学"
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 = {
'王力鸿':
{"部门": "科技部",
"工资": 3000,
"级别": 1
},
'周杰伦':
{"部门": "市场部",
"工资": 5000,
"级别": 2
},
'林俊杰':
{"部门": "市场部",
"工资": 7000,
"级别": 3
}
}
for key in comployee_list:
print(f"{key}:{comployee_list[key]}")
for level in comployee_list.keys():
if comployee_list[level]["级别"] == 1:
comployee_list[level]["级别"] = 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通用语言
open()函数有7个参数,这里只列举了3个,其他省略了,encoding实际是第4个,注意不要用位置参数,要用关键字参数。


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}个itheima")

f.read()生成的是str类型



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("测试") == 0:
f.write(words)
else:
continue
f.close()
k.close()

BUG






模块写在代码开头。只有import模块,使用时要import.功能()调用,如果from 模块 import功能,使用时直接功能()




调用同一个名称的模块,后一个将把前一个覆盖。

main,用于测试模块且不会把值输出给调用程序。

* 只调用all里面的功能。
如果不用 import *,直接 import test_B,是可以调用的,因为*只对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", "💎")



官方画廊(各种可视化图表代码):https://gallery.pyecharts.org/
安装pyecharts包:win+r进入命令窗口,输入:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyecharts
安装好后,输入python进入环境,再输入import pyecharts,如果没有报错,说明安装成功。
标粉位置必须一致。
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



