网站设计师薪资,网站app的区别是什么,推广策划方案模板,淄博搜索引擎优化条件判断 条件判断使用if、elif和else关键字。它们用于根据条件执行不同的代码块。
# 条件判断
age 18
if age 18:print(你还是个孩子#xff01;)
elif age 18:print(永远十八岁#xff01;)
else:print(你还年轻#xff01;)…条件判断 条件判断使用if、elif和else关键字。它们用于根据条件执行不同的代码块。
# 条件判断
age 18
if age 18:print(你还是个孩子)
elif age 18:print(永远十八岁)
else:print(你还年轻)
循环 Python中有两种主要的循环结构for循环和while循环。
for循环 for循环用于遍历序列如列表、元组、字符串等中的每个元素。
# for循环 遍历序列
array [test,1,[1,2,3,4,5]]
for arr in array:# 使用type函数判断类型 类似于java的判断类型instanceof 多进行类比if type(arr)list:for a in arr:print(我是第二层循环:,a)else: print(arr)
# 循环字典 实例
disc [{name:python,age:18},{name:java,age:18}]
for arr in disc:# 使用type函数判断类型 类似于java的判断类型instanceof 多进行类比if arr.get(name)python:print(我是python)else:print(我是java)
while循环 while循环在条件为True时重复执行代码块。
# while循环 比如累加计算
count 0
total 0
while count 100:count 1total total count
print(total)
循环内部操作
关键字作用适用场景pass空操作什么都不做仅占位。当语法上需要语句但逻辑上不需要操作时。在编写代码时如果某些逻辑暂时不需要实现但需要保持语法正确性可以使用 pass简单来说就是保证语法的正确性。continue跳过当前迭代的剩余代码直接进入下一次循环。当需要跳过某些特定条件的迭代时。break立即终止整个循环不再执行后续迭代。当满足某个条件时需要提前退出循环时。else当循环正常结束未被 break 中断时执行 else 块中的代码。当需要在循环正常结束后执行某些操作时。
# pass使用 当语法上需要语句但逻辑上不需要操作时 在编写代码时如果某些逻辑暂时不需要实现但需要保持语法正确性可以使用 pass。可以点到range里看下 很多用到了pass
for i in range(10):if i 2:passprint(f i {i} )else:print(i)if i 3:# 保证代码的完整性 如果有复杂的代码 需要待实现的部分就可以使用了pass
# continue跳过本次循环
for i in range(10):if i 3:continueelse:print(i) # 打印结果 0-9 不含3
# break结束本次循环
for i in range(10):if i 3:breakelse:print(i) # 打印结果 0 - 2
# else 除了break 不执行else外 其他的都会执行
for i in range(10):break
else:print(循环正常结束) # 因为break 不会被执行
for i in range(10):continue
else:print(循环正常结束) # 正常打印
for i in range(10):pass
else:print(循环正常结束) # 正常打印
列表操作 学到了循环就要学习关于列表的元素的操作其中包括查询、修改、插入、删除、排序等操作(实例animal [dog,cat,elephant,rabbit]也可以看下面的查询修改中的实例)。
操作方法/语法描述示例查询list[index]通过索引访问元素索引从 0 开始。animal[0] 返回 doglist[-1]或者list[len(list)-1]访问最后一个元素。animal[-1] 或者animal[len(animal)-1]返回 rabbitlist[start:end]切片操作获取从 start 到 end-1 的子列表。左开右闭区间。animal[0:2] 返回 [dog, cat]list.index(value)返回第一个匹配元素的索引。animal.index(cat) 返回 1value in list检查元素是否在列表中。monkey in animal 返回 False修改list[index] value修改指定索引处的元素。animal[1] monkey 将 cat 修改为 monkeylist[start:end] [values]修改切片范围内的元素。animal[0:2] [giraffe,ant] 替换 dog 和 cat删除del list[index]删除指定索引处的元素。del animal[1] 删除 catlist.remove(value)删除第一个匹配的元素。animal.remove(elephant) 删除 elephantlist.pop(index)删除并返回指定索引处的元素默认删除最后一个元素。animal.pop(1) 删除并返回 doglist.clear()清空列表中的所有元素。animal.clear() 清空列表并返回 []插入list.append(value)在列表末尾添加一个元素。animal.append(rat) 添加ratlist.insert(index, value)在指定索引处插入一个元素。animal.insert(2, lion) 在索引 2 处插入 lionlist.extend(iterable)将另一个可迭代对象的所有元素添加到列表末尾。animal.extend([fish, shrimp]) 添加 fish 和 shrimp其他操作len(list)返回列表的长度。len(animal) 返回 4list.sort()对列表进行排序默认升序。animal.sort() 排序列表list.reverse()反转列表中的元素顺序。animal.reverse() 反转列表list.copy()返回列表的浅拷贝。深拷贝方法为deepcopy()new_animal animal.copy() 创建 animal的副本list.count(value)返回列表中匹配元素的个数。animal.count(cat) 返回 1
查询
# 列表查询操作 通过索引访问元素索引从0开始。
animal [dog,cat,elephant,rabbit]
# 获取cat
print(animal[1]) # 输出cat
# list[-1] 函数访问最后一个函数 或者 list[len()-1]
print(animal[len(animal)-1]) # 输出rabbit
# list[start:end] 切片操作获取从start到end-1的子列表。左闭右开
print(animal[0:2]) # 输出[dog, cat]
# list.index(value) 返回第一个匹配元素的索引。
print(animal.index(cat)) #输出1
# in 是否在集合中
print(monkey in animal) #输出false
修改
# 列表修改操作
animal [dog,cat,elephant,rabbit]
# cat修改为monkey
animal[1] monkey
print(animal) # 输出 [dog, monkey, elephant, rabbit]
# list[start:end] [values] 修改切片范围内的元素。 dog和monkey修改为giraffe和ant 注意使用[1,2]会变成添加[dog, giraffe, ant, elephant, rabbit]考虑为什么
animal[0:2] [giraffe,ant]
print(animal) # 输出 [dog,monkey,giraffe,ant]
删除
# 列表删除操作
animal [dog,cat,elephant,rabbit]
# del list[index] 删除指定索引处的元素。
del animal[1]
print(animal) # 输出 [dog, elephant, rabbit]
# list.remove(value) 删除第一个匹配的元素。
animal.remove(elephant)
print(animal) # 输出 [dog, rabbit]
# list.pop(index) 删除并返回指定索引处的元素默认删除最后一个元素。
animal.pop()
print(animal) # 输出 [dog]
# list.clear() 清空列表中的所有元素。 animal.clear()清空列表
animal.clear()
print(animal) # 输出 []
插入
# 列表添加操作
animal [dog,cat,elephant,rabbit]
# list.append(value) 在列表末尾添加一个元素
animal.append(rat)
print(animal) # 输出 [dog,cat,elephant,rabbit,rat]
# list.insert(index, value) 在指定索引处插入一个元素。
animal.insert(2,lion)
print(animal) # 输出 [dog, cat, lion, elephant, rabbit, rat]
# list.extend(iterable) 将另一个可迭代对象的所有元素添加到列表末尾。 shrimp虾
animal.extend([fish,shrimp])
print(animal) # 输出 [dog, cat, lion, elephant, rabbit, rat, fish, shrimp] 其他操作
# 列表其他操作
animal [dog,cat,elephant,rabbit]
# len(list) 返回列表的长度。
print(len(animal)) # 4
# list.sort() 对列表进行排序默认升序。
animal.sort()
print(animal) # [cat, dog, elephant, rabbit]
# list.reverse() 反转列表中的元素顺序。
animal.reverse()
print(animal) # [rabbit, elephant, dog, cat]
# list.copy() 返回列表的浅拷贝。
new_animal animal.copy()
print(new_animal) # [rabbit, elephant, dog, cat]
# list.count(value) 返回列表中匹配元素的个数。
print(animal.count(cat)) # 1
代码块 在Python中代码块是通过缩进来定义的。通常使用4个空格或一个Tab键跟java不同的是使用代码缩进来完成java代表是以{开始或者以}结束为一个代码块来缩进。代码块用于组织代码使其在逻辑上成为一个整体。
# 这是一个代码块 input函数式输入框 类似与java的system.in输入 系统输入
s int(input(请输入一个数字))
if s 0: # 也可以写if(s 0 )但是没必要 因为python给个提示建议去掉 保证代码的按照缩进来进行更加规范print(这个数字是大于0的数字!) # 这行代码属于if语句的代码块
elif s 0:print(这个数字是等于0的数字!) # 这行代码属于elif语句的代码块
else:print(这个数字是小于0的数字!) # 这行代码属于else语句的代码块
方法块 在Python中使用def关键字来定义函数。函数是一段可重用的代码可以接受参数并返回结果。
# def来定义方法
def method1(name,age):print(姓名name,年龄age)
input(method1(python,18)) 文章转载自: http://www.morning.bsrcr.cn.gov.cn.bsrcr.cn http://www.morning.wskn.cn.gov.cn.wskn.cn http://www.morning.fgwzl.cn.gov.cn.fgwzl.cn http://www.morning.nkdmd.cn.gov.cn.nkdmd.cn http://www.morning.rdgb.cn.gov.cn.rdgb.cn http://www.morning.dfrenti.com.gov.cn.dfrenti.com http://www.morning.jkdtz.cn.gov.cn.jkdtz.cn http://www.morning.lfcfn.cn.gov.cn.lfcfn.cn http://www.morning.fgqbx.cn.gov.cn.fgqbx.cn http://www.morning.lkbyj.cn.gov.cn.lkbyj.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.psxcr.cn.gov.cn.psxcr.cn http://www.morning.hrnrx.cn.gov.cn.hrnrx.cn http://www.morning.ybshj.cn.gov.cn.ybshj.cn http://www.morning.mrlls.cn.gov.cn.mrlls.cn http://www.morning.wxckm.cn.gov.cn.wxckm.cn http://www.morning.nuobeiergw.cn.gov.cn.nuobeiergw.cn http://www.morning.jhzct.cn.gov.cn.jhzct.cn http://www.morning.rfpq.cn.gov.cn.rfpq.cn http://www.morning.rttp.cn.gov.cn.rttp.cn http://www.morning.rqckh.cn.gov.cn.rqckh.cn http://www.morning.qbwtb.cn.gov.cn.qbwtb.cn http://www.morning.yznsx.cn.gov.cn.yznsx.cn http://www.morning.ghssm.cn.gov.cn.ghssm.cn http://www.morning.kxmyj.cn.gov.cn.kxmyj.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.kqhlm.cn.gov.cn.kqhlm.cn http://www.morning.rgrys.cn.gov.cn.rgrys.cn http://www.morning.pnbls.cn.gov.cn.pnbls.cn http://www.morning.xfxnq.cn.gov.cn.xfxnq.cn http://www.morning.bwznl.cn.gov.cn.bwznl.cn http://www.morning.lwcqh.cn.gov.cn.lwcqh.cn http://www.morning.gqwpl.cn.gov.cn.gqwpl.cn http://www.morning.mbaiwan.com.gov.cn.mbaiwan.com http://www.morning.xglgm.cn.gov.cn.xglgm.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.rzmkl.cn.gov.cn.rzmkl.cn http://www.morning.bnxfj.cn.gov.cn.bnxfj.cn http://www.morning.knpbr.cn.gov.cn.knpbr.cn http://www.morning.nrydm.cn.gov.cn.nrydm.cn http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn http://www.morning.fhqsm.cn.gov.cn.fhqsm.cn http://www.morning.kfwqd.cn.gov.cn.kfwqd.cn http://www.morning.iknty.cn.gov.cn.iknty.cn http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn http://www.morning.sbkb.cn.gov.cn.sbkb.cn http://www.morning.nzkkh.cn.gov.cn.nzkkh.cn http://www.morning.rqckh.cn.gov.cn.rqckh.cn http://www.morning.jhgxh.cn.gov.cn.jhgxh.cn http://www.morning.ljdhj.cn.gov.cn.ljdhj.cn http://www.morning.tgnr.cn.gov.cn.tgnr.cn http://www.morning.wgbsm.cn.gov.cn.wgbsm.cn http://www.morning.msmtf.cn.gov.cn.msmtf.cn http://www.morning.tqsgt.cn.gov.cn.tqsgt.cn http://www.morning.hcszr.cn.gov.cn.hcszr.cn http://www.morning.kcsx.cn.gov.cn.kcsx.cn http://www.morning.xzjsb.cn.gov.cn.xzjsb.cn http://www.morning.bkkgt.cn.gov.cn.bkkgt.cn http://www.morning.tklqs.cn.gov.cn.tklqs.cn http://www.morning.mdlqf.cn.gov.cn.mdlqf.cn http://www.morning.gxeqedd.cn.gov.cn.gxeqedd.cn http://www.morning.mpsnb.cn.gov.cn.mpsnb.cn http://www.morning.mmqng.cn.gov.cn.mmqng.cn http://www.morning.mrlkr.cn.gov.cn.mrlkr.cn http://www.morning.yfnjk.cn.gov.cn.yfnjk.cn http://www.morning.tfsyk.cn.gov.cn.tfsyk.cn http://www.morning.ybgpk.cn.gov.cn.ybgpk.cn http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn http://www.morning.kgfsz.cn.gov.cn.kgfsz.cn http://www.morning.lpgw.cn.gov.cn.lpgw.cn http://www.morning.rhchr.cn.gov.cn.rhchr.cn http://www.morning.prqdr.cn.gov.cn.prqdr.cn http://www.morning.ljmbd.cn.gov.cn.ljmbd.cn http://www.morning.mrskk.cn.gov.cn.mrskk.cn http://www.morning.qfgxk.cn.gov.cn.qfgxk.cn http://www.morning.chhhq.cn.gov.cn.chhhq.cn http://www.morning.lrflh.cn.gov.cn.lrflh.cn http://www.morning.mnqg.cn.gov.cn.mnqg.cn http://www.morning.dtlqc.cn.gov.cn.dtlqc.cn http://www.morning.hdqqr.cn.gov.cn.hdqqr.cn