《Python基础教程第二版》第五章-条件、循环和其他语句(一)

news/2024/6/28 18:28:58

print和import

print打印多个表达式,用逗号,隔开

print 'abc:', 42, 'nonono'
#输出在每个参数之间添加空格

print在结尾处加上逗号,,接下来的语句会与前一条语句打印在同一行

print 'hello',
print 'word!'
#hello word!

import从模块中导入函数

import module
from module import function
from module import function,function2,function3
from module import*

as子句,给出想要使用模块的别名

import math as fu1
fu1.sqrt(4)from math import sqrt as fun2   
fun2(4)

赋值

序列解包/递归解包

多个赋值操作同时进行

x,y,z = 1,2,3

交换变量

x,y = y,x

popitem
popitem 获取并删除字典中任意的键值对,返回键值对元组,可以直接赋值到两个变量中。

scoundrel = {'name':'Robin', 'girlfriend':'Marion'}
key, value = scoundrel.popitem()

Note:

所解包的序列中的元素数量必须和赋值符号=左边的变量数量完全一致。
#除非使用星号运算符:
# a,b,*rest = [1,2,3,4]
# rest结果为[3,4]

链式赋值

x = y = function()

增量赋值

x = 2 
x += 2

语句块:缩进排版

创建语句块:

代码前放置空格缩进语句可以创建语句块。
Note: 块中的每行都应该缩进同样的量。
line1
line2:blocksame blockthe last bolck
line3

其中,

1. 冒号:标识语句块开始;
2. 块中每一个语句都是缩进相同量;
3. 退回到和已经闭合的块一样的缩进量时,表示当前块结束。

条件和条件语句

布尔变量

假fales(0)

Fales None 0 "" () [] {}
#其他都为真true(1)

bool函数

bool('I think I'm ok.')
#true,一般Python会自动转换这些值

if语句

if

name = raw_input(‘your name:’)
if name.endwith('afra'):print 'hell, afra!'

else

name = raw_input(‘your name:’)
if name.endswith('afra'):print 'hell, afra!'
else:print 'who?'

elif
检查多个条件,else if简写

num = input('number:')
if num > 0:print '>0'
elif num < 0:print '<0'
else:print '0'

嵌套
if嵌套if语句

name = raw_input('name?')
if name.endswith('afra'):if name.startswith('Mr.'):print 'Mr.afra!'elif name.startswith('Mrs.'):print 'Mrs.afra'else:print 'afra'
else:print 'who?'        

运算符

  1. 比较运算符

    < 
    == 
    >
    >= 
    <= 
    != 
    is  同一对象
    is not  不同对象昂
    x in y  x是y容器的成员
    not in    不是容器内的成员

连接运算符
比较运算符和赋值运算符都可以连接

0 < age < 10
  1. 相等运算符
    ==
  2. is同一性运算符

    `is`判定同一性,而不是相等性.
    使用`==`运算符来判定两个对象是否相等,使用`is`判定两个是否是同一个对象。
  3. in成员资格运算符

    name = raw_input('name?')
    if 's' in name:print 's'
    else:print '?'
  4. 字符串和序列比较

    字符串可按照字母顺序比较。
    字母的顺序值可以用 ord函数 查到
    ord()和chr()功能相反
    忽略大小写可以使用 upper()\lower()'KiJ'.lower() == 'kIj'.lower()
  5. 布尔运算符
    and / or / not

    number = input('number?')
    if number <= 10 and number >=1:print 'yes'
    else:print 'no'
    #连接比较 
    1 <= number <= 10#三个运算符结合
    if ((cash > price) or customer_has credit) and not out_stock:give()     
    #短路逻辑
    略
    #Python内置条件表达式
    a if b else c
    b为真,返回a;否则,返回c
    

断言

asser

if not condition:crash program

可以要求某些条件必须为真,例如,检查函数参数的属性、作为初期测试和调试过程中的辅助条件。

age = 10
assert 0 < age < 100
age = -1
assert 0 < age < 100, 'must be realistic'

http://lihuaxi.xjx100.cn/news/237339.html

相关文章

arduino 呼吸灯_如何改善您的Arduino呼吸机:用于临时COVID-19呼吸机设计的RTS和SCS简介...

arduino 呼吸灯The world as we know it was recently taken by storm. That storm was the outbreak of the COVID-19 pandemic. This has in turn created a shortage of ventilators world wide which has led many people to foray into the world of ventilator design. 我…

Centos 7 冗余备份磁盘配置介绍

Centos 7 冗余备份磁盘配置介绍我们上一盘介绍了Centos 7 磁盘阵列配置介绍&#xff0c;今天继续上一篇的配置介绍&#xff0c;通过上一篇的配置介绍我们发现了一个问题。&#xff0c;运维人员需要在硬盘硬件出现故障后&#xff0c;手动增加新的硬盘进去&#xff0c;这样很不方…

我理解的区块链

链客&#xff0c;专为开发者而生&#xff0c;有问必答&#xff01; 此文章来自链客区块链技术问答社区&#xff0c;未经允许拒绝转载。 区块链&#xff1a;一种保证陌生人之间或陌生组织之间可自动建立信任的技术&#xff0c;这种信任是值得的信任&#xff0c;是真实的信任&…

白盒交换机操作系统混战

白盒交换机的出现给了用户选择最佳软硬件平台的权利&#xff0c;它仅仅提供交换机硬件和ONIE&#xff08;开放网络安装环境&#xff09;&#xff0c;用户可以自行选择最合适的交换机芯片&#xff0c;降低成本实现最大效益。但是白盒交换机没有软件是无法使用的&#xff0c;因此…

响应因特网端口ping命令_如何使用Ping命令识别基本的Internet问题

响应因特网端口ping命令Next time you call your help desk, do you want to wow them with your networking knowledge? Using a command called “ping”, built right into your existing Mac, Windows, or Linux computer, will help identify basic connection problems.…

java中Array和ArrayList区别

2019独角兽企业重金招聘Python工程师标准>>> 1&#xff09;精辟阐述&#xff1a; 可以将 ArrayList想象成一种“会自动扩增容量的Array”。 2&#xff09;Array&#xff08;[]&#xff09;&#xff1a;最高效&#xff1b;但是其容量固定且无法动态改变&#xff1b; …

终于,「最近邻搜索」有通用方法了

作者&#xff1a;Kevin Hartnett 编译&#xff1a;Bing 如果你打算开一家咖啡馆&#xff0c;你一定想知道&#xff1a;“附近最近的一家咖啡馆在哪&#xff1f;”了解这些信息有助于应对商业竞争。 这种现象是计算机科学中广泛研究的问题&#xff0c;称为“最近邻搜索”。它的问…

DOM---文档对象模型(Document Object Model)的基本使用

一、DOM简介  文档对象模型&#xff08;Document Object Model&#xff0c;简称DOM&#xff09;&#xff0c;是W3C组织推荐的处理可扩展置标语言的标准编程接口。它是一种与平台和语言无关的应用程序接口(API),它可以动态地访问程序和脚本,更新其内容、结构和www文档的风格(目…