Python for Moon

Asked 2 years ago, Updated 2 years ago, 19 views

n=list(input())

n.sort(reverse=True)

sum=0
for i in n:
    sum+=int(i)

Here

Do these two mean different things?

python

2022-09-20 10:49

1 Answers

Hello.

range is actually a function that creates a list of consecutive numbers. Even if it is the same in , there is a big difference depending on whether you use range or put the list directly in the back.

There is a list as follows:

list = ["Ga", "Me", "Da", "La"]

The list contains four string data.

If you want to print out all the elements in the list using range, you will need to write the following code.

for index in range(list):
    print(list[index])

From the above code, range returns 0 to the length of the list in sequence. In other words, it returns the index of each procedure.

This time, we will print out all the elements without using range.

for element in list:
    print(element)

This allows you to import elements directly without having to access the list through the index.

In fact, because range(list) returns [0, 1, 2, 3], In the end, you can see that the two methods have the same principle of action. Just refer to the values you get from each iteration directly from the list The difference between the numbers from range to refer to.


2022-09-20 10:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.