博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python基础学习笔记(六)
阅读量:7286 次
发布时间:2019-06-30

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

 

学到这里已经很不耐烦了,前面的数据结构什么的看起来都挺好,但还是没法用它们做什么实际的事。

 

基本语句的更多用法


 

使用逗号输出

>>> print 'age:',25age: 25

如果想要同时输出文本和变量值,却又不希望使用字符串格式化的话,那这个特性就非常有用了:

>>> name = 'chongshi'>>> salutation = 'Mr'>>> greeting = 'Hello.'>>> print greeting,salutation,nameHello. 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,z1 2 3>>> x,y=y,x>>> print x,y,z2 1 3

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

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

链式赋值

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

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

增理赋值

>>> x = 2>>> x += 1  #(x=x+1)>>> x *= 2  #(x=x*2)>>> x6

 

 

控制语句


 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 "
", line 1, in
assert 0 < age < 100 , 'the age must be realistic'AssertionError: the age must be realistic

 

 

循环语句


 打印1100的数(while循环)

x= 1while x <= 100:    print x  x += 1#输出1234..100

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

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

打印1100的数(for 循环)

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

是不是比while 循环更简洁,但根据我们以往学习其它语言的经验,while的例子更容易理解。

 

一个简单for 语句就能循环字典的所有键:

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

 

break语句

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

from math import sqrtfor 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 lengthenter something:ha        #输入长度小于3,要求重输enter something:hah       #输入长度等于3,提示信息Input is of sufficient lengthenter something:quit       #输入内容等于quit,结果

 

 

 

转载于:https://www.cnblogs.com/fnng/archive/2013/04/21/3034442.html

你可能感兴趣的文章
使用 django-blog-zinnia 搭建个人博客
查看>>
设置一个临时变量交换两个变量的值
查看>>
java:关于SerialVersionUid
查看>>
nagios与cacti的整合
查看>>
使用RHEL6.3+PXE+DHCP+Apache+NFS+KickStart 无人值守安装RHEL6.3
查看>>
JXL GC 问题探讨
查看>>
并发编程四之互斥
查看>>
极速免费-Magento开源产品上传利器magmi
查看>>
magento如何获得产品的属性Minimum Qty Allowed in Shopping Cart
查看>>
FreeMarker循环变量内建函数
查看>>
Python中time模块详解
查看>>
java 的模板方式设计模式
查看>>
跳转到servlet出现java.lang.InstantiationException:
查看>>
RedHat7 配置VNCServer
查看>>
git 回滚版本
查看>>
Nginx反向代理实现会话(session)保持的两种方式
查看>>
Nginx配置指令location匹配符优先级和安全问题
查看>>
sc create 创建启动服务带参数 目录不能有空格
查看>>
Glusterfs初体验
查看>>
Centos搭建SVN服务器三步曲
查看>>