python基础学习笔记(六)
学到这里已经很不耐烦了,前面的数据结构什么的看起来都挺好,但还是没法用它们做什么实际的事。
基本语句的更多用法
使用逗号输出
>>> print 'age:',25
age: 25
如果想要同时输出文本和变量值,却又不希望使用字符串格式化的话,那这个特性就非常有用了:
>>> name = 'chongshi'
>>> salutation = 'Mr'
>>> greeting = 'Hello.'
>>> print greeting,salutation,name
Hello. Mr chongshi
模块导入函数
从模块导入函数的时候,可以使用
import somemodule
或者
form somemodule immport somefunction
或者
from somemodule import somefunction.anotherfunction.yetanotherfunction
或者
from somemodule import *
最后一个版本只有确定自己想要从给定的模块导入所有功能进。
如果两个模块都有open函数,可以像下面这样使用函数:
module.open(...)
module.open(...)
当然还有别的选择:可以在语句末尾增加一个as子句,在该子句后给出名字。

>>> import math as foobar #为整个模块提供别名
>>> foobar.sqrt(4)
2.0
>>> from math import sqrt as foobar #为函数提供别名
>>> foobar(4)
2.0

赋值语句
序列解包

>>> x,y,z = 1,2,3
>>> print x,y,z
1 2 3
>>> x,y=y,x
>>> print x,y,z
2 1 3

可以获取或删除字典中任意的键-值对,可以使用popitem方

>>> scoundrel ={'name':'robin','girlfriend':'marion'}
>>> key,value = scoundrel.popitem()
>>> key
'name'
>>> value
'robin'

链式赋值
链式赋值是将同一个值赋给多个变量的捷径。

>>> x = y = 42
# 同下效果:
>>> y = 42
>>> x = y
>>> x
42

增理赋值
>>> x = 2
>>> x += 1 #(x=x+1)
>>> x *= 2 #(x=x*2)
>>> x
6
控制语句
if 语句:

name = raw_input('what is your name?')
if name.endswith('chongshi'):
print 'hello.mr.chongshi'
#输入
>>>
what is your name?chongshi #这里输入错误将没有任何结果,因为程序不健壮
#输出
hello.mr.chongshi

else子句

name = raw_input('what is your name?')
if name.endswith('chongshi'):
print 'hello.mr.chongshi'
else:
print 'hello,strager'
#输入
>>>
what is your name?hh #这里输和错误
#输出
hello,strager

elif 子句
它是“else if”的简写

num = input('enter a numer:')
if num > 0:
print 'the numer is positive'
elif num < 0:
print 'the number is negative'
else:
print 'the nuber is zero'
#输入
>>>
enter a numer:-1
#输出
the number is negative

嵌套
下面看一下if嵌套的例子(python是以缩进表示换行的)

name = raw_input('what is your name?')
if name.endswith('zhangsan'):
if name.startswith('mr.'):
print 'hello.mr.zhangsan'
elif name.startswith('mrs.'):
print 'hello.mrs.zhangsan'
else:
print 'hello.zhangsan'
else:
print 'hello.stranger'

如果输入的是“mr.zhangsan”输出第一个print的内容;输入mrs.zhangshan,输出第二个print的内容;如果输入“zhangsan”,输出第三个print的内容;如果输入的是别的什么名,则输出的将是最后一个结果(hello.stranger)
断言
如果需要确保程序中的某个条件一定为真才能让程序正常工作的话,assert 语句可以在程序中设置检查点。

>>> age = 10
>>> assert 0 < age < 100
>>> age = -1
>>> assert 0 < age < 100 , 'the age must be realistic' Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
assert 0 < age < 100 , 'the age must be realistic'
AssertionError: the age must be realistic

循环语句
打印1到100的数(while循环)

x= 1
while x <= 100:
print x
x += 1
#输出
1
2
3
4
.
.
100

再看下面的例子(while循环),用一循环保证用户名字的输入:

name = ''
while not name:
name = raw_input('please enter your name:')
print 'hello.%s!' %name
#输入
>>>
please enter your name:huhu
#输出
hello.huhu!

打印1到100的数(for 循环)

for number in range(1,101):
print number
#输出
1
2
3
4
.
.
100

是不是比while 循环更简洁,但根据我们以往学习其它语言的经验,while的例子更容易理解。
一个简单for 语句就能循环字典的所有键:

d = {'x':1,'y':2,'z':3}
for key in d:
print key,'corresponds to',d[key]
#输出
>>>
y corresponds to 2
x corresponds to 1
z corresponds to 3

break语句
break 用来结束循环,假设找100以内最大平方数,那么程序可以从100往下迭代到0,步长为-1

from math import sqrt
for n in range(99,0,-1):
root = sqrt(n)
if root == int(root):
print n
break
#输出
>>>
81

continue 语句
continue结束当前的迭代,“跳”到下一轮循环执行。

while True:
s=raw_input('enter something:')
if s == 'quit':
break
if len(s) < 3:
continue
print 'Input is of sufficient length'
#输入
>>>
enter something:huzhiheng #输入长度大于3,提示信息
Input is of sufficient length
enter something:ha #输入长度小于3,要求重输
enter something:hah #输入长度等于3,提示信息
Input is of sufficient length
enter something:quit #输入内容等于quit,结果
python基础学习笔记(六)的更多相关文章
- 0003.5-20180422-自动化第四章-python基础学习笔记--脚本
0003.5-20180422-自动化第四章-python基础学习笔记--脚本 1-shopping """ v = [ {"name": " ...
- Python基础学习笔记(六)常用列表操作函数和方法
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-lists.html 3. http://www.liaoxuef ...
- python 基础学习笔记(1)
声明: 本人是在校学生,自学python,也是刚刚开始学习,写博客纯属为了让自己整理知识点和关键内容,当然也希望可以通过我都博客来提醒一些零基础学习python的人们.若有什么不对,请大家及时指出, ...
- Python 基础学习笔记(超详细版)
1.变量 python中变量很简单,不需要指定数据类型,直接使用等号定义就好.python变量里面存的是内存地址,也就是这个值存在内存里面的哪个地方,如果再把这个变量赋值给另一个变量,新的变量通过之前 ...
- Python基础学习笔记(十三)异常
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-exceptions.html Python用异常对象(excep ...
- Python基础学习笔记(十二)文件I/O
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-files-io.html ▶ 键盘输入 注意raw_input函 ...
- Python基础学习笔记(十一)函数、模块与包
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-functions.html 3. http://www.liao ...
- Python基础学习笔记(十)日期Calendar和时间Timer
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-date-time.html 3. http://www.liao ...
- Python基础学习笔记(九)常用数据类型转换函数
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-variable-types.html 3. http://www ...
- Python基础学习笔记(八)常用字典内置函数和方法
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-dictionary.html 3. http://www.lia ...
随机推荐
- fedora输入法
fedora自带输入法,另外如果自己鼓捣的话很可能身心俱疲. 打开设置(在桌面右击也能打开) 区域和语言 在输入源中添加 汉语(中国) 快捷键 输入源切换:win+space 中英文切换:shift
- Django框架的简介
Django框架的背景 Django是一款基于Python开发的全栈式一体化Web 应用框架.2003 年问世之初,它只是 美国一家报社的内部工具,2005 年 7 月使用 BSD 许可证完成了开源. ...
- 实验吧web题(26/26)全writeup!超详细:)
#简单的SQL注入 http://www.shiyanbar.com/ctf/1875 1)试着在?id=1,没有错误 2)试着?id=1',出错了,有回显,说明有注入点: You have an e ...
- Expires和Cache-Control的理解
最近在学Nginx,学到有个地方式设置响应头信息,其中有两项是设置Expires和Cache-Control,来设置缓存时间,后来发现"并没有什么卵用",于是就搜了下资料,把整个思 ...
- P2626 斐波那契数列(升级版)(合数的质数分解, 大数为素数的概率十分小的利用)
题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: f(1)=1f(1) = 1 f(1)=1 f(2)=1f(2) = 1f(2)=1 f(n)=f(n−1)+f(n−2)f(n) = f ...
- Qt+QGis二次开发:打开S-57格式(*.000)电子海图数据,并设置多边形要素的显示风格
不过多的废话了,直接上源码: addChartlayers()方法时“打开海图”按钮的triggered()信号所绑定的槽函数. //添加海图数据小按钮槽函数 void MainWindow::add ...
- jconsole使用
先看一张图 根据JConsole和任务管理器对比,堆内存大小在250M左右,差不多空跑一个程序用idea启动springboot就是这个大小 项目启动初始类在一万个左右,活动线程50个上下,cpu利用 ...
- Objective-C autoreleasepool深入理解
Objective-C autorelease // main.m int main(int argc, char * argv[]) { @autoreleasepool { } } clang - ...
- Omi-touch实战 移动端图片轮播组件的封装
pc端的轮播,移动端的轮播都很常见.一年前,我还为手机端没有左滑,右滑事件从而封装了一个swipe库,可以自定义超过多少滑动时间就不触发,也可以设置滑动多少距离才触发,这一个功能的代码就达到400多行 ...
- TerraGate软件安装后,服务无法启动的解决方法
前些天有朋友问,TerraGate安装后,服务无法启动,而且启动按钮也是不可用的状态,不知道是为什么,如下图: 打开TerraGate的安装目录,发现里面缺少一些文件,如下图: *解决办法: 重新安装 ...