Difference between revisions of "Python-3.4.3/C3/Getting-started-with-tuples/English"
Nancyvarkey (Talk | contribs) |
|||
Line 267: | Line 267: | ||
Type, '''b''' | Type, '''b''' | ||
− | We see that the '''values''' are '''swapped''' easily. | + | We can see that the '''values''' are '''swapped''' easily. |
|- | |- | ||
Line 366: | Line 366: | ||
# Define a '''tuple''' containing two values as given below. The first value is '''integer''' 4 and the second value is '''float''' 2.5. | # Define a '''tuple''' containing two values as given below. The first value is '''integer''' 4 and the second value is '''float''' 2.5. | ||
# If we type, '''a''' ''is equal to''''' 5''' ''comma ''then what is the '''datatype''' of '''a'''? | # If we type, '''a''' ''is equal to''''' 5''' ''comma ''then what is the '''datatype''' of '''a'''? | ||
− | # If we type, '''a''' ''is equal to inside brackets '''''2''' ''comma ''''' | + | # If we type, '''a''' ''is equal to inside brackets '''''2''' ''comma '''''3 a''' ''inside square brackets '''''0''' ''comma'' '''a''' ''inside square brackets '''''1''' ''is equal to inside brackets '''''3''' ''comma'' '''4''' What is the output? |
|- | |- |
Latest revision as of 11:09, 19 September 2018
|
|
Show Slide: Title | Welcome to the tutorial on Getting Started with tuples. |
Show Slide
Learning Objectives |
In this tutorial we will:
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Pre-requisites |
To practise this tutorial, you should know how to
If not, see the relevant Python tutorials on this website. |
Show slide
Tuples in Python |
First we will learn about tuples.
|
Show slide
Tuple declaration (1, 2.5) and 1, 2.5 are same (1, ) and 1, are same ((1,),) and (1,), are same |
Here are few examples for declaring tuples.
Inside brackets 1, 2.5 is a tuple with two elements. The same can be declared as 1, 2.5.
The same can be declared as 1 comma
The same can be declared as inside brackets 1 comma and outside bracket a comma. |
Open the terminal | Let us start ipython.
|
Type ipython3 and press Enter | Type ipython3 and press Enter.
|
Type, t = (1, 2.5, "hello", -4, "world", 1.24, 5) | Let's learn to create a tuple.
Type, t is equal to inside brackets 1 comma 2.5 comma inside double quotes hello comma -4 comma inside double quotes world comma 1.24 comma 5 |
Type, t
|
Type, t
It is similar to list except that parentheses are used instead of square brackets. At least one comma is mandatory for a tuple. The brackets are optional, but should be added for clarity. |
t[3] | The items in the tuple can be accessed by their index positions.
|
t[1:5:2] | Type, t inside square brackets 1 colon 5 colon 2
It prints the corresponding slice. |
t[2] = "Hello"
|
Now we try to change an element in the tuple.
|
for x in t:
print(x)
|
We can iterate over tuples like lists.
Type, for x in t colon print inside brackets x
|
Pause the video.
| |
Show Slide
Exercise 1 |
Let us look at a simple problem of swapping values.
Given, a is equal to 5 and b is equal to 7, swap the values of a and b. |
Switch terminal | Switch to the terminal for the solution. |
Type,
a = 5 b = 7
b |
Type,
a is equal to 5 b is equal to 7
Type, b |
Type,
temp = a a = b b = temp a b
|
We now create a variable say, temp and swap the values using this variable.
temp is equal to a a is equal to b b is equal to temp Then type, a Type, b We can see that the values are successfully swapped now.
|
Type,
a = 5 b = 7 a, b = b, a
b |
Now let us do it in the Python way.
a is equal to 5 b is equal to 7 a, b is equal to b, a
Type, a Type, b We can see that the values are swapped easily. |
Type,
a = 2.5 b = "hello" a, b = b, a
b |
We can also do this for different datatypes.
a is equal to 2.5 b is equal to inside double quotes hello a, b is equal to b, a
Type, b <<PAUSE>> |
Highlight a, b = b, a | This is possible because of the immutability of tuples.
|
5,
|
Let us first see about tuple packing.
Type, 5 comma
|
5, "hello", 2.5 | Type, 5 comma inside double quotes hello comma 2.5
|
Show Slide
Summary slide
|
This brings us to the end of this tutorial. Let us summarize.
|
Show Slide
Evaluation
|
Here are some self assessment questions for you to solve.
|
Show Slide
Solutions
|
And the answers,
|
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. |
Show 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. |