Lecture 7
Sequence type.
-
List
-
Tuple
-
List is a collection which is ordered and changeable. Allows duplicate members.list is mutable.
-
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.tuple is immutable.
LIST
A list is an object that contains multiple data items. Lists are mutable, which means that their contents can be changed.Lists are dynamic data structures.item can be added or removed from the list.use square brackets to create a list or use the
list()function.
Create list in Python
The list can be created using either the list constructor or using square brackets [ ]. Usinglist() constructor: In general, the constructor of a class has its class name. Similarly, Create a list by passing the comma-separated values inside the list().
Using square bracket ([]): In this method, we can create a list simply by enclosing the items inside the square brackets.
Example
LIST WITH OPERATIONS
Repeatition operators can use copy the list.
list * n(n is integer)orlist + list
can use to concatenate the list.
Example
You can iterate through the list using for loop.

you can use functionmax()to find the maximum value in the list. or fucntionmin()to find the minimum value in the list.

-
use functionsum()to find the sum of the list.use function
len()to find the length of the list. -

Example Program

-
LIST METHODS BUILT-IN FUNCTIONS
- IndexError exception is raised if the index is out of range.
- The index of last elements for loop we can use len()-1 function.

-
Use the operator in to check whether an item is in the list.return True or False.
-
Use the operator not in to check whether an item is not in the list.return True or False.

-
Use the function append() to add an item to the end of the list.
-
Use the function index() determine where an item is located in a list.
-
Use the function insert() to add an item at a specific location in the list.
-
Use the function sort() to sort the items in the list.
-
Use the function remove() to remove an item from the list.
-
Use the function reverse() to reverse the order of the items in the list.
LET BUILD PROGRAM HERO!
1.Display Heroes
2.Add Heroes
3.Insert Heroes
4.Remove Heroes
5.Display Sorted Heroes
6.Clear list
7.Add many name
Main Menu
TUPLES
-
- A tuple is a collection which is ordered and unchangeable.
- In Python tuples are written with round brackets.
- Tuples are used to store multiple items in a single variable.
- Tuples are immutable, which means that we cannot change the elements of a tuple once it is assigned.
- Tuples are faster than lists.
- If you have data that doesn't change, implementing it as tuple will guarantee that it remains write-protected.
-
Tuples do not support the method:
append(), insert()
remove(), pop()
sort(), reverse()
clear()
create empty tuple shoud write a comma behind value like this
my_tuple = (1,)Packing and Unpacking
A tuple can also be created without using a tuple() constructor or enclosing the items inside the parentheses. It is called the variable “Packing.”
In Python, we can create a tuple by packing a group of variables. Packing can be used when we want to collect multiple values in a single variable. Generally, this operation is referred to as tuple packing.
-
MATHPLOT
Install Mathplot library
run this command in terminalpip install matplotlib
Example
Bar
Bar function has a color
color code |
color |
|---|---|
| 'b' | blue |
| 'g' | green |
| 'r' | red |
| 'c' | cyan |
| 'm' | magenta |
| 'y' | yellow |
| 'k' | black |
| 'w' | white |
Last, Pie
Finish !!
Thanks ! Goodluck
If you have any questions, please feel free to contact me.

