博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python学习笔记8—语句
阅读量:7236 次
发布时间:2019-06-29

本文共 1202 字,大约阅读时间需要 4 分钟。

条件语句

有的程序里写的是 /usr/bin Python,表示 Python 解释器在/usr/bin 里面。但是,如果写成 /usr/bin/env,则表示要通过系统搜索路径寻找 Python 解释器。不同系统,可

能解释器的位置不同,所以这种方式能够让代码更将拥有可移植性。

#/usr/bin/env python#coding=utf-8number = int(raw_input("请输入任意一个整数:"))if number == 10:        print "您输入的数字是:  %d"%numberelif number > 10:    print "This number is more than 10."elif number < 10:    print "This number is less than 10."else:    print "Are you a human?"

三元操作符

三元操作,是条件语句中比较简练的一种赋值方式,它的模样是这样的:

>>> a = "5" if 6>5 else 3>>> a'5'>>> a = "5" if 6<5 else 3>>> a3

循环语句

for

#/usr/bin/env pythonfruits=["apple","orange","banana"]for i in fruits:    print i

多个

a=[(1,2),(3,4),(5,6)]d=[]for x,y in a:    d.append(x+y)print d

while

#!/usr/bin/env python #coding:utf-8 a = 9 while a:     if a%2 == 0:       break     else:       print "%d is odd number"%a       a = 0 print "%d is even number"%a

while...else

一遇到 else 了,就意味着已经不在 while 循环内了。

#!/usr/bin/env Pythoncount = 0while count < 5:print count, " is less than 5"count = count + 1else:print count, " is not less than 5"

for...else

这个循环也通常用在当跳出循环之后要做的事情。

#!/usr/bin/env python# coding=utf-8from math import sqrtfor n in range(99, 1, -1):root = sqrt(n)if root == int(root):print nbreakelse:print "Nothing."

 

转载地址:http://tdlfm.baihongyu.com/

你可能感兴趣的文章
数字签名时间戳服务器的原理 !
查看>>
C++ Split string into vector<string> by space
查看>>
JavaScript学习——内置属性
查看>>
Oracle Profile 使用详解--zhuanzai
查看>>
Hadoop-1.1.2、HBase-0.94.7完全分布式集群结构
查看>>
TP-Link wr703N 使用华为HiLink系列上网卡的设置【转】
查看>>
士兵杀敌(四)(树状数组+线段树)
查看>>
Linux 高可用(HA)集群基本概念2
查看>>
Struts+Spring+Hibernate整合入门详解
查看>>
[转载]浅谈组策略设置IE受信任站点
查看>>
【转】maven导出项目依赖的jar包
查看>>
JS实现文本复制与剪切
查看>>
s标签可以if elseif else
查看>>
每天一个linux命令(20):linux chmod命令
查看>>
MySQL复合分区
查看>>
eval解析JSON中的注意点
查看>>
atitit.跨语言实现备份mysql数据库 为sql文件特性 api 兼容性java c#.net php js
查看>>
startActivityForResult不返回结果
查看>>
/dev/null简介
查看>>
uber优步提高成单率,轻松拿奖励!
查看>>