Lists and Loops

Lists and For Loops


In this chapter, we will learn a new datatype called lists. List contains a group of numbers or strings.

a = ['1','2','3','4','5'] print(type(a))

Here 'a' is a list datatype.

Now, we will find the sum the list and length of the list.

a = ['1','2','3','4','5'] print(sum(a)) print(len(a))

This throws an error because the elements of the list are not integers rather they are of string datatype. So we need to define the list in a proper way.


a = [1, 2, 3, 4, 5] print(sum(a)) print(len(a))

Now, let's find the mean of the list.

a = [1, 2, 3, 4, 5] print("Mean value: %0.2f" %(sum(a)/len(a)))