简体   繁体   English

不使用循环打印数字范围?

[英]Printing range of numbers without using loop?

Can someone explain to me how this code works?有人可以向我解释这段代码是如何工作的吗? It prints numbers from 0 to 100, but I cannot understand how.它打印从 0 到 100 的数字,但我不明白如何。

print(*range(True,ord("e")))

ord accepts a character and returns the ASCII code. ord接受一个字符并返回 ASCII 码。 In this case "e" returns 101. The *range is unpacking the iterable that range creates.在这种情况下, "e"返回 101。 *range正在解包 range 创建的可迭代对象。 Enabling you to print out the values from True (1) to 101 - 1.使您能够打印出从 True (1) 到 101 - 1 的值。

I found this out by googling each piece of code individually.我通过单独谷歌搜索每段代码发现了这一点。 Type in "ord python" then another search was "star range python".输入“ord python”,然后另一个搜索是“star range python”。 These searches lead to information you are seeking.这些搜索会导致您正在寻找的信息。

print(*range(True,ord("e")))

Firstly, print() means that we are displaying some information on the screen.首先, print()意味着我们正在屏幕上显示一些信息。

Secondly, the * indicates that there may be more than one object to be printed.其次, *表示可能要打印多个object。

Now, think of the True as a 0. True does nothing.现在,将True视为True什么都不做。 The ord("e") means where is e in the Unicode. ord("e")表示e在 Unicode 中的位置。 It returns that number, which is 101. Now, range(start, end) means every value between the start value (0), and end value (1).它返回该数字,即 101。现在, range(start, end)表示起始值 (0) 和结束值 (1) 之间的每个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM