Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Python List

list of python for pt3 grade 9

Uploaded by

sonution5
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Python List

list of python for pt3 grade 9

Uploaded by

sonution5
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

THE INDIAN INTERNATIONAL SCHOOL, DSO

PYTHON LIST

Lists are used to store multiple items in a single variable.

Lists are one of 4 built-in data types in Python used to store collections of data,
the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

Lists are created using square brackets:

mylist = ["apple", "banana", "cherry"]

eg:

a = ["apple", "banana", "cherry"]

print(a)

List Items

List items are ordered, changeable, and allow duplicate values.

List items are indexed, the first item has index [0], the second item has
index [1] etc.

Ordered

When we say that lists are ordered, it means that the items have a defined order,
and that order will not change.

If you add new items to a list, the new items will be placed at the end of the list.

Changeable

The list is changeable, meaning that we can change, add, and remove items in a
list after it has been created.

Allow Duplicates

Since lists are indexed, lists can have items with the same value:

Eg:

a = ["apple", "banana", "cherry", "apple", "cherry"]

print(a)
List Length

To determine how many items a list has, use the len() function:

a = ["apple", "banana", "cherry"]


print(len(a))
List items can be of any data type:
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
A list can contain different data types:
list1 = ["abc", 34, True, 40, "male"]
type()

From Python's perspective, lists are defined as objects with the data type 'list':

<class 'list'>
mylist = ["apple", "banana", "cherry"]
print(type(mylist))
#Accessing elements from list
Positive indexing
a=[21,22,"welcome","cat",30.5]
print(a[0])
Negative indexing
a=[21,22,"welcome","cat",30.5]
print(a[-1])
Range with Slicing a list
a = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(a[2:5])
b= ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(b[:4])
c = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(c[2:])
d = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(d[-4:-1])
a=[21,22,"welcome","cat",30.5]
print(a[2:4])
Changing the value of items in a list
a=[21,22,"welcome","cat",30.5]
print(a[2])
a[3]=1000
a[-3]="hello"
print(a[3])
print(a)
Add List Items
Append Items
To add an item to the end of the list, use the append() method:

Insert Items
To insert a list item at a specified index, use the insert() method.
The insert() method inserts an item at the specified index:

Extend List
To append elements from another list to the current list, use
the extend() method.

Remove List Items


Remove Specified Item
The remove() method removes the specified item.
Remove Specified Index
The pop() method removes the specified index.

The del keyword also removes the specified index:

The del keyword can also delete the list completely.

Clear the List


The clear() method empties the list.
The list still remains, but it has no content.

Sort Lists
Sort List Alphanumerically
List objects have a sort() method that will sort the list alphanumerically,
ascending, by default:

Sort Descending
To sort descending, use the keyword argument reverse = True:
Join Lists
There are several ways to join, or concatenate, two or more lists in Python.
One of the easiest ways are by using the + operator.

Another way to join two lists is by appending all the items from list2 into list1,
one by one:

Method Description

append() Adds an element at the end of the list

clear() Removes all the elements from the list

copy() Returns a copy of the list

count() Returns the number of elements with the specified value

extend() Add the elements of a list (or any iterable), to the end of the
current list

index() Returns the index of the first element with the specified value

insert() Adds an element at the specified position


pop() Removes the element at the specified position

remove() Removes the item with the specified value

reverse() Reverses the order of the list

sort() Sorts the list

You might also like