[Python Beginner][Part3 a] Lists.


The basic idea is to introduce you to the building blocks of all python code you will write in the future.

Now, what are lists exactly?

List are basically variables that can store multiple values in them at once. The second question becomes. What good does that do?

Instead of working with multiple variables, we use lists to achieve the same thing. This is usually the case with related variables.

It becomes a lot clearer with examples.

First, how to create a blank list:

# initialize an empty list
var = []
#or var = list()

list() is a keyword function.

In python, square brackets designate a list.

Now to create a list and access each element individually.

students = ['Ram', 'Shyam', 'Vicky']

for i in student:

Basic for loop in python, we use indentation to specify the elements inside the loop.

Now, since lists contain multiple elements, the naming is usually done as plurals.

We can also access elements in the list as individual variables. Remember, the index of a list starts from 0. So, 0(zero) is the first element of any list.

student = ['Ram', 'Shyam', 'Vicky']

print(student[0]) #Ram
print(student[1]) #Shyam
print(student[2]) #Vicky

Now, remember the article on strings, we can do some tricks with the elements

student = ['Ram', 'Shyam', 'Vicky']

print(student[0].title()) #Ram
print(student[1].lower()) #shyam
print(student[2].upper()) #VICKY

Now, remember the very first list we did, we can access all elements inside the list using a ‘for loop’.

Now when I said, the elements of the for loop are specified by indentation

students = ['Ram', 'Shyam', 'Vicky']

for i in students:
print(i, 'has passed the exam')
print('Congratulations', i, '!')

print('This print statement is outside the loop')

That is all for now, we shall continue on this next article.

If you wish to contact me, the LinkedIn account and Facebook group links are here.

Image for post

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store