• Python Sequence Example

    Python Sequence Example

    This Python Sequence Example is fulfilled the need of Python Data Structure. The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number called its position, or index. The first index is zero (0), the second index is one (1), and so forth. Python has six built-in types of sequences.

    The Python Sequences are useful when you want to work with a collection of values. You might have a sequence representing a numbers in an arithmetic database. Written as a list (the items of a list are separated by commas and enclosed in square brackets), that would look like this:

    >>> odd = [1, ,3, 5, 7, 9]

    But sequences can contain other sequences, too so you could make a list of such numbers, which would be your database:

    >>> odd = [1, 3, 5, 7, 9]

    >>> even = [2, 4, 6, 8, 10]

    >>> numbers = [odd, even]

    >>> numbers

    [[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]

    Python has a basic notation of a kind of data structure called a container, which is basically any object that can contain other objects. The two main kinds of containers are sequences (Such as lists and tuples) and mappings (Such as dictionaries). While the elements of sequence are numbered, each element in a mapping has a name (a called a key).

    Common Sequence Operations

    There are certain things you can do with all sequence types. These operations include indexing, slicing, adding, multiplying, and for checking for membership. In addition, Python sequence has built-in functions for finding the length of a sequence, and for finding its largest and smallest elements.

    Indexing

    All elements in a sequence are numbered – from zero and upwards. You can access them individually with a number, like this:

    >>> name = “Python”

    >>> name[0]

    ‘P’

    Note: A string is just a sequence of characters. The index 0 refers to the first element, in this case the letter ‘P’.

    When we use a negative index, Python counts from the right, from the last element. The last element is at position -1 from right side.

    >>> name[-1]

    ‘n’

    Python Sequence Example:

    In my Python Sequence Example, an example that asks for a year, a month (as a number from 1 to 12), and a day (1 to 31), and then print outs the date with the proper month name and so on. The example is in video tutorial below. Please subscribe and share my video tutorial.

    Download Python Sequence Example Source Code

    Appreciate my work :Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrDigg thisShare on YummlyShare on VKFlattr the authorBuffer this page

Leave a Reply

Your email address will not be published. Required fields are marked *