Python-3.4.3/C3/Sequence-datatypes/English
| |
|
| 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. |