Image

Anyone Can Code.





Image


Sequence



Jyotirmay Zamre, Sammarth Kumar



A sequence is a collection of values inside a storage unit. A variable can store a sequence of values in three forms:

  1. string
  2. list
  3. tuple

In this tutorial we will be briefly touch upon these data types, but we will explore them further in tutorial 6.

String

In Python, strings can be any assortment of alpha-numeric characters or symbols surrounded by single quotation marks, double quotation marks, or for a multi line string, triple quotation marks. The string itself can be an entire sentence or just a single word/number/special character.

To assign a variable with a string value, you use a = operator and then enclose the string within single, double or triple quotes.


    x = 'hello'
    y = '''hel
        lo'''
    print(x)
    print(y)
                      
hello hel lo

Strings are immutable which means that thier values cannot be overwritten. You can, however, add values to it at the end.

Two strings can be combined, or concatenated, using the + operator. However, strings cannot be combined with numbers. Python will give an error if you try to combine strings and numbers.


    a = 'Hello'
    b = '5'
    c = a + b
    print(c)
                      
Hello5

String slicing is also an important part of strings but it will be covered in a different tutorial along with an in-depth look at dictionaries, lists and tuples.

Lists

A list is a collection of data which is ordered and mutable (its contents can be overwritten). It allows duplicate members and is written using square brackets. Each member of the list is separated using a comma and the list can contain multiple data types (although it is preferred to keep the data type consistent throughout a list). A list is assigned to a variable and the variable identifier (name) is used throughout a program to reference the entire list itself or individual members of the list.


    list_name = ['food', 'cupboard', 'fifteen', 7]
    print(list_name)
                      
['food', 'cupboard', 'fifteen', 7]

Tuples

In Python, a tuple is similar to a list. It is a collection of data values which is ordered but unlike lists, tuples are immutable (its values cannot be overwritten). Tuples consist of comma separated values, enclosed in round brackets.


    tuple_name = ('food', 'cupboard', 'fifteen', 7)
    print(tuple_name)
                      
('food', 'cupboard', 'fifteen', 7)

Indexing

each member in a sequence has a unique position number (or more commonly known in Python as an index). However, in Python and other programming languages, the index values don't start with 1. The first member of any sequence always has the index value of 0 and it goes up from there. Individual members of a sequence can be accessed by referring to its index value.

+ve Indices 0 1 2 3 4 5
Characters s t r i n g
-ve Indices -6 -5 -4 -3 -2 -1

In a list or tuple, the first index value (0) is held by the first element in the list/tuple.


    string1 = "qprogramming"
    list1 = [2,3,4]
    tup1 = (5,7,8)
    print(string1[2])
    print(list1[1])
    print(tup1[2])
                      
r 3 8

Converting between Sequences

To convert between sequences, we can use built-in functions (str,list,tuple):


    tuple_name = ('food', 'cupboard', 'fifteen', 7)
    x = str(tuple_name)
    print(x)
    print(x[2])
    x = list(tuple_name)
    print(x)
    print(x[3])
    x = tuple(x)
    print(x)
                      
('food', 'cupboard', 'fifteen', 7) f ['food', 'cupboard', 'fifteen', 7] 7 ('food', 'cupboard', 'fifteen', 7)

As you can see, we used str,list and tuple to convert between different types of sequences. To prove that they were actually converted, we used indexing to access different elements. In a string we will get a single character, but for lists and tuples we will get complete values.


That's it for this tutorial! Click Next to access the next tutorial!

❮ Back

Next ❯









Copyright © 2020, Q-Programming | Created by Arjun Sodhi, Balpreet Juneja and Sammarth Kumar | Developed and Designed by Sammarth Kumar