Python 常用语句
条件语句
a=input("请输入数字a的值:\n")
a=int(a) #从控制台接收到的都是字符串类型,需要转换
if a==0: #也可以写成if(a==0):
print("a=0")
elif a>0: #注意是elif
print("a>0")
else:
print("a<0")
循环语句
1、while语句
i=1
sum=0
while i<=100: #冒号
sum+=i
i=i+1 #注意python中没有++、--运算符
print(sum)
2、for语句
python中for语句和其他编程语言的for语句大不相同。python中for语句:
for var in eleSet:
statements
eleSet指的是元素集,可以是字符串、列表、元组、集合、字典,也可以是range()函数创建的某个数字区间。
使用for语句遍历字符串、列表、元组、集合、字典:
for ele in "hello": #遍历字符串的每个字符
print(ele) for ele in [1,2,3]: #遍历列表、元组、集合中的每个元素
print(ele)
dict={"name":"张三","age":10,"score":90} #遍历字典——方式1
for item in dict.items(): #item是元组,(key,value)的形式
print(item) #输出一个键值对
print(item[0],":",item[1]) #输出key:value
#遍历字典——方式2
for key in dict.keys():
print(key,":",dict.get(key))
使用for语句遍历某个数字区间:
for i in range(10): #遍历[0,10)
print(i) for i in range(20,30): #遍历[20,30)
print(i) for i in range(50,100,10): #遍历50,60,70,80,90,[50,100)上,步长为10
print(i) """
range()函数用于产生数列:
range([min,]max[,step])
数字区间是[min,max),max是必须的。缺省min时,默认为0,缺省step时,默认为1。
step支持负数,即负增长。
"""
产生指定数字区间上的元素集:
myList=list(range(10)) #强制类型转换,列表
print(myList) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] myTuple=tuple(range(10)) #元组
print(myTuple) #(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) mySet=set(range(10)) #集合
print(mySet) #{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} myDict=dict.fromkeys(range(10)) #字典
print(myDict) #{0: None, 1: None, 2: None, 3: None, 4: None, 5: None, 6: None, 7: None, 8: None, 9: None}
使用for语句+索引遍历字符串、列表、元组:
myList=["百度","腾旭","阿里"]
for i in range(len(myList)):
print(i,"\t",myList[i]) """
0 百度
1 腾旭
2 阿里
"""
else语句可以和while语句/for语句配合使用,当整个while/for循环执行完毕后,会执行else语句。
i=1
while i<5:
print(i)
i=i+1
else:
print("i=5") #整个循环执行完毕时,会自动执行else语句块
print("while over") for i in range(5):
print(i)
else:
print("for over")
print("application over")
continue语句:结束当前本次循环
break语句:结束当前循环
这2个语句只能写在循环体中,不能写在else语句块中。
i=1
while i<5:
i=i+1
if(i==2):
continue
print(i)
else:
print("over")
如果循环被break终止了,则else语句不会被执行:
i=1
while i<5:
i=i+1
if(i==5):
break
else:
print("else running...") #如果循环被break终止,else语句块会被跳过。只要循环中执行了break,else语句块就不再执行。
print("over") #over
pass语句
pass语句是一个空语句,即什么都不做。
command=input("请输入指令:\n")
if command=="nothing":
pass #什么都不做
elif command=="exit":
exit() #退出程序
else:
print("无法识别的指令")
说明
- python中没有switch语句、do...while语句。
- python使用缩进来标识语句块。
- 如果语句块只有一条语句,可以写在同一行:
if True:print(True)
Python 常用语句的更多相关文章
- Python—常用语句 if for while
Python-常用语句 判断语句 循环语句 break语句和continue语句 判断语句: if语句是最简单的添加判断语句,它可以控制程序的执行流程. if结构: if条件: 要执行的操作 ... ...
- python常用语句
流程控制if...else.... name = '疯子' res = input('你叫什么名字?') if res == name: print('帅哥') else: print('丑男') 如 ...
- sys模块和Python常用的内建函数
1.sys模块 当Python执行import sys语句的时候,它在sys.path变量中所列目录中寻找sys.py模块.如果找到了这个文件,这个模块的主块中的语句将被运行,然后这个模块将能够被使用 ...
- python——常用模块2
python--常用模块2 1 logging模块 1.1 函数式简单配置 import logging logging.debug("debug message") loggin ...
- 转:python常用运维脚本实例
python常用运维脚本实例 转载 file是一个类,使用file('file_name', 'r+')这种方式打开文件,返回一个file对象,以写模式打开文件不存在则会被创建.但是更推荐使用内置函 ...
- python常用运维脚本实例【转】
file是一个类,使用file('file_name', 'r+')这种方式打开文件,返回一个file对象,以写模式打开文件不存在则会被创建.但是更推荐使用内置函数open()来打开一个文件 . 首先 ...
- Python 常用 PEP8 编码规范
Python 常用 PEP8 编码规范 代码布局 缩进 每级缩进用4个空格. 括号中使用垂直隐式缩进或使用悬挂缩进. EXAMPLE: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则
python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess ...
- Python常用功能函数
Python常用功能函数汇总 1.按行写字符串到文件中 import sys, os, time, json def saveContext(filename,*name): format = '^' ...
随机推荐
- VS 编译总是出现错误: "LC.EXE 已退出,代码为-1"
最近在开发CS的一个项目时,编译总是出现错误: "LC.EXE 已退出,代码为-1" 解决方法一:用记事本打开*.licx,里面写的全是第三方插件的指定DLL,删除错误信息,保存, ...
- [PHP] Laravel使用redis保存SESSION
Laravel使用redis保存SESSION 首先确认服务器已经安装redis服务,php安装了redis扩展. 1.打开config/database.php.在redis配置项中增加sessio ...
- vb.net 读取 excel
Dim myConn AsNew ADODB.Connection myConn.CursorLocation = ADODB.CursorLocationEnum.adUseClient ' 用 ...
- html 指定页面字符集的两种方式
1.html指定页面字符集的两种方式 方式一: <meta charset="utf-8"> 方式二: <meta http-equiv="Cont ...
- 每日一问:谈谈 volatile 关键字
这是 wanAndroid 每日一问中的一道题,下面我们来尝试解答一下. 讲讲并发专题 volatile,synchronize,CAS,happens before, lost wake up 为了 ...
- 2018-2019-2 20165315《网络对抗技术》Exp9 Web安全基础
2018-2019-2 20165315<网络对抗技术>Exp9 Web安全基础 目录 一.实验内容 二.实验步骤 1.Webgoat前期准备 2.SQL注入攻击 Command Inje ...
- elasticsearch 分片的创建 集群重启分片运作
2016年11月12日ENGINEERING Every shard deserves a home 作者 Joshua Backing Share Here are some great slide ...
- mapreduce 函数入门 一
MapReduce 程序的业务编码分为两个大部分,一部分配置程序的运行信息,一部分 编写该 MapReduce 程序的业务逻辑,并且业务逻辑的 map 阶段和 reduce 阶段的代码分别继 承 Ma ...
- python mysqldb批量执行语句executemany
MySQLdb提供了两个执行语句的方法,一个是execute(),另一个是executemany() execute(sql) 可接受一条语句从而执行 executemany(templet,args ...
- 笔记:Map(就是用来Ctrl+C,V的)
JDK1.8:List -> Map: 1,Map<String, String> maps = userList.stream().collect(Collectors.toMap ...