32

View Count
// Kakaotalk Link
// https://developers.kakao.com/docs/js-reference#kakao_link_senddefault
Kakao.init('df2f57b1f880d511dad31e5443d75a0d');
Kakao.Link.createDefaultButton({
objectType: 'feed',
container: '#kakao-link-btn',
content: {
title: '[Python] range() function for creating iterations, and arrays',
description: 'None',
imageUrl: '',
link: {
webUrl: 'https://webisfree.com/en/2019-08-18/[python]-range()-function-for-creating-iterations-and-arrays',
mobileWebUrl: 'https://webisfree.com/en/2019-08-18/[python]-range()-function-for-creating-iterations-and-arrays'
}
},
buttons: [
{
title: 'Web view',
link: {
webUrl: 'https://webisfree.com/en/2019-08-18/[python]-range()-function-for-creating-iterations-and-arrays',
mobileWebUrl: 'https://webisfree.com/en/2019-08-18/[python]-range()-function-for-creating-iterations-and-arrays'
}
},
{
title: 'App view',
link: {
webUrl: 'https://webisfree.com/en/2019-08-18/[python]-range()-function-for-creating-iterations-and-arrays',
mobileWebUrl: 'https://webisfree.com/en/2019-08-18/[python]-range()-function-for-creating-iterations-and-arrays'
}
}
]
});

View Count

Learn about the range() functions available in Python.

! What is the range() function?

The range() function of Python is a function that returns consecutive numbers. It's very simple to create a sequence. Below is a simple grammar.

range(startNumber, stopNumber, stepSize)

- startNumber : indicates start value, default 0 (optional)

- stopNumber : indicates the last value to be continuous. However, it does not include self (required value)

- stepSize: Determines a unit value of the number of consecutive units (optional)

It's easy to make a sequence of numbers. So let's look at the simple iteration and how it's used in the arrangement with an example.

(adsbygoogle = window.adsbygoogle || []).push({}); # range()See Example of a function repeat statement

In this example, you are only going to use the required stopNumber. As an example of entering range (3) in the code to be executed, the start value will be the default value of 0..

The results are as follows.

The sum of up to 2 is displayed except for yourself 3.

! Example of using start and end values

In the following example, you want to use range() to obtain a sum of 1 to 10.

The output results are as follows.

That is, the sum of 55 from 1 to 10. The reason we used 11 here is because it does not include the second factor, stopNumber. So you have to use 11 to add up to 10.

! See another range() example

This time, set the step value to 5 and try to write an example.

The output results are as follows.

As you can see, we're going to print a series of numbers from 5 to 50, and we're going to see a total of 220.

(adsbygoogle = window.adsbygoogle || []).push({}); # Create an array as a range() function

Let's create a list type, which is an array of Pythons. Below is a simple example of a sequence of array values.

When you print it out, it will appear as follows:

As expected, the array was created.

So here's a quick look at Python's range() function.

! What is the range() function?

The range() function of Python is a function that returns consecutive numbers. It's very simple to create a sequence. Below is a simple grammar.

range(startNumber, stopNumber, stepSize)

- startNumber : indicates start value, default 0 (optional)

- stopNumber : indicates the last value to be continuous. However, it does not include self (required value)

- stepSize: Determines a unit value of the number of consecutive units (optional)

It's easy to make a sequence of numbers. So let's look at the simple iteration and how it's used in the arrangement with an example.

In this example, you are only going to use the required stopNumber. As an example of entering range (3) in the code to be executed, the start value will be the default value of 0..

for i in range(3):

print(i)

print(i)

The results are as follows.

0

1

2

1

2

The sum of up to 2 is displayed except for yourself 3.

! Example of using start and end values

In the following example, you want to use range() to obtain a sum of 1 to 10.

sum = 0

for i in range(1, 11):

print(i)

sum = sum + i

print(sum)

for i in range(1, 11):

print(i)

sum = sum + i

print(sum)

The output results are as follows.

1

2

3

4

5

6

7

8

9

10

55

2

3

4

5

6

7

8

9

10

55

That is, the sum of 55 from 1 to 10. The reason we used 11 here is because it does not include the second factor, stopNumber. So you have to use 11 to add up to 10.

! See another range() example

This time, set the step value to 5 and try to write an example.

sum = 0

for i in range(5, 55, 5):

print(i)

sum = sum + i

print(sum)

for i in range(5, 55, 5):

print(i)

sum = sum + i

print(sum)

The output results are as follows.

5

10

15

20

25

30

35

40

45

50

220

10

15

20

25

30

35

40

45

50

220

As you can see, we're going to print a series of numbers from 5 to 50, and we're going to see a total of 220.

Let's create a list type, which is an array of Pythons. Below is a simple example of a sequence of array values.

myArray = list(range(1, 6, 2))

print (myArray)

print (myArray)

When you print it out, it will appear as follows:

[1, 3, 5]

As expected, the array was created.

So here's a quick look at Python's range() function.

Author ByEnSSo