Python for Moon

Asked 2 years ago, Updated 2 years ago, 13 views

When you want to print out bc three times

Number 1

for bc in range(0,3)
    print('bc')

In this way, it is described in the book the same way as the bc in the for door and the bc in the print.

Number 2

for a in range(0,3)
    print('bc')

Value is

bc
bc
bc

I learned the practice like 2 times, but I was confused because the value of a and bc are different in the for statement So I want to know why number 2 is possible. Just the role of a is the number of times, and print is bc as many times as a ?

python

2022-09-22 12:32

3 Answers

Oh, I understand.

for a in range(0,3):
    pass

The values of 0, 1, and 2 are sequentially added to the value of a.

The pass syntax is executed 3 times in the repeat statement.

So the phrase print ('bc') is executed three times by a repetitive statement.

And the values of the variables a and bc in Example 1 and Example 2 are the same.

It's just that the variable names are different.

You can find the answer by checking the code below with the code executor.

=-=-=-=-==-=-=-=-==-=-=-=-==-=-=-=-==-=-=-=-=

Example 1

for a in range(0,3):
    print("The value of variable a is :" + str(a))

Example 2

for bc in range(0,3):
    print("The value of variable a is :" + str(bc))

Hope it's resolved :)


2022-09-22 12:32

It's hard to understand the question.Are you talking about using a and bc at once?

for a in range(0,3):
    print('{} bc'.format(a))

When programming, you often put the value of the variable in the middle of the string and output it. In Python, write {} in the middle of the string, followed by .format {}Use the method of putting variables into place one by one.

It depends on the language. For example, in Ruby, you can write #{} and write variables between brackets.

(0..3).each do |a|
    print("#{a} db")
end


2022-09-22 12:32

for a in range(0,3):
    print(str(a) + 'bc')

I don't really understand the questionOnly

By the range syntax, the value of a is entered in 0,1,2 sequentially.

It is not related to the bc in the print syntax.

So it doesn't have to be matched.

If you want to print it out only when it matches,

bc = 0
for a in range(0,3):
    if a == bc:
        print("a" and bc" match.")

Wouldn't this work?

I hope you can apply it.


2022-09-22 12:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.