# -*- coding: utf-8 -*-
#条件语句
'''
if 判断条件:
执行语句……
else:
执行语句……
''' flag = False
name = 'python'
if name == 'python': # 判断变量否为'python'
flag = True # 条件成立时设置标志为真
print 'welcome boss' # 并输出欢迎信息
else:
print name # 条件不成立时输出变量名称 '''
if 判断条件1:
执行语句1……
elif 判断条件2:
执行语句2……
elif 判断条件3:
执行语句3……
else:
执行语句4……
''' num = 2
if num == 3: # 判断num的值
print 'boss'
elif num == 2:
print 'user'
elif num == 1:
print 'worker'
elif num < 0: # 值小于零时输出
print 'error'
else:
print 'roadman' # 条件均不成立时输出 num = 9
if num >= 0 and num <= 10: # 判断值是否在0~10之间
print 'hello' num = 10
if num < 0 or num > 10: # 判断值是否在小于0或大于10
print 'hello'
else:
print 'undefine' num = 8
# 判断值是否在0~5或者10~15之间
if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):
print 'hello'
else:
print 'undefine' var = 100
if ( var == 100 ) : print "变量 var 的值为100"
print "Good bye!" #while语句
'''
while 判断条件:
执行语句……
'''
count = 0
while (count < 9):
print 'The count is:', count
count = count + 1 print "Good bye!" # continue 和 break 用法 i = 1
while i < 10:
i += 1
if i%2 > 0: # 非双数时跳过输出
continue
print i # 输出双数2、4、6、8、10 i = 1
while 1: # 循环条件为1必定成立
print i # 输出1~10
i += 1
if i > 10: # 当i大于10时跳出循环
break #死循环
'''
var = 1
while var == 1 : # 该条件永远为true,循环将无限执行下去
num = raw_input("Enter a number :")
print "You entered: ", num print "Good bye!"
''' #while … else
count = 0
while count < 5:
print count, " is less than 5"
count = count + 1
else:
print count, " is not less than 5" #简单语句组
flag = 1
while (flag): print 'Given flag is really true!';flag=0;
print "Good bye!" #for语句
'''
for iterating_var in sequence:
statements(s)
'''
for letter in 'Python': # 第一个实例
print '当前字母 :', letter fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # 第二个实例
print '当前水果 :', fruit print "Good bye!" #序列索引迭代
fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print '当前水果 :', fruits[index] print "Good bye!" #for...else
for num in range(10,20): # 迭代 10 到 20 之间的数字
for i in range(2,num): # 根据因子迭代
if num%i == 0: # 确定第一个因子
j=num/i # 计算第二个因子
print '%d 等于 %d * %d' % (num,i,j)
break # 跳出当前循环
else: # 循环的 else 部分
print num, '是一个质数' #嵌套循环
i = 2
while(i < 100):
j = 2
while(j <= (i/j)):
if not(i%j): break
j = j + 1
if (j > i/j) : print i, " 是素数"
i = i + 1 print "Good bye!" #break语句
for letter in 'Python': # First Example
if letter == 'h':
break
print 'Current Letter :', letter var = 10 # Second Example
while var > 0:
print 'Current variable value :', var
var = var -1
if var == 5:
break print "Good bye!" #continue语句
for letter in 'Python': # 第一个实例
if letter == 'h':
continue
print '当前字母 :', letter var = 10 # 第二个实例
while var > 0:
var = var -1
if var == 5:
continue
print '当前变量值 :', var
print "Good bye!" #pass语句
# 输出 Python 的每个字母
for letter in 'Python':
if letter == 'h':
pass
print '这是 pass 块'
print '当前字母 :', letter print "Good bye!" #格式字符串
print "My name is %s and weight is %d kg!" % ('Zara', 21) #时间与日期
import time; # This is required to include time module. ticks = time.time()
print "Number of ticks since 12:00am, January 1, 1970:", ticks localtime = time.localtime(time.time())
print "Local current time :", localtime localtime = time.asctime( time.localtime(time.time()) )
print "Local current time :", localtime import calendar cal = calendar.month(2008, 1)
print "Here is the calendar:"
print cal;

Python基本语法--语句的更多相关文章

  1. 简单探讨python中的语句和语法

    python程序结构 python"一切皆对象",这是接触python听到最多的总结了.在python中最基层的单位应该就是对象了,对象需要靠表达式建立处理,而表达式往往存在于语句 ...

  2. python学习第四讲,python基础语法之判断语句,循环语句

    目录 python学习第四讲,python基础语法之判断语句,选择语句,循环语句 一丶判断语句 if 1.if 语法 2. if else 语法 3. if 进阶 if elif else 二丶运算符 ...

  3. Python基本语法_控制流语句_if/while/for

    目录 目录 前言 软件环境 If 语句 While循环 break continue for 循环 遍历String 遍历Tuple 遍历List 遍历Dictionary 最后 前言 控制流语句用于 ...

  4. python基础语法_8循环语句

    http://www.runoob.com/python3/python3-loop.html while 循环 Python中while语句的一般形式: while 判断条件: 语句 无限循环 我们 ...

  5. python之最强王者(2)——python基础语法

    背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...

  6. 理解python的with语句

    Python’s with statement provides a very convenient way of dealing with the situation where you have ...

  7. Python基本语法[二],python入门到精通[四]

    在上一篇博客Python基本语法,python入门到精通[二]已经为大家简单介绍了一下python的基本语法,上一篇博客的基本语法只是一个预览版的,目的是让大家对python的基本语法有个大概的了解. ...

  8. Python基本语法,python入门到精通[二]

    在上一篇博客Windows搭建python开发环境,python入门到精通[一]我们已经在自己的windows电脑上搭建好了python的开发环境,这篇博客呢我就开始学习一下Python的基本语法.现 ...

  9. Python特殊语法:filter、map、reduce、lambda [转]

    Python特殊语法:filter.map.reduce.lambda [转] python内置了一些非常有趣但非常有用的函数,充分体现了Python的语言魅力! filter(function, s ...

随机推荐

  1. LSTM基础

    DNN,CNN,RNN:1.DNN:深度神经网络,或称多层感知机.解决早期单层感知机对于复杂函数不能模拟的情况.其形式为层之间全连接.实用sig等连续性函数模拟神经对机理的响应,训练算法使用    B ...

  2. 初识 Javascript.02 -- Date日期、Math对象、数据类型转换、字符串、布尔Boolean、逻辑运算符、if else 、三元表达式、代码调试方法、

    Date()对象: Date对象用于处理日期和时间. 1.1 Math对象  ◆Math.ceil()   天花板函数    向上取整  只取整数,不足则进1 ◆Math.floor()  地板函数 ...

  3. Tcl与Design Compiler (六)——基本的时序路径约束

    本文属于原创手打(有参考文献),如果有错,欢迎留言更正:此外,转载请标明出处 http://www.cnblogs.com/IClearner/  ,作者:IC_learner 时序约束可以很复杂,这 ...

  4. Struts(五)之OGNL、contextMap

    一.OGNL 1.1.定义 OGNL是Object-Graph Navigation Language的缩写,它是一个单独的开源项目. Struts2框架使用OGNL作为默认的表达式语言.它是一种功能 ...

  5. HTTP相关整理(上)

    这次整理HTTP相关知识点的初衷是因为项目中有大量与网络请求相关的知识细节点,所以这次整理的更多的是日常中用得到的点(参考图解HTTP),另外给打算做FE的新人们一些建议:多重视网络这方面的知识.文章 ...

  6. Ubuntu 16.04 设置MySQL远程访问权限

    本文记录一下在Ubuntu 16.04版本下设置MySQL数据库的远程访问. 第一步:修改配置文件的端口绑定 打开的目录可能会根据MySQL的版本稍有不同,可以先尝试打开/etc/mysql/my.c ...

  7. WSDL/WebService/SOAP/REST/AXIS/CXF 开放式服务

    WebService是一种数据交换标准.通过WebService标准,你可以把项目中的方法作为接口提供给其他项目使用. 有时候我们习惯性地将具体提供服务的某个方法称为WebService.比如图书系统 ...

  8. Hibernate基础学习(五)—对象-关系映射(下)

    一.单向n-1 单向n-1关联只需从n的一端可以访问1的一端. 域模型: 从Order到Customer的多对一单向关联.Order类中定义一个Customer属性,而在Customer类不用存放Or ...

  9. 对MySQL数据量日益增长产生的一点小想法

    最近一直在想一个问题 MySQL数据量日益庞大,目前单表总记录数有 300W+,导致sql语句执行的速度变慢,如果一直这样增长下去,总有一天会爆炸的.怎么办??怎么办?? 第一:想到的必然是 添加索引 ...

  10. Python 列表(List) 的三种遍历(序号和值)方法

    三种遍历列表里面序号和值的方法: 最近学习python这门语言,感觉到其对自己的工作效率有很大的提升,特在情人节这一天写下了这篇博客,下面废话不多说,直接贴代码 #!/usr/bin/env pyth ...