Wednesday, 1 March 2023

List in Python

 In Python, a list is a collection of items, which can be of any type (such as integers, strings, or even other lists). Lists are created using square brackets [] and commas to separate items. For example, here is a list of integers:

css
my_list = [1, 2, 3, 4, 5]

You can access individual items in a list by their index, which starts at 0. For example, to access the first item in my_list, you would use my_list[0], which would return the value 1. You can also use negative indexing to access items from the end of the list. For example, to access the last item in my_list, you would use my_list[-1], which would return the value 5.

Lists are mutable, which means that you can modify them by adding or removing items. For example, to add an item to the end of my_list, you would use the append method:

go
my_list.append(6)

This would modify my_list to be [1, 2, 3, 4, 5, 6]. You can remove items from a list using the del keyword or the remove method.

Lists are a fundamental data structure in Python, and they are used extensively in Python programs to store and manipulate collections of data.

No comments:

Post a Comment

Program For String

 # This program demonstrates various string operations # Define a string variable my_string = "Hello, World!" # Print the string p...