python示例

It is generally good practice for you not to mix tabs and spaces when coding in Python. Doing this can possibly cause a TabError, and your program will crash. Be consistent when you code - choose either to indent using tabs or spaces and follow your chosen convention throughout your program.

通常,对于您来说,在Python中进行编码时,不要混用制表符和空格是一种很好的做法。 这样做可能会导致TabError ,并且您的程序将崩溃。 编码时保持一致-选择使用制表符或空格进行缩进,并在整个程序中遵循所选的约定。

代码块和缩进 (Code Blocks and Indentation)

One of the most distinctive features of Python is its use of indentation to mark blocks of code. Consider the if-statement from our simple password-checking program:

Python最独特的功能之一是它使用缩进来标记代码块。 考虑一下我们简单的密码检查程序中的if语句:

if pwd == 'apple':print('Logging on ...')
else:print('Incorrect password.')print('All done!')

The lines print(‘Logging on …’) and print(‘Incorrect password.’) are two separate code blocks. These ones happen to be only a single line long, but Python lets you write code blocks consisting of any number of statements.

行print('Logging on ...')和print('Incorrect password。')是两个单独的代码块。 这些代码恰好只有一行,但是Python允许您编写由任意数量的语句组成的代码块。

To indicate a block of code in Python, you must indent each line of the block by the same amount. The two blocks of code in our example if-statement are both indented four spaces, which is a typical amount of indentation for Python.

要在Python中指示代码块,您必须使代码块的每一行缩进相同的数量。 我们的示例if语句中的两个代码块都缩进了四个空格,这是Python的典型缩进量。

In most other programming languages, indentation is used only to help make the code look pretty. But in Python, it is required for indicating what block of code a statement belongs to. For instance, the final print(‘All done!’) is not indented, and so is not part of the else-block.

在大多数其他编程语言中,缩进仅用于帮助使代码看起来更漂亮。 但是在Python中,需要指出语句属于哪个代码块。 例如,最终打印(“ All done!”)没有缩进,因此也不是else块的一部分。

Programmers familiar with other languages often bristle at the thought that indentation matters: Many programmers like the freedom to format their code how they please. However, Python indentation rules are quite simple, and most programmers already use indentation to make their code readable. Python simply takes this idea one step further and gives meaning to the indentation.

熟悉其他语言的程序员经常会想到缩进很重要:许多程序员喜欢自由地格式化自己喜欢的代码。 但是,Python缩进规则非常简单,大多数程序员已经使用缩进使代码可读。 Python只是将这一想法更进一步,并为缩进赋予了意义。

如果/省略语句 (If/elif-statements)

An if/elif-statement is a generalized if-statement with more than one condition. It is used for making complex decisions. For example, suppose an airline has the following “child” ticket rates: Kids 2 years old or younger fly for free, kids older than 2 but younger than 13 pay a discounted child fare, and anyone 13 years or older pays a regular adult fare. The following program determines how much a passenger should pay:

if / elif语句是具有多个条件的广义if语句。 它用于做出复杂的决定。 例如,假设一家航空公司具有以下“儿童”机票价格:2岁或以下的儿童免费乘坐飞机,2岁以上但13岁以下的儿童可享受折扣儿童票价,而13岁以上的任何人均可享受常规成人票价。 以下程序确定乘客应支付的费用:

# airfare.py
age = int(input('How old are you? '))
if age <= 2:print(' free')
elif 2 < age < 13:print(' child fare)
else:print('adult fare')

After Python gets age from the user, it enters the if/elif-statement and checks each condition one after the other in the order they are given. So first it checks if age is less than 2, and if so, it indicates that the flying is free and jumps out of the elif-condition. If age is not less than 2, then it checks the next elif-condition to see if age is between 2 and 13. If so, it prints the appropriate message and jumps out of the if/elif-statement. If neither the if-condition nor the elif-condition is True, then it executes the code in the else-block.

Python从用户处获得年龄后,它将进入if / elif语句,并按照给出的顺序依次检查每个条件。 因此,首先检查年龄是否小于2,如果小于2,则表明飞行是自由的,并跳出了省略条件。 如果age不小于2,则它检查下一个elif条件,以查看age是否在2到13之间。如果是,则打印适当的消息并跳出if / elif语句。 如果if条件和elif条件都不为True,则它将在else块中执行代码。

条件表达式 (Conditional expressions)

Python has one more logical operator that some programmers like (and some don’t!). It’s essentially a shorthand notation for if-statements that can be used directly within expressions. Consider this code:

Python还有一个逻辑运算符,有些程序员喜欢(有些则不喜欢!)。 它本质上是if语句的简写形式,可以在表达式中直接使用。 考虑以下代码:

food = input("What's your favorite food? ")
reply = 'yuck' if food == 'lamb' else 'yum'

The expression on the right-hand side of = in the second line is called a conditional expression, and it evaluates to either ‘yuck’ or ‘yum’. It’s equivalent to the following:

第二行中=右侧的表达式称为条件表达式,其结果为'yuck'或'yum'。 等效于以下内容:

food = input("What's your favorite food? ")
if food == 'lamb':reply = 'yuck'
else:reply = 'yum'

Conditional expressions are usually shorter than the corresponding if/else-statements, although not quite as flexible or easy to read. In general, you should use them when they make your code simpler.

条件表达式通常比相应的if / else语句短,尽管不够灵活或不易阅读。 通常,当它们使您的代码更简单时,应使用它们。

Python Documentation - Indentation

Python文档-缩进

翻译自: https://www.freecodecamp.org/news/indentation-in-python/

python示例

python示例_Python中的缩进示例相关推荐

  1. python参数化_Python 中如何实现参数化测试的方法示例

    之前,我曾转过一个单元测试框架系列的文章,里面介绍了 unittest.nose/nose2 与 pytest 这三个最受人欢迎的 Python 测试框架. 本文想针对测试中一种很常见的测试场景,即参 ...

  2. python括号_python中的括号

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! >>> x=>>> x2>>&g ...

  3. kafka python框架_Python中如何使用Apache Avro——Apache的数据序列化系统

    了解如何创建和使用基于Apache Avro的数据,以实现更好,更有效的传输. 在这篇文章中,我将讨论Apache Avro,这是一种开源数据序列化系统,Spark,Kafka等工具正在使用该工具进行 ...

  4. python标准化_python中标准化

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! sdk 3.0 实现了统一化,各个语言版本的 sdk具备使用方法相同.接口调用方 ...

  5. python字符集_PYTHON 中的字符集

    Python中的字符编码是个老生常谈的话题,今天来梳理一下相关知识,希望给其他人些许帮助. Python2的 默认编码 是ASCII,不能识别中文字符,需要显式指定字符编码:Python3的 默认编码 ...

  6. python 示例_Python中带有示例的关键字除外

    python 示例 Python关键字除外 (Python except keyword) except is a keyword (case-sensitive) in python, it is ...

  7. python 示例_Python中带有示例的class关键字

    python 示例 Python类关键字 (Python class keyword) class is a keyword (case-sensitive) in python, it is use ...

  8. python锁_Python中四种锁的使用示例(代码)

    本篇文章给大家带来的内容是关于Python中四种锁的使用示例(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. Lock互斥锁 使用前num = 0 def a(): globa ...

  9. python 遍历_python中使用iterrows()对dataframe进行遍历的示例

    假设我们有一个很简单的OTU表: 现在对这个表格进行遍历,一般写法为: import pandas as pd otu = pd.read_csv("otu.txt",sep=&q ...

最新文章

  1. 一维循环数组最大子数组求解
  2. 为炒股每天只花3元 MM从贷款上学到掌控千万
  3. docker设置国内镜像源
  4. hbase查看表结构_HBase 与Hive的集成
  5. 华为机试——坐标移动
  6. Java直接遍历并读取zip压缩文件的内容以及错误处理
  7. 拓扑链表c语言,数据结构2.2 链表的实现
  8. 区块链技术指南之分布式系统核心问题
  9. java p2p编程_JXTA-JAVA P2P网络编程技术(入门篇)
  10. 计算机毕业设计ssm图书管理系统
  11. cef调用本地html,在CefSharp中使用本地构建的网页(Working with locally built web page in CefSharp)...
  12. 感恩母亲节主题活动照片作品征集小程序
  13. 千岛湖2日团建旅行!游览天下第一秀水,感受湖岛遍布的磅礴气势!_团建拓展_嗨牛团建_杭州站...
  14. 理解ARC在Objective-C中的应用
  15. Windows bat 编程基本语法
  16. The Source and Develop of RootKit
  17. ng-alain中的st表格
  18. 飞腾服务器如何查看cpu型号,飞腾cpu怎么样 飞腾cpu简介及对比评测【详解】
  19. 英伟达光追支持Java吗,英伟达新显卡驱动发布 GTX 10系显卡现已支持光追
  20. win10降win7_软硬兼施Win7:8核笔记本/移动工作站出坑记

热门文章

  1. 【AI视野·今日CV 计算机视觉论文速览 第196篇】Wed, 12 May 2021
  2. 【shell资源限制】RLIMIT_MEMLOCK too small
  3. SpringBoot—CORS跨域问题详解和解决方案
  4. 数组算法 java 115918581
  5. 四窗口卖票 自己的票
  6. html列表的三种形式 1128
  7. mysql-复习表的基本操作01
  8. 编辑工具-sublime使用介绍
  9. Mybaits之Mapper动态代理开发
  10. Linux 6.8 源码安装MySQL8.0