Python pass Statement and Uses In The Coding to Write No Statement

In Python pass Statement and Uses In The Coding to Write No Statement when need to implement a blank block and/or a blank loop.It is used when a statement is required syntactically but you do not want any command or code to execute.

The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go, but has not been written yet (e.g., in stubs for example):

Syntax

pass

Example

#!/usr/bin/python

for letter in 'Python': 
   if letter == 'h':
      pass
      print 'This is pass block'
   print 'Current Letter :', letter

print "Good bye!"

When the above code is executed, it produces following result −

Current Letter : P
Current Letter : y
Current Letter : t
This is pass block
Current Letter : h
Current Letter : o
Current Letter : n
Good bye!
 If you have any further clarification on this topic please give comment below on the Python pass Statement and Uses In The Coding to Write No Statement

Python Basic Free Course For Selenium

This course designed by keeping eyes to the armature and experienced programmer those heaving prior experience in programing they can also grab this topic as well as the armature will find very easy syntax and due to high label in nature.

     1.Syntax

    • Python programs can be written using any text editor and should have the extension .py.
    • Python programs do not have a required first or last line, but can be given the location of python as their first line: #!/usr/bin/python and become executable. Otherwise, python programs can be run from a command prompt by typing python file.py.
    • There are no braces {} or semicolons ; in python.
    • It is a very high level language. Instead of braces, blocks are identified by having the same indentation.


  • if (x > y):
    print("x is greater than y")
    x = x -1
    else:
    print("x is less than or equal to y")

    Comments are supported in the same style as Perl:

    print(“This is a test”) #This is a comment.
    #This is also a comment. There are no multi-line comments.

     2.Variables and Datatype

    • Variables in Python follow the standard nomenclature of an alphanumeric name beginning in a letter or underscore. Variable names are case sensitive. Variables do not need to be declared and their data types are inferred from the assignment statement.

Python supports the following data types:

  • Boolean
  • integer
  • long
  • float
  • string
  • list
  • object
  • None
Example:

bool = True
name = "Craig"
age = 26
pi = 3.14159
print(name + ' is ' + str(age) + ' years old.')

-> Craig is 26 years old.

 

Variable Scope: Most variables in Python are local in scope to their own function or class. For instance if you define a = 1 within a function, then a will be available within that entire function but will be undefined in the main program that calls the function. Variables defined within the main program are accessible to the main program but not within functions called by the main program.
Global Variables: Global variables, however, can be declared with the global keyword.

a = 1
b = 2
def Sum():
global a, b
b = a + b
Sum()
print(b)
-> 3

3.Statements and Expressions


Some basic Python statements include:

  • print: Output strings, integers, or any other datatype.
  • The assignment statement: Assigns a value to a variable.
  • input: Allow the user to input numbers or booleans. WARNING: input accepts your input as a command and thus can be unsafe.
  • raw_input: Allow the user to input strings. If you want a number, you can use the int or float functions to convert from a string.
  • import: Import a module into Python. Can be used as import math and all functions in math can then be called by math.sin(1.57) or alternatively from math import sin and then the sine function can be called with sin(1.57).
Examples:

print "Hello World"
print('Print works with or without parenthesis')
print("and single or double quotes")
print("Newlines can be escaped like\nthis.")
print("This text will be printed"),
print("on one line becaue of the comma.")
name = raw_input("Enter your name: ")
a = int(raw_input("Enter a number: "))
print(name + "'s number is " + str(a))
a = b = 5
a = a + 4
print a,b
9 5
Python expressions can include:
a = b = 5 #The assignment statement
b += 1 #post-increment
c = “test”
import os,math #Import the os and math modules
from math import * #Imports all functions from the math module

4.Operators and Maths

Operators:

  • Arithmetic: +, -, *, /, and % (modulus)
  • Comparison: ==, !=, <, >, <=, >=
  • Logical: and, or, not
  • Exponentiation: **
  • Execution: os.system(‘ls -l’)
    #Requires import os
Maths: Requires import math
  • Absolute Value: a = abs(-7.5)
  • Arc sine: x = asin(0.5) #returns in rads
  • Ceil (round up): print(ceil(4.2))
  • Cosine: a = cos(x) #x in rads
  • Degrees: a = degrees(asin(0.5)) #a=30
  • Exp: y = exp(x) #y=e^x
  • Floor (round down): a = floor(a+0.5)
  • Log: x = log(y); #Natural Log
    x = log(y,5); #Base-5 log
  • Log Base 10: x = log10(y)
  • Max: mx = max(1, 7, 3, 4) #7
    mx = max(arr) #max value in array
  • Min: mn = min(3, 0, -1, x) #min value
  • Powers: x = pow(y,3) #x=y^3
  • Radians: a = cos(radians(60)) #a=0.5
  • Random #: Random number functions require import random
    random.seed() #Set the seed based on the system time.
    x = random() #Random number in the range [0.0, 1.0)
    y = randint(a,b) #Random integer in the range [a, b]
  • Round: print round(3.793,1; #3.8 – rounded to 1 decimal
    a = round(3.793,0) #a=4.0
  • Sine: a = sin(1.57) #in rads
  • Square Root: x = sqrt(10) #3.16…

  • Tangent: print tan(3.14)# #in rads

5.Strings

Strings can be specified using single quotes or double quotes. Strings do not expand escape sequences unless it is defined as a raw string by placing an r before the first quote: print ‘I\’ll be back.’.
print r’The newline \n will not expand’
a = “Gators”
print “The value of a is \t” + a

-> The value of a is       Gators
If a string is not defined as raw, escapes such as \n, \r, \t, \\, and \” may be used.
Optional syntax: Strings that start and end with “”” may span multiple lines: print “””
This is an example of a string in the heredoc syntax.
This text can span multiple lines
“””
String Operators:
Concatenation is done with the + operator.
Converting to numbers is done with the casting operations:
x = 1 + float(10.5) #$x=11.5, float
x = 4 – int(“3”) #$x=1, int
You can convert to a string with the str casting function:
s = str(3.5)
name = “Lee”
print name + “‘s number is ” + str(24)Comparing Strings:
Strings can be compared with the standard operators listed above: ==, !=, <, >, <=, and >=.

String Functions:


s = "Go Gators! Come on Gators!"

  • Extracting substrings: Strings in Python can be subscripted just
    like an array: s[4] = 'a'. Like in IDL,
    indices can be specified with slice notation i.e., two indices separated
    by a colon. This will return a substring containing characters index1 through
    index2-1. Indices can also be negative, in which case they count from the
    right, i.e. -1 is the last character. Thus substrings can be extracted
    like
    x = s[3:9] #x = "Gators"
    x = s[:2] #x = "Go"
    x = s[19:] #x = "Gators!"
    x = s[-7:-2] #x = "Gator"
    However, strings are immutable so s[2] = 'a' would cause an error.
  • int count(sub [,start[,end]]): returns the number of occurances
    of the substring sub in the string
    x = s.count("Gator") #x = 2
  • boolean endswidth(sub [,start[,end]]): returns true if the string ends with the specified substring and false otherwise:
    x = s.endswith("Gators") #x = False
  • int find(sub [,start[,end]]): returns the numeric position of the first occurance of sub in the string. Returns -1 if sub is not found.
    x = s.find("Gator") #x = 3
    x = s.find("gator") #x = -1
  • string join(array): combines elements of the string array into a
    single string and returns it. The separator between elements is the string
    providing this method.
    a = ['abc','def','ghi']
    t = "--"
    x = t.join(a) #x = abc--def--ghi
  • int len(string): returns the length of the string
    x = len(s) #x = 26
  • string lower(): returns a version of a string with all lower case lettters.
    print s.lower() #go gators! come on gators!
  • string replace(old, new [,count]): returns a copy of the string
    with all occurances of old replaced by new. If the optional count argument
    is given, only the first count occurances are replaced.
    x = s.replace("Gators","Tigers",1) #x = Go Tigers! Come on Gators!
  • int rfind(sub [,start[,end]]): same as find but returns the numeric
    position of the last occurance of sub in the string.
    x = s.rfind("Gator") #x = 19
  • array split([sep [,maxsplit]]): splits a single string into a
    string array using the separator defined. If no separator is defined,
    whitespace is used. Consecutive whitespace delimiters are then treated as
    one delimiter. Optionally you can specify the maximum number of splits so
    that the max size of the array would be maxsplit+1.
    a = s.split() #a=['Go', 'Gators!', 'Come', 'on', 'Gators!']
  • boolean startswidth(sub [,start[,end]]): returns true if the string
    starts with the specified substring and false otherwise:
    x = s.startswith("Go") #x = True
  • string strip([chars]): returns a copy of the string with leading
    and trailing characters removed. If chars (a string) is not specified,
    leading and trailing whitespace is removed.

 

  • string upper(): returns a version of a string with all upper case lettters.

6.Arrays


Arrays in basic Python are actually lists that can contain mixed datatype. However, the numarray module contains support for true arrays, including multi-dimensional arrays, as well as IDL-style array operations and the where function. To use arrays, you must import numarray or from numarray import *. Unfortunately, numarray generally only suports numeric arrays. Lists must be used for strings or objects. By importing numarray.strings and numarray.objects, you can convert string and object lists to arrays and use some of the numarray features, but only numeric lists are fully supported by numarray.

  • Creating lists: A list can be created by defining it with []. A numbered list can also be created with the range function which takes start and stop values and an increment.
    list = [2, 4, 7, 9]
    list2 = [3, “test”, True, 7.4]
    a = range(5)
    #a = [0,1,2,3,4]
    a = range(10,0,-2) #a = [10,8,6,4,2]
    An empty list can be initialized with [] and then the append command can be used to append data to the end of the list:
    a=[]
    a.append(“test”)
    a.append(5)
    print a
    -> [‘test’, 5]
    Finally, if you want a list to have a predetermined size, you can create a list and fill it with None’s:
    a=[None]*length
    a[5] = “Fifth”
    a[3] = 6
    print len(a)
    -> 10
    print a
    -> [None, None, None, 6, None, ‘Fifth’, None, None, None, None]
  • Removing from lists: The pop method can be used to remove any item from the list:
    a.pop(5)
    print a

    -> [None, None, None, 6, None, None, None, None, None]
  • Creating arrays: An array can be defined by one of four procedures: zeros, ones, arange, or array. zeros creates an array of a specified size containing all zeros:
    a = zeros(5) #a=[0 0 0 0 0]
    ones similarly creates an array of a certain size containing all ones:
    a = ones(5) #a=[1 1 1 1 1]
    arange works exactly the same as range, but produces an array instead of a list:
    a = arange(10,0,-2) #a = [10 8 6 4 2] And finally, array can be used to convert a list to an array. For instance, when reading from a file, you can create an empty list and take advantage of the append command and lists not having a fixed size. Then once the data is all in the list, you can convert it to an array:
    a = [1, 3, 9] #create a list and append it
    a.append(3)
    a.append(5)
    print a

    -> [1, 3, 9, 3, 5]
    a = array(a)
    print a

    -> [1 3 9 3 5]
  • Multi-dimensional lists: Because Python arrays are actually lists, you are allowed to have jagged arrays. Multi-dimensional lists are just lists of lists:
    a=[[0,1,2],[3,4,5]]
    print a[1]
    -> [3, 4, 5]
    s = [“Lee”, “Walsh”, “Roberson”]
    s2 = [“Williams”, “Redick”, “Ewing”, “Dockery”]
    s3 = [s, s2]
    print s3[1][2]
    -> Ewing
  • Multi-dimensional arrays: However, numarray does support true multi-dimensinoal arrays. These can be created through one of five methods: zeros, ones, array, arange, and reshape. zeros and ones work the same way as single dimensions except that they take a tuple of dimensions (a comma separated list enclosed in parentheses) instead of a single argument:
    a = zeros((3,5))
    a[1,2] = 8
    print a
    -> [[0 0 0 0 0]
    [0 0 8 0 0]
    [0 0 0 0 0]]
    b = ones((2,3,4)) #create a 2x3x4 array containing all ones.array works the same way as for 1-d arrays: you can create a list and then convert it to an array. Note with multi-dimensional arrays though, trying to use array to convered a jagged list into an array will cause an error. Lists must be rectangular to be able to be converted to arrays.
    s = [“Lee”, “Walsh”, “Roberson”, “Brewer”]
    s2 = [“Williams”, “Redick”, “Ewing”, “Dockery”]
    s3 = [s, s2]
    s4 = array(s3)
    print s4 + “test”
    -> [[‘Leetest’, ‘Walshtest’, ‘Robersontest’, ‘Brewertest’],
    [‘Williamstest’, ‘Redicktest’, ‘Ewingtest’, ‘Dockerytest’]]

    print s4[:,1:3]
    -> [[‘Walsh’, ‘Roberson’],
    [‘Redick’, ‘Ewing’]]
    arange also works the same as with 1-d arrays except you need to pass the shape parameter:
    a = arange(25, shape=(5,5)),br> And finally, reshape can be used to convert a 1-d array into a multi-dimensional array. To create a 5×5 array with the elements numbered from 0 to 24, you could use:
    b = arange(25)
    b = reshape(b,5,5)
  • Array Dimensions and Subscripts: When creating a multi-dimensional array, the format is ([[depth,] height,] width). Therefore, when accessing array elements in a two dimensional array, the row is listed first, then the column. When accessing an element of a two-dimensional list, the following notation must be used: list[i][j]. However, two dimensional arrays can also use the notation: array[i,j]. In fact, this is the preferred notation of the two for arrays because you cannot use wildcards in the first dimension of the array[i][j] notation (i.e., array[1:3][4] would cause an error whereas array[1:3,4] is valid).Wildcards can be used in array subscripts using the : , which is known as slicing. This is similar to IDL, with one major difference: if a=[0 1 2 3 4 5], in IDL a[1:4] = [1 2 3 4], but in Python, a[1:4] = [1 2 3]. In Python, when slicing array[i:j], it returns an array containing elements from i to j-1. Just like with strings, indices of arrays can be negative, in which case they count from the right instead of the left, i.e. a[-4:-1] = [2 3 4]. A : can also specify the rest of the elements or up to element, or all elements and arrays or lists can be used to subscript other arrays:
    print a[:3] #[0 1 2]
    print a[4:] #[4 5]
    print a[:] #[0 1 2 3 4 5]
    print a[[1,3,4]] #[1 3 4]
    Note that slicing in python does not create a new array but just a pointer to the original array. b=a[0:10] followed by b[0] = 5 also changes a[0] to 5. To avoid this, use b = copy(a[0:10])

Array Operators:

  • Concatenation:
    • Lists: a + b
      For Lists, the + operator appends the list on the right (b) to the list on the left.
      a = [“Roberson”, “Walsh”]
      b = [“Lee”, “Humphrey”]
      -> a+b = [“Roberson”, “Walsh”, “Lee”, “Humphrey”]
    • Arrays: concatenate((a,b)[,axis])
      For arrays, use the numarry function concatenate. It also allows you to specify the axis when concatenating multi-dimensional arrays.
      b = arange(5)
      print concatenate((b, arange(6)))
      -> [0 1 2 3 4 0 1 2 3 4 5]
      b=reshape(b,5,1)
      print concatenate((b,a),axis=1)
      -> [[0 0 0 0]
      [1 0 0 0]
      [2 0 8 0]
      [3 0 0 0]
      [4 0 0 0]]
  • Equality: a == b and Inequality: a != b
    For lists, these work the same as for scalars, meaning they can be used in if statments. For arrays, they return an array containing true or false for each array element.

Array Functions: All functions but len are for arrays only

  • len: returns the length of a list/array.

    s = ["Lee", "Walsh", "Roberson", "Brewer"]
    print len(s)
    #4
  • argmax([axis]): returns the index of the largest element in a 1D
    array or an array of the largest indices along the specified axis for a
    multi-dimensional array.

    a = array([[1,6,9], [2,4,0], [7,4,8]])
    print a.argmax(1)

    -> [2 1 2]
  • argmin([axis]): returns the index of the smallest element in a 1D
    array or an array of the smallest ndices along the specified axis for a
    multi-dimensional array.

    b = array([2,4,7,1,3,-1,5])
    print b.argmin()

    -> 5
  • argsort([axis]): returns an array of indices that allow access to
    the elements of the array in ascending order.
       print b.argsort()
    -> [5 3 0 4 1 6 2]
       print b[b.argsort()]
    -> [-1 1 2 3 4 5 7]
       print a.argsort(1)
    -> [[0 1 2]
    [2 0 1]
    [1 0 2]]
  • astype(type): returns a copy of the array converted to the
    specified type.
       a = a.astype('Float64')
    b = b.astype('Int32')
  • copy(): returns a copy of the array.
       c = a[:,2].copy()
    print c

    -> [9 0 8]
  • diagonal(): for multi-dimensional arrays, returns the diagonal
    elements of the array, where the row and column indices are equal.
       print a.diagonal()

    -> [1 4 8]
  • info(): prints informations about the array which may be useful
    for debugging.
  • max(): returns the largest element in the array
       print a.max()

    -> 9
  • mean(): returns the average of all elements in an array
       print a.mean()

    -> 4.55555555556
  • min(): returns the smallest element in the array
       print b.min()

    -> -1
  • nelements(): returns the total number of elements in the array
       print a.nelements()

    -> 9
  • product(array [,axis]): returns the product of an array or
    an array of the products along an axis of an array.
       print product(b)

    -> -840

       print product(a,1)

    -> [ 54 0 224]
  • reshape(array, shape): function that changes the shape of an
    array. But the new shape must have the same size as the old shape, otherwise
    an error will occur.
       c = reshape(a, 9)
    a = reshape(c,(3,3))
  • resize(shape): shrinks/grows the array to a new shape. Can be
    called as a method (replaces old array) or a function. The new shape
    does not have to be the same size as the old shape. If it is smaller,
    values will be cut off, and it if is bigger, values will repeat.
       a.resize(5)
    print a

    -> [1 6 9 2 4]
       a.resize(2,6)
    print a

    -> [[1 6 9 2 4 0]
    [7 4 8 1 6 9]]

       c = resize(a,(2,2))
    print c

    -> [[1 6]
    [9 2]]
  • shape(array): returns the dimensions of the array in a tuple

    print shape(a), shape(b), shape(a)[0]*shape(a)[1]

    -> (3,3)
    (7,)
    9

  • sort(array [,axis]): returns an array containing a copy of the data
    in the array and the elements sorted in increasing order. In the case of
    a multi-dimensional array, the data will be sorted along one axis and not
    across the whole array.
       print sort(b)

    -> [-1 1 2 3 4 5 7]
       print sort(a)

    -> [[1 6 9]
    [0 2 4]
    [4 7 8]]

       print sort(a,0)

    -> [[1 4 0]
    [2 4 8]
    [7 6 9]]
  • stddev(): returns the std deviation of all elements in the array
       print a.stddev()

    -> 3.16666666667
  • sum(): Can be called as a method or a function. The behavior is
    identical for 1-d arrays. But for multi-dimensional arrays, calling as
    a method returns the sum of the entire array, whereas calling it as a function
    allows you to specify an axis and returns an array with the sums along that
    axis.
       print a.sum()

    -> 41

       print sum(a)

    -> [10 14 17]

       print sum(a,1)

    -> [16 6 19]
  • trace(): Returns the sum of the diagonal elements of an array
       print a.trace()

    -> 13
  • type(): returns a string containing the type of the array.
       print a.type()

    -> Int32
  • tolist(): returns a list containing the same data as the array.
       c = a.tolist()
  • transpose(): Can be called as a method (replaces old array)
    or a function. Returns the transpose of the array.
       a.transpose()
    b = transpose(a)
  • where(expr, 1, 0): Similar to the IDL where function. Returns
    an array of the same size and dimensions containing 1 if the condition is
    true and 0 if the condition is false. Any value may be substituted for 1
    and 0, but they are the recommended values (i.e. true, false) so that
    compress can be used to extract values from the array:
    compress(mask_array, data_array).
       c = where(b > 2, 1, 0)
    print c

    -> [0 1 1 0 1 0 1]
       print compress(c,b)

    -> [4 7 3 5]
       c = where(a > 2, 1, 0)
    print c

    -> [[0 1 1]
    [0 1 0]
    [1 1 1]]

       print compress(c,a)

    -> [6 9 4 7 4 8]

 

 

  • Conditionals
    • if:
      if expr: statement
    • if-else:
      if expr: statement1
      else: statement2
    • if-elseif: if expr: statement1
      elif expr: statement2
      else: statement3Multiple elifs can be included in the same if statement. There is no switch or case statement so multiple elifs must be used instead. While parenthesis are not required around the expression, they can be used.
    Examples:

    if a > b: print "a is greater than b";
    if (a > b):
    print “a is greater than b”
    print “blocks are defined by indentation”
    elif (a < b):
    print “a is less than b”
    else:
    print “a is equal to b”
  • Loops
    • for: for var in range(start [,stop [,inc]]): statements
      Not unsimilar to IDL and basic, except for the range statement. var can be any variable. The range statement can take start and stop values, and an increment.
    • while: while expr: statements
      Executes statements while the expression is true.
    • continue: continue
      Skips the rest of the body of the loop for the current iteration and continue execution at the beginning of the next iteration.
    • break: break
      Ends the execution of the current loop.
    • else: else
      for and while loops can both have else clauses, which are executed after the loop terminates normally by falsifying the conditional, but else clauses are not executed when a loop terminates via a break statement.
    • foreach: for x in array: statements
      Loops over the array given by array. On each iteration, the value of the current element is assigned to x and the internal array pointer is advanced by one.
    Examples:


    for j in range(10): print "Value number " + str(j) +" is "+value[j]
    for j in range(10,0,-2):
    x = x + j
    print xwhile (b < a):
    print “b is less than a.”
    b=b+1for j in range(0,10):
    while(k < j):
    print “j = ” + str(j) + ” k = “+str(k)
    if (j == 1): break
    k=k+1
    print “j equals k or j equals 1”a = [“abc”,”def”,”ghi”]
    for x in a:
    print x


  • Functions
    • Definition: Functions in Python are defined with the following syntax:
      def funct(arg_11, arg_2, …, arg_n):
      print “This is a function.”
      return value
      Any Python code, including other function and class definitions, may appear inside a function. Functions may also be defined within a conditional, but in that case the function’s definition must be processed prior to its being called. Python does not support function overloading but does support variable number of arguments, default arguments, and keyword arguments. Return types are not specified by functions.
    • Arguments: Function arguments are passed by value so that if you change the value of the argument within the function, it does not get changed outside of the function. If you want the function to be able to modify non-local variables, you must declare them as global in the first line of the function. Note that if you declare any variables as global, that name cannot be reused in the argument list, i.e. this would cause an error:
      function double(x):
      global x
      x = x*2
      return
      double(x)
      Instead this could be done
      function double(n):
      n = n * 2
      return n
      x = double(x)

      Or
      function doubleX():
      global x
      x = x * 2
      return
      doubleX()
    • Default Arguments: A function may define default values for arguments. The default must be a constant expression or array and any defaults should be on the right side of any non-default arguments.
      def square(x = 5):
      return x*x
      If this function is called with square(), it will return 25. Otherwise, if it is called with square(n) , it will return n^2.
    • Variable length argument lists: Variable length arguments are supported by being wrapped up in a tuple. Before the variable number of arguments, zero or more normal arguments may occur:
      def var_args(arg1, arg2, *args):
    • Keyword arguments: Functions can also be called using arguments of the form keyword = value:
      def player(name, number, team=”Florida”):
      print(name + “wears number ” + str(number) + “for ” + team)
      player(“Matt Walsh”, 44)
      player(number = 44, name = “David Lee”)
      player(“Anthony Roberson”, number = 1)
      player(name = “J.J. Redick”, number = 4, team = “Duke”)
    • Return: Values are returned from the function with the return command: return var. You can not return multiple values, but that can be achieved by returning an array or object. Return immediately ends execution of the function and passes control back to the line from which it was called.
    • Variable Functions: Python supports the concept of variable functions. That means that if a variable can point to a function instead of a value. Objects within a method can be called similarly.
      def test():
      print ‘This is a test.’
      var = test
      var()
      #this calles test()
      var = circle.setRadius
      var(3)
      #this calls circle.setRadius(3)
  • Classes and OOP
    Python supports OOP and classes to an extent, but is not a full OOP language. A class is a collection of variables and functions working with these variables. Classes are defined somewhat similarly to Java, but differences include self being used in place of this and constructors being named __init__ instead of classname. Also note that self must be used every time a class-wide variable is referenced and must be the first argument in each function’s argument list, including the constructor. In addition, functions and constructors cannot be overloaded, but as discussed above, do support default arguments instead. Like functions, a class must be defined before it can be instantiated. In Python, all class members are public.

    • Initializing vars: Only constant initializers for class variables are allowed (n = 1). To initialize variables with non-constant values, you must use the constructor. You cannot declare unitialized variables.
    • Encapsulation: Python does not really support encapsulation because it does not support data hiding through private and protected members. However some pseudo-encapsulation can be done. If an identifier begins with a double underline, i.e. __a, then it can be referred to within the class itself as self.__a, but outside of the class, it is named instance._classname__a. Therefore, while it can prevent accidents, this pseudo-encapsulation cannot really protect data from hostile code.
    • Inheritence: Python allows classes to be extended (see right) by adding the base class name in parenthesis after the derived class name: class Derived(Base):. The child class takes all the variables and functions from the parent class and can extend that class by adding additional variables and adding or overriding functions. If class B extends class A, then A or B can be used anywhere an A is expected, but only B can be used where a B is expected because it contains additional information/methods not found in A. In addition, Python supports multiple inheritence: class Derived(Base1, Base2, Base3):
    • Abstract classes: Abstract classes and interfaces are not supported in Python. In Python, there is no difference between an abstract class and a concrete class. Abstract classes create a template for other classes to extend and use. Instances can not be created of abstract classes but they are very useful when working with several objects that share many characteristics. For instance, when creating a database of people, one could define the abstract class “Person”, which would contain basic attributes and functions common to all people in the database. Then child classes such as “SinglePerson”, “MarriedCouple”, or “Athlete” could be created by extending “Person” and adding appropriate variables and functions. The database could then be told to expect every entry to be an object of type “Person” and thus any of the child classes would be a valid entry. In Python, you could create a class Person and extend it with the child classes listed above, but you could not prevent someone from instantiating the Person class.
    • Parent: The parent keyword is not supported by Python, but you can call methods from the base classes directly: BaseClass.method_name(self, arguments) (see right).
    • Constructors: Constructors are fuctions that are automatically called when you create a new instance of a class. They can be used for initialization purposes. A function is a constructor when it has the name __init__. When extending classes, if a new constructor is not defined, the constructor from the parent class is used (see right). When an object of type RectWithPerimeter is created, the constructor from Rectangle is called. If however, I were to add a function in RectWithPerimeter with the name __init__ , then that function would be used as its constructor.
    • Comparing Objects: Objects can be compared using the == and != operators. Two objects are equal only if they are the same instance of the same object. Even if two objects have the same attributes and values and are instances of the same class, they are not equal if the are separate instances.
    Example Class:
    class Rectangle:
    #Optionally define variable width
    width = 0
    #Constructor with default arguments
    def __init__(self, width = 0, height = 0):
    self.width = width
    self.height = height
    #functions
    def setWidth(self, width):
    self.width = width
    def setHeight(self, height):
    self.height = height
    def getArea(self):
    return self.width * self.height
    arect = Rectangle()#create a new Rectangle with dimensions 0x0.
    arect.setWidth(4)
    arect.setHeight(6)
    print arect.getArea()

      -> 24
    rect2 = Rectangle(7,3) #new Rectangle with dimensions 7×3.Extended Class:
    class RectWithPerimeter(Rectangle):
    #add new functions
    def getPerimeter(self):
    return 2*self.height + 2*self.width
    def setDims(self, width, height):
    #call base class methods from Rectangle
    Rectangle.setWidth(self, width)
    Rectangle.setHeight(self, height)
    arect = RectWithPerimeter(6,5)
    #Uses the constructor from Rectangle because no new constructor is provided to override it.
    print arect.getArea() #Uses the getArea function from Rectangle and prints 30.
    print arect.getPerimeter() #Uses getPerimeter from RectWithPerimeter and prints 22.
    arect.setDims(4,9) #Use setDims to change the dimensions.
  • File I/O
    • Opening Files: file open(string filename, string mode):
      open can be used to open files for reading, writing, and appending. It binds a named file object to a stream that can then be used to read/write data. Possible modes include:

      • ‘r’: Open for reading.
      • ‘w’: Open for writing. Any existing data will be overwritten.
      • ‘a’: Open for writing. New data will be appended to existing data.
      • ‘b’: Use this flag when working with binary files (e.g. ‘rb’).
    • Checking Files: Python supports several methods of checking if a file exists and checking its properties:
      • bool os.access(string path, int mode): returns TRUE if the filename exists and matches the mode query. The mode query can be any of the following constants:
        • os.F_OK: test the existence of path
        • os.R_OK: tests if path exists and is readable
        • os.W_OK: tests if path exists and is writable
        • os.X_OK: tests if path exists and is executable
    • File Operations: Python also supports file operations such as renaming and deleting files. And of course any shell command can be excecuted via os.system.
      • bool os.system(string command): attempts to execute the supplied shell command and returns true if the command executed.
      • bool chmod(string path, int mode): Changes the permissions of path to mode. Mode should be defined as an octal (i.e. 0644 or 0777).
      • list listdir(string path): Returns a list containing all the files in the current directory. The special entries “.” and “..” are not included.
      • bool mkdir(string pathname [, int mode]): Makes a directory pathname with permissions mode (e.g. mkdir(‘new_dir’, 0700);)
      • bool remove(String filename): Deletes filename
      • bool rename(string oldname, string newname): Renames a file
      • bool symlink(string target, string link): Creates a symbolic link to the existing target with name link.
    • Reading Files: Files can be read by several methods.
      • string read([int length]): Reads up to a specified number of bytes from the file into a string. It will read until it encounters EOF or the specified length is reached (default is all data).
      • string readline([int length]): Reads one entire line from a file, or up to length bytes, into a string. Reading stops when length bytes have been read or a newline or EOF is reached. A trailing newline character is kept in the string (but may be absent on the last line of the file).
      • list readlines([int sizehint]): Reads from a file using readline() until EOF and returns a list containing the lines read. If sizehint is present, whole lines totaling approximately sizehint bytes are read.
    • EOF: end-of-file is reached when read or readline returns an empty string. while (s != “”):
      s = f.readline()
      do_something
    • Writing to files: Files that have been opened for writing with open can be written to by two methods.
      • void write(string string): Writes the contents of string to the file. Does not append a newline character to the string. Only strings can be written so other datatypes must be converted to strings.
      • void writelines(list data): Writes a list or array of strings to the file. Newlines will not be added between the elements of the list/array.
    • Concurrency: File locking is available through the flock method in the fcntl module. Though be warned, flock does not work reliably on all operating systems. Therefore you may want to develop your own semaphores instead. The syntax is: flock(fileDescriptor fd, int operation), where the file descriptor can be obtained by calling the fileno() method of a file object and operation can be LOCK_SH to acquire a shared lock (reader), LOCK_EX to acquire an exclusive lock (writer), LOCK_UN to release a lock, or LOCK_NB if you don’t want flock to block while locking.
    • Serializing Objects: An object can be serialized with methods in the pickle module. This will create a string representation of the object that can be stored in a file and later reconstructed into the object. In this way, ints, floats, or any object can be written to a file in addition to strings. If the object is an instance of a class, that class must be defined or imported in the python program that unserializes the object (i.e. if you have an object of type A in a.py, serialize it, write it to a file, and on b.py you read it back in from the file, then class A must be defined in b.py or included via import a to unserialize the object. An easy solution is to put the definition of class A in a file to be imported in both a.py and b.py). Arrays can be serialized as well. If you have an object x, you can serialize it and save it to a file:
      f = open(“file.dat”,”wb”)
      pickle.dump(x,f)
      f.close()
      It can then be unserialized and restored by:
      f = open(“file.dat”,”rb”)
      y = pickle.load(f)
      f.close()
    • Sockets: To use sockets in Python, import socket . A server socket can then be opened with:
      mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      mySocket.bind((”, 2727))
      mySocket.listen(1)
      The first line creates a socket object. The second line binds the socket to an address. In this case, ” is a symbolic name meaning localhost and we select port 2727. The address parameter should be in the form of a tuple as shown above. Finally, the third line listens for connections made to a socket. The argument is the maximum number of queued connections. Now that a server socket is open, we need to be able to accept data:
      conn, addr = mySocket.accept()
      print ‘Connected with ‘, addr
      while True:
      data = conn.recv(1024)
      if not data: break
      print data
      conn.send(“Data received”)
      conn.close()
      The accept() method accepts a connection and returns a pair (conn, address), where conn is a new socket object usable to send and receive data on the connection and address is the address bound to the socket on the other end (client side) of the connection. We then enter a loop and receive data from the client using the recv(bufsize) method. recv returns a string of the data received with a maximum amount of data specified by bufsize. If data is false, we break out of the loop. Otherwise we print the data and use send(string) to send a message back to the client.
      Now our server is complete, but we need a client-side socket:
      cSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      cSocket.connect((“polaris.astro.ufl.edu”,”2727″))
      The first line of course creates a socket object. The second line is similar to bind except that it connects to an existing server socket specified by the address. Note that (“localhost”, 2727) would be another valid address. Now we need to send and receive data:
      cSocket.send(“Hello world!”)
      data = cSocket.recv(1024)
      cSocket.close()
      print data
      send and recv work just the same as they do in the server socket. We send data to the server (“Hello world!”), receive the response (“Data received”), close the connection (which causes data to become false on the server program and terminate the loop), and print out the data.
    Examples:
    file = open(“data/teams.txt”,”rb”)
    team = “nonempty”
    while (team != “”):
    team = file.readline()
    if (team != “”): print team[:-1]   #get rid of extra newline character
    file.close()
    file = open(“data/teams.txt”,”rb”)
    team = file.readlines()
    file.close()list = [“Florida”,”Clemson”,”Duke”]
    file = open(“data/teams.txt”,”wb”)
    for j in list: file.write(j+”\n”)
    file.close()import pickle, fcntl
    player = Player(“J.J. Redick”, “Duke”, 4)
    file = open(“data/players.txt”, “a”)
    fcntl.flock(file.fileno(), fcntl.LOCK_EX)
    pickle.dump(player, file)
    fcntl.flock(file.fileno(), fcntl.LOCK_UN)
    file.close()
  • Images in Python
    FITS files: Python supports FITS files via the module pyfits. Once this module has been imported, you can read and write FITS files. FITS files are read and stored in and HDUList object, which has two components: header and data. The header is a list-like object and data is usually an array. To read in a FITS file, use

    • HDUList open(string): open a filename
    • info(): print a summary of the objects in the file.
      Note that FITS files can have what are called multiple extensions– multiple images and/or headers in a single file. info will list all objects in the file, their name, type, cards (number of entries in the header), dimensions, and format (i.e., Int16 or Float32).

    Now that you have a FITS object, you can access its header and data. Since each object within a file can have its own header and data, you would access the primary header as x[0].header and the data as x[0].data.

    Headers:You can print the entire header by calling the x[0].header.ascardlist() method. You can access individual elements in the header directly by keyword (x[0].header[‘NAXIS1’]) or by index (x[0].header[3]). If you know that a keyword is already present in the header, you can update its value using the same notation:
    x[0].header[‘NAXIS1’] = 265
    But if the keyword might not be present and you want to add it if it isn’t, use the update() method instead:
    x[0].header.update(‘NAXIS1’,265)

    Data: Since the data is an array, you can use any numarray methods on it. The data can thus be accessed using slice notation as well.
    print shape(x[0].data)
    print x[0].data[0:5,0:5]

    Writing FITS Files: Once the data and header have been modified, you can write them back to a new FITS file using writeto(string). This writes to a new file and closes that file, but further operations can still be done on the data in memory. Note that if a file exists with the specified name, it will NOT be overwritten and an error will be raised. To close the input file, use x.close().

    Examples:
    x = pyfits.open(“NGC3031.fits”)
    x.info()

    -> Filename: NGC3031.fits
    No.  Name     Type    Cards Dimensions Format
    0  PRIMARY PrimaryHDU    6  (530, 530)  UInt8

    print x[0].header.ascardlist()

    -> SIMPLE  =                    T
    BITPIX  =                    8
    NAXIS   =                    2
    NAXIS1  =                  530
    NAXIS2  =                  530
    HISTORY Written by XV 3.10a

    print x[0].header[‘NAXIS1’]
    -> 530
    print x[0].header[3]
    -> 530

    print x[0].data[3,0:5]
    -> [11 11 11 9 9]

    x[0].data[3,0:3] = array([0,0,0])
    print x[0].data[3,0:5]

    -> [0 0 0 9 9]

    x[0].data += 5 #using numarray to operate on entire array
    print x[0].data[3,0:5]
    -> [ 5 5 5 14 14]

    x.writeto(“new_file.fits”)
    x.close()

  • Guess My Number

    Here is the code for Guess My Number in Python, a program that generates a random number between 1 and 100 and asks the user to guess it. It will tell the user if the number is higher or lower after each guess and keep track of the number of guesses.

    #!/usr/bin/python
    import random, math
    random.seed()
    x = math.floor(random.random()*100)+1
    z = 0
    b = 0
    while x != z:
       b=b+1
       z = input("Guess My Number: ")
       if z < x: print("Higher!")
       if z > x: print("Lower!")
    print("Correct! " + str(b) + " tries.")
    
    
  • Python reference
    Python.org: includes an introductory tutorial and a full manual.
    Numarray homepage: includes a full manual about numarray features.

 

Data types in Python or Different Types of Objects in Python

Every language has its own data types and data structures. Depending upon the data when we create a variable that is known as static typing.Where we can not able to store any other types of data into the variable.But pyhton not operate the same way.It has the concept of dynamic typing where the data can be stored in the different data objects.We will go through the data types and the operation available for the particular data types.

Python has below standard Data Types:

  • Numbers
  • String
  • List
  • Set
  • Tuple
  • Dictionary
  • Sets
  • ByteArray

Python sets the variable type based on the value that is assigned to it. Unlike more riggers languages, Python will change the variable type if the variable value is set to another value. For example:

var = 123 # This will create a number integer assignment
var = 'john' # the `var` variable is now a string type.

Numbers

Python numbers variables are created by the standard Python method:

var = 382

Most of the time using the standard Python number type is fine. Python will automatically convert a number from one type to another if it needs. But, under certain circumstances that a specific number type is needed (ie. complex, hexidecimal), the format can be forced into a format by using additional syntax in the table below:

Type Format Description
int a = 10 Signed Integer
long a = 345L (L) Long integers, they can also be represented in octal and hexadecimal
float a = 45.67 (.) Floating point real values
complex a = 3.14J (J) Contains integer in the range 0 to 255.

Most of the time Python will do variable conversion automatically. You can also use Python conversion functions (int(), long(), float(), complex()) to convert data from one type to another. In addition, the type function returns information about how your data is stored within a variable.

message = "Good morning"
num = 85
pi = 3.14159

print(type(message))  # This will return a string
print(type(n))  # This will return an integer
print(type(pi))  # This will return a float

String

Create string variables by enclosing characters in quotes. Python uses single quotes ' double quotes " and triple quotes """ to denote literal strings. Only the triple quoted strings """ also will automatically continue across the end of line statement.

firstName = 'john'
lastName = "smith"
message = """This is a string that will span across multiple lines. Using newline characters
and no spaces for the next lines. The end of lines within this string also count as a newline when printed"""

Strings can be accessed as a whole string, or a substring of the complete variable using brackets ‘[]’. Here are a couple examples:

var1 = 'Hello World!'
var2 = 'RhinoPython'

print var1[0] # this will print the first character in the string an `H`
print var2[1:5] # this will print the substring 'hinoP`

Python can use a special syntax to format multiple strings and numbers. The string formatter is quickly covered here because it is seen often and it is important to recognize the syntax.

print "The item {} is repeated {} times".format(element,count))

The {} are placeholders that are substituted by the variables element and count in the final string. This compact syntax is meant to keep the code more readable and compact.

Python is currently transitioning to the format syntax above, but python can use an older syntax, which is being phased out, but is still seen in some example code:

print "The item %i is repeated %i times"% (element,count)

For more information on the string formatter in Python see the article: PyFormat Website

For a more detailed look at string variables in Python, see the Tutorialspoint Python Tutorial on stings. Note: this article does use the older formatting syntax, but has a lot useful information.

String Formatting Operator

One of Python’s coolest features is the string format operator %. This operator is unique to strings and makes up for the pack of having functions from C’s printf() family. Following is a simple example −

#!/usr/bin/python

print "My name is %s and weight is %d kg!" % ('Zara', 21) 

When the above code is executed, it produces the following result −

My name is Zara and weight is 21 kg!

Here is the list of complete set of symbols which can be used along with % −

Format Symbol Conversion
%c character
%s string conversion via str() prior to formatting
%i signed decimal integer
%d signed decimal integer
%u unsigned decimal integer
%o octal integer
%x hexadecimal integer (lowercase letters)
%X hexadecimal integer (UPPERcase letters)
%e exponential notation (with lowercase ‘e’)
%E exponential notation (with UPPERcase ‘E’)
%f floating point real number
%g the shorter of %f and %e
%G the shorter of %f and %E

Other supported symbols and functionality are listed in the following table −

Symbol Functionality
* argument specifies width or precision
left justification
+ display the sign
<sp> leave a blank space before a positive number
# add the octal leading zero ( ‘0’ ) or hexadecimal leading ‘0x’ or ‘0X’, depending on whether ‘x’ or ‘X’ were used.
0 pad from left with zeros (instead of spaces)
% ‘%%’ leaves you with a single literal ‘%’
(var) mapping variable (dictionary arguments)
m.n. m is the minimum total width and n is the number of digits to display after the decimal point (if appl.)
String Special Operators

Assume string variable a holds ‘Hello’ and variable b holds ‘Python’, then −

Operator Description Example
+ Concatenation – Adds values on either side of the operator a + b will give HelloPython
* Repetition – Creates new strings, concatenating multiple copies of the same string a*2 will give -HelloHello
[] Slice – Gives the character from the given index a[1] will give e
[ : ] Range Slice – Gives the characters from the given range a[1:4] will give ell
in Membership – Returns true if a character exists in the given string H in a will give 1
not in Membership – Returns true if a character does not exist in the given string M not in a will give 1
r/R Raw String – Suppresses actual meaning of Escape characters. The syntax for raw strings is exactly the same as for normal strings with the exception of the raw string operator, the letter “r,” which precedes the quotation marks. The “r” can be lowercase (r) or uppercase (R) and must be placed immediately preceding the first quote mark. print r’\n’ prints \n and print R’\n’prints \n
% Format – Performs String formatting See at next section

Built-in String Methods

Python includes the following built-in methods to manipulate strings −

SN Methods with Description
1 capitalize()
Capitalizes first letter of string
2 center(width, fillchar)

Returns a space-padded string with the original string centered to a total of width columns.

3 count(str, beg= 0,end=len(string))

Counts how many times str occurs in string or in a substring of string if starting index beg and ending index end are given.

4 decode(encoding=’UTF-8′,errors=’strict’)

Decodes the string using the codec registered for encoding. encoding defaults to the default string encoding.

5 encode(encoding=’UTF-8′,errors=’strict’)

Returns encoded string version of string; on error, default is to raise a ValueError unless errors is given with ‘ignore’ or ‘replace’.

6 endswith(suffix, beg=0, end=len(string))
Determines if string or a substring of string (if starting index beg and ending index end are given) ends with suffix; returns true if so and false otherwise.
7 expandtabs(tabsize=8)

Expands tabs in string to multiple spaces; defaults to 8 spaces per tab if tabsize not provided.

8 find(str, beg=0 end=len(string))

Determine if str occurs in string or in a substring of string if starting index beg and ending index end are given returns index if found and -1 otherwise.

9 index(str, beg=0, end=len(string))

Same as find(), but raises an exception if str not found.

10 isalnum()

Returns true if string has at least 1 character and all characters are alphanumeric and false otherwise.

11 isalpha()

Returns true if string has at least 1 character and all characters are alphabetic and false otherwise.

12 isdigit()

Returns true if string contains only digits and false otherwise.

13 islower()

Returns true if string has at least 1 cased character and all cased characters are in lowercase and false otherwise.

14 isnumeric()

Returns true if a unicode string contains only numeric characters and false otherwise.

15 isspace()

Returns true if string contains only whitespace characters and false otherwise.

16 istitle()

Returns true if string is properly “titlecased” and false otherwise.

17 isupper()

Returns true if string has at least one cased character and all cased characters are in uppercase and false otherwise.

18 join(seq)

Merges (concatenates) the string representations of elements in sequence seq into a string, with separator string.

19 len(string)

Returns the length of the string

20 ljust(width[, fillchar])

Returns a space-padded string with the original string left-justified to a total of width columns.

21 lower()

Converts all uppercase letters in string to lowercase.

22 lstrip()

Removes all leading whitespace in string.

23 maketrans()

Returns a translation table to be used in translate function.

24 max(str)

Returns the max alphabetical character from the string str.

25 min(str)

Returns the min alphabetical character from the string str.

26 replace(old, new [, max])

Replaces all occurrences of old in string with new or at most max occurrences if max given.

27 rfind(str, beg=0,end=len(string))

Same as find(), but search backwards in string.

28 rindex( str, beg=0, end=len(string))

Same as index(), but search backwards in string.

29 rjust(width,[, fillchar])

Returns a space-padded string with the original string right-justified to a total of width columns.

30 rstrip()

Removes all trailing whitespace of string.

31 split(str=””, num=string.count(str))

Splits string according to delimiter str (space if not provided) and returns list of substrings; split into at most num substrings if given.

32 splitlines( num=string.count(‘\n’))

Splits string at all (or num) NEWLINEs and returns a list of each line with NEWLINEs removed.

33 startswith(str, beg=0,end=len(string))

Determines if string or a substring of string (if starting index beg and ending index end are given) starts with substring str; returns true if so and false otherwise.

34 strip([chars])

Performs both lstrip() and rstrip() on string

35 swapcase()

Inverts case for all letters in string.

36 title()

Returns “titlecased” version of string, that is, all words begin with uppercase and the rest are lowercase.

37 translate(table, deletechars=””)

Translates string according to translation table str(256 chars), removing those in the del string.

38 upper()

Converts lowercase letters in string to uppercase.

39 zfill (width)

Returns original string leftpadded with zeros to a total of width characters; intended for numbers, zfill() retains any sign given (less one zero).

40 isdecimal()

Returns true if a unicode string contains only decimal characters and false otherwise.

List

Lists are a very useful variable type in Python. A list can contain a series of values. List variables are declared by using brackets [ ] following the variable name.

A = [ ] # This is a blank list variable
B = [1, 23, 45, 67] # this list creates an initial list of 4 numbers.
C = [2, 4, 'john'] # lists can contain different variable types.

All lists in Python are zero-based indexed. When referencing a member or the length of a list the number of list elements is always the number shown plus one.

mylist = ['Rhino', 'Grasshopper', 'Flamingo', 'Bongo']
B = len(mylist) # This will return the length of the list which is 3. The index is 0, 1, 2, 3.
print mylist[1] # This will return the value at index 1, which is 'Grasshopper'
print mylist[0:2] # This will return the first 3 elements in the list.

You can assign data to a specific element of the list using an index into the list. The list index starts at zero. Data can be assigned to the elements of an array as follows:

mylist = [0, 1, 2, 3]
mylist[0] = 'Rhino'
mylist[1] = 'Grasshopper'
mylist[2] = 'Flamingo'
mylist[3] = 'Bongo'
print mylist[1]

Lists aren’t limited to a single dimension. Although most people can’t comprehend more than three or four dimensions. You can declare multiple dimensions by separating an with commas. In the following example, the MyTable variable is a two-dimensional array :

MyTable = [[], []]

In a two-dimensional array, the first number is always the number of rows; the second number is the number of columns.

For a detailed look at managing lists, take a look at the article

  • Python Lists – Google developer
  • TutorialPoint Python Lists

Built-in List Functions & Methods:

Python includes the following list functions −

SN Function with Description
1 cmp(list1, list2)

Compares elements of both lists.

2 len(list)

Gives the total length of the list.

3 max(list)

Returns item from the list with max value.

4 min(list)

Returns item from the list with min value.

5 list(seq)

Converts a tuple into list.

Python includes following list methods

SN Methods with Description
1 list.append(obj)

Appends object obj to list

2 list.count(obj)

Returns count of how many times obj occurs in list

3 list.extend(seq)

Appends the contents of seq to list

4 list.index(obj)

Returns the lowest index in list that obj appears

5 list.insert(index, obj)

Inserts object obj into list at offset index

6 list.pop(obj=list[-1])

Removes and returns last object or obj from list

7 list.remove(obj)

Removes object obj from list

8 list.reverse()

Reverses objects of list in place

9 list.sort([func])

Tuple

Tuples are a group of values like a list and are manipulated in similar ways. But, tuples are fixed in size once they are assigned. In Python the fixed size is considered immutable as compared to a list that is dynamic and mutable. Tuples are defined by parenthesis ().

myGroup = ('Rhino', 'Grasshopper', 'Flamingo', 'Bongo')

Here are some advantages of tuples over lists:

  1. Elements to a tuple. Tuples have no append or extend method.
  2. Elements cannot be removed from a tuple.
  3. You can find elements in a tuple, since this doesn’t change the tuple.
  4. You can also use the in operator to check if an element exists in the tuple.
  5. Tuples are faster than lists. If you’re defining a constant set of values and all you’re ever going to do with it is iterate through it, use a tuple instead of a list.
  6. It makes your code safer if you “write-protect” data that does not need to be changed.

It seems tuples are very restrictive, so why are they useful? There are many datastructures in Rhino that require a fixed set of values. For instance a Rhino point is a list of 3 numbers [34.5, 45.7, 0]. If this is set as tuple, then you can be assured the original 3 number structure stays as a point (34.5, 45.7, 0). There are other datastructures such as lines, vectors, domains and other data in Rhino that also require a certain set of values that do not change. Tuples are great for this.

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also. For example −

tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";

The empty tuple is written as two parentheses containing nothing −

tup1 = ();

To write a tuple containing a single value you have to include a comma, even though there is only one value −

tup1 = (50,);

Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on.

Accessing Values in Tuples:

To access values in tuple, use the square brackets for slicing along with the index or indices to obtain value available at that index. For example −

#!/usr/bin/python

tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5, 6, 7 );

print "tup1[0]: ", tup1[0]
print "tup2[1:5]: ", tup2[1:5]

When the above code is executed, it produces the following result −

tup1[0]:  physics
tup2[1:5]:  [2, 3, 4, 5]

Updating Tuples

Tuples are immutable which means you cannot update or change the values of tuple elements. You are able to take portions of existing tuples to create new tuples as the following example demonstrates −

#!/usr/bin/python

tup1 = (12, 34.56);
tup2 = ('abc', 'xyz');

# Following action is not valid for tuples
# tup1[0] = 100;

# So let's create a new tuple as follows
tup3 = tup1 + tup2;
print tup3

When the above code is executed, it produces the following result −

(12, 34.56, 'abc', 'xyz')

Delete Tuple Elements

Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting together another tuple with the undesired elements discarded.

To explicitly remove an entire tuple, just use the del statement. For example:

#!/usr/bin/python

tup = ('physics', 'chemistry', 1997, 2000);

print tup
del tup;
print "After deleting tup : "
print tup

This produces the following result. Note an exception raised, this is because after del tup tuple does not exist any more −

('physics', 'chemistry', 1997, 2000)
After deleting tup :
Traceback (most recent call last):
  File "test.py", line 9, in <module>
    print tup;
NameError: name 'tup' is not defined

Basic Tuples Operations

Tuples respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new tuple, not a string.

In fact, tuples respond to all of the general sequence operations we used on strings in the prior chapter −

Python Expression Results Description
len((1, 2, 3)) 3 Length
(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) Concatenation
(‘Hi!’,) * 4 (‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’) Repetition
3 in (1, 2, 3) True Membership
for x in (1, 2, 3): print x, 1 2 3 Iteration

Indexing, Slicing, and Matrixes

Because tuples are sequences, indexing and slicing work the same way for tuples as they do for strings. Assuming following input −

L = ('spam', 'Spam', 'SPAM!')
Python Expression Results Description
L[2] ‘SPAM!’ Offsets start at zero
L[-2] ‘Spam’ Negative: count from the right
L[1:] [‘Spam’, ‘SPAM!’] Slicing fetches sections

No Enclosing Delimiters

Any set of multiple objects, comma-separated, written without identifying symbols, i.e., brackets for lists, parentheses for tuples, etc., default to tuples, as indicated in these short examples −

#!/usr/bin/python

print 'abc', -4.24e93, 18+6.6j, 'xyz'
x, y = 1, 2;
print "Value of x , y : ", x,y

When the above code is executed, it produces the following result −

abc -4.24e+93 (18+6.6j) xyz
Value of x , y : 1 2

Built-in Tuple Functions

Python includes the following tuple functions −

SN Function with Description
1 cmp(tuple1, tuple2)

Compares elements of both tuples.

2 len(tuple)

Gives the total length of the tuple.

3 max(tuple)

Returns item from the tuple with max value.

4 min(tuple)

Returns item from the tuple with min value.

5 tuple(seq)

Converts a list into tuple.

Dictionary

Dictionaries in Python are lists of Key:Value pairs. This is a very powerful datatype to hold a lot of related information that can be associated through keys. The main operation of a dictionary is to extract a value based on the key name. Unlike lists, where index numbers are used, dictionaries allow the use of a key to access its members. Dictionaries can also be used to sort, iterate and compare data.

Dictionaries are created by using braces ({}) with pairs separated by a comma (,) and the key values associated with a colon(:). In Dictionaries the Key must be unique. Here is a quick example on how dictionaries might be used:

room_num = {'john': 425, 'tom': 212}
room_num['john'] = 645  # set the value associated with the 'john' key to 645
print (room_num['tom']) # print the value of the 'tom' key.
room_num['isaac'] = 345 # Add a new key 'isaac' with the associated value
print (room_num.keys()) # print out a list of keys in the dictionary
print ('isaac' in room_num) # test to see if 'issac' is in the dictionary.  This returns true.

Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}.

Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.

Accessing Values in Dictionary:

To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value. Following is a simple example −

#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}

print "dict['Name']: ", dict['Name']
print "dict['Age']: ", dict['Age']

When the above code is executed, it produces the following result −

dict['Name']:  Zara
dict['Age']:  7

If we attempt to access a data item with a key, which is not part of the dictionary, we get an error as follows −

#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}

print "dict['Alice']: ", dict['Alice']

When the above code is executed, it produces the following result −

dict['Alice']:
Traceback (most recent call last):
   File "test.py", line 4, in <module>
      print "dict['Alice']: ", dict['Alice'];
KeyError: 'Alice'

Updating Dictionary

You can update a dictionary by adding a new entry or a key-value pair, modifying an existing entry, or deleting an existing entry as shown below in the simple example −

#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}

dict['Age'] = 8; # update existing entry
dict['School'] = "DPS School"; # Add new entry


print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']

When the above code is executed, it produces the following result −

dict['Age']:  8
dict['School']:  DPS School

Delete Dictionary Elements

You can either remove individual dictionary elements or clear the entire contents of a dictionary. You can also delete entire dictionary in a single operation.

To explicitly remove an entire dictionary, just use the del statement. Following is a simple example −

#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}

del dict['Name']; # remove entry with key 'Name'
dict.clear();     # remove all entries in dict
del dict ;        # delete entire dictionary

print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']

This produces the following result. Note that an exception is raised because after del dict dictionary does not exist any more −

dict['Age']:
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    print "dict['Age']: ", dict['Age'];
TypeError: 'type' object is unsubscriptable

Note: del() method is discussed in subsequent section.

Properties of Dictionary Keys

Dictionary values have no restrictions. They can be any arbitrary Python object, either standard objects or user-defined objects. However, same is not true for the keys.

There are two important points to remember about dictionary keys −

(a) More than one entry per key not allowed. Which means no duplicate key is allowed. When duplicate keys encountered during assignment, the last assignment wins. For example −

#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'}

print "dict['Name']: ", dict['Name']

When the above code is executed, it produces the following result −

dict['Name']:  Manni

(b) Keys must be immutable. Which means you can use strings, numbers or tuples as dictionary keys but something like [‘key’] is not allowed. Following is a simple example:

#!/usr/bin/python

dict = {['Name']: 'Zara', 'Age': 7}

print "dict['Name']: ", dict['Name']

When the above code is executed, it produces the following result −

Traceback (most recent call last):
   File "test.py", line 3, in <module>
      dict = {['Name']: 'Zara', 'Age': 7};
TypeError: list objects are unhashable

Built-in Dictionary Functions & Methods −

Python includes the following dictionary functions −

SN Function with Description
1 cmp(dict1, dict2)

Compares elements of both dict.

2 len(dict)

Gives the total length of the dictionary. This would be equal to the number of items in the dictionary.

3 str(dict)

Produces a printable string representation of a dictionary

4 type(variable)

Returns the type of the passed variable. If passed variable is dictionary, then it would return a dictionary type.

Python includes following dictionary methods −

SN Methods with Description
1 dict.clear()

Removes all elements of dictionary dict

2 dict.copy()

Returns a shallow copy of dictionary dict

3 dict.fromkeys()

Create a new dictionary with keys from seq and values set to value.

4 dict.get(key, default=None)

For key key, returns value or default if key not in dictionary

5 dict.has_key(key)

Returns true if key in dictionary dict, false otherwise

6 dict.items()

Returns a list of dict‘s (key, value) tuple pairs

7 dict.keys()

Returns list of dictionary dict’s keys

8 dict.setdefault(key, default=None)

Similar to get(), but will set dict[key]=default if key is not already in dict

9 dict.update(dict2)

Adds dictionary dict2‘s key-values pairs to dict

10 dict.values()

Returns list of dictionary dict‘s values

Sets

Python Variable Declaration Rules and Syntax

Variable and Value

  • A variable is a memory location where a programmer can store a value. Example : roll_no, amount, name etc.
  • Value is either string, numeric etc. Example : “Sara”, 120, 25.36
  • Variables are created when first assigned.
  • Variables must be assigned before being referenced.
  • The value stored in a variable can be accessed or updated later.
  • No declaration required
  • The type (string, int, float etc.) of the variable is determined by Python
  • The interpreter allocates memory on the basis of the data type of a variable.

Python Variable Name Rules

  • Must begin with a letter (a – z, A – B) or underscore (_)
  • Other characters can be letters, numbers or _
  • Case Sensitive
  • Can be any (reasonable) length
  • There are some reserved words which you cannot use as a variable name because Python uses them for other things.

Good Variable Name

  • Choose meaningful name instead of short name. roll_no is better than rn.
  • Maintain the length of a variable name. Roll_no_of_a-student is too long?
  • Be consistent; roll_no or or RollNo
  • Begin a variable name with an underscore(_) character for a special case.

Variable assignment

We use the assignment operator (=) to assign values to a variable. Any type of value can be assigned to any valid variable.

a = 5
b = 3.2
c = "Hello"

Here, we have three assignment statements. 5 is an integer assigned to the variable a.

Similarly, 3.2 is a floating point number and "Hello" is a string (sequence of characters) assigned to the variables b and c respectively.

Multiple assignments

In Python, multiple assignments can be made in a single statement as follows:

a, b, c = 5, 3.2, "Hello"

If we want to assign the same value to multiple variables at once, we can do this as

x = y = z = "same"

This assigns the “same” string to all the three variables.

If  you have any further clarification on this topic please give comment bellow Python Variable Declaration Rules and Syntax