Introduction
In this session, we are going to discuss how to create a list in Python. The following link will help you to understand the basics and environment setup of Python.
Assign List in the Variable name
Now, we are going to discuss how to assign a list of values to a variable.
CODE
- >>> myList = []
- >>> myList
- [ ]
- >>> myList = ["PHP", "Python", "Java", "Perl"]
- >>> myList
- ['PHP', 'Python', 'Java', 'Perl']
- >>>

In the above screen, we have created the variable named “myList”. Initially, we have not assigned any value to the List. We declared the myList = [] and hit enter.
Then, you can see that an empty array has been created for myList variable on Shell.
Get specific value
If you want to get a specific value from the list, you can use the following Syntax on Python Shell.
Syntax
Variable_name[array_index]
Let’s discuss this with a live example.
CODE
- >>> myList[1]
- 'Python'
- >>> myList[2]
- 'Java'
- >>>

In the above screen, you can see that we have got a specific value from the list.
Add a new value to the List
If you want to add a new record into the List, you should use the append keyword. Find the below screenshot for reference.

When you assign a new record, check it in the variable name on Shell.
CODE
- >>> myList
- ['PHP', 'Python', 'Java', 'Perl']
- >>> myList.append("New Language")
- >>> myList
- ['PHP', 'Python', 'Java', 'Perl', 'New Language']
- >>>






Kartik PawarPosted Jun 14, 2018, 4:10 AM
Nice article... Keep sharing...