Friday, 10 February 2017

Range Functions and Sequences in Python


Range() generates lists containing arithmetic progression.
Three variations of range() function
>> range(stop) – Starts from 0 till (stop – 1)
>> range(start,stop) – Ends at (stop – 1)
>> range(start,stop,step) – Step cannot be 0, default is 1
Example of Range Function
range(stop) - If the range is defined as 5, it would simply show the list of numbers falling in the range from 0 to 5. The default range starts from 0 and stops before 5, as defined.
range(start, stop) – The point of starting as well as stopping is defined in this. As shown in the example below, the start range has been defined as 5, while stop range as 10. Hence, it would display the numbers 5, 6, 7, 8, 9, which range between 5 to 10.
range(start, stop, step) - The first two values defined here are the same, start and stop, while the third one is step, which means the difference between every two consecutive numbers. For example, if range is defined in this way: range(0, 10, 2). It will give away numbers between 0 to 10, but with a difference of 2, in this way: [0, 2, 4, 6, 8]. The step here cannot be given 0 value. It has to be 1 or greater than 1.
Sequences in Python
A sequence is the succession of values bound together by a container that reflects their type. Almost every stream that we put in Python is a sequence.
Types of Sequences
  • Lists
  • Tuples
  • Xrange
  • String
The python is supported by some other sequences are strings, lists, tuples and Xrange objects. Python has a bevy of methods and formatting operations that can perform.
List
  • A list is a sort of container which holds the number of other objects, in a given order.
  • The list type implements the sequence protocol which allows adding and removing objects from the sequence.
  • It is an ordered set of elements enclosed in square brackets.
Simple definition of list – li = []
li = list() # empty list
li = list(sequence)
li = list(expression for variable in sequence)
example
            >>> list(a)
            [‘e’, ‘x’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’]
            >>> list3 = [‘Hadoop’, ‘Python’, ‘Data Science’, ‘Pig’, ‘hive’]
            >>> list3
            [‘Hadoop’, ‘Python’, ‘Data Science’, ‘Pig’, ‘hive’]
            >>> list3[2:]
            [‘Data Science’, ‘Pig’, ‘hive’]
            >>> list3[2:3]
            [‘Data Science’]
            >>> list3[2:4]
            [‘Data Science’, ‘Pig’]
            >>> list3[2:5]
            [‘Data Science’, ‘Pig’, ‘hive’]
            >>>
Accessing List Elements
To access the elements of a list:
n = len(li)
item = li[index] #Indexing
slice = li[start:stop] #Slicing
List Indexing

list[i] returns the value at index i, where i is an integer. A negative index accesses elements from the end of the list counting backwards. The last element of any non-empty list is always li[-1]. Python raises an IndexError exception, if the index is outside the list.

Accessing Command Line Arguments

Python supports the creation of programs that would run on the command line, completely with command-line arguments. It provides getopt modules that parse the command line options and arguments. The Python sys module provides access to any of the command-line arguments via sys.argv. It solves two purposes:
  • sys.argv is the list of command line arguments
  • len(sys.argv) is the number of command line arguments that are in the command line
  • sys.argv[0] is the program, i.e. script name

Executing Python

The python should be executed in the following
$python Commands.py inp1, inp2 inp3

Example

import sys
print ‘Number of arguments:’, len (sys.argv), ‘arguments.’
print ‘Argument List:’, str(sys.argv)
It will produce the following output:
Number of arguments: 4 arguments.
Argument List: [‘sample.py’, ‘inp1’, ‘inp2’, ‘inp3’]
If you have any queries? Mention them in the comments section and we will clarify you.

No comments:

Post a Comment