Most Useful Sequences in Python for Beginners👨‍💻
Tags: python, tutorial, beginners, codenewbie
Published: 2021-08-29 11:45:37

Today in this blog we will learn about the most useful **sequences** that every **Python** programmer should know.
> Here is the agenda.

## Agenda
* [What is Sequence?](#sequence)
* [Most Useful Sequences](#useful_sequences)
* [What is list?](#list)
* [What is list comprehension?](#comprehension)
* [How to create and take input in list?](#list_input)
* [Function provided by list class](#list_function)
* [What is str?](#str)
* [How to create and take input in str?](#str_input)
* [Function provided by str class](#str_function)
* [What is tuple?](#tuple)
* [Difference between tuple and list?](#list_vs_tuple)
* [How to create and take input in tuple?](#tuple_input)
* [Function provided by tuple class](#tuple_function)
* [Build-in Methods for any Sequences](#build-in)
* [Conclusion](#end)

***

## What is *Sequence*?<a name="sequence"></a>
![What is Sequence](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1fypjn46mecvbrr0rl1v.png)

* Sequences are a **generic** term for an **ordered set** which means that the order in which we **input** the items will be the same when we **access** them.
```python
 Input: [1,2,3,4,5]
```
> So here if I traverse using loop then it will print the output in the same order as I input data.

* Sequences are iterable but an iterable **may not** be sequence as **set** and **dict**
* Sequence have concept of indexing.

## Most Useful Sequence<a name="useful_sequences"></a>
There are 3 most useful sequences.
* str
* list
* tuple

> List is the most useful sequence
We will discuss about them one by one. So let start's with **list**

***

## What is **list**?<a name="list"></a>
* List is a sequence 
* List stores multiples element in a single variable
```python
list=[1,'UG-SEP','dev',1.2,(7,'Python')]
```
* List can contain heterogenous element.
* List is mutable.

## What is *list comprehension*?<a name="comprehension"></a>
List Comprehension offer a syntax to compress the code it used to take input in list in just one line. It is a very awesome feature to compress code and easy to understand.
### Syntax
```
List= [ expression(element) for element in old List if condition ]
```
we will learn how to take input from user using **List Comprehension** in forward topic

## How to *create* and *assign* value in list?<a name="list_input"></a>

List are created using square brackets( [] ) in the square brackets we write elements separating by commas( , )
```python
list=['Welcome','Dev','Community']
```
As you can see I writes elements separating by commas.
### How to create empty list?
```python
# if the square brackets are empty it will considered as empty list
l1=[]
# list() function used to creates list objects
l2=list()
```
### How to take input from user
```python
# Using List comprehension
l1=[eval(i) for i in input("Enter data separating by commas").split(',')]

# So in the above line what happen is that first we will enter in
# list comprehension and take input from user after that it will 
# be split each element using commas as the criteria the split() 
# function split() string on a given criteria and I pass , as the 
# criteria so no the each element separated by , are treated as 
# list element and now the for run and we evaluate the element and 
# assign it.

# using for loop
# create a empty list
l2=list()
# take the size of list
for i in range(0,int(input("Enter the size"))):
# append is a function to add data at the last of the list we will 
# learn about it 
     l2.append(eval(input()))
```
## Function provided by list class<a name="list_function"></a>
```
 append() : used to append element at the end of the list
 clear()  : to remove all element from the list
 copy()   : return shallow copy of the list
 count()  : to the number of occurrence of a particular element
 index()  : to get the index of a particular element
 insert() : to insert element at a particular index
 pop()    : to pop a particular element by index no. remove the 
             last element if index not provided
 sort()   : to sort the element in an order
 reverse(): to reverse the list
 remove() : remove the particular element
```
Syntax:
```python
l=[1,2,3]
# append 4 at the end so l=[1,2,3,4]
l.append(4)
# clear all element l=[]
l.clear()
# shallow copy of l shlcpy=[]
shlcpy=l.copy()
# count the occurrence of 1 but before that let add some value in l
l=[1,2,2,3,1,3,5,7]
print(l.count(1))
# index of a particular element prints the first occurrent index 0
print(l.index(1))
# insert at the 2 and 4th index l=[1,2,2,3,2,1,3,5,7]
l.insert(4,2)
# pop 0 index value l=[2,2,3,2,1,3,5,7]
l.pop(0)
# sort the list l=[1,2,2,2,3,3,5,7]
l.sort()
# reverse the list l=[7,5,3,3,2,2,2,1]
l.reverse()
# sort in descending to ascending
l.sort(reverse=True)
# remove particular element 7 l=[5,3,3,2,2,2,1]
l.remove(1)
```
***

## What is str?<a name="str"></a>
* str is a sequence of characters which are iterable
* str is immutable
* str elements are indexed

```python
s="Welcome to my blog follow me for more post like this."
``` 

## How to create and take input in str?<a name="str_input"></a>
```python
# Empty str
s=str()
```
### Ways to write str constant

```python
# by using double quotes "
str1="Dev.to"
# by using single quotes '
str2='Follow me'
# by using """ 
str3="""Love the post"""
```
### How to take input from user in str?
```python
# input() is a function used to take input from user return str
str=input("Enter a string")
```
## Function provided by str class<a name="str_function"></a>
```
s.replace()   : replace text by another text
s.index()     : get index of a particular text
s.count()     : count the occurrence of a given text
s.split()     : split str on the basis of given criteria returns 
                list
s.join()      : join each sequence elements separated by the given 
                criteria
s.startswith(): check whether the string start with the given text
s.endwith()   : check whether the string end with the given text
s.find()      : find some given text in the string
s.upper()     : convert the string to uppercase
s.lower()     : convert the string to lowercase
s.strip()     : remove extra white space from the left and right 
                of the string
```
> str contain more functions

### Syntax
```Python
# Let first create a str
s='text'
# let replace 'tex' by 'res' gives 'rest' does not changes in s 
# return str object which i have again stored in s so it become 
# rest
s=s.replace('tex','res')
# find the index of s which is 2
s.find('s')
# count the occurrence of 'st' which is 1
s.count('st')
# split string on the basis of '' empty space means each char will 
# become element of the list l=['r','e','s','t']
l=s.split('')
# join l on the separating by ',' s="r,e,s,t"
s=','.join(l)
# check whether s starts with 'r,e,s' or not True
s.startswith('r,e,s')
# check whether s ends with 's,t' or not True
s.endswith('s,t')
# remove extra white space of '  My  ' using strip s='My'
s='  My  '.strip() 
# lowercase the string
s.lowercase()
# uppercase the string
s.uppercase()
```
***
## What is tuple?<a name="tuple"></a>
* tuple is immutable
* tuple can store heterogenous element
* tuple elements are separated by ','
```python
t=(1,'Comment','Share',1.5)
``` 
> The most common doubt 👇

## Difference between tuple and list?<a name="list_vs_tuple"></a>
![Difference between tuple and list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17orp2eoa9akamk022q7.jpg)

## How to create and take input in tuple?<a name="tuple_input"></a>

Tuple is created using Parenthesis '()' inside it we write element separated by ','

### How to create empty tuple
```python
# by using tuple() function which create tuple object
t=tuple()
# by leaving the parenthesis empty
t=()
```
### How to take input in tuple from user?
```python
# using List comprehension we convert the list into tuple using 
# tuple() function let check how
t=tuple([eval(i) for i in input("Enter data separated by commas ").split(',')])
```
> You cannot add, modify and delete element in tuple this is a big difference between tuple and list
 
## Functions provided by tuple class<a name="tuple_function"></a>

tuple class only provide two functions
```python
t.index(): get the index of particular text
t.count(): count the occurrence of given text
``` 
### Syntax
```python
# create a tuple
t=(1,2,3,4,'blogging',1)
# get the index of 3 in t which is 2
t.index(3)
# count the occurrence of 1 in t which is 2
t.count(1)
```
***
## Build-in Methods for any sequences<a name="build-in"></a>
```
len(): find the length of a given sequence
min(): finds the min value of the given sequence
max(): finds the min value of the given sequence
sum(): return the sum of the element of given sequence
sorted(): return list after sorting the given sequence
```
### Syntax
```python
# create a sequence
l=[2,5,1,4,3]
# find the len of l which is 5
len(l)
# find the max element which is 5
max(l)
# find the min element which is 1
min(l)
# find the sum which is 15
sum(l)
# sort l but no changes in l return list which is [1,2,3,4,5]
sorted(l)
```
***
## Conclusion<a name="end"></a>

This Post will help you to enhance your python skills as a beginner. Don't Forget to [Follow me](https://dev.to/ug) and Give a **Unicorn** if this post is informative and useful for you.
If you have any doubt then comment below.
This post is created on the request of @meenagupta5 
Hope you love it💖

## Read more:
{% link https://dev.to/ug/5-project-to-master-in-python-for-beginners-4blm %}
{% link https://dev.to/ug/how-to-create-multiple-pr-in-github-443m
%}