Skip to main content

Python Tuples

Python Tuples

Tuples are immutable lists and cannot be change in anyway once it is created.
Some of the characteristics features of Tuples are:
  • Tuples are defined in the same way as lists
  • They are enclosed within parenthesis and not within square braces
  • Elements of the tuple must have a defined order
  • Negative indices are counted from the end of the tuple, just like lists
Tuple also have the same structure where the values are separated by commas.
An example showing how to build tuples in Python:
Example:
#!/usr/bin/python

tupl1
= ('computersc', 'IT', 'CSE');
tup2
= (6,5,4,3,2,1);

Accessing Values In Tuples

Programs to show how to access values in Tuples:
Example:
#!/usr/bin/python

Programs to show how to access values in Tuples:
tupl1
= ('computersc', 'IT', 'CSE');
tupl2
= (1993, 2016);
tupl3
= (2, 4, 6, 8, 10, 12, 14, 16);
print "tupl1[0]", tupl1[0]
print "tupl3[2:4]", tupl3[2:4]

Updating Tuples

They are immutable i.e. the values can’t be changed directly. So we can just update by joining tuples. Let’s demonstrate this with an example:
Example:
#!/usr/bin/python

tupl1
= (2, 3, 4);
tupl2
= ('ab', 'cd');
tupl3
= tupl1 + tupl2
print tupl3
This code snippet will execute a combination of two tuples using the “+” operator.
(2, 3, 4, 'ab', 'cd')

Delete Elements From Tuples

To delete a tuple, we can use the del-statement. The syntax for deleting the tuple is:
Syntax:
#!/usr/bin/python

del tuple_name;
Example:
#!/usr/bin/python

tupl3
= (2, 4, 6, 8, 10, 12, 14, 16);

del tupl3;



Comments

For Programs Click Here

Popular posts from this blog

Python Lists

Python Lists Dealing with data in a structured format is quiet generous, and this is possible if those data are set accordingly in a specific manner. So, Python provides these data structures named ‘lists’ and ‘tuples’ that are used to organize data in  single set. Python has 6 built-in sequences and among them the most famous are “lists and tuples”. The lists are containers that hold a number of other objects in a given order. It usually puts into practice the sequence protocol and allows programmers to add or remove objects from that sequence. Each element of the sequence is assigned a number i.e. he index and the first index is 0 (zero). This versatile data-type of Python is written in a sequence of list separated by commas between expressions. Creating Lists To build a list, just put a number of expressions in square brackets. The syntax is: Syntax: lst1 = [ ] # lst1 is the name of the list lst2 = [ expression1 , …. , expression_N ] Example: #!/usr/bin/python ls...

C++ Qualifiers and Storage Classes

C++ Qualifiers and Storage Classes Qualifiers and storage class are smaller but important programming concept that helps to make the quality of a variable more accurate for using that variable within the program. In this chapter you will learn about how qualifiers are used with variables and what the roles of different storage classes in C++ are. What is a Qualifier? A qualifier is a token added to a variable which adds an extra  quality , such as specifying volatility or constant-ness to a variable. They act like an adjective for a variable. With or without a qualifier, the variable itself still occupies the same amount of memory, and each bit has the same interpretation or contribution to the state/value. Qualifiers just specify something about how it may be accessed or where it is stored. Three qualifiers are there in C++. These are: const : This is used to define that the type is constant. volatile : his is used to define that the type is volatile. mutable:  applies to non...

C++ if-else Statements

C++ if-else Statements If else statements in C++ is also used to control the program flow based on some condition, only the difference is: it’s used to execute some statement code block if expression is evaluated to true, otherwise executes else statement code block. The basic format of if else statement is: Syntax: if ( test_expression ) { //execute your code } else { //execute your code } Figure – Flowchart of if else Statement: Example of a C++ Program to Demonstrate if else Statement Example: #include <iostream> using namespace std ; int main () { int a = 15 , b = 20 ; if ( b > a ) {    cout << "b is greater" << endl ; } else {    cout << "a is greater" << endl ; }    system ( "PAUSE" ); } Program Output: Example: #include <iostream> using namespace std ; int main () { char name ; int password ; cout << "Enter the name: " ; cin >>...