Range function in Python:¶
Range function in python is a list. It creates a list from the given input range.
In [1]:
for i in range(4):
print(i)
In [2]:
for i in [0,1,2,3]:
print(i)
Creating a list using range function.¶
Let's create a list with values from 1 to 100.
In [3]:
list_100 = list(range(1,101))
print(list_100)
Range object :¶
syntax: range(start,stop,[step] . step is a jump value here. For example, let's print all the even number between 0 and 100
In [5]:
print(list(range(0,100,2)))
Iterating list elements:¶
In [8]:
cities = ["chennai","delhi","mumbai","bangalore","hyderabad"]
for i in range(len(cities)):
print(cities[i])
print("====another way:===")
for city in cities:
print(city)
Multiple assignments:¶
In [2]:
cat = ["Mikki","white","small"]
name = cat[0]
color = cat[1]
size = cat[2]
print(name,color,size)
In [3]:
#Multiple assignment way:
name,color,size = cat
print(name,color,size)
Swapping two variables:¶
In [4]:
a = 10
b = 20
print("before swap:",a,b)
a,b = b,a
print("after swap ",a,b)
In [ ]:
No comments :
Post a Comment