Tuple DataType in Python

Hey peeps! Welcome to another tutorial on data types in Python. Our purpose in Python education is to get a grip on the basic concepts so that we may work on deep learning easily. In the previous lecture, we read a lot about lists as we are working on the subtypes of the sequence data type. In the present lecture, you are going to understand more types of sequences. If you know the list well, this lecture will be a piece of cake for you. We will start the introductions in just a bit, but before that, there must be a quick review of the topics that you are going to understand:

  • How do you introduce the tuples in Python?

  • What are some important characteristics of a tuple that must be kept in mind when we are dealing with it?

  • How can you practically perform the tuples in TensorFlow?

  • How do you associate tuples with the list?

  • Can you delete, update, add, or delete the elements in a tuple? If yes, then how?

  • Which is the way to delete the tuple entirely?

All of these are important interview questions about the tuple and if you are dealing with these data types, you must know and perform these by yourself in TensorFlow. All the information about tuples will be discussed with you and you have to practice more and more in the code to understand the concepts.

Tuple in Sequence 

For the concept of a list, we have given you the reference of an array. This time, we will be mentioning the name of the list for better understanding. If we talk about the topic of today, a tuple is also a group of items that are arranged when required in Python. The working and the definition of the tuple seem like a list because both of them are types of a single data type, which is the sequence. But there are some differences that make them ideal for different kinds of situations. The following points will make this more clear:

  • The tuple is represented by enclosing the data in parentheses. 

  • A tuple is immutable, which means that once you declare the items in the tuple, you can not change them as you can in a list. 

  • When you try to change the value of the element in the tuple, an error is shown on the screen, so while declaring the new tuple, you have to be very clear about what is on your mind and what you want to do with the tuple. 

  • In a tuple, you can also use the function in the tuple and it becomes easy to perform different tasks through codes.

  • A single element in the tuple is necessary to build the tuple. In other words, unlike the strings, the tuple must contain at least one element in it.

  • In the tuple, any data type can be saved whether it is boolean, int, or string. Moreover, there is a possibility to add different types of data in a single tuple. So we can conclude that we can have a homogeneous or heterogeneous tuple according to our choice. 

    Order of The Tuple

    As we have mentioned, when using the tuple, we get the ordered list of objects, which means we get the specific order, and this order, once mentioned, can not be changed, unlike other data types of the sequence. 

    Unchangeable Tuple

    Not just the order, but the size, sequence, and entries are unchangeable, so during the initialization and declaration of the tuple, the concept must be clear about what you want from the particular tuple. 

    Duplication in Tuple

    Another thing that must be kept in mind is, the tuple has the index form, and therefore, every element has a specific number of indexes. This is the reason, the elements are easily accessible, and hence, you can also have duplication in the tuple. The elements are recognized by the index number, and therefore, the compiler knows when which element is being called.  

    Length of the Tuple

    The length is the number of elements in the tuple, and we have read about the length function in the previous lecture. Similar to the list, a tuple can also be used in the length function, and the programmer gets the length of the tuple. Right now, this function is not looking very attractive because we are dealing with small tuples. But take the case in your mind when the tuple contains hundreds or thousands of elements and get the length of the tuple in just a few moments. 

    Tuple in TensorFlow

    It is now time to go over some basic tuple-related code using TensorFlow. The steps for starting it are the same as those we always follow; have a look at these steps:

    • Open your Anaconda Navigator by browsing it from your window panel. 

    • Search for the Jupyter lab in the environment section. 

    • Wait for the PC to open the new tab in your browser. 

    • Go to the new cell.

    • Start coding.

    With the help of code, we will try to create a tuple, and then, by using the function on it we will try to retrieve the data from the tuple in different ways. So, have a look at the code given next and then guess the output in your mind.

    print('Make a tuple with stationaries item')

    myTuple=('pen', 'paper', 'eraser', 'pencil', 'sharpener', 'notebooks')

    print('The tuple has following items: ',myTuple)

    print()

    print('print only the third item of the tuple:', myTuple[3])

    print()

    print('print the item by using the index: ',myTuple[1])

    Now, have a look at the output. Is it exactly the same as you were expecting?

    You can see how simple it is to get the item from the tuple when it is declared in the tuple. This is the same as what we did with the list. But what if we want more than one element from the tuple at the same time?

    print('Make a tuple with fruits and prices in dollars')

    myTuple=('strawberry','kiwi','banana','orange','apple', 2.5,3,12, 4,6)

    print('The following items in the fruit shop are available: ',myTuple)

    print()

    print('print only the forth fruit of the fruit shop:', myTuple[-7])

    print()

    print('print the name of only fruits: ',myTuple[0:5])

    Looking at the code, you will observe that the negative number is used in the index. This is the way to tell the compiler that we are counting from the end of the index. Moreover, the index on the end side starts at 1 instead of zero. Another point to notice is that the start and end limits are specified by separating them with the colon, and as a result, we get the whole tuple in the output. 

    A new concept that is to be shared is that when you are providing the limits to the tuple, you have to take care that the right numbers are being used because it will then not work properly. You must be thinking that the compiler will throw the error when you feed the wrong limits in the tuple, but instead, the output will be empty, and you will get the parentheses with nothing in them.

    Another thing that must be mentioned here is that you can check whether the tuple has a specific element or not. For this, we get the help of a statement. We know that, until now, we have not learned about the statements, and therefore, we suggest just looking at the code and output to understand the concept. 

    print('Make a tuple with fruits and prices in dollars')

    myTuple=('strawberry','kiwi','banana','orange','apple', 2.5,3,12, 4,6)

    print('The following items in the fruit shop are available: ',myTuple)

    if "apple" in myTuple:

      print("Yes, 'apple' is in the present in the fruit shop")

    This program searches for the elements specified in the “if condition” and then prints the result on the screen. 

    How to Make Changes in A Tuple

     If you are reading this lecture from the beginning, you must be thinking we have mentioned again and again that a tuple is immutable and unchangeable. Yet, programmers have the logic and solutions to every problem, and if the programmers have specified the tuple and want some changes made to it, they can do so with the help of the list. It is one of the reasons why we have discussed the list before in this series. Have a look at the following code and output, and then we will discuss it in detail.

    print('Make a tuple with fruits and prices in dollars')

    myTuple=('strawberry','kiwi','banana','orange','apple', 2.5,3,12, 4,6)

    print('The following items in the fruit shop are available: ',myTuple)

    myList=list(myTuple)

    print(myList)

    myList[2]='pear'

    print(myList)

    myTuple=tuple(myList)

    print(myTuple)

    Copy this code and run it on your TensorFlow, you will get the following output:

    First, look at the brackets carefully and check the output in a sequence with the points given next:

    • At the start, the string message is shown, which shows the main title of the tuple. 

    • The name of the tuple here is “myTuple” and it contains the fruits’ names and prices.

    • To make the changes in the tuple, we have to use another approach, and we know the reason why. For this, we are using the list. It is possible to convert the list into a tuple and vice versa, and we are doing the same in this. We are just using the name of the data type as a function and inputting the name of the data type to be changed. So, we changed the tuple into a list and then made the changes according to our choice. 

    • By using the index number and feeding the value into the list, the list is updated. This can be observed with the help of square brackets.

    • Once we have seen the updated list, we can now easily convert it into a tuple again. 

    • The conversion is done with the same procedure, and we get the updated tuple. 

    This was a simple example, other operations such as the addition of the new element, deleting the elements from the tuple, removing the single elements from the tuple, etc. are done with the help of lists. 

    Deleting the Tuple Entirely

    Now that you know all the ways to initiate, use, and update the tuple in detail, a last method for the tuple is ready for you. In some cases, when you do not want to use a particular tuple for any reason, such as if you are no longer using it, you can do so in just a simple step. This is used in the programs when long calculations and collections of data types are needed for a particular time and then there is no need for them anymore. So for this, we use the delete operation. The programmers have to simply use the del keyword before the name of the tuple to be, and the compiler has to do its job well. Have a look at the example given next:

    Tuple = ("Artificial Intelligence", "Machine Learning", "Deep Learning")

    print(Tuple)

    del(Tuple)

    print(Tuple)

    So, when the compiler was on the second line, it showed us the results, but on the next line, when we deleted the declared tuple, the compiler was not able to show us the result because it had been removed from its memory.

    So, it was an informative data types lecture in which we clarified many concepts about tuples. These are the data types that belong to the class of sequences. We read a lot about it and started the discussion with the introduction. The tuple characteristics were thoroughly discussed, and then, with these in mind, we tested the functions of the tuple using TensorFlow examples. We attempted similar examples and carefully observed the operation of tuples, which also involved the list. In the next lecture, you will know more about the data types in Python, so stay connected with us.

    Automatic Garage Door with PLC Ladder Logic

    Hello my friends! I hope your doing very good. Today we are going to practice what we have learnt through the ladder logic tutorial series. We bring a new .project which is mostly exist in our daily life that is electrical door control. in garage for domestic and public garage, you should found automatic garage gate or door that is controlled by the PLC ladder logic program. So today we are going to implement the project including determining the input and output components, design the logic, programming and testing using the simulator.

    Project details

    Figure 1 shows the project’s components, inputs and outputs that are included in controlling the garage door. As you can see my friends, there are inputs like open, close, and stop requested by push buttons. Also, you can see sensors like the two limit switches on the bottom and on the top to prevent overload that might have occurred on the motors. Moving to the outputs, there are motor up and down directions, open, close, and ajar status indicators lamps. So what is the next step in our plan to achieve such job? Of course we need to list these inputs and outputs and give them addresses. Then we design the logic based on the user requirements and the safe operation.

    Inputs and Outputs list

    Table 1 lists the inputs and outputs showing the name in the first column, the description In the second column, while the addresses are listed in the last column.


    Component 

    Description 

    Address00

    Open push button

    One of the inputs to request open operation  

    I: 1 / 00

    Close push button

    One of the inputs that enables user to shut down the door

    I: 1 / 01

    Stop push button

    The push button to stop operation at any time

    I: 1 / 02

    Limit switch 1

    A sensor to stop opening operation beyond the opening limits

    I : 1 / 03

    Limit switch 2

    A sensor to stop closing operation beyond the closing limits

    I : 1 / 04

    Shut down indicator  

    One of the outputs that tells the shutdown operation is in progress using an Indicator lamp 

    O: 2 / 04

    Open door indicator

    One of the outputs that tells the opening door operation is in progress using an Indicator lamp 

    O: 2 / 03

    Ajar door status

    One of the outputs that tells the door is ajar using an Indicator lamp 

    O: 2 / 02

    Motor Up

    A relay that directs the door in opening direction 

    O: 2 / 00

    Motor Down

    A relay that directs the door in closing direction 

    O: 2 / 01

    The logic operation

    After receiving the requirements of the client that tell how the door will be operated, managed, and controlled, we need to add the restrictions and safety conditions that secure the equipment like the motor, supply, sensing components. So the following lines state the operation and restrictions that need to be followed to achieve the designated task as follows:

    • The door opening and or closing shall be stopped at any time user / operator hit stop push button

    • The door shall open by requesting opening operation using the designated push button

    • The door shall close by requesting shut down operation using the designated push button

    • The indicator lamp of opening operation shall be lit while opening operation is in progress

    • The indicator lamp of closing operation shall be lit while shut down operation is in progress

    • The ajar status lamp shall be lit at any time the door is not fully opened or totally closed

    Restrictions:

    • The opening operation can not be requested while shut down process is in progress

    • The shut down operation can not be requested while opening operation is in progress

    • The opening operation can not proceed any further beyond the designated limit to avoid any overload on the motor

    • The shut down operation can not proceed any further beyond the designated limit to avoid any overload on the motor.

    Ladder Logic Code for Garage Door Project

    The ladder logic program of the project is shown by figure 2. It shows two rungs, one for opening process and second for the shut down operation. The program can be more lengthy but we professionally resumed it in only two main rungs. The first rung represents the door opening operation in which, open push button initiates the opening process and the latching is there to let the opening process continue till the limit switch LS1 contacted or the stop is requested by hitting the stop push button. In addition, security of the process has been considered by including the condition of not having a shutdown process in progress represented by O:2/1. On the other hand, the shut down process is represented by the second rung. The shut down process initiated by the shut down push button and latching the door closing relay for continuing the shut down process unless one of the restriction conditions is met. The restriction conditions of shut down are the limit switch LS2, the door opening in progress, or a stop has been requested by operator using the stop push button. Also, indicator of the process status are included to show the opening operation thanks to the door opening indicator and the shutdown indicator. But we missed the ajar status of the door so let us add one rung for that.

    As you see guys, the third rung has been added to implement the ajar status. It is simply clear that when the opening is in progress without reaching to the fully open indicator, or the shut down process is in progress without reaching to the totally closed, the ajar lamp is energized. Now let us go testing what we have coded so far and see if it is correct or not. But first you guys should sit and list the test cases that you should try to make sure the system is going to perform correctly and safely as well.

    Testing the project

    Door Opening test

    In this test , we hit the opening door push button to request opening the door. As you see my friends, the door is opening as in figure 3 and the opening indicator is lit. also you can see the ajar indicator is lighted because the door is not reached the final opening position.

    Figure 5 shows the state of the process after reaching the final position for opening the door. It is clear the process safely completed and the limit switch LS1 is gone green. Also the ajar status and opening indicators have been turned off.

    Door closing test

    In this test , we hit the door shut down push button to request closing the door. As you see my friends, the door is closing as in figure 6 and the closing indicator is lit. also you can see the ajar indicator is lighted because the door has not reached the final shut down position.

    Figure 7 shows the state of the process after reaching to the final position for closing the door. It is clear the process safely completed and the limit switch LS2 is gone red. Also the ajar status and shut down indicators have been turned off.

    Security and Safety Test

    In this test case we want to request opening .the door while it is shuting down and try to request shut down the door while it is opening to see is there any issue or not. Figure 8 shows what is going on when we requested opening the door while a shuting down process is inprogress. You can see guys in rung number one, the opening push button ,is pressed as circled and highlighted see I:1/0. However, the requested process has not performed due to the restriction that the shutdown process should not be in progress. Same thing when opening door is in progress, shut down requests are forbidden.

    Syed Zain Nasir

    I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

    Share
    Published by
    Syed Zain Nasir