Difference between revisions of "Python-3.4.3/C3/Sequence-datatypes/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "{| style="border-spacing:0;" | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| <cen...")
 
Line 15: Line 15:
  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In this tutorial, we will learn about-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In this tutorial, we will learn about-
 
  
 
* '''Sequence Data types''' such as
 
* '''Sequence Data types''' such as
Line 21: Line 20:
 
** '''String '''and
 
** '''String '''and
 
** '''Tuple'''
 
** '''Tuple'''
* Accessing the above data types using index
+
* Accessing the above '''data types''' using '''index'''
* Convert list to tuple and vice-versa and  
+
* Convert list to '''tuple''' and vice-versa and  
* Convert string to list and vice-versa.  
+
* Convert '''string''' to '''list''' and vice-versa.  
 
+
 
+
  
 
|-
 
|-
Line 32: Line 29:
 
System Specifications  
 
System Specifications  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To record this tutorial, I am using  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To record this tutorial, I am using  
 
  
 
* '''Ubuntu Linux 16.04''' operating system
 
* '''Ubuntu Linux 16.04''' operating system
 
* '''Python 3.4.3 '''and
 
* '''Python 3.4.3 '''and
 
* '''IPython 5.1.0'''
 
* '''IPython 5.1.0'''
 
 
  
 
|-
 
|-
Line 65: Line 59:
 
* '''string '''and
 
* '''string '''and
 
* '''tuple '''
 
* '''tuple '''
 
 
  
 
|-
 
|-
Line 72: Line 64:
  
 
Lists
 
Lists
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| First let us understand what is list.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| First let us understand what is '''list'''.
  
  
Line 81: Line 73:
  
  
A''' list''' can store data of any type.
+
A''' list''' can store data of any '''type'''.
  
  
List is most useful when storing data of identical type.
+
'''List''' is most useful when storing data of identical '''type'''.
  
  
Line 91: Line 83:
 
* Name of books in a library and
 
* Name of books in a library and
 
* Passengers on a flight  
 
* Passengers on a flight  
 
 
  
 
|-
 
|-
Line 130: Line 120:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''<nowiki>var_list = [1, 1.2, [1,2]] </nowiki>'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''<nowiki>var_list = [1, 1.2, [1,2]] </nowiki>'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can have a list something like this.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can have a '''list''' something like this.
  
  
Line 141: Line 131:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''greeting_string = "hello"'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''greeting_string = "hello"'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now let us look at another sequence data type, '''strings'''.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now let us look at another '''sequence data type, strings'''.
  
  
Line 191: Line 181:
  
  
To create a '''tuple''' we use normal brackets unlike square brackets for list.
+
To create a '''tuple''' we use normal brackets unlike square brackets for '''list'''.
  
  
Line 198: Line 188:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''<nowiki>num_list[2] </nowiki>'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''<nowiki>num_list[2] </nowiki>'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next, let us see how to access the list using index numbers.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next, let us see how to access the '''list''' using '''index''' numbers.
  
  
Line 211: Line 201:
  
  
Recall''' '''that, the index of the elements starts with 0 instead of 1.
+
Recall that, the '''index''' of the '''elements''' starts with 0 instead of 1.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''<nowiki>greeting_string[1]</nowiki>'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''<nowiki>greeting_string[1]</nowiki>'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now let us access the string elements.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now let us access the '''string elements'''.
  
  
Line 232: Line 222:
  
  
We can see that the output is displayed as per the index value.
+
We can see that the output is displayed as per the '''index''' value.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''person_tuple<nowiki>[2]</nowiki>'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''person_tuple<nowiki>[2]</nowiki>'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next let us '''access''' '''tuple''' '''elements'''.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next let us '''access tuple elements'''.
  
  
 
Type '''person '''''underscore '''''tuple '''''inside square brackets''''' 2'''
 
Type '''person '''''underscore '''''tuple '''''inside square brackets''''' 2'''
 
 
 
  
 
|-
 
|-
Line 252: Line 239:
  
  
The output shows the weight and age of the person as per the index value.
+
The output shows the weight and age of the person as per the '''index''' value.
  
  
Line 261: Line 248:
  
 
Highlight the output
 
Highlight the output
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next, we will see about how to add sequences.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next, we will see about how to add '''sequences'''.
  
  
Line 274: Line 261:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Likewise, we will do for '''string''' data type.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Likewise, we will do for '''string data type'''.
  
  
 
Type '''a '''''underscore '''''string''''' is equal to inside double quotes '''''<nowiki><sp</nowiki>ace> another string'''
 
Type '''a '''''underscore '''''string''''' is equal to inside double quotes '''''<nowiki><sp</nowiki>ace> another string'''
 
 
 
  
 
|-
 
|-
Line 290: Line 274:
  
  
We can see that both the strings are added now.
+
We can see that both the '''strings''' are added now.
  
 
|-
 
|-
Line 307: Line 291:
  
 
Adding a '''comma''' after '''Student''' is important to make '''t2''' a '''tuple'''.
 
Adding a '''comma''' after '''Student''' is important to make '''t2''' a '''tuple'''.
 
 
 
  
 
|-
 
|-
Line 326: Line 307:
  
  
We use '''len''' '''function''' for that'''.'''
+
We use '''len function''' for that.
  
  
Line 332: Line 313:
  
  
The output shows the number of objects of the list.
+
The output shows the number of '''objects''' of the '''list'''.
  
 
|-
 
|-
Line 365: Line 346:
  
  
We use '''max''' function to find the maximum value.
+
We use '''max function '''to find the maximum value.
  
  
Line 372: Line 353:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''min(greeting_string)'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''min(greeting_string)'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| The '''min '''function''' '''is used to find minimum value.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| The '''min function '''is used to find minimum value.
  
  
Line 382: Line 363:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''sum(num_list) '''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''sum(num_list) '''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next we will find the sum of a list'''.'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next we will find the sum of a '''list'''.
  
  
Line 463: Line 444:
  
  
'''Tuple''' function converts the '''list''' to '''tuple'''.
+
'''Tuple function''' converts the '''list''' to '''tuple'''.
  
 
|-
 
|-
Line 487: Line 468:
  
  
'''List''' function converts the '''tuple''' to '''list'''.
+
'''List function''' converts the '''tuple''' to '''list'''.
  
 
|-
 
|-
Line 504: Line 485:
  
  
Let us say we have the following string.  
+
Let us say we have the following '''string'''.  
  
 
|-
 
|-
Line 524: Line 505:
  
  
Let us say we have the following list.
+
Let us say we have the following '''list'''.
  
 
|-
 
|-
Line 541: Line 522:
  
 
Highlight the string
 
Highlight the string
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Similarly we can do on spaces.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Similarly we can do on '''spaces'''.
  
  
Line 572: Line 553:
  
  
Check if 3 is an element of the following list.
+
Check if 3 is an '''element''' of the following '''list'''.
  
  
Change the third element in the list to 21.  
+
Change the third '''element''' in the '''list''' to 21.  
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the terminal for the solution.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the '''terminal''' for the solution.  
  
 
|-
 
|-
Line 654: Line 635:
  
  
In this tutorial, we have learned to,''' '''
+
In this tutorial, we have learnt to,
  
 
+
* Use the '''sequence data types''' such as '''List, String '''and''' Tuple '''
* Use the sequence data types such as '''List, String '''and''' Tuple '''
+
* Split and join a list using '''split''' and '''join functions '''respectively  
* Split and join a list using '''split''' and '''join''' functions respectively  
+
 
* Convert '''list''' to '''tuple''' and vice-versa and  
 
* Convert '''list''' to '''tuple''' and vice-versa and  
 
* Convert '''string''' to '''tuple''' and vice-versa
 
* Convert '''string''' to '''tuple''' and vice-versa
 
 
  
 
|-
 
|-
Line 668: Line 646:
  
 
Evaluation  
 
Evaluation  
 
 
  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve  
  
  
# What is the major difference between tuple and list?
+
# What is the major difference between '''tuple''' and '''list'''?
# Split the below string on whitespaces  
+
# Split the below '''string''' on '''whitespaces'''
 
+
 
+
  
 
|-
 
|-
Line 689: Line 663:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers,''' '''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers,''' '''
  
1. The major difference between tuple and list is that,
+
1. The major difference between '''tuple''' and '''list''' is that,
  
* Tuple is immutable while list is not
+
* '''Tuple''' is immutable while '''list''' is not
* Tuple is used to store data related to an item
+
* '''Tuple''' is used to store data related to an item
* But list is typically used to store items of identical type
+
* 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  
+
2. To split the '''string''' on '''whitespace''', we use the''' split function''' without any '''argument'''
  
 
|-
 
|-

Revision as of 12:34, 24 May 2018

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 share the property of being immutable.


We 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 is immutable while list is not
  • 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