0% found this document useful (0 votes)
7 views47 pages

Section 3 Python-List

Uploaded by

Magy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
7 views47 pages

Section 3 Python-List

Uploaded by

Magy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 47

Python Getting

Started
Python dir
Python help
Python Lists
Python Tuples Agenda
Python Sets
Python Dictionaries
“Python dir”
 Definition and Usage
 The dir() function returns all properties and methods of the specified object, without the
values.
 This function will return all the properties and methods, even built-in properties which are
default for all object.
“Python help”
 The help() function is used to execute the built-in help system.
“Python help”
 The help() function is used to execute the built-in help system.
“Python help”
 The help() function is used to execute the built-in help system.
Python Lists
“Python Arrays”
 There are four collection data types in the Python programming language:
 List is a collection which is ordered and changeable. Allows duplicate members.
 Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
 Set is a collection which is unordered and unindexed. No duplicate members.
 Dictionary is a collection which is unordered, changeable and indexed. No duplicate
members.
 When choosing a collection type, it is useful to understand the properties of that type.
Choosing the right type for a particular data set could mean retention of meaning, and, it
could mean an increase in efficiency or security.
“Python Lists”
• List is a collection which is ordered and changeable. Allows duplicate members.
• 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
• List items are indexed, the first item has index [0], the second item has index [1]
• 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, meaning that we can change, add, and remove items in a list after created.
• Allow Duplicates ,Since lists are indexed, lists can have items with the same value:
“Python Lists”

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

List Items - Data Types List Items can be of any data type:
“Python Lists”
A list can contain different data types:
“Python - Access List Items”
Access Items are indexed and you can access them by referring to the index number:

Negative indexing means start from the end -1 refers to the last item, -2 refers to the second last item.
“Python - Access List Items”
You can specify a range of indexes by specifying where to start and where to end the range.
When specifying a range, the return value will be a new list with the specified items.
Note: The search will start at index 2 (included) and end at index 5 (not included).
Remember that the first item has index 0.
“Python - Access List Items”
By leaving out the start value, the range will start at the first item:

By leaving out the end value, the range will go on to the end of the list:
“Python - Access List Items”
Range of Negative Indexes : Specify negative indexes if you want to start the search from
the end of the list:
“Python - Access List Items”
Check if Item Exists : To determine if a specified item is present in a list use the in keyword:
“ Python - Change List Items”
Change Item Value : To change the value of a specific item, refer to the index number:

Change a Range of Item Values : To change the value of items within a specific range, define a list
with the new values, and refer to the range of index numbers where you want to insert the new
values:
“Python - Insert Items”
To insert a new list item, without replacing any of the existing values, we can
use the insert() method.
The insert() method inserts an item at the specified index:
Note: As a result of the example above, the list will now contain 4 items.
“Python - Add List Items”
Append Items :To add an item to the end of the list, use the append()
method:

Extend List : To append elements from another list to the current list, use the extend() method.
“Python - Remove Item”
Remove Specified Item Remove Specified Index
The remove() method removes the specified item. The pop() method removes the specified index
. If you do not specify the index, the pop() method removes the
last item.

The del keyword also removes the specified index: Clear the List : The clear() method empties the list.
The list still remains, but it has no content.
“Python - Loop Lists”
Using a While Loop
Loop Through a List : You can loop
through the list items by using a
for loop:

Looping Using List Comprehensive


Loop Through the Index Numbers
Sort List Alphanumerically
“Python - Sort Lists”
List objects have a sort() method that will sort the list Sort the list numerically:
alphanumerically, ascending, by default:

Sort Descending Sort the list descending:


To sort descending, use the keyword argument
reverse = True:
“Python - Sort Lists”
Case Insensitive Sort
By default the sort() method is case sensitive, resulting in all So if you want a case-insensitive sort function,
capital letters being sorted after lower case letters: use str.lower as a key function:

Reverse Order
“Python – Copy List”
Copy a List
You cannot copy a list simply by typing list2 = list1, because: list2 will only be a reference to
list1, and changes made in list1 will automatically also be made in list2.
“Python - Copy Items”
Make a copy of a list with the copy() method: Make a copy of a list with the list() method:
“Python - List Methods”
Python Tuples
“Python
A tuple is a collection which is ordered
- Tuples”
Access Tuple Items
and unchangeable. In Python tuples are
You can access tuple items by referring to the
written with round brackets.
index number, inside square brackets:

Negative Indexing Range of Indexes


Negative indexing means beginning from the end, -1
refers to the last item
Change Tuple Values
“Python - Tuples”
Once a tuple is created, you cannot Loop Through a Tuple
change its values. Tuples You can loop through the tuple items
are unchangeable, or immutable as by using a for loop.
it also is called.
But there is a workaround. You can
convert the tuple into a list, change the
list, and convert the list back into a
tuple.

Check if Item Exists


Tuple Length
“Python - Tuples”
To determine how many items a tuple has, use the len() Create Tuple With One Item
method: To create a tuple with only one item, you have to
add a comma after the item, otherwise Python
will not recognize it as a tuple.

Remove Items
Note: You cannot remove items
in a tuple.
“Python - Tuples”
Tuples are unchangeable, so you cannot remove items from it, but you can delete the tuple completely:

Join Two Tuples


To join two or more tuples you can use the + operator:
Python Sets
“Python - Sets”
A set is a collection which is unordered and unindexed. In Python, sets are written
with curly brackets.

Access Items ,You cannot access items in a set by referring to an index or a key. But you can loop through the set
items using a for loop, or ask if a specified value is present in a set, by using the in keyword.
“Python - Sets”
Check if "banana" is present in the set:

Change Items
Once a set is created, you cannot change its items,
but you can add new items.

Add Items Add multiple items to a set, using the update()


To add one item to a set use the add() method. method:
“Python - Sets”
Get the Length of a Set Remove "banana" by using the
discard() method:

Remove Item
To remove an item in a set, use the remove() You can also use the pop(), method to remove an item, but this
Note: If the item to remove does not exist, method will remove the last item. Remember that sets are
remove() will raise an error. unordered, so you will not know what item that gets removed.
“Python - Sets”
The clear() method empties the set: Join Two Set , The union() method returns a
new set with all items from both sets:

The update() method inserts the items in set2 into set1:


The del keyword will delete the set completely:
“Python - Sets Methods”
Python
Dictionaries
“Python - Dictionary”
A dictionary is a collection which is Accessing Items
unordered, changeable and indexed. You can access the items of a dictionary by
In Python dictionaries are written with curly referring to its key name, inside square brackets:
brackets, and they have keys and
values.

There is also a method called get() that will give you the same
result:
“Python - Dictionary”
Change Values
You can change the value of a specific item by Loop Through a Dictionary
referring to its key name:
“Python - Dictionary”
You can also use the values() method to return values of a Check if Key Exists
dictionary:
“Python - Dictionary”
Dictionary Length Removing Items
The pop() method removes the item with the specified key
name:

Adding Items
The popitem() method removes the last inserted item
“Python - Dictionary”
The del keyword removes the item with the specified The clear() method empties the dictionary:
key name:

The del keyword can also delete the dictionary Copy a Dictionary
completely:
Copy a Dictionary
“Python - Dictionary”
You cannot copy a dictionary simply by typing dict2 = dict1, because: dict2 will only be a reference to dict1, and changes
made in dict1 will automatically also be made in dict2.

one way is to use the built-in Dictionary method copy(). Make a copy of a dictionary with the dict() function:
“Python - Dictionary”
Nested Dictionaries
“Python - Dictionary Methods”
Thanks!
Any questions?

You might also like