Python基本语法--语句
# -*- 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基本语法--语句的更多相关文章
- 简单探讨python中的语句和语法
python程序结构 python"一切皆对象",这是接触python听到最多的总结了.在python中最基层的单位应该就是对象了,对象需要靠表达式建立处理,而表达式往往存在于语句 ...
- python学习第四讲,python基础语法之判断语句,循环语句
目录 python学习第四讲,python基础语法之判断语句,选择语句,循环语句 一丶判断语句 if 1.if 语法 2. if else 语法 3. if 进阶 if elif else 二丶运算符 ...
- Python基本语法_控制流语句_if/while/for
目录 目录 前言 软件环境 If 语句 While循环 break continue for 循环 遍历String 遍历Tuple 遍历List 遍历Dictionary 最后 前言 控制流语句用于 ...
- python基础语法_8循环语句
http://www.runoob.com/python3/python3-loop.html while 循环 Python中while语句的一般形式: while 判断条件: 语句 无限循环 我们 ...
- python之最强王者(2)——python基础语法
背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...
- 理解python的with语句
Python’s with statement provides a very convenient way of dealing with the situation where you have ...
- Python基本语法[二],python入门到精通[四]
在上一篇博客Python基本语法,python入门到精通[二]已经为大家简单介绍了一下python的基本语法,上一篇博客的基本语法只是一个预览版的,目的是让大家对python的基本语法有个大概的了解. ...
- Python基本语法,python入门到精通[二]
在上一篇博客Windows搭建python开发环境,python入门到精通[一]我们已经在自己的windows电脑上搭建好了python的开发环境,这篇博客呢我就开始学习一下Python的基本语法.现 ...
- Python特殊语法:filter、map、reduce、lambda [转]
Python特殊语法:filter.map.reduce.lambda [转] python内置了一些非常有趣但非常有用的函数,充分体现了Python的语言魅力! filter(function, s ...
随机推荐
- IOS百度地图之--->第一篇《环境配置与基本使用》
Ios 百度地图SDK简易使用说明:http://developer.baidu.com/map/index.php?title=iossdk 先道歉:对于原来上传的Demo我很抱歉,什么都没有,也没 ...
- Centos下PXE+Kickstart无人值守安装操作系统
一.简介 1.1 什么是PXE PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持 ...
- 我的第一本docker书-阅读笔记
花了三四天看完了我的第一本docker书,话说书写的还是挺简单易懂的.与传统的VM,VirtualBox,或者与那种内核虚拟的xen,kvm相比,docker作为一种容器的虚拟方式,以启动进程的方式来 ...
- Struts(五)之OGNL、contextMap
一.OGNL 1.1.定义 OGNL是Object-Graph Navigation Language的缩写,它是一个单独的开源项目. Struts2框架使用OGNL作为默认的表达式语言.它是一种功能 ...
- httpclient源码分析之 PoolingHttpClientConnectionManager 获取连接
PoolingHttpClientConnectionManager是一个HttpClientConnection的连接池,可以为多线程提供并发请求服务.主要作用就是分配连接,回收连接等.同一个rou ...
- [SinGuLaRiTy] 平衡树
[SinGuLaRiTy-1009] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 二叉查找树 二叉查找树是指具有下列性质的非空二叉树: ⑴ ...
- 【Electron】Electron开发入门(三):main process和web page 通信
一.main process 和 web page 通信 electron框架主进程(Main Process)与嵌入的网页(web page,也就是renderer process)之间的通信 Ma ...
- Xmpp实现简单聊天系列 --- ①openfire部署
1. 下载最新的openfire安装文件 官方下载站点:http://www.igniterealtime.org/downloads/index.jsp#openfire 2. 下载完成后,执行你的 ...
- UWP: 掌握编译型绑定 x:Bind
在 UWP 开发中,我们在进行数据绑定时,除了可以使用传统的绑定 Binding,也可以使用全新的 x:Bind,由于后者是在程序编译时进行初始化操作(不同于 Binding,它是在运行时创建.初始化 ...
- hdoj_2546饭卡(强忍悲痛,好好写题解)
Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...