Read more
Introduction to List
Programming in Python
In Python, programs can be written in two ways namely Interactive mode and Script mode. The Interactive mode allows us to write codes in Python command prompt (>>>) whereas in script mode programs can be written and stored as separate file with the extension .py and executed. Script mode is used to create and edit python source file
Create a List in Python
In python, a list is simply created by using square bracket. Th e elements of list should be specifi ed within square brackets. Th e following syntax explains the creation of list.
Accessing elements using for loop
In Python, the for loop is used to access all the elements in a list one by one. This is just like the for keyword in other programming language such as C++.
Syntax:
for index_var in list:
print (index_var)
Example:
Marks=[23, 45, 67, 78, 98]
for x in Marks:
print( x )
Output
23
45
67
78
98
In the above example, Marks list has 5 elements; each element is indexed from 0 to 4. The Python reads the for loop and print statements like English: “For (every) element (represented as x) in (the list of) Marks and print (the values of the) elements”
0 Reviews