Difference between revisions of "Python-3.4.3/C3/Sequence-datatypes/English"
(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...") |
Nancyvarkey (Talk | contribs) |
||
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, | + | | 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''' ''' | + | 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 | + | | 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''' | + | | 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 | + | 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''' | + | 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 | + | | 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 | + | | 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''' | + | '''Tuple function''' converts the '''list''' to '''tuple'''. |
|- | |- | ||
Line 487: | Line 468: | ||
− | '''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 | + | 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''' | + | |
* 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''' | + | 2. To split the '''string''' on '''whitespace''', we use the''' split function''' without any '''argument''' |
|- | |- |
Revision as of 12:34, 24 May 2018
|
|
Show Slide title | Welcome to the tutorial on Sequence data types. |
Show Slide
Objectives
|
In this tutorial, we will learn about-
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Pre-requisite slide |
To practise this tutorial, you should know how to
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.
|
Slide:
Lists |
First let us understand what is list.
|
Open terminal
Type, ipython3 --pylab press Enter |
Let us start ipython3 pylab.
|
Type, num_list = [1, 2, 3, 4] | Now we will create our first list.
|
Type, num_list
|
Type, num underscore list
|
Type, var_list = [1, 1.2, [1,2]] | We can have a list something like this.
|
Type, var_list | Type, var underscore list |
Type, greeting_string = "hello" | Now let us look at another sequence data type, strings.
|
Type, greeting_string | Type greeting underscore string
|
k = 'Single quote' | Python strings can actually be defined in three different ways as follows.
|
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"""
|
m is equal to inside triple quotes This is another “example” for string
|
Type, person_tuple = (17,"Ram",56.8)
|
The last in the list of sequence data types is tuple.
|
Type num_list[2] | Next, let us see how to access the list using index numbers.
|
Type, num_list[-1]
|
num underscore list inside square brackets -1
|
greeting_string[1] | Now let us access the string elements.
|
greeting_string[3] | greeting underscore string inside square brackets 3 |
greeting_string[-2]
|
greeting underscore string inside square brackets -2
|
person_tuple[2] | Next let us access tuple elements.
|
person_tuple[-3]
|
person underscore tuple inside square brackets -3
|
num_list+var_list
Highlight the output |
Next, we will see about how to add sequences.
|
a_string = " another string"
|
Likewise, we will do for string data type.
|
greeting_string+a_string
|
greeting underscore string plus a underscore string
|
t2 = (“Student”,)
|
Next we will see for tuple.
|
person_tuple+t2
|
Type, person underscore tuple plus t2
|
len(num_list) | Next let us see how to find the length of a variable.
|
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
|
'H' in greeting_string | Inside single quotes H in greeting underscore string |
"Sita" in person_tuple | Inside double quotes Sita in person underscore tuple
|
max(num_list) | Next we will find the maximum and minimum values.
|
min(greeting_string) | The min function is used to find minimum value.
|
sum(num_list) | Next we will find the sum of a list.
|
num_list[1]=9 | So far we have talked about many similar features of list, string and tuple.
|
num_list
|
num underscore list
|
greeting_string[1]='k'
|
Type, greeting underscore string inside square brackets 1 is equal to inside single quotes k
|
person_tuple[0]=23
|
Now let us try it in tuple.
|
list_tuple = [(17, 'Ram', 56.8), (16, 'Sita', 48.9), (22, 'ravan', 63.2)] | First let us learn to convert list to tuple.
|
t = tuple(list_tuple) | t is equal to tuple inside brackets list underscore tuple
|
t
|
Type, t
|
tuple_list = ([2,"two"],[3,"three"],[4,"four"]) | Next let us learn to convert tuple to list.
|
l = list(tuple_list) | Type, l is equal to list inside brackets tuple underscore list
|
l
|
Type, l
|
otherstring = "Tim,Amy,Stewy,Boss" | Next let us learn to convert string to list and list to string.
|
otherstring.split(',')
|
Type otherstring dot split inside brackets inside single quotes comma
|
otherlist=['List','joined','on','commas'] | join function does the opposite.
|
','.join(otherlist)
|
Type, inside single quotes comma dot join inside brackets otherlist
|
spacestring=['Now','on','spaces']
|
Similarly we can do on spaces.
|
' '.join(spacestring)
|
Type, inside single quotes space dot join inside brackets spacestring
|
Show Slide
Exercise 1 |
Pause the video.
|
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
|
l[3]=21 | Type, l inside square brackets 3 is equal to 21 |
l
|
Type, l
|
Show Slide
Exercise 2 |
Pause the video.
|
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
|
Type, s
|
Show Slide
Summary slide
|
This brings us to the end of this tutorial.
|
Show Slide
Evaluation |
Here are some self assessment questions for you to solve
|
Show Slide
|
And the answers,
1. The major difference between tuple and list is that,
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.
|
Show Slide Thank You | This is Priya from IIT Bombay signing off.
Thanks for watching. |