Hi Guys,
Today's target is List in Python which is the synonym for arrays. Lists are incredibly useful tools to store large data and access it individually. Let's see how list works in Python. Here we go :
# Program to show importance of lists
# Lists in python are similar to arrays in other programming languages
# Unlike variables, list needs to defined. You can either initialize them or add data into them by appending.
# initialization of a list
x=[2,3,4,5,'2','crow', 2.2, 0]
# List elements must be separated by comma. List can be mixture of int,float,string. String needs to put ' '
print("Initialization results")
print(x[0], x[4], x[5], x[6])
# Additional elements can also be added in the list using append
x.append(0.2)
# Also existing can be changed
x[7] = x[7]*2
print(x)
# defining empty list and then adding elements later in the program
y=[]
#using for loop to add data into list
print("Appending results")
for i in range (1,10):
y.append(i) # adding value of i into the list. Note that max value of will be 9
print(y)
#watch the indentation. Printing list outside the loop, after all the elements have been added.
Output:
No comments:
Post a Comment