Difference between revisions of "Python/C3/Getting-started-with-tuples/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 48: Line 48:
 
|-
 
|-
 
|01:10
 
|01:10
| For example,
+
| For example,First you must type t
 
+
|-
+
|01:11
+
|First you must type t
+
  
 
|-
 
|-
Line 224: Line 220:
 
|-
 
|-
 
|05:35
 
|05:35
|Define tuples.
+
|Define tuples.Understand the similarities of tuples with lists, like indexing and iterability.
 
+
|-
+
|05:36
+
|Understand the similarities of tuples with lists, like indexing and iterability.
+
  
 
|-
 
|-

Latest revision as of 12:34, 27 March 2017

Time Narration
00:00 Hello friends and welcome to the tutorial on "getting started with tuples".
00:05 At the end of the tutorial, you will be able to,

Understand of what tuples are. Compare them with lists. Know why they are needed and where to use them.

00:15 Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with lists".
00:21 Let us start our ipython interpreter.
00:23 Type ipython and hit Enter.
00:27 Let's get started by defining a tuple.
00:29 A tuple is defined by enclosing parentheses around a sequence of items separated by commas.
00:39 It is similar to defining a list except that parentheses are used instead of square brackets.
00:45 So type t is equal to within brackets 1, 2.5, "hello",-4, "world", 1.24,5.
01:02 The items in the tuple are indexed using numbers and can be accessed by using their position.
01:10 For example,First you must type t
01:18 Then type t within square brackets 3.
01:24 It prints -4 which is the fourth item of the tuple.
01:29 Similarly type t within square brackets 1 colon 5 colon 2 and hit Enter.
01:40 It prints the corresponding slice.
01:42 This behaviour is similar to that of lists.
01:46 But the difference can be seen when we try to change an element in the tuple.
01:51 So type t within square brackets 2 is equal to in double quotes Hello, H is capital.
02:05 We can see that, it raises an error saying 'tuple object does not support item assignment'.
02:10 Tuples are immutable, and hence cannot be changed after creation.
02:13 Then, what is the use of tuples?
02:16 We shall understand that soon.
02:19 But let us look at a simple problem of swapping values.
02:24 Pause the video here, try out the following exercise and resume the video.
02:30 Given, a is equal to 5 and b is equal to 7.
02:33 Swap the values of a and b.
02:38 Switch to terminal for solution
02:40 Type a is equal to 5, then b is equal to 7 , then type a and then b, you can see the values.
02:50 We now create a variable say, temp and swap the values using this variable.
02:56 So type temp is equal to a.Then a is equal to b ;then type b is equal to temp.
03:08 Then type a.
03:10 Then b.
03:13 This is the traditional approach
03:16 So let us do it the python way.
03:21 So type a; then b; then a comma b is equal to b comma a.
03:33 then a ; then b to see the output.
03:38 We see that the values are swapped.
03:43 This idiom works for different data-types also.
03:46 So lets type a is equal to 2 point 5; then b within double quotes hello where h is small letter.
03:59 Then a comma b is equal to b comma a.
04:09 Then a; then b.
04:13 Moreover this type of behavior is something that feels natural and you'd expect to happen.
04:19 This is possible because of the immutability of tuples.
04:22 This process is called tuple packing and unpacking.
04:26 So type 5 comma to see what is tuple packing
04:37 What we see in the tuple is 1 element.
04:41 So type 5 comma within double quotes hello where h is small letter comma 2.5.
04:57 Now, It is a tuple with 3 elements.
05:03 So when we are actually typing 2 or more elements separated by comma the elements are packed into a tuple.
05:10 When we type a comma b is equal to b comma a first the value of b and a are packed into a tuple from the right side then unpack into the variables a and b.
05:21 Immutability of tuples ensure that values are not changed during the packing and unpacking.
05:29 So This brings us to the end of this tutorial.
05:33 In this tutorial, we have learn't to,
05:35 Define tuples.Understand the similarities of tuples with lists, like indexing and iterability.
05:44 Know about the immutability of tuples.
05:48 Swap values, the python way.
05:52 Understand the concept of packing and unpacking tuples.
05:57 Here are some self assessment questions for you to solve.
06:01 Define a tuple containing two values.
06:04 The first being integer 4 and second is a float 2.5
06:08 If a = 5, then what is the type of a ?
06:13 The options are int , float, tuple, string.
06:19 The final question the third one is if a = (2, 3)
06:25 What does a[0], a[1] = (3, 4) produce.
06:34 And the answers,
06:38 A tuple is defined by enclosing parentheses around a sequence of items separated by commas.
06:44 Hence, we write our tuple as,within brackets 4 comma 2.5.
06:53 Since the given data is 5 followed by a comma, it means that it is a tuple
07:01 The operation a in square brackets 0, a in square brackets 1 is equal to in brackets 3 comma 4 will result in an error because tuples are immutable.
07:14 Hope you have enjoyed this tutorial and found it useful.
07:17 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha