python示例_Python中的缩进示例-程序员宅基地

技术标签: python  java  人工智能  编程语言  mysql  

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示例

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cumi6497/article/details/108153110

智能推荐

整合项目的实现方案-程序员宅基地

文章浏览阅读216次。 “消除信息孤岛,实现资源共享”是现在应用软件都争取实现的目标,我现在一直都很关注这方面的解决方案,现在总结了一下学习的心得:请看这个原理图:原理图 这个是完成了数据从各个子系统,汇总到中心数据库中,是一个数据ETL(Extract-Transform-Load的缩写,即数据抽取、转换、装载的过程)的过程; 然后我们的程序依据就是整合完成的数据库,在数据库上面..._项目整合方案

redis如何清空当前缓存和所有缓存-程序员宅基地

文章浏览阅读1.5k次。Windows环境下使用命令行进行redis缓存清理1、redis安装目录下输入cmd2、redis-cli -p 端口号3、flushdb 清除当前数据库缓存4、flushall 清除整个redis所有缓存转载于:https://www.cnblogs.com/lxwphp/p/10870399.html..._bladex 项目中 清除redis 缓存

FileUpload文件上传_list<fileitem> items = fileupload. parserequest(re-程序员宅基地

文章浏览阅读706次。1.进行文件上传时,表单需要做的准备:1).请求方式为POST:<form action="uploadServlet" method="post"....>2).使用file的表单域:<input type="file" name="file" />3).请求的编码方式:<form action="uploadServlet" method="post" en..._list items = fileupload. parserequest(request);

自己做量化交易软件(44)小白量化实战17--利用小白量化金融模块在迅投QMT极速策略交易系统上仿大智慧指标回测及实战交易设计_小白量化平台-程序员宅基地

文章浏览阅读1.2w次,点赞8次,收藏36次。自己做量化交易软件(44)小白量化实战17–利用小白量化金融模块在迅投QMT极速策略交易系统上仿大智慧指标回测及实战交易设计小白量化平台是由若干小白金融模块构成。其中包含行情接收模块,仿通达信大智慧公式计算模块,K线及指标绘图模块,回测模块,Tkinter GUI窗口设计模块等构成。每个模块都能独立应用。最新实战版本小白量化xb2f压缩包中,提供了最新的公式库,除了增加了几十个公式函数外,还集成了通达信数百个常用公式,例如kd,rsi,macd,boll…等等,使用者不用复制函数,可直接使用这些系统默认_小白量化平台

在线大数据学习,效果怎么样?-程序员宅基地

文章浏览阅读687次。(一)在线学习过程性活动记录子系统虚拟的在线学习过程可以看作是五类元素的组合,即学习者、学习资源、交互、事件以及学习结果。这五个元素之间相互影响,密切相关,共同构成系统的在线学习活动。根据在线学习活动属性与关键内容,我们将记录子系统中的过程性活动分为互动交流、资源使用、学习作品、资源分享、平台利用、自我评价、学伴评价、教师点评、学习反思和成长记录等核心活动。Web爬虫具有目标信息采集准确、应用...

查找编号(倍增)-程序员宅基地

文章浏览阅读118次。查找编号输入样例11 31 3 3 3 5 7 9 11 13 15 151 3 6AC代码#include<cstdio>using namespace std;int n,m,a[2000005];int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=m;i++) { int x,ans=0;_查找编号

随便推点

Kotlin 反射--笔记_kotlin文件获取 class对象-程序员宅基地

文章浏览阅读1.4k次。1.类引用kotlin是基于java1.6设计,完全兼容java,所以和java很多功能都是互通的。如java反射中Class对象,在Kotlin叫KClass对象。1.1 Class和KClassClass//在kotlin中获取Class对象class ReflectDemo(val x: Int = 0) { constructor() : this(0) { } fun test() { println(x) }}//获取Cl_kotlin文件获取 class对象

如何编写自己的数据访问层_论文数据访问层次怎么写-程序员宅基地

文章浏览阅读204次。概述在二开(族库、算量等)或者大部分管理软件的开发中,多数系统架构是基于数据库设计的,那么怎么设计数据访问层呢?一、设计框架图中分成三块:1、左边的xxxServices为app公开访问数据接口;2、中间红色部分为底层操作数据库接口,通过依赖注入的方式给xxxServices使用;3、xxx DAL为底层操作数据库接口的具体实现,可能是SQL Server的实现,可能是用于程序开发的的Fake数据提供的实现,也可能是阿里云、腾讯云等云服务器的数据访问的实现等;二、导出DAL代码1、使用“P_论文数据访问层次怎么写

tk-mybatis使用注意事项_tkmybatis 驼峰-程序员宅基地

文章浏览阅读906次,点赞2次,收藏2次。1.实体类和表的映射如果表的设计是这样的:table_name : unit字段1:unit_id (主键)字段2:unit_name而实体类是这样的:@Table(name = "unit")public class Unit { @Id private Integer unit_id; private String unit_name;}此时,实体类的属性和字段是对应的,这样没问题..._tkmybatis 驼峰

GDKOI游记_gdkoi 游记-程序员宅基地

文章浏览阅读601次。day0又到了写游记的时候了~ 这次koi摆到了冬令营之前,还设置了一个讲课日,很悠闲啊。然而之前一个月都在做5h3题的模拟赛,可能会对4h4题不适应day1早上出门,感到凉凉。 集体迟到了15min…结果试机的时间也没有了。只得匆匆应战。看完所有题,第一题马上有了思路,但是发现自己写出来是两个log的。计算一下感觉有点虚。但是评测时开O2,我就大胆写了。 结果样例调了挺久,一对拍又是错。。很_gdkoi 游记

Eclipse Maven项目搭建-程序员宅基地

文章浏览阅读61次。为什么80%的码农都做不了架构师?>>> ..._eclipse meven项目搭建

【转载】matlab函数调用-程序员宅基地

文章浏览阅读904次。Matlab是一个强大的数学计算/仿真工具,其内置了很多实用的现成的函数,而且我们经常也自己定义很多m函数。但在很多情况下,我们不得不使用VC编程。那么,如何在VC中利用matlab的资源呢? 在这里我简要的以一个简单的例子来说明一下如果在VC中调用matlab中定义的.m文件。繁多的理论就不说了,简明扼要的说一个实例。相信大家看过之后都会马上学会的J 其中..._matlab 调用函数 csdn

推荐文章

热门文章

相关标签