Python-3.4.3/C3/Sequence-datatypes/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue
Narration
Show Slide title Welcome to the tutorial on Sequence data types.
Show Slide

Objectives


In this tutorial, we will learn about-
  • Sequence Data types such as
    • List
    • String and
    • Tuple
  • Accessing the above data types using index
  • Convert list to tuple and vice-versa and
  • Convert string to list and vice-versa.
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3 and
  • IPython 5.1.0
Show Slide

Pre-requisite slide

To practise this tutorial, you should know how to
  • run basic Python commands on the ipython console

If not, see the relevant Python tutorials on this website.

Show slide:

Sequence data types

Sequence data types are those in which elements are kept in a sequential order.


The elements are accessed using index numbers.


The sequence data types in Python are

  • list
  • string and
  • tuple
Slide:

Lists

First let us understand what is list.


List is a container that holds a number of objects in the given order.


For example: num underscore list is equal to inside square brackets 1, 2, 3, 4


A list can store data of any type.


List is most useful when storing data of identical type.


Examples:

  • Name of books in a library and
  • Passengers on a flight
Open terminal

Type, ipython3 --pylab

press Enter

Let us start ipython3 pylab.


Open the terminal.


Type ipython3 space hyphen hyphen pylab and press Enter.


From here onwards, remember to press the Enter key after typing every command on the terminal.

Type, num_list = [1, 2, 3, 4] Now we will create our first list.


Type, num underscore list is equal to inside square brackets 1, 2, 3, 4

Type, num_list


Highlight [1, 2, 3, 4]

Type, num underscore list


Comma-separated items enclosed in square brackets constitute a list.

Type, var_list = [1, 1.2, [1,2]] We can have a list something like this.


Type, var underscore list is equal to inside square brackets 1, 1.2, inside square brackets 1,2

Type, var_list Type, var underscore list
Type, greeting_string = "hello" Now let us look at another sequence data type, strings.


Type greeting underscore string is equal to inside double quotes hello

Type, greeting_string Type greeting underscore string


It is now a string variable with the value hello.

k = 'Single quote' Python strings can actually be defined in three different ways as follows.


k is equal to inside single quotes Single quote

l = "Let's see how to include a single quote" l is equal to inside double quotes Let's see how to include a single quote
m = """This is another “example” for string"""


Highlight the quotes

m is equal to inside triple quotes This is another “example” for string


Here single, double and triple quotes are used as delimiters.

Type, person_tuple = (17,"Ram",56.8)


Highlight (17,"Ram",56.8)


Highlight (

The last in the list of sequence data types is tuple.


Type person underscore tuple is equal to inside brackets 17, inside double quotes Ram, 56.8


This tuple contains age,name and weight of a person.


To create a tuple we use normal brackets unlike square brackets for list.


<<PAUSE>>

Type num_list[2] Next, let us see how to access the list using index numbers.


Type, num underscore list inside square brackets 2

Type, num_list[-1]


num underscore list inside square brackets -1


Recall that, the index of the elements starts with 0 instead of 1.

greeting_string[1] Now let us access the string elements.


Type, greeting underscore string inside square brackets 1

greeting_string[3] greeting underscore string inside square brackets 3
greeting_string[-2]


Highlight the outputs

greeting underscore string inside square brackets -2


We can see that the output is displayed as per the index value.

person_tuple[2] Next let us access tuple elements.


Type person underscore tuple inside square brackets 2

person_tuple[-3]


Highlight the outputs

person underscore tuple inside square brackets -3


The output shows the weight and age of the person as per the index value.


<<PAUSE>>

num_list+var_list

Highlight the output

Next, we will see about how to add sequences.


Type num underscore list plus var underscore list


Addition gives a new sequence containing both the sequences.

a_string = " another string"


Likewise, we will do for string data type.


Type a underscore string is equal to inside double quotes <space> another string

greeting_string+a_string


Highlight the output

greeting underscore string plus a underscore string


We can see that both the strings are added now.

t2 = (“Student”,)


Highlight comma


Next we will see for tuple.


Type, t2 is equal to inside brackets inside double quotes Student comma


Adding a comma after Student is important to make t2 a tuple.

person_tuple+t2


Highlight student

Type, person underscore tuple plus t2


Now we can see that Student is added to the tuple.

len(num_list) Next let us see how to find the length of a variable.


We use len function for that.


Type, len inside brackets num underscore list


The output shows the number of objects of the list.

len(greeting_string) Type, len inside brackets greeting underscore string
len(person_tuple) len inside brackets person underscore tuple
3 in num_list Next we will learn to check the presence of an element using the 'in' keyword


Type, 3 in num underscore list

'H' in greeting_string Inside single quotes H in greeting underscore string
"Sita" in person_tuple Inside double quotes Sita in person underscore tuple


We can see that the output displays True and False accordingly.

max(num_list) Next we will find the maximum and minimum values.


We use max function to find the maximum value.


Type, max inside brackets num underscore list

min(greeting_string) The min function is used to find minimum value.


Type, min inside brackets greeting underscore string


For string data type, ASCII value is used to get the maximum and minimum values.

sum(num_list) Next we will find the sum of a list.


Type, sum inside brackets num underscore list


<<PAUSE>>

num_list[1]=9 So far we have talked about many similar features of list, string and tuple.


But there are many important features in list that differ from string and tuple.


Let us see them using some examples.


Type, num underscore list inside square brackets 1 is equal to 9

num_list


Highlight the output

num underscore list


Here the value at index 1 is assigned to 9.

greeting_string[1]='k'


Highlight the error

Type, greeting underscore string inside square brackets 1 is equal to inside single quotes k


The first command executes without a problem.


But there is an error in the second one.

person_tuple[0]=23


Highlight the error

Now let us try it in tuple.


Type person underscore tuple inside square brackets 0 is equal to 23


We can see the same error again.


This is because strings and tuples cannot change the value at a particular index.


But we can convert list to tuple and tuple to list.


<<PAUSE>>

list_tuple = [(17, 'Ram', 56.8), (16, 'Sita', 48.9), (22, 'ravan', 63.2)] First let us learn to convert list to tuple.


Type as shown

t = tuple(list_tuple) t is equal to tuple inside brackets list underscore tuple


Tuple function converts the list to tuple.

t


Highlight the brackets

Type, t


Now the list is successfully converted to tuple.

tuple_list = ([2,"two"],[3,"three"],[4,"four"]) Next let us learn to convert tuple to list.


Type as shown.

l = list(tuple_list) Type, l is equal to list inside brackets tuple underscore list


List function converts the tuple to list.

l


Highlight the square brackets

Type, l


Now the tuple is successfully converted to list.

otherstring = "Tim,Amy,Stewy,Boss" Next let us learn to convert string to list and list to string.


Let us say we have the following string.

otherstring.split(',')


Highlight the output

Type otherstring dot split inside brackets inside single quotes comma


This produces a list with the string split at comma.

otherlist=['List','joined','on','commas'] join function does the opposite.


It joins a list to make a string.


Let us say we have the following list.

','.join(otherlist)


Highlight the output

Type, inside single quotes comma dot join inside brackets otherlist


Thus we get a list joined on commas.

spacestring=['Now','on','spaces']


Highlight the string

Similarly we can do on spaces.


Type as shown.


Note that the list has to be a list of strings to apply join operation.

' '.join(spacestring)


Highlight the output

Type, inside single quotes space dot join inside brackets spacestring


We can see that the list is joined on spaces.


<<PAUSE>>

Show Slide

Exercise 1

Pause the video.


Try this exercise and then resume the video.


Check if 3 is an element of the following list.


Change the third element in the list to 21.

Switch to terminal Switch to the terminal for the solution.
l=[1,7,5,3,4] Type, l is equal to inside square brackets 1,7,5,3,4
3 in l 3 in l


The output is True. So the element 3 is in the list.

l[3]=21 Type, l inside square brackets 3 is equal to 21
l


Highlight 21

Type, l


We can see that the element 3 is changed to 21.

Show Slide

Exercise 2

Pause the video.


Try this exercise and then resume the video.


Convert the string "Elizabeth is queen of England" to "Elizabeth is queen"

Switch to terminal Switch to the terminal for the solution.
s="Elizabeth is queen of england" Type s is equal to inside double quotes Elizabeth is queen of england
stemp=s.split() stemp is equal to s.split open and close brackets
s=' '.join(stemp[:3]) s is equal to inside single quotes space dot join inside brackets stemp inside square brackets colon 3
s


Highlight the output

Type, s


We got the required output.

Show Slide

Summary slide


This brings us to the end of this tutorial.


In this tutorial, we have learnt to,

  • Use the sequence data types such as List, String and Tuple
  • Split and join a list using split and join functions respectively
  • Convert list to tuple and vice-versa and
  • Convert string to tuple and vice-versa
Show Slide

Evaluation

Here are some self assessment questions for you to solve


  1. What is the major difference between tuple and list?
  2. Split the below string on whitespaces
Show Slide


Solutions


And the answers,

1. The major difference between tuple and list is that,

  • Tuple cannot change the value at a particular index
  • But list can change the value
  • Tuple is used to store data related to an item
  • But list is typically used to store items of identical type

2. To split the string on whitespace, we use the split function without any argument

Show Slide Forum Please post your timed queries in this forum.
Show Slide Fossee Forum Please post your general queries on Python in this forum.
Slide Textbook Companion FOSSEE team coordinates the TBC project.
Slide Acknowledgement Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.


For more details, visit this website.

Show Slide Thank You This is Priya from IIT Bombay signing off.

Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Priyacst