General Middleware

Getting Started with Python

Python has gained in popularity due to its easy learning curve and quick implementation. If you are aware of any other programming language, it is very easy to get up to speed with Python. Although, it takes more time for an advanced level, but this module will help you to get started.

Python2 vs Python3

Python3 is the latest module and was released back in 2008. It is always recommended to use Python3. Python2.7 was released later on to bridge the gap between Python2 and Python3. Programs that runs on lower versions of Python2 are compatible with 2.7. Moreover, most of the syntax followed by Python2.7 is also compatible with much of Python3 syntax.

In summary, if you are starting fresh with Python, then go with Python3. However, while running old codes written in Python2, always use the Python2.7 version.

Installing Python

Installation of Python is straight forward. The install steps are clearly mentioned in the documentation.

Variables, Strings, String Functions, String Concatenation and Formatting

1. Define Variable

variable_name = <value>

a. Variables are names that store values

b. Variables must start with a letter, but may contain numbers and underscores

2. Create String

name = "Animesh"
surname = 'Banerjee'

Strings can be surrounded by single or double quotes

3. Accessing string indices

name = "Animesh Banerjee"
print(name[0])
print(name[-1])
print(name[-2])
print(name[-3])

Output

A
e
e
j

4. String functions

name = "Animesh Banerjee"
place = "Singapore"
print("My name is {}".format(name))
print("My name is {} and I stay in {}".format(name, "Singapore"))
print('This is how you can include "double quotes" inside single quotes')
print('This is how you can include single quote (\') inside single quotes')
print("This is how you concatenate strings. Name and place concatenated becomes --> {}".format(name + "," + place))
print("This is how you repeat strings - {}".format('+'*10 + '*'*20))
print("Capitalized name is {}".format(name.capitalize()))
print("Your name in upper case is {}".format(name.upper()))
print("Your name in lower case is {}".format(name.lower()))
print("The number of charaters in my name is {}".format(len(name)))

Output

My name is Animesh Banerjee
My name is Animesh Banerjee and I stay in Singapore
This is how you can include "double quotes" inside single quotes
This is how you can include single quote (') inside single quotes
This is how you concatenate strings. Name and place concatenated becomes --> Animesh Banerjee,Singapore
This is how you repeat strings - ++++++++++********************
Capitalized name is Animesh banerjee
Your name in upper case is ANIMESH BANERJEE
Your name in lower case is animesh banerjee
The number of charaters in my name is 16

5. Built in functions

print("This is a print statement")
print(len("print the length of this string"))
#This is how convert integers and strings
print("Converting string to integer before adding - {}".format(2 + int("2")))
print("Converting integer to string - {}".format("2" + str(2)))
customer_name = input("Enter your name - ")
print("Customer name is {}".format(customer_name))

Output

This is a print statement
31
Converting string to integer before adding - 4
Converting integer to string - 22
Enter your name - Moumita
Customer name is Moumita

6. Creating number series using the range function

for i in range(1,10):
    print(str(i))
print("\n\n")
for i in range(1,10, 2):
    print(str(i))

Output

1
2
3
4
5
6
7
8
9



1
3
5
7
9

7. How to access format strings using index

customer_name = input("Enter your name - ")
customer_location = input("What is your current location? ")

print("Customer name {1} and location is {0}".format(customer_location, customer_name))

Output

Enter your name - Sudipto
What is your current location? USA
Customer name Sudipto and location is USA

8. Advanced Formatting using format function

customer_name = "Sudipto"
customer_location = "Singapore"

print("{0:20} |".format(customer_name))
print("{:20} |".format(customer_location))

The {0:20} means the string at the first argument of format function should be printed and total number of characters should be 20. If there are less number of characters in customer_name, the additional characters are filled with spaces.

The {:20} means the same as {0:20}. Note that the index is optional, if left out, it is assumed to be 0.

Output

Sudipto              |
Singapore            |

The alignment of the strings can be controlled further with >, < or ^ notations as shown below.

customer_name = "Sudipto"
customer_location = "Singapore"

print("| {0:>20} |".format(customer_name))
print("| {:>20} |".format(customer_location))

Output

|              Sudipto |
|            Singapore |

If no alignment is mentioned, then left alignment is the default

customer_name = "Sudipto"
customer_location = "Singapore"

print("| {0:20} |".format(customer_name))
print("| {:20} |".format(customer_location))

Output

| Sudipto              |
| Singapore            |

^ is used for a center alignment. Look at the below program output.

customer_name = "Sudipto"
customer_location = "Singapore"

print("| {0:^20} |".format(customer_name))
print("| {:^20} |".format(customer_location))

Output

|       Sudipto        |
|      Singapore       |

9. Global Variables

You can define variables as global so that it can be accessed from anywhere in the code. For example, any variable defined inside functions are local and they cannot be accessed outside the function. However, a global variable inside a function can be access from outside the function as well. Look at the example below.

def demo():
  global a
  a = 2
  print('Value of b inside function demo = ',b)
def newfunc():
  global b
  b=3
  print('Value of a inside function newfunc = ', a)
newfunc()
demo()
print('Value of a outside functions', a)
print('Value of b outside functions', b)

Output

Value of a inside function newfunc =  2
Value of b inside function demo =  3
Value of a outside functions 2
Value of b outside functions 3

Comments

1. Single Line Comments

#This is a comment

2. Multiline Comments using triple quotes

"""
Multiline comments
You can keep anything inside these comments
"""

Numbers, Numeric Operations and Numeric Functions

1. Convert number into string

a=2
str(a)

2. Convert string to number

a="2"
int(a)

3. Convert string to a float

a="2"
float(a)

4. Numeric operations

>>> 2+2
4
>>> 2/3
0.6666666666666666
>>> 4%3
1
>>> 3-4
-1

Note: Division always results in a floating point number. Hence 2/2 will result in 1.0 and not 1

>>> 2/2
1.0

Booleans and Conditionals

1. Booleans are either True or False. Comparators compare one numeric value with another and result is a boolean.

print(2>1)      ## True
print(1==5)     ## False

2. Boolean operators (and, or, not) compare two statements or negate a statement and results in a boolean

print(2>1 and 4==6)     ## False
print(2>1 or 4==6)      ## True
print(not 2==2)         ## False

3. Use parenthesis to control the order of operations

print((not 2==4) and (5>2 or 6==5) )     ## True

4. A code block is a section of code at the same level of indentation. Conditionals include if, if/else and if/elif/else

name = input("What is your name? ")
if name == 'Sudipto':
    print("You are the owner of the company")
else:
    print("You are not the owner of the company")


location = input("What is your location? ")
if location == 'Singapore':
    print("Singapore is a nice place to visit")
elif location == "USA":
    print("USA is a nice place for higher studies")
elif location == "India":
    print("There are so many famous places to visit in India")
else:
    print("I do not have much information about the place you stay")

Output

What is your name? Animesh
You are not the owner of the company
What is your location? Singapore
Singapore is a nice place to visit

Functions

1. Define a function

Note that a function must be defined before it can be called.

def sum(a, b):
    _s = a+b
    return _s

def increment(a, b=1):
    _s = a+b
    return _s

Note that the increment function has a default value defined for the second parameter. In this case, the function can be called with or without the second parameter. In case it is called with only one parameter, the second one is assumed to be the one assigned (b=1 in this case).

2. Call a function

print(sum(2,32))            ## Output = 34
print(increment(23))        ## Output = 24
print(increment(34, 5))     ## Output = 39

In the output of increment function with one parameter (23 in this case), the output is incremented by 1, since the default value of the second parameter is taken as 1 (b=1)

3. Function description

You can describe a function with multi line text using “”” or ”’. Look at the example below.

def add(x,y):
  """
  This is a function
  It can be used to add 2 numbers
  """
  return x+y
sum = add(2,3)
print(sum)

def add2(x,y):
  '''
  This is a function
  It can be used to add 2 numbers
  '''
  return x+y
sum2 = add2(2,5)
print(sum2)

Lambda Functions

Lambda functions are inline functions that are written in one line. This is a short form of a regular function, where only one operation has to be performed. For example, we can use below lambda function for add operation

add = lambda x,y: (x+y)**2
print(add(3,6))

Use below function to calculate the discounted price of an item.

discounted_price = lambda price, discount : (100-discount)*price/100
newPrice = discounted_price(3988, 21)
print(newPrice)

Note that the value of the expression in a lambda function is returned without an explicit return statement. In fact, return statement is not valid within lambda functions.

Two Types of Function Arguments

1. Positional Arguments

These are the regular arguments passed, while calling the function. They have to maintain strict order, in which the arguments will be mapped to the variables of the function. Look at a below example.

def getEmployeeDetails(name, company, location):
  nm = "Employee Name - "+name
  co = "Employee works at "+company
  lo = "Employee is located at "+location
  return nm, co, lo
name, company, location = getEmployeeDetails("Animesh", "Visa", "Singapore")
print(name)
print(company)
print(location)

Output

Employee Name - Animesh
Employee works at Visa
Employee is located at Singapore

Here there is one-one mapping between the caller and the callee, and the order of arguments is extremely important. For example, you cannot pass the location first, before the name and company. It has to be in there order name, company, location. Such arguments are called positional arguments.

2. Keyword Arguments

On the contrary, you can pass keyword arguments in any order you want. Look at the below example.

def getEmployeeDetails(name="default", company="default", location="default"):
  nm = "Employee Name - "+name
  co = "Employee works at "+company
  lo = "Employee is located at "+location
  return nm, co, lo
name, company, location = getEmployeeDetails(name="Animesh", location="Singapore", company="Visa")
print(name)
print(company)
print(location)

Output

Employee Name - Animesh
Employee works at Visa
Employee is located at Singapore

Note that the keyword arguments don’t need to follow order and they have to have to default value specified by the function. If the default value is left blank, it produces syntax error.

Function with variable number of arguments

You can use the special variable format *args to define functions with a variable number of positional arguments. For example, to define an add function that can be used to add any number of integers, use the below function.

def add(*args):
  total = 0
  for i in args:
    total += i
  return total
print(add(2,4,5,5,67))

Note that the *args will take all the input parameters as a list and you can iterate over the list to get all the parameters.

Also, note that instead of *args, you can also use *nums or *numbers or *n. The name is not important, but the format has to be correct. For example, below works as well.

def add(*n):
  total = 0
  for i in n:
    total += i
  return total
print(add(2,4,5,5,67))

Handling Multiple Keyword Arguments

If you want to pass multiple Keyword arguments to a function, it can be handled with a special argument **kwargs. Look at the below example.

def custom_print(**kwargs):
      for key,val in kwargs.items():
        print(val)
custom_print(name_1="Animesh", location="Singapore")

Output

Animesh
Singapore

Here, the function custom_print is called with a set of keyword arguments. The **kwargs takes all those arguments, which can be manipulated and processed by iterating over the list.

Handling positional and keyword arguments together

Note that a function can contain both the type of arguments, however there is an order that must be maintained. The positional arguments has to be passed at the beginning, then the keyword arguments. Below is an example.

def newPrice(*args, **kwargs):
  totalPrice = 0
  totalDiscount = 0
  for i in args:
    totalPrice += i
  for key,val in kwargs.items():
    totalDiscount += val
  return totalPrice - (totalPrice)*totalDiscount/100
print(newPrice(19, 59, 78, 32, baseDiscount=5, additionalDiscount=3, occasionDiscount=4))

Here the positional arguments are mentioned in orange and keyword arguments in purple. The positional arguments are mentioned first and then the keyword arguments. *args is used to handle the positional arguments, whereas **kwargs handles the keyword arguments.

Below is another scenario. Look at the colour coding to understand how the arguments passed are mapped to the function.

def newPrice(*args, baseDiscount=2, **kwargs):
  totalPrice = 0
  totalDiscount = 0
  for i in args:
    totalPrice += i
  for key,val in kwargs.items():
    totalDiscount += val
  return totalPrice - (totalPrice)*totalDiscount/100
print(newPrice(19, 59, 78, 32, baseDiscount=5, additionalDiscount=3, occasionDiscount=4))

Lists, Slices, Exception handling, Loops, Sorting and Ranges

1. Define a List

places = ["Singapore", "India", "USA", "Germany", "London", "Malaysia", "Indonesia", "Thailand"]

2. Access elements in a list

print(places[3])      ## Germany
print(places[-1])     ## Thailand

3. Sort and update the list

places.sort()

4. Sort and create a new list, without impacting the original list.

new_sorted_places = sorted(places)

5. Append an element at the end of the list

places.append("Poland")

6. Remove an element from the list

places.remove("Malaysia")

7. Insert an element at a particular index in the list

places.insert(0, "Italy")

8. Append another list to the existing list

more_places = ["Norway", "Uruguay", "Korea"]
places.extend(more_places)

9. Get the index of an element in the list

places.index("Pakistan")

Note that if the element is not found in the list “places”, then an error is thrown and the program execution stops. To avoid this, we must use error handling using try/except.

try:
    print(places.index("Pakistan"))
except:
    print("Pakistan is not in the list")

10. Slice a list

print(places)               ## ['Germany', 'India', 'Indonesia', 'Italy', 'London', 'Poland', 'Singapore', 'Thailand', 'USA', 'Norway', 'Uruguay', 'Korea']
print(places[slice(2,4)])   ## ['Indonesia', 'Italy']
print(places[slice(4,7)])   ## ['London', 'Poland', 'Singapore']

Note that slice(2,4) will return the elements from index 2 to index 3. Similarly, slice(4, 7) will return the elements from index 4 to index 6.

11. Iterate through a list

for place in places:
    print("for - {}".format(place))

Output

for - Germany
for - India
for - Indonesia
for - Italy
for - London
for - Poland
for - Singapore
for - Thailand
for - USA
for - Norway
for - Uruguay
for - Korea

List Comprehension

List comprehension is used to modify a list using inline code. Look at the examples below.

nums = [2,5,78,65,89,3]
print(nums)
incrementedList = [i+1 for i in nums]
print(incrementedList)
conditionalList = ["Yes" if i<50 else "No" for i in nums]
print(conditionalList)
multiplicationList = [i*3 for i in nums]
print(multiplicationList)

Output

[2, 5, 78, 65, 89, 3]
[3, 6, 79, 66, 90, 4]
['Yes', 'Yes', 'No', 'No', 'No', 'Yes']
[6, 15, 234, 195, 267, 9]

This way, it is very easy to update an existing list based on different conditions. The conditions can go up to any complexity level.

Dictionaries

1. Define a dictionary

identity = {
    "Animesh": 98987766,
    "Sudipto": 76765432,
    "Moumita": 87965444,
    "Aishani": 90989878
}

2. Add an element to a dictionary

identity["Laxmi Kanta"] = 56566565

3. Get the value of a particular key

identity.get("Moumita")

4. Get a list of all keys

identity.keys()

5. Get a list of all values

identity.values()

6. Iterate through a list and print the keys and values

for k in identity.keys():
    print("|{:^30}|{:^30}|".format(k, identity.get(k)))

Output

|           Animesh            |           98987766           |
|           Sudipto            |           76765432           |
|           Moumita            |           87965444           |
|           Aishani            |           90989878           |
|         Laxmi Kanta          |           56566565           |

7. Delete a key/value pair

del identity["Laxmi Kanta"]

8. Check if a value exists in a Dictionary

print("Laxmi Kanta" in identity.values())    ## False

Tuples

Tuples are immutable list that cannot be changed, once they are defined.

1. Define a tuple

places_tuple = ('Singapore', 'India', 'USA', 'Germany', 'London', 'Malaysia', 'Indonesia', 'Thailand')

2. Convert a list to tuple and vice versa using the built in functions list() and tuple()

places_tuple = ('Singapore', 'India', 'USA', 'Germany', 'London', 'Malaysia', 'Indonesia', 'Thailand')
places_list = list(places_tuple)
print(places_list)

places_t = tuple(places_list)
print(places_t)

Output

['Singapore', 'India', 'USA', 'Germany', 'London', 'Malaysia', 'Indonesia', 'Thailand']
('Singapore', 'India', 'USA', 'Germany', 'London', 'Malaysia', 'Indonesia', 'Thailand')

3. Build in functions

print(max(places_t))  ## USA
print(min(places_t))  ## Germany

File Handling

File handling is really important to read, write and append contents to files. Below are the different ways to handle the same.

1. Open file in read mode

f = open("countries.txt", 'r')

2. Read the entire contents of the file

f.read()

3. Move the cursor position from where read is started.

Note that f.read() will read the entire contents of the file and move the cursor to the end of the file. Hence, if it is read again, nothing will be returned. This cursor or read start position can be moved forward using the seek function.

f.seek(4)

The above command will move the cursor 4 positions forwards from the current. Trying to read the file after that will return the contents only after that seek position.

4. Closing a file

It is highly recommended that when you open files, the code should close it as well. Without that, it is possible to get a too many open files error.

f.close()

5. Easy way to handle open and close

with open("countries.txt", 'r') as f_obj_var:
    print(f_obj_var.read())

With the above code, you don’t need to close the file separately. As soon as the code block exits, the file is closed automatically.

6. Read each line of a file

with open("countries.txt") as f_obj_var:
    for line in f_obj_var:
        print(line.rstrip())

7. Write a file

with open("addresses.txt", 'w') as f_obj_var:
    for i in range(1, 200,5):
        f_obj_var.write(str(i) + "\n")

8. Append contents to a file

with open("addresses.txt", 'a') as f_obj_var:
    for i in range(1, 200,5):
        f_obj_var.write(str(i) + "\n")

Modules

Python modules are files that have a .py extension and can implement as set of variables, functions and classes.

1. Import a module

import os

2. Call methods from an imported module

os.cpu_count()

3. Import a specific method from a module and call that using just the method name

from os import cpu_count
print(cpu_count())

4. The default module search path is determined by your Python installation. To manipulate the module search path, modify sys.path or set the PYTHONPATH environment variable

5. File all the constituents of an imported module

import os
print(dir(os))

Output

['CLD_CONTINUED', 'CLD_DUMPED', 'CLD_EXITED', 'CLD_KILLED', 'CLD_STOPPED', 'CLD_TRAPPED', 'DirEntry', 'EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', 'EX_IOERR', 'EX_NOHOST', 'EX_NOINPUT', 'EX_NOPERM', 'EX_NOUSER', 'EX_OK', 'EX_OSERR', 'EX_OSFILE', 'EX_PROTOCOL', 'EX_SOFTWARE', 'EX_TEMPFAIL', 'EX_UNAVAILABLE', 'EX_USAGE', 'F_LOCK', 'F_OK', 'F_TEST', 'F_TLOCK', 'F_ULOCK', 'GenericAlias', 'Mapping', 'MutableMapping', 'NGROUPS_MAX', 'O_ACCMODE', 'O_APPEND', 'O_ASYNC', 'O_CLOEXEC', 'O_CREAT', 'O_DIRECTORY', 'O_DSYNC', 'O_EVTONLY', 'O_EXCL', 'O_EXLOCK', 'O_FSYNC', 'O_NDELAY', 'O_NOCTTY', 'O_NOFOLLOW', 'O_NOFOLLOW_ANY', 'O_NONBLOCK', 'O_RDONLY', 'O_RDWR', 'O_SHLOCK', 'O_SYMLINK', 'O_SYNC', 'O_TRUNC', 'O_WRONLY', 'POSIX_SPAWN_CLOSE', 'POSIX_SPAWN_DUP2', 'POSIX_SPAWN_OPEN', 'PRIO_PGRP', 'PRIO_PROCESS', 'PRIO_USER', 'P_ALL', 'P_NOWAIT', 'P_NOWAITO', 'P_PGID', 'P_PID', 'P_WAIT', 'PathLike', 'RTLD_GLOBAL', 'RTLD_LAZY', 'RTLD_LOCAL', 'RTLD_NODELETE', 'RTLD_NOLOAD', 'RTLD_NOW', 'R_OK', 'SCHED_FIFO', 'SCHED_OTHER', 'SCHED_RR', 'SEEK_CUR', 'SEEK_DATA', 'SEEK_END', 'SEEK_HOLE', 'SEEK_SET', 'ST_NOSUID', 'ST_RDONLY', 'TMP_MAX', 'WCONTINUED', 'WCOREDUMP', 'WEXITED', 'WEXITSTATUS', 'WIFCONTINUED', 'WIFEXITED', 'WIFSIGNALED', 'WIFSTOPPED', 'WNOHANG', 'WNOWAIT', 'WSTOPPED', 'WSTOPSIG', 'WTERMSIG', 'WUNTRACED', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_check_methods', '_execvpe', '_exists', '_exit', '_fspath', '_fwalk', '_get_exports_list', '_spawnvef', '_walk', '_wrap_close', 'abc', 'abort', 'access', 'altsep', 'chdir', 'chflags', 'chmod', 'chown', 'chroot', 'close', 'closerange', 'confstr', 'confstr_names', 'cpu_count', 'ctermid', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'environb', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fchdir', 'fchmod', 'fchown', 'fdopen', 'fork', 'forkpty', 'fpathconf', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fstatvfs', 'fsync', 'ftruncate', 'fwalk', 'get_blocking', 'get_exec_path', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getegid', 'getenv', 'getenvb', 'geteuid', 'getgid', 'getgrouplist', 'getgroups', 'getloadavg', 'getlogin', 'getpgid', 'getpgrp', 'getpid', 'getppid', 'getpriority', 'getsid', 'getuid', 'initgroups', 'isatty', 'kill', 'killpg', 'lchflags', 'lchmod', 'lchown', 'linesep', 'link', 'listdir', 'lockf', 'lseek', 'lstat', 'major', 'makedev', 'makedirs', 'minor', 'mkdir', 'mkfifo', 'mknod', 'name', 'nice', 'open', 'openpty', 'pardir', 'path', 'pathconf', 'pathconf_names', 'pathsep', 'pipe', 'popen', 'posix_spawn', 'posix_spawnp', 'pread', 'preadv', 'putenv', 'pwrite', 'pwritev', 'read', 'readlink', 'readv', 'register_at_fork', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sched_get_priority_max', 'sched_get_priority_min', 'sched_yield', 'sendfile', 'sep', 'set_blocking', 'set_inheritable', 'setegid', 'seteuid', 'setgid', 'setgroups', 'setpgid', 'setpgrp', 'setpriority', 'setregid', 'setreuid', 'setsid', 'setuid', 'spawnl', 'spawnle', 'spawnlp', 'spawnlpe', 'spawnv', 'spawnve', 'spawnvp', 'spawnvpe', 'st', 'stat', 'stat_result', 'statvfs', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sync', 'sys', 'sysconf', 'sysconf_names', 'system', 'tcgetpgrp', 'tcsetpgrp', 'terminal_size', 'times', 'times_result', 'truncate', 'ttyname', 'umask', 'uname', 'uname_result', 'unlink', 'unsetenv', 'urandom', 'utime', 'wait', 'wait3', 'wait4', 'waitpid', 'waitstatus_to_exitcode', 'walk', 'write', 'writev']

See how all the functions, variables and classes gets populated from the module.

Leave a Reply

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