A
A
Artem Matyashov2016-04-06 12:49:47
Python
Artem Matyashov, 2016-04-06 12:49:47

Which code entry is more correct in terms of Python?

There is a code:

a = 1
while <условие>:
    print (a)
    a += 1

Which notation is more correct and why "while a != 10: " or "while a < 10:"

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Alexandrovich, 2016-04-06
@RoxT

for a in range(1, 10):
    print a

while is generally better not to use for this type of loops

S
Sergey Gornostaev, 2016-04-06
@sergey-gornostaev

a < 10 is closer to "self-documenting" code, and therefore preferable.

A
Alexey Ukolov, 2016-04-06
@alexey-m-ukolov

Which notation is more correct and why "while a != 10: " or "while a < 10:"

These records are semantically unequal. Specifically, in your example, they will work the same way, but if at some point you need to add 2 to a , and not 1, then the record while a != 10will turn into an endless loop. And Dmitry Alexandrovich
has already answered how correctly from the point of view of a pythonist .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question