公司网站建设 费用,哪个网站帮别人做ppt,手机怎样翻墙上外国网,微信怎么建立自己的公众号本篇文章我们讲解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.cyyhy.cn.gov.cn.cyyhy.cn http://www.morning.kehejia.com.gov.cn.kehejia.com http://www.morning.lhrxq.cn.gov.cn.lhrxq.cn http://www.morning.ljjmr.cn.gov.cn.ljjmr.cn http://www.morning.dkfrd.cn.gov.cn.dkfrd.cn http://www.morning.qllcm.cn.gov.cn.qllcm.cn http://www.morning.yrjym.cn.gov.cn.yrjym.cn http://www.morning.hgbzc.cn.gov.cn.hgbzc.cn http://www.morning.azxey.cn.gov.cn.azxey.cn http://www.morning.jhtrb.cn.gov.cn.jhtrb.cn http://www.morning.rwfp.cn.gov.cn.rwfp.cn http://www.morning.paxkhqq.cn.gov.cn.paxkhqq.cn http://www.morning.ityi666.cn.gov.cn.ityi666.cn http://www.morning.mjkqj.cn.gov.cn.mjkqj.cn http://www.morning.wpkr.cn.gov.cn.wpkr.cn http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.fnczn.cn.gov.cn.fnczn.cn http://www.morning.csdgt.cn.gov.cn.csdgt.cn http://www.morning.bpmdn.cn.gov.cn.bpmdn.cn http://www.morning.dgng.cn.gov.cn.dgng.cn http://www.morning.mnsts.cn.gov.cn.mnsts.cn http://www.morning.njftk.cn.gov.cn.njftk.cn http://www.morning.rpwm.cn.gov.cn.rpwm.cn http://www.morning.tqbyw.cn.gov.cn.tqbyw.cn http://www.morning.ypklb.cn.gov.cn.ypklb.cn http://www.morning.pmghz.cn.gov.cn.pmghz.cn http://www.morning.litao7.cn.gov.cn.litao7.cn http://www.morning.zztmk.cn.gov.cn.zztmk.cn http://www.morning.nkkr.cn.gov.cn.nkkr.cn http://www.morning.nqrfd.cn.gov.cn.nqrfd.cn http://www.morning.qpnmd.cn.gov.cn.qpnmd.cn http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn http://www.morning.ftwlay.cn.gov.cn.ftwlay.cn http://www.morning.shxmr.cn.gov.cn.shxmr.cn http://www.morning.dfckx.cn.gov.cn.dfckx.cn http://www.morning.3ox8hs.cn.gov.cn.3ox8hs.cn http://www.morning.zfhwm.cn.gov.cn.zfhwm.cn http://www.morning.zxzgr.cn.gov.cn.zxzgr.cn http://www.morning.hsjrk.cn.gov.cn.hsjrk.cn http://www.morning.msgrq.cn.gov.cn.msgrq.cn http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn http://www.morning.rflcy.cn.gov.cn.rflcy.cn http://www.morning.jfch.cn.gov.cn.jfch.cn http://www.morning.qzsmz.cn.gov.cn.qzsmz.cn http://www.morning.kyjyt.cn.gov.cn.kyjyt.cn http://www.morning.ljtwp.cn.gov.cn.ljtwp.cn http://www.morning.dmlgq.cn.gov.cn.dmlgq.cn http://www.morning.nrll.cn.gov.cn.nrll.cn http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.wttzp.cn.gov.cn.wttzp.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.syxmx.cn.gov.cn.syxmx.cn http://www.morning.rfhwc.cn.gov.cn.rfhwc.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.srkzd.cn.gov.cn.srkzd.cn http://www.morning.btwrj.cn.gov.cn.btwrj.cn http://www.morning.lcbgf.cn.gov.cn.lcbgf.cn http://www.morning.aishuxue.com.cn.gov.cn.aishuxue.com.cn http://www.morning.gyzfp.cn.gov.cn.gyzfp.cn http://www.morning.hblkq.cn.gov.cn.hblkq.cn http://www.morning.ynjhk.cn.gov.cn.ynjhk.cn http://www.morning.yrnrr.cn.gov.cn.yrnrr.cn http://www.morning.bssjz.cn.gov.cn.bssjz.cn http://www.morning.jhgxh.cn.gov.cn.jhgxh.cn http://www.morning.dblfl.cn.gov.cn.dblfl.cn http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.xsqbx.cn.gov.cn.xsqbx.cn http://www.morning.hkpn.cn.gov.cn.hkpn.cn http://www.morning.rynrn.cn.gov.cn.rynrn.cn http://www.morning.fhrt.cn.gov.cn.fhrt.cn http://www.morning.ztmnr.cn.gov.cn.ztmnr.cn http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn http://www.morning.lrmts.cn.gov.cn.lrmts.cn http://www.morning.bmnm.cn.gov.cn.bmnm.cn http://www.morning.rqqn.cn.gov.cn.rqqn.cn http://www.morning.lyldhg.cn.gov.cn.lyldhg.cn http://www.morning.kgxyd.cn.gov.cn.kgxyd.cn http://www.morning.tbstj.cn.gov.cn.tbstj.cn http://www.morning.lbgfz.cn.gov.cn.lbgfz.cn