公司网站建设 费用,做资料网站是自己建服务器好还是租用好,网站上图片不能下载 该怎么做,国内ip地址代理免费本篇文章我们讲解Python最基础语法#xff0c;包含#xff1a;数据类型、注释、变量、类型转换、命名规范、运算符、字符串拼接、字符串格式化、if条件判断、while循环、for循环、函数、读取文件、写入文件、异常捕获、包导入等。通过讲解语法注意事项实例代码详解#xff0…本篇文章我们讲解Python最基础语法包含数据类型、注释、变量、类型转换、命名规范、运算符、字符串拼接、字符串格式化、if条件判断、while循环、for循环、函数、读取文件、写入文件、异常捕获、包导入等。通过讲解语法注意事项实例代码详解希望能帮到大家。1、Python中6种常用的数据类型实例代码# 整数
num1 10
num2: int 20
print(num1)
print(num2)# float
float1 10.0
float2: float 11.0
print(float1)
print(float2)# boolean
bo1 True
bo2: bool False
print(bo1)
print(bo2)# 字符串
str1 字符串
str2 字符串2
str3: str 字符串3
print(str1)
print(str2)
print(str3)# List
list_1 [1, 2, 3, 4, 5]
print(type(list_1))
list_2: list [1, 2, 3, 4]
print(list_1)
print(list_2)# tuple
t_1 (1, 2, 3, 4, 5)
print(type(t_1))
t_2: tuple (1, 2, 3, 4)
print(t_1)
print(t_2)# set
s_1 {1, 2, 3, 4}
print(type(s_1))
s_2: set {1, 2, 34}
print(s_1)
print(s_2)# dict
d_1 {key: 1, name: 张三}
print(type(d_1))
d_2: dict {key: 2, name: 李四}2、Python中的注释使用代码实例# 单行注释多行注释
多行注释
3、Python中查看变量类型python中使用type()查询变量类型实例# dict
d_1 {key: 1, name: 张三}
print(type(d_1))
d_2: dict {key: 2, name: 李四}效果4、Python中常用类型转换实例str: str 1
print(type(str))
# 将字符串str转换为int类型
num: int int(str)
print(type(num))
5、Python中命名规范6、Python中的运算符7、Python中字符串拼接、格式化注意Python非字符串类型不能和字符串一起拼接name 张三
age 23
print(这个人名字叫 name 年龄是 age)
报错7.1、字符串格式化的两种方式name 张三
age 23
# print(这个人名字叫 name 年龄是 age)方式一
# 单条变量
print(这个人的名字叫%s % name)# 多条变量
print(这个人的名字叫%s年龄是%s % (name, age))方式二 格式 fxxx{变量}xxx{变量2},xxx{变量3}print(f这个人的名字叫:{name}年龄是{age})
执行效果8、Python中的if语句格式实例代码age 20# if
if age 20:print(你已经成年了)# if else
if age 20:print(成年了烦恼多)
else:print(儿童的时光让人怀念)# if elif else
if age 20:print(成年了烦恼多)
elif age 5:print(马上要上幼儿园了)
else:print(不知道怎么说了)
9、Python中的循环9.1、While实例
i 10
while i 1:i - 1print(f当前I值是{i})# 九九乘法表y 1
while y 9:x 1while x y:print(f{x}*{y}{x * y}, end )x 1print()y 19.2、For实例
name abcdef
for c in name:print(c)# range(num) 获取一个从0开始到num结束的数字序列不含num本身
# 例如 range(5)取得的数据是[0, 1, 2, 3, 4]
for i in range(5):print(i)# 获得一个从num1开始到num2结束的数字序列不含num2本身
# 如range(5, 10)取得的数据是[5, 6, 7, 8, 9]
for i in range(5, 10):print(i)# 获得一个从num1开始到num2结束的数字序列不含num2本身
# 数字之间的步长以step为准step默认为1
# 如range(5, 10, 2)取得的数据是[5, 7, 9]
for i in range(5, 10, 2):print(i)# for 打印九九乘法表
for y in range(1, 9):for x in range(1, y):print(f{x}*{y}{x * y}, end )print()
9.3、break、continue
# breaky 1
while True:if y 10:breakprint(f当前Y值{y})y 1# continuefor c in [a, b, c, d]:if c c:print(不打印c)continueprint(c)# 综合练习money 10000
for num in range(1, 20):tempNum random.randint(1, 10)if tempNum 5:print(f员工{num}绩效分{tempNum},低于5不发工资)continueif money 0:print(f账户没钱了下次再发)breakmoney - 1000print(f向员工{num}发工资{1000},账户余额{money})10、函数无参 无返回值数函数
def no_arg_fun():print(我是无参数函数)no_arg_fun()有参 无返回值数函数
def had_arg_fun(msg):print(fhello:{msg})had_arg_fun(world)有参数有返回值 函数
def had_arg_and_return_fun(msg):return fhello:{msg}print(had_arg_and_return_fun(有参有返回值))多参数
def mul_arg_fun(num1: int, num2: int) - int:return num1 num2# 传参数方式1多参数必须按顺序传入
print(mul_arg_fun(1, 2))
# 传参数方式2 多参数之间不需要舒心
print(mul_arg_fun(num24, num13))不定长参数 不定长参数一定是最后一个参数nums 被作为一个元组tuple传入
def mul_arg_fun_3(num1: int, *nums: int) - int:print(type(nums))for i in nums:num1 num1 ireturn num1print(mul_arg_fun_3(2, 3, 4, 5, 6, 7))不定长参数2 不定长参数一定是最后一个参数keyMaps 被作为一个字典
def mul_arg_fun_4(**key_maps) - int:print(type(key_maps))print(key_maps)print(mul_arg_fun_4(age4, name张三))函数嵌套使用
def fun_a():print(函数A)def fun_b():print(调用函数A之前)fun_a()print(调用函数A之后)fun_b()函数作为参数传递
def test_fun(compute):result compute(1, 2)print(result)def compute(x, y):return x y# 正常函数传入
test_fun(compute)
# 匿名函数传入
test_fun(lambda x, y: x y)
11、Phton中文件操作11.1、读取文件实例读文件
f open(1.txt, r, encodingUTF-8)
print(f.readlines())
f.close()print(我是分隔符1)f open(1.txt, r, encodingUTF-8)
print(f.read())
f.close()print(我是分隔符2)f open(1.txt, r, encodingUTF-8)
print(f.readline())
f.close()print(我是分隔符3)
# 此操作未关闭文件对象
for line in open(1.txt, r, encodingUTF-8):print(line, end)print()
print(我是分隔符 没打印)
with open(1.txt, r, encodingUTF-8) as f:f.readlines()
print(结束)
效果11.2、写入文件写入实例文件内容被替换写入操作
# 写入之前看看文件内容
f open(1.txt, r, encodingUTF-8)
print(f.readlines())
f.close()print(我是分隔符1)f open(1.txt, w, encodingUTF-8)
f.write(新写入一行)
f.flush()# 写入之后看看文件内容
f open(1.txt, r, encodingUTF-8)
print(f.readlines())
f.close()print(我是分隔符1)
追加实例写入操作
# 写入之前看看文件内容
f open(1.txt, r, encodingUTF-8)
print(f.readlines())
f.close()print(我是分隔符1)f open(1.txt, a, encodingUTF-8)
f.write(新写入二行)
f.flush()# 写入之后看看文件内容
f open(1.txt, r, encodingUTF-8)
print(f.readlines())
f.close()print(我是分隔符1)
12、Python中异常捕获13、Python中包模块导入这篇文章如果是有过Java或者其他开发语言基础的朋友很快就能掌握无非就是语法不一致可以将此篇文章作为日记后续忘记了翻下即可好了下篇文章我们实例讲解pyecharts生成可视化图形。
文章转载自: http://www.morning.hmmtx.cn.gov.cn.hmmtx.cn http://www.morning.skql.cn.gov.cn.skql.cn http://www.morning.ljsxg.cn.gov.cn.ljsxg.cn http://www.morning.nkjpl.cn.gov.cn.nkjpl.cn http://www.morning.hrpmt.cn.gov.cn.hrpmt.cn http://www.morning.llcsd.cn.gov.cn.llcsd.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.cjmmn.cn.gov.cn.cjmmn.cn http://www.morning.lhztj.cn.gov.cn.lhztj.cn http://www.morning.sgmis.com.gov.cn.sgmis.com http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.qyxnf.cn.gov.cn.qyxnf.cn http://www.morning.pmsl.cn.gov.cn.pmsl.cn http://www.morning.xqmd.cn.gov.cn.xqmd.cn http://www.morning.sbrrf.cn.gov.cn.sbrrf.cn http://www.morning.qznkn.cn.gov.cn.qznkn.cn http://www.morning.rqdx.cn.gov.cn.rqdx.cn http://www.morning.fnpyk.cn.gov.cn.fnpyk.cn http://www.morning.jhqcr.cn.gov.cn.jhqcr.cn http://www.morning.gsksm.cn.gov.cn.gsksm.cn http://www.morning.sqnrz.cn.gov.cn.sqnrz.cn http://www.morning.xsjfk.cn.gov.cn.xsjfk.cn http://www.morning.rui931.cn.gov.cn.rui931.cn http://www.morning.kpygy.cn.gov.cn.kpygy.cn http://www.morning.ccdyc.cn.gov.cn.ccdyc.cn http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.wmfr.cn.gov.cn.wmfr.cn http://www.morning.hqgkx.cn.gov.cn.hqgkx.cn http://www.morning.fznj.cn.gov.cn.fznj.cn http://www.morning.ynstj.cn.gov.cn.ynstj.cn http://www.morning.lcbgf.cn.gov.cn.lcbgf.cn http://www.morning.qytby.cn.gov.cn.qytby.cn http://www.morning.llxns.cn.gov.cn.llxns.cn http://www.morning.kqfdrqb.cn.gov.cn.kqfdrqb.cn http://www.morning.lgqdl.cn.gov.cn.lgqdl.cn http://www.morning.zqkms.cn.gov.cn.zqkms.cn http://www.morning.hfrbt.cn.gov.cn.hfrbt.cn http://www.morning.jgmlb.cn.gov.cn.jgmlb.cn http://www.morning.btypn.cn.gov.cn.btypn.cn http://www.morning.ljbch.cn.gov.cn.ljbch.cn http://www.morning.glswq.cn.gov.cn.glswq.cn http://www.morning.ctxt.cn.gov.cn.ctxt.cn http://www.morning.wjzzh.cn.gov.cn.wjzzh.cn http://www.morning.rmdsd.cn.gov.cn.rmdsd.cn http://www.morning.crkmm.cn.gov.cn.crkmm.cn http://www.morning.qclmz.cn.gov.cn.qclmz.cn http://www.morning.wrlff.cn.gov.cn.wrlff.cn http://www.morning.yqpck.cn.gov.cn.yqpck.cn http://www.morning.rsmtx.cn.gov.cn.rsmtx.cn http://www.morning.cmzgt.cn.gov.cn.cmzgt.cn http://www.morning.jstggt.cn.gov.cn.jstggt.cn http://www.morning.fhqsm.cn.gov.cn.fhqsm.cn http://www.morning.cywf.cn.gov.cn.cywf.cn http://www.morning.dblfl.cn.gov.cn.dblfl.cn http://www.morning.ynbyk.cn.gov.cn.ynbyk.cn http://www.morning.rdgb.cn.gov.cn.rdgb.cn http://www.morning.blfgh.cn.gov.cn.blfgh.cn http://www.morning.mlnzx.cn.gov.cn.mlnzx.cn http://www.morning.rykmf.cn.gov.cn.rykmf.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.nhzxr.cn.gov.cn.nhzxr.cn http://www.morning.lkbyq.cn.gov.cn.lkbyq.cn http://www.morning.kljhr.cn.gov.cn.kljhr.cn http://www.morning.guangda11.cn.gov.cn.guangda11.cn http://www.morning.mrfnj.cn.gov.cn.mrfnj.cn http://www.morning.qbgdy.cn.gov.cn.qbgdy.cn http://www.morning.bxczt.cn.gov.cn.bxczt.cn http://www.morning.rkck.cn.gov.cn.rkck.cn http://www.morning.llcsd.cn.gov.cn.llcsd.cn http://www.morning.krbjb.cn.gov.cn.krbjb.cn http://www.morning.kfwqd.cn.gov.cn.kfwqd.cn http://www.morning.rftk.cn.gov.cn.rftk.cn http://www.morning.wslpk.cn.gov.cn.wslpk.cn http://www.morning.rnnts.cn.gov.cn.rnnts.cn http://www.morning.qpsdq.cn.gov.cn.qpsdq.cn http://www.morning.ljllt.cn.gov.cn.ljllt.cn http://www.morning.cwnqd.cn.gov.cn.cwnqd.cn http://www.morning.hqpyt.cn.gov.cn.hqpyt.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.mqtzd.cn.gov.cn.mqtzd.cn