Friday, August 14, 2026
Home General Creating List in Python

Creating List in Python

Lists are probably the most versatile data structures in Python. A list can be defined by writing a list of comma separated values in square brackets. Lists might contain items of different types. Python lists are mutable – individual elements of a list can be changed while the identity does not change.

Country =['NEPAL','INDIA','USA','GERMANY','UK','AUSTRALIA']

Temperature =[22, 44, 28, 20, 18, 25, 45, 67]

We just created two lists, one for Country names (strings) and another one for Temperature data (whole numbers).

Accessing individual elements of a list

  • Individual elements of a list can be accessed by writing an index number in square bracket. The first index of a list starts with 0 (zero) not 1. For example, Country[0] can be used to access the first element, ‘INDIA’
  • A range of elements can be accessed by using start index and end index but it does not return the value of the end index. For example, Temperature[1:4] returns three elements, the second through fourth elements [28, 20, 18], but not the fifth element

Tasks

# Create a list of squared numbers
squares_list = [0, 1, 4, 9, 16, 25]

# Now write a line of code to create a list of the first five odd numbers and store it in a variable odd_numbers
odd_numbers= [1, 3, 5, 7, 9]

# Print the first element of squares_list
print (squares_list[0])

# Print the second to fourth elements of squares_list
print(squares_list[1:4])

Practice it in DataCamp here

datasagarhttp://www.DataSagar.com
Sagar is multidisciplinary technologist, educator, and entrepreneur based in Nepal. As the founder of Illionso Technologies and Kashi Garden Resort, he operates at the intersection of web architecture, data intelligence, innovation, and digital transformation. From building digital solutions to scaling real-world concepts, his mission is to share knowledge alongside merge emerging as well as disruptive technologies with transformative offline experiences.
RELATED ARTICLES

Why Linux Won the Infrastructure War? Lessons from Linus Torvalds

In 1991, 21-year-old Linus Torvalds announced a "hobby" operating system. Today, Linux powers supercomputers, cloud servers, and Android. Explore his journey, the open-source economy, and the lessons for modern developers.

What Is Vibe Coding? A Complete Beginner’s Guide to Building Software and Web Pages With AI

A few years ago, building a website meant sitting down with a programming book, picking a language, and slowly working through syntax...

What is Answer Engine Optimization (AEO)? Getting Ready for AI Powered Search and Agentic Era Digital Marketing

Search is changing faster than it has in the last two decades. For years, businesses focused on ranking their websites on search...

Most Popular

Why Linux Won the Infrastructure War? Lessons from Linus Torvalds

In 1991, 21-year-old Linus Torvalds announced a "hobby" operating system. Today, Linux powers supercomputers, cloud servers, and Android. Explore his journey, the open-source economy, and the lessons for modern developers.

What Is Vibe Coding? A Complete Beginner’s Guide to Building Software and Web Pages With AI

A few years ago, building a website meant sitting down with a programming book, picking a language, and slowly working through syntax...

What is Answer Engine Optimization (AEO)? Getting Ready for AI Powered Search and Agentic Era Digital Marketing

Search is changing faster than it has in the last two decades. For years, businesses focused on ranking their websites on search...

The Plain Text Web: Why llms.txt Is Becoming the New Site Standard

The web built today is heavy. Between client-side JavaScript, cookie banners, tracker scripts, and responsive CSS grids, a typical webpage carries megabytes...