L4 - LIST AND TUPLES
Tags: beginners, programming, tutorial, python
Published: 2022-04-28 13:46:28




# LISTS

***Lists are mutable datatype in python.***
Python lists are containers to store values of any datatype.

# LIST SLICING 
## How to get character of a given index in a list in python

![1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649435156190/4ZUg_xMdN.png)

## How to slice a given list.
**SYNTAX - ** name_of_variable[start : end : stop]

![2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649493197691/bJv50-D34.png)

NOTES -

If nothing is written in place of start , then the function will start slicing from the index = 0

If nothing is written in place of end , then the function will do slicing till the end of the string.

If nothing is written in place of step , then the function will print every character.

**NOTE -> **   **We can access the characters of a list and also change them using this function . But we cannot do this in strings because strings are immutable datatype in python but lists are mutable datatype in python.**


![3.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649493704305/WXj2tq3jj.png)

# CONCATENATION OF LISTS 

![4.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649493976537/3DD1a5fC0.png)

# LIST METHODS
## name_of_list.sort() 
It sorts the values of the list in ascending order. 

![5.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1650300037095/_abTXYZYm.png)

## name_of_list.reverse()
It reverses the order of elements in the given list.

![6.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1650300300174/FvMoMXiCj.png)

## name_of_list.append(x) 
Adds the given item at the end of the list.

![7.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1650305298767/8fmMDEyCj.png)

## name_of_list.insert(x,y)
It adds y at index x .


![8!.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1650306620237/3TevSoTky.png)

## name_of_list.pop(x)
It deletes the element at index = x and returns it's value.

![9.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1650306396930/20Q-s95Yt.png)

## name_of_list.remove(x)
It will remove the element x from the list.

![10.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1650346790717/nZqB2BEA8.png)

## ALL LIST METHODS CHANGE THE ORIGINAL STRING THEY DO NOT RETURN NEW VALUES. 
You can see this in the example below

![11.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1650348328951/Q1-L0EBUk.png)

# TUPLES 
*TUPLES ARE IMMUTABLE DATATYPE* IN PYTHON.
Tuples are just like lists but the only difference is that tuples are immutable but lists are mutable datatype in python . 

# HOW TO CREATE A TUPLE

![12.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1650910481433/g4rR38UCC.png)

# TUPLE METHODS 
## name_of_tuple.count(x)
It counts no of times x occurs in the given tuple.

![13.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1650910835090/pOV5ZSOLp.png)

## name_of_tuple.index(x) 
It will return the index of first occurence of x in the given string.

![14.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1650911021319/JKr30lPYs.png)

***We can access and slice the elements of a tuple the same way we do for strings and lists***