Skip to main content

-Tuples in Python-

 Tuple 

A tuple is a collection of objects, ordered and immutable. 

EX:

What all details are required to create an address?

 ● House Number 

● Apartment /Area 

● Street Name 

● Place 

● Pin code 

● District

 So for storing an address, will it be easy if we have to create separate variables for each of these parameters?

 Tuples help us in such cases.

 address= “12/1 “, “Rose Garden”, “The Mall”, “Rauria”,” Jorhat” 

Here the parameters separated by commas (,) are considered as a tuple and assigned to a variable.

Why ordered? 

It is indexed and can be iterated sequentially (same as the list). 

Immutable

 Tuples once created, cannot be changed. 

eg: address= “12/1 “, “Rose Garden”, “The Mall”, “Rauria”,” Jorhat”

 address=( “12/1 “, “Rose Garden”, “The Mall”, “Rauria”,” Jorhat”) 

Here, both are tuples. () are optional in creating a tuple. Objects separated by commas form a tuple.

 Note: Tuples are sequences, just like lists. indexing, slicing, and functions are common for lists and tuples.  The only difference is that mutable functions like insert, remove,  etc cannot be used with tuple.

 Also, the replacement of objects is not allowed in tuples.

 A tuple once created, cannot be modified.


 Packing/Unpacking of tuple

 address=( “12/1 “, “Rose Garden”, “The Mall”, “Lauria”,” Jorhat”) 

 Here, several elements are referred to together with a single name. t This is known as packing. houseNo,apartment,street,location,district=address

 Here each element of the tuple- ‘address’, will be assigned to each variable on left. This is known as unpacking. 

Now, houseNo  has a value of “12/1” apartment has a value of “Rose Garden” etc.

 The same technique can be used in multiple variable assignments also. eg: x,y,z=1,2,3 ( x will be assigned 1, y 2 and z 3)

Create a tuple that stores the address of a student, unpack it into separate variables, and print it.



 


Create a tuple that stores the phone number of a student. (only one element) 



 If the comma is not there, it will be treated as a string. The below program illustrates it. 







 Output






Note: type() returns the datatype of a variable.

From the address tuple created above, print only the house no. (concept : indexing)








Print each element of the above address in separate lines. (concept :iteration) 



 Create a tuple containing 10 marks of a student. 

From the above tuple,

 ● Find if the student has scored 100% in a subject (use ‘in’) 

● Print no of subjects in which student has scored 90 (use count() ) 

● Marks of first 5 subjects (concept: slicing) 

● Marks of last 3 subjects (concept: slicing) 














Calculate the average marks and print it.


Comments

Popular posts from this blog

- Functions in Python -

  Function-  ● A function is a named block of code designed to perform a specific task.  ● It takes in some input (optional), processes it, and gives us some output. (optional)  ● A function once defined, can be called many times. eg: function to draw a square, function to calculate average, etc. A function has 2 parts - function definition and function call.  Have you ever used a soft drink vending machine?  You give it some input (the drink you want, money ) and it does some processing inside(checks the amount, checks availability) and gives you the output(the soft drink).  Here, you are not bothered about, how the machine works or from where it gets the soft drink. right? You just give the input and you get the required output. isn’t it?  Functions in programming also behave in a similar way. Function definition: Set of instructions that form the function.  Syntax  def  <function name>(<parameter>):     ...

- Quiz game in Python -

 Game Overview  ● Questions are stored in a file. We will have to read it from the file and store it in a dictionary.  ● 2 points for the correct answer and -1 point for the wrong answer  ● Max no. of questions asked:5  ● Questions will be shuffled once and then asked sequentially.  File Template (for the program)  Each question in the file is stored as <Question> : <Answer> Here ‘:’ acts as a separator between question and answer.  Each line read from the file will be of string data type. ‘:’ helps in separating the answer from the question.  Note: Make sure you add a ‘:’ between question and answer if adding a question manually.  Algorithm  Now that you have seen how the quiz game works, Let's have look at the algorithm for the same.  1. Read the entire file  2. From each line read, get the question and answer a. Add to dictionary  3. Print the rules of the game 4. Shuffle the questions  5. Get eac...

-List Slicing in Python-

 List Slicing : Slicing helps in creating a new list from an existing list.  Syntax list_name [start :end:step] Suppose you have a list of Avengers, arranged in the order of your preference.   avengers=[“Iron Man”,”Thor”,”Hulk”,”Spider Man”,”Captain America”,”Black Panther”,”Hawk Eye”,”Vision”,”Black Widow”] Later you decide to split them into a separate list like  favourite=[“Iron Man”,”Thor”,”Hulk”]                                                   less_favourite =[”Spider Man”,”Captain America”,”Black Panther”]                             least_favourite=[”Hawk Eye”,”Vision”,”Black Widow”]  Now, we have 3 lists formed from the main list. Would you want to create these lists again from the beginning? This is where list slicing helps us. Here is the code to  G...