DEV Community

Cover image for Most Useful Sequences in Python for Beginners👨‍💻
Ujjwal (UG THE SEP)
Ujjwal (UG THE SEP)

Posted on

Most Useful Sequences in Python for Beginners👨‍💻

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?

What is Sequence

  • 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.
 Input: [1,2,3,4,5]
Enter fullscreen mode Exit fullscreen mode

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

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?

  • List is a sequence
  • List stores multiples element in a single variable
list=[1,'UG-SEP','dev',1.2,(7,'Python')]
Enter fullscreen mode Exit fullscreen mode
  • List can contain heterogenous element.
  • List is mutable.

What is list comprehension?

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 ]
Enter fullscreen mode Exit fullscreen mode

we will learn how to take input from user using List Comprehension in forward topic

How to create and assign value in list?

List are created using square brackets( [] ) in the square brackets we write elements separating by commas( , )

list=['Welcome','Dev','Community']
Enter fullscreen mode Exit fullscreen mode

As you can see I writes elements separating by commas.

How to create empty list?

# if the square brackets are empty it will considered as empty list
l1=[]
# list() function used to creates list objects
l2=list()
Enter fullscreen mode Exit fullscreen mode

How to take input from user

# 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()))
Enter fullscreen mode Exit fullscreen mode

Function provided by list class

 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
Enter fullscreen mode Exit fullscreen mode

Syntax:

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)
Enter fullscreen mode Exit fullscreen mode

What is str?

  • str is a sequence of characters which are iterable
  • str is immutable
  • str elements are indexed
s="Welcome to my blog follow me for more post like this."
Enter fullscreen mode Exit fullscreen mode

How to create and take input in str?

# Empty str
s=str()
Enter fullscreen mode Exit fullscreen mode

Ways to write str constant

# by using double quotes "
str1="Dev.to"
# by using single quotes '
str2='Follow me'
# by using """ 
str3="""Love the post"""
Enter fullscreen mode Exit fullscreen mode

How to take input from user in str?

# input() is a function used to take input from user return str
str=input("Enter a string")
Enter fullscreen mode Exit fullscreen mode

Function provided by str class

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
Enter fullscreen mode Exit fullscreen mode

str contain more functions

Syntax

# 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()
Enter fullscreen mode Exit fullscreen mode

What is tuple?

  • tuple is immutable
  • tuple can store heterogenous element
  • tuple elements are separated by ','
t=(1,'Comment','Share',1.5)
Enter fullscreen mode Exit fullscreen mode

The most common doubt 👇

Difference between tuple and list?

Difference between tuple and list

How to create and take input in tuple?

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

How to create empty tuple

# by using tuple() function which create tuple object
t=tuple()
# by leaving the parenthesis empty
t=()
Enter fullscreen mode Exit fullscreen mode

How to take input in tuple from user?

# 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(',')])
Enter fullscreen mode Exit fullscreen mode

You cannot add, modify and delete element in tuple this is a big difference between tuple and list

Functions provided by tuple class

tuple class only provide two functions

t.index(): get the index of particular text
t.count(): count the occurrence of given text
Enter fullscreen mode Exit fullscreen mode

Syntax

# 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)
Enter fullscreen mode Exit fullscreen mode

Build-in Methods for any sequences

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
Enter fullscreen mode Exit fullscreen mode

Syntax

# 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)
Enter fullscreen mode Exit fullscreen mode

Conclusion

This Post will help you to enhance your python skills as a beginner. Don't Forget to Follow me 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:

Oldest comments (3)

Collapse
 
meenagupta5 profile image
Programmer

Thanks a lot @ug 😊
It is very helpful to me you are really awesome.
You stand on as you said I will post on Monday.
Really to much helpful for me as a beginner...

Collapse
 
ug profile image
Ujjwal (UG THE SEP)

My pleasure
Open to help 🙏

Collapse
 
ujjwalindia profile image
Enter Programmer

Unicorn🦄