龙空技术网

中英双语 Python第二章 变量和简单的数据类型-5(数字)

数理模型王 121

前言:

而今朋友们对“python 输入 数字”大体比较注重,大家都需要知道一些“python 输入 数字”的相关内容。那么小编同时在网摘上汇集了一些关于“python 输入 数字””的相关文章,希望看官们能喜欢,大家一起来了解一下吧!

Numbers 数字

Numbers are used quite often in programming to keep score in games, represent data in visualizations, store information in web applications, and so on. Python treats numbers in several different ways, depending on how they’re being used. Let’s first look at how Python manages integers, because they’re the simplest to work with.

在编程中数字被用的非常多,例如数字可以被用来在游戏中记录分数、在可视化中表示数据、在web应用程序中存储信息等。Python根据数字的使用方式,以几种不同的方式处理数字。让我们先来看看Python是如何管理整数的,因为它们是最简单的。

Integers 整数

You can add (+), subtract (-), multiply (*), and divide (/) integers in Python.

在Python中,您可以对整数进行加(+)、减(-)、乘(*)和除(/)运算。

>>> 2 + 3

5

>>> 3 - 2

1

>>> 2 * 3

6

>>> 3 / 2

1.5

In a terminal session, Python simply returns the result of the operation. Python uses two multiplication symbols to represent exponents:

在终端会话中,Python只返回操作的结果。Python使用两个乘法符号来表示指数:

>>> 3 ** 2

9

>>> 3 ** 3

27

>>> 10 ** 6

1000000

Python supports the order of operations too, so you can use multiple operations in one expression. You can also use parentheses to modify the order of operations so Python can evaluate your expression in the order you specify. For example:

Python也支持操作顺序,因此可以在一个表达式中使用多个操作。您还可以使用括号来修改操作的顺序,以便Python可按照您指定的顺序计算表达式。例如:

>>> 2 + 3*4

14

>>> (2 + 3) * 4

20

The spacing in these examples has no effect on how Python evaluates the expressions; it simply helps you more quickly spot the operations that have priority when you’re reading through the code.

这些示例中的空格对Python如何计算表达式没有影响;它只是帮助您在阅读代码时更快地发现具有优先级的操作。

Floats 浮点数

Python calls any number with a decimal point a float. This term is used in most programming languages, and it refers to the fact that a decimal point can appear at any position in a number. Every programming language must be carefully designed to properly manage decimal numbers so numbers behave appropriately no matter where the decimal point appears.

Python将任何带小数点的数字称为浮点数。这个术语在大多数编程语言中都使用,它指的是小数点可以出现在数字的任何位置。每种编程语言都必须进行精心设计,以正确管理浮点型数字,以使无论小数点出现在哪里,数字都能正确运行。

For the most part, you can use decimals without worrying about how they behave. Simply enter the numbers you want to use, and Python will most likely do what you expect:

在大多数情况下,你可以使用小数,而不用担心它们的行为。只需输入您想要使用的数字,Python很可能会按照您的期望执行操作:

>>> 0.1 + 0.1

0.2

>>> 0.2 + 0.2

0.4

>>> 2 * 0.1

0.2

>>> 2 * 0.2

0.4

But be aware that you can sometimes get an arbitrary number of decimal places in your answer:

但要注意,有时你的答案中会有任意小数位数:

>>> 0.2 + 0.1

0.30000000000000004

>>> 3 * 0.1

0.30000000000000004

This happens in all languages and is of little concern. Python tries to find a way to represent the result as precisely as possible, which is sometimes difficult given how computers have to represent numbers internally. Just ignore the extra decimal places for now; you’ll learn ways to deal with the extra places when you need to in the projects in Part II.

这种情况在所有语言中都会发生,而且很少引起关注。Python试图找到一种尽可能精确地表示结果的方法,考虑到计算机必须在内部表示数字,这有时很困难。暂时忽略多余的小数位数;在第二部分的项目中,您将学习如何在需要时处理多余的位置。

Integers and Floats 整数和浮点数

When you divide any two numbers, even if they are integers that result in a whole number, you’ll always get a float:

当你对任意两个数字做除法时,即使它们都是整数,它们的商也是整数,在程序中你也会得到一个浮点数:

>>> 4/2

2.0

If you mix an integer and a float in any other operation, you’ll get a float as well:

如果你对整数和浮点数混合进行任何其他操作,也会得到一个浮点数:

>>> 1 + 2.0

3.0

>>> 2 * 3.0

6.0

>>> 3.0 ** 2

9.0

Python defaults to a float in any operation that uses a float, even if the output is a whole number.

Python在使用浮点数的任何操作中都默认输出为浮点数,即使输出是整数。

Underscores in Numbers 数字下划线

When you’re writing long numbers, you can group digits using underscores to make large numbers more readable:

写长数字时,可以使用下划线对数字进行分组,以使大数字更可读:

>>> universe_age = 14_000_000_000

When you print a number that was defined using underscores, Python prints only the digits:

打印使用下划线定义的数字时,Python只打印数字:

>>> print(universe_age)

14000000000

Python ignores the underscores when storing these kinds of values. Even if you don’t group the digits in threes, the value will still be unaffected. To Python, 1000 is the same as 1_000, which is the same as 10_00. This feature works for integers and floats, but it’s only available in Python 3.6 and later.

Python在存储这些类型的数值时会忽略下划线。即使不是将三个数字分为一组,该值也不会受到影响。对于Python,1000与1_000相同,与10_00也相同。此功能适用于整数和浮点数,但仅在Python 3.6及更高版本中可用。

Multiple Assignment 多重赋值

You can assign values to more than one variable using just a single line. This can help shorten your programs and make them easier to read; you’ll use this technique most often when initializing a set of numbers.

只需使用一行即可为多个变量赋值。这可以帮助缩短你的程序,使它们更容易阅读;在初始化一组数字时,最常使用这种技术。

For example, here’s how you can initialize the variables x, y, and z to zero:

例如,以下是如何将变量x、y和z初始化为零:

>>> x, y, z = 0, 0, 0

You need to separate the variable names with commas, and do the same with the values, and Python will assign each value to its respectively positioned variable. As long as the number of values matches the number of variables, Python will match them up correctly.

您需要用逗号分隔变量名,并对变量值执行同样的操作,Python会将每个值分配给其各自对应的变量。只要值的数量与变量的数量匹配,Python就会正确地匹配它们。

Constants 常量

A constant is like a variable whose value stays the same throughout the life of a program. Python doesn’t have built-in constant types, but Python programmers use all capital letters to indicate a variable should be treated as a constant and never be changed:

常量就像一个变量,其值在程序的整个生命周期中保持不变。Python没有内置的常量类型,但Python编程人员使用所有大写字母来表示变量应被视为常量,并且永远不会更改:

MAX_CONNECTIONS = 5000

When you want to treat a variable as a constant in your code, make the name of the variable all capital letters.

当您想在代码中将变量视为常量时,请将变量的名称设置为全大写字母。

Comments 注释

Comments are an extremely useful feature in most programming languages. Everything you’ve written in your programs so far is Python code. As your programs become longer and more complicated, you should add notes within your programs that describe your overall approach to the problem you’re solving. A comment allows you to write notes in English within your programs.

在大多数编程语言中注释都是非常有用的功能。到目前为止,您在程序中编写的所有内容都是Python代码。随着你的程序变得越来越长、越来越复杂,你应该在程序中添加注释,描述你解决问题的整体方法。注释允许您在程序中用英语写笔记。

How Do You Write Comments? 如何写注释

In Python, the hash mark (#) indicates a comment. Anything following a hash mark in your code is ignored by the Python interpreter. For example:

# Say hello to everyone.

print("Hello Python people!")

在Python中,标记(#)表示一个注释。Python解释器会忽略代码中#标记后面的任何内容。

Python ignores the first line and executes the second line.

Python忽略掉第一行执行第二行。

Hello Python people!

What Kind of Comments Should You Write? 应该写什么样的注释

The main reason to write comments is to explain what your code is supposed to do and how you are making it work. When you’re in the middle of working on a project, you understand how all of the pieces fit together. But when you return to a project after some time away, you’ll likely have forgotten some of the details. You can always study your code for a while and figure out how segments were supposed to work, but writing good comments can save you time by summarizing your overall approach in clear English.

写注释主要是解释代码用于做什么以及如何使其工作。当你正在做一个项目时,你会理解所有的部分是如何组合在一起的。但是,当你离开一段时间后回到一个项目时,你可能已经忘记了一些细节。你可以研究你的代码一段时间,弄清楚每段是如何工作的,但通过用清晰的英语总结你的整体方法,写好评论可以节省时间。

If you want to become a professional programmer or collaborate with other programmers, you should write meaningful comments. Today, most software is written collaboratively, whether by a group of employees at one company or a group of people working together on an open source project. Skilled programmers expect to see comments in code, so it’s best to start adding descriptive comments to your programs now. Writing clear, concise comments in your code is one of the most beneficial habits you can form as a new programmer.

如果你想成为一名专业的程序员或与其他程序员合作,你应该写含义清晰的评论。如今,大多数软件都是协作编写的,无论是由一家公司的一群员工还是由一群人共同开发开源项目。熟练的程序员希望在代码中看到注释,所以现在开始最好在程序中添加描述性的注释。作为一名新程序员,在代码中写出清晰、简洁的注释是最有益的习惯之一。

When you’re determining whether to write a comment, ask yourself if you had to consider several approaches before coming up with a reasonable way to make something work; if so, write a comment about your solution. It’s much easier to delete extra comments later on than it is to go back and write comments for a sparsely commented program. From now on, I’ll use comments in examples throughout this book to help explain sections of code.

当你决定是否写评论时,问问自己,在想出一个合理的方法之前,是否必须考虑几种方法;如果是的话,写一篇关于你的解决方案的注释。以后删除额外的注释要比为一个注释稀少的程序写注释容易得多。从现在起,我将在整本书的示例中使用注释来帮助解释代码的各个部分。

标签: #python 输入 数字