Wednesday, March 20, 2024

Python Tutorial - Range - 008

Fungsi range adalah hal umum dipakai dalam bahasa Python. Dipakai untuk menghasikan deretan angka berdasarkan parameter khusus. 


Image - Ruko di kota kecil Amerika

range(start, stop, step)
start, stop, step .....> disebut parameter

Parameter start .....> starting value .....> default value is 0 (zero)
Parameter stop .....> upper limit
Parameter step .....> interval between numbers ....> default value is 1

# range(0,5)
Note: To make the code range(0,5) work, you can convert it to a list. In Python, the range() function generates a sequence of numbers, but it doesn’t create a list directly. To create a list containing the numbers from 0 to 4, you can use the list() function to convert the range to a list:

>>> my_list = list(range(0, 5))
>>> print(my_list)
[0, 1, 2, 3, 4]
>>> 


# range(1,6)
my_list = list(range(0,6))
print(my_list)
output : [0, 1, 2, 3, 4, 5]

# range(6, 0, -1)
result = list(range(6, 0, -1))
print(result)
output: [6, 5, 4, 3, 2, 1]

# range(0, -10, -2)
badut = list(range(0, -10, -2))
print(badut)
Output: no output


## Posting sebelumnya:
JJJJJJJJJJJJ

No comments:

Post a Comment