
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Correct Syntax to Create Python Lists
The correct syntax to create a Python List is using the square brackets. Creating a list is as simple as putting different comma-separated values between square brackets. Through this, using Lists you can store multiple items. A list can have integer, string or float elements. With that, we can also create a List with mixed datatypes.
The list can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type
Create a Python List with Integer elements
We will create a list with 10 integer elements and display it. The elements are enclosed by square brackets. With that, we have also displayed the length of the list and how we can access specific elements using the square brackets ?
Example
# Create a list with integer elements mylist = [25, 40, 55, 60, 75, 90, 105, 130, 155, 180]; # Display the list print("List = ",mylist) # Display the length of the list print("Length of the List = ",len(mylist)) # Fetch 1st element print("1st element = ",mylist[0]) # Fetch last element print("Last element = ",mylist[-1])
Output
List = [25, 40, 55, 60, 75, 90, 105, 130, 155, 180] Length of the List = 10 1st element = 25 Last element = 180
Create a Python List with String elements
We can also add string elements to a Python List. We will create a list with 5 string elements and display it. The elements are enclosed by square brackets. With that, we have also displayed the length of the list and how we can access the first and last element using the square brackets ?
Example
# Create a list with string elements mylist = ["BMW","Audi","Tesla","Honda","Toyota"]; # Display the list print("List = ",mylist) # Display the length of the list print("Length of the List = ",len(mylist)) # Fetch 1st element print("1st element = ",mylist[0]) # Fetch last element print("Last element = ",mylist[-1])
Output
List = ['BMW', 'Audi', 'Tesla', 'Honda', 'Toyota'] Length of the List = 5 1st element = BMW Last element = Toyota
Create Python List with different datatypes
We will create a list with mixed datatypes i.e. integer, string, and float type. All the elements are enclosed in a single square bracket to form a list ?
Example
# Create a list with mixed datatype elements mylist = [55, 2,7, 3.9, "BMW", "Toyota"] # Display the list print("List = ",mylist) # Display the length of the list print("Length of the List = ",len(mylist)) # Fetch 1st element print("1st element = ",mylist[0]) # Fetch last element print("Last element = ",mylist[-1])
Output
List = [55, 2, 7, 3.9, 'BMW', 'Toyota'] Length of the List = 6 1st element = 55 Last element = Toyota
Above, we have set mixed datatype elements in a list. Through this, you can also create a list with different datatypes are enclose them in a square bracket.