LEARN PYTHON PROGRAMMING FOR BEGINNERS: A beginner's guide comprehending python. Develop your programming skills and learn all the tricks with this crash course. by Willard D. Sanders

LEARN PYTHON PROGRAMMING FOR BEGINNERS: A beginner's guide comprehending python. Develop your programming skills and learn all the tricks with this crash course. by Willard D. Sanders

Author:Willard D. Sanders [Sanders, Willard D.]
Language: eng
Format: azw3
Published: 2020-10-01T00:00:00+00:00


You’ll discover how to manage and handle errors and exceptions effectively.

Python Tuples

In Python, Tuples are collections of data types that cannot be changed but can be arranged in a specific order. Tuples allow for duplicate items and are written within round brackets, as shown in the syntax below.

Tuple = (“string001”, “string002”, “string003”)

print (Tuple)

Tuples are similar to lists and creating them is quite simple, one has to put commas to separate values and these values can also be enclosed in parenthesis. For example: tup1 = (‘chemistry’, ‘physics’, 1998, 2000); tup2 = (7, 8, 9); tup3 = “x”, “y”, “z” ; Listed below are some of the basic features of tuple:

a) For writing an empty tuple two parentheses are used – tup1 = ();

b) Even of the tuple contains a single value one has to include comma—tup1 = (50,);

c) The indices in tuple start with at 0 and slicing can also be done.

d) Square brackets are used to access values in tuples

e) One cannot change or update the values in tuples

f) Removing the tuple element is not possible; however one can use del to remove the entire tuple.

g) All general operators can be used There are few built-in tuples like:

For comparing different elements - cmp(tuple1, tuple2)

To find the total length of tuple -len(tuple)

Converting a list to tuple – tuple(seq)

Find maximum value—max(tuple)

Minimum value – min(tuple)

Similar to the Python List, you can selectively display the desired string from a Tuple by referencing the position of that string inside the square bracket in the print command as shown below.

Tuple = (“string001”, “string002”, “string003”)

print (Tuple [1])

OUTPUT – (“string002”)

The concept of negative indexing can also be applied to Python Tuple, as shown in the example below: Tuple = (“string001”, “string002”, “string003”, “string004”, “string005”)

print (Tuple [-2])

OUTPUT – (“string004”)

You will also be able to specify a range of indexes by indicating the start and end of a range. The result in values of such command on a Python Tuple would be a new Tuple containing only the indicated items, as shown in the example below:

Tuple = (“string001”, “string002”, “string003”, “string004”, “string005”, “string006”)

print (Tuple [1:5])

Output

(“string002”, “string003”, “string004”, “string005”)

* Remember the first item is at position 0 and the final position of the range, which is the fifth position in this example, is not included.

You can also specify a range of negative indexes to Python Tuples, as shown in the example below: Tuple = (“string001”, “string002”, “string003”, “string004”, “string005”, “string006”)

print (Tuple [-4: -2])

Output – (“string004”, “string005”)

* Remember the last item is at position -1 and the final position of this range, which is the negative fourth position in this example is not included in the Output.

Unlike Python lists, you cannot directly change the data value of Python Tuples after they have been created. However, conversion of a Tuple into a List and then modifying the data value of that List will allow you to subsequently create a Tuple from that updated List. Let’s look at the example below: Tuple1 = (“string001”, “string002”, “string003”, “string004”, “string005”, “string006”) List1 = list (Tuple1)

List1 [2]



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.