Creating List in python class-11 notes

Creating list in python class-11 provides you detailed study material for CBSE Computer science.

Introduction to Creating list in python class-11

As you know list python supports different types of data types like numbers, strings and boolean values.

These values you can store in different variables. But sometimes we need a list of similar or different types of data in single unit. Or we need something which prepare a set of similar or different values.

For example, Let’s assume a student as object. Now a student can have rollno, name, class, date of birth etc. These details we want to use in program all should come in single unit. So here list will helpful to serve this purpose.

Definition of List

A list a type of a python collection that can store and manipulate similar as well as different types data values.

Those who are familiar with other programming languages may familiar with Arrays. Python lists are quite similar to that but only one difference is there that array is collection of homogeneous (similar kind) data where python list is collection of heterogeneous (different type) data.

Creating a list in python

To create a list square brackets are used. It can be represented in following forms:

  1. Empty List: [] –> Empty lists can be created when you need add values later or size is not fixed
  2. List of numeric values: [11,22,33,44,55] –> This list contains numbers as elements.
  3. List of mixed numbers: [11,22.22,33.33,44] –> This list is a collection of integers and float numbers.
  4. List of letters: [‘x’,’y’,’z’] –> This list is collection of letters or alphabets
  5. List of words: [‘Hello’,’How’,’are’,’you’] –> This list is collection few words
  6. List of mix different data types: [‘00001′,’Sagar’,10001,’12/12/2005′] –> This list is collection of a student record with different data values

Creating a list in python – An Empty List

To create an empty list declare one list and initialize with square brackets without a any value.

l = []

In above statement, I have created an empty list named as l. Now whenever I want to access elements from this list I will use l and then manipulate the list with different expressions.

Creating lists with multiple values

To create lists with multiple values initialize your list with values you want to put in the list.

l = [22,33.33,45,’17/08/2001′,’Hetal’,’Parmar’]

The list values can be accessed by its index.

IndexValue
0 – l[0]22
1 – l[1]33.33
2 – l[2]45
3 – l[3]17=/08/2001
4 – l[4]Hetal
5 – l[5]Parmar

Now observe this code:

Creating nested list

A list can be an element of another list. This type of list is known as a nested list.

l = [1,’Manish’,[56,67,89]]

The values can be access in following:

l[0]=1

l[1]=Manish

l[2][0]=56

l[2][1]=67

l[2][2]=89

Creating list from a word or text

The text initialized with list function returned as a list.

l = list(‘Python Lists’)

print(l)

Output –> [‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’, ‘ ‘, ‘L’, ‘i’, ‘s’, ‘t’, ‘s’]

Creating list from user input

Use input function to create a list in following manner.

l = list(input(“Enter a value”))

print(l)

Output –>

Enter values:4567
[‘4’, ‘5’, ‘6’, ‘7’]

Observe the output given above, when you take values from input it always returns the numbers as string with single quotes in a list. To avoid this you can use this.

To do this use eval function and enter values with square brackets:

l = eval(input(“Enter values”))

print(l)

Output–>

Enter values: [4,5,6,7]

[4,5,6,7]

Whenever eval function is used with input you can enter the values in form of list it self at the input console.

Comment Your Views

%d bloggers like this: