Types of function arguments in python

Python Function - Argument Types

Unlike many other programming languages python supports multiple types formal arguments in the function definition. It is one of the flexibility that python offers.

Types of function (formal) arguments that python supports,

Following picture illustrates these different types of arguments by their order according to the syntax and how we can use them.

Python Function Definition, Arguments Kind and Order
Python Function Definition, Arguments Kind and Order

Let’s see these arguments in detail. Before we proceed you need to know difference between formal arguments and actual arguments.

Formal Arguments VS Actual Arguments

Formal arguments are identifiers used in the function definition to represent corresponding actual arguments.
Actual arguments are values(or variables)/expressions that are used inside the parentheses of a function call.

Above definition and terminology of Formal/Actual arguments is same across different programming languages.

Regular Positional Arguments

These are regular positional argument as it sounds regular. Regular positional arguments are very much similar to arguments that you see in any kind of programming language. These are just (variables) identifiers that values of actual arguments are copied to formal arguments by their position in the function call *.

def add(a, b):
return a + b
# function call
add(2, 3)

If count of actual arguments and formal arguments don’t match interpreter will throw TypeError exception

>>> add(2)
Traceback (most recent call last):
File "", line 1, in <module>
TypeError: add() missing 1 required positional argument: 'b'

Default Arguments

Default arguments allow us to provide defaults for corresponding formal arguments in the function definition. Python uses these defaults if corresponding actual arguments are passed in the function call.

Default arguments should always be followed by non-default arguments in function definition

def greetings(name, msg="Welcom, Good Morning"):
message = "Hello {}! {}".format(name, msg)

As we can see here, the argument msg got it’s default in the function definition. If the argument msg is not passed in the function call it’s default given in the definition will be used. So called default arguments.

>>> greetings("neo")
Hello neo! Welcom, Good Morning

Keyword Arguments

Keyword arguments are passed by it’s name instead of their position as opposed to positional arguments in the function call. As a result we don’t have to mind about position of arguments when calling a function.

Keyword arguments should always be followed by positional arguments in the function call

Let’s call above defined function greetings using keyword arguments

greetings(name="neo", msg="Welcome")

Since we are passing arguments by their names/keywords order or position doesn’t matter

greetings(msg="Welcome", name="neo")

We can mix both positional and keyword arguments but keyword arguments should follow positional arguments

greetings("neo", msg="Welcome")

If you break the rule and pass arguments in such a way that positional arguments follow keyword arguments which is a syntax error.

>>> greetings(msg="Welcome", "neo")
... ...
SyntaxError: positional argument follows keyword argument

Variable Length Positional Arguments

Variable length positional arguments are useful when we are not certain about number of arguments that are required in the function definition or function would receive. It is quite useful to write functions that take countless number of positional arguments for certain use cases.

Syntax to receive variable length positional arguments

def foo(a, b, c, *varargs):

Python first fills the slots given formal arguments by passed positional actual arguments. Any left over actual (positional) arguments are captured by python as a tuple, which is available through variable which is having prefixed * in the function definition.

As we can see in the above syntax. variables a, b, c need to be filled by actual arguments any left over passed arguments can be accessed as tuple using varargs.

def foo(a, b, c, *varargs):
print("Regular Positional Arguments", a, b, c)
print("Variable Length Positional arguments", varargs)
>>> foo(2, 3, 4)
Regular Positional Arguments 2 3 4
Variable Length Positional arguments ()

Since there are no extra positional arguments are passed in the above function call. The variable varargs is a empty tuple

>>> foo(1, 2, 3, 4, 5, 7)
Regular Positional Arguments 1 2 3
Variable Length Positional arguments (4, 5, 7)

As you can see in the above function call, left over arguments are added to tuple and can be accessed using given variable varargs.

You can unpack and pass each element in the sequence as individual argument just by prefixing * in front of the sequence

>>> foo(1, 2, 3, *'hello')
Regular Positional Arguments 1 2 3
Variable Length Positional arguments ('h', 'e', 'l', 'l', 'o')

Where as, if you don’t put * in front of a string or sequence in the function call. Which will be taken as single argument as a whole.

>>> foo(1, 2, 3, 'hello')
Regular Positional Arguments 1 2 3
Variable Length Positional arguments ('hello',)

keyword Only Arguments

Keyword Only arguments are new feature in python 3. Main purpose of keyword only arguments is to be able to pass arguments by it’s keyword only for selected arguments.

Read More here at python keyword only arguments

There are two types of keyword only arguments

Variable Length Keyword Arguments

Variable length keyword arguments are very much similar to variable length positional arguments. It is the mechanism to accept variable length arguments in the form keyword arguments which is useful if we are not certain about number of keyword arguments that function receives. Python captures all extra keyword arguments as dictionary and makes it available by name(variable) which is prefixed by ** in the function definition.

Variable Length Keyword Arguments Syntax

def foo(a, b, c, **kwargs):
. . .

kwargs gets left over keyword arguments after formal arguments are filled up by passed keyword arguments.

def printKWArgs(a, b, c, **kwargs):
print("Regular Positional Arguments", a, b, c)
print("Variable Length keyword arguments", kwargs)
>>> printKWArgs(2, 3, c=5)
Regular Positional Arguments 2 3 5
Variable Length keyword arguments {}

In the above function call since we haven’t passed any extra keyword arguments variable kwargs is a empty dictionary.

>>> printKWArgs(2, 3, c=4, d=9, e=10)
Regular Positional Arguments 2 3 4
Variable Length keyword arguments {'d': 9, 'e': 10}

In the above function call left over keyword arguments d and e can be accessed through variable kwargs which is dictionary. Where key will be the passed keyword name and value will be value given for corresponding keyword in the function call.

Combining both varargs and kwargs

You can use both variable length positional arguments and variable length keyword arguments combined.

def foo(a, b, *varargs, **kwargs):

Different Types Of Function Arguments In Action

Here is the simple function which makes use of all kind of arguments and prints passed arguments by it’s kind

Let’s call the above function by passing these several kind of arguments

printArgsByKind(1, 2, 3, 3.15, float, 4, 5, 6, operation="count", return_type=int, abc=3, xyz=9)

The above function call would print the given arguments by it’s kind as follows,

Regular Positional Arguments a: 1, b: 2, c: 3
Default Arguments default: 3.15, intype: <class 'float'>
Variable length positional arguments(tuple) more: (4, 5, 6)
Non-Default Keyword-Only Arguments operation: count, return_type: <class 'int'>
Default Keyword-Only Arguments print_result: False, ignore_exception: True
Variable length keyword arguments kwargs: {'abc': 3, 'xyz': 9}

Endianness. Little Endian vs Big Endian

Endianness. Little Endian vs Big Endian

Endianness describes the byte order in which bytes of large values are stored, transferred and accessed. Based on order in which bytes are stored, transmitted and accessed there are two main types of endianness called little-endian and big-endian .

Before we understand Little Endian and Big Endian we need to understand how memory and it’s addressing work.

Computer Memory Address

Computer memory is generally abstracted as an ordered series of bytes. Memory address is a number which is reference to a specific memory location in which data(by bytes) is stored . Where computer divides memory into bytes, each byte is assigned a unique number which is a memory address so called byte addressable. Whole memory (like RAM) is addressed by bytes regardless of whether it is being used or not for storage. So that CPU can track where data and instructions are stored in RAM by using memory address.

Memory Addressing. Flat Memory Model
Memory Addressing. Flat Memory Model

First memory location(byte) is assigned lowest possible memory address and further locations are addressed with increasing memory address. Large values are divided into bytes and placed in each memory location.

Question: How large values which require more than one byte to store or transmit are arranged ?

Order in which bytes of large values are stored or transmitted over network is described by Endianness or Byte Order .

To better understand byte order. Have a look at terminology Least Significant Byte, Most Significant Byte, Left Most Byte and Right Most Byte.

In any positional number system including binary we count position from right to left. As we write numbers(from left to write) in binary representation of decimal number we call,

  • Byte which is having least positional value is called Least Significant Byte
  • Least Significant Byte can also be called as Right Most Byte since it comes right most as we write from left to right
  • Byte which is having greatest positional value is called Most Significant Byte
  • Most Significant Byte can also be called as Left Most Byte since it comes left most as write from left to right

Following Image illustrates terms Least Significant Byte and Most Significant Byte. By taking number 0x1A2B3C4D which is of size 32 bits.

Bit Numbering. Terms - Least Significant Byte, Most Significant Byte
Bit Numbering. Terms – Least Significant Byte and Most Significant Byte

Least significant byte can also be referred as LSB or LSByte
Most significant byte can also be referred as MSB or MSByte


The Origin Of Words Of Endianness

Words Little Endian and Big Endian are originated from the 1726 novel Gulliver’s Travels by Jonathan Swift. In which civil war breaks out over whether the big end or the little end of the egg is proper to crack open. Words Little Endian and Big Endian are used to name two groups who fight over little end and big end of the egg is proper way to break egg receptively .

The origin of words of Endianness. Little Endian and Big Endian
The origin of words of Endianness. Little Endian and Big Endian

The novel further describes an intra-Lilliputian quarrel over the practice of breaking eggs. Traditionally, Lilliputians broke boiled eggs on the larger end; a few generations ago, an Emperor of Lilliput, the Present Emperor’s great-grandfather, had decreed that all eggs be broken on the smaller end after his son cut himself breaking the egg on the larger end. The differences between Big-Endians (those who broke their eggs at the larger end) and Little-Endians had given rise to “six rebellions… wherein one Emperor lost his life, and another his crown”. The Lilliputian religion says an egg should be broken on the convenient end, which is now interpreted by the Lilliputians as the smaller end. The Big-Endians gained favour in Blefuscu.

Excerpt of story from wiki Lilliput and Blefuscu

Endianness. The Byte Order

Endianness is also called as Byte Order which describes order in which bytes of large values are stored in memory or transmitted over network in a transmission protocol or a stream (ex. an audio, video streams).

There are two main endianness formats based on whether Most Significant Byte (MSB) or Least Significant Byte (LSB) comes first, when storing data or transmitting streams. Which are Little-Endian and Big-Endian

  • If Least Significant Byte (LSB) is stored or transmitted first, it is said to be Little-Endian
  • If Most Significant Byte (MSB) is stored or transmitted first, it is said to be Big-Endian

Using words Little-Endian and Big-Endian for byte order is analogous to cracking egg on little end and big end respectively

Endianness - The Byte Order - Little Endian and Big Endian
Endianness – The Byte Order – Little Endian and Big Endian. Cracking Egg Analogy

It is interesting topic in computer science, since these two formats are conflicting. Using wrong byte order may potentially read wrong data or corrupt when writing.

Most Computers prefer to handle all of it’s data in single byte order. Systems native byte order is default

You may not need to worry about endianness and endianness conversion unless you work with assembly, drivers, middle level languages like c, embedded systems and low level networking transports. High level programming languages will take care of it for you.

Endianness - Little Endian vs Big Endian
Endianness – Little Endian vs Big Endian

Little Endian

Like Little-Endians crack egg on little end, least significant byte is stored or transmitted over network first in Little-Endian format. Little endian format is also called as host byte order.

Little-Endian is also known as the “Intel convention”. As Intel used little-endian format from it’s earlier processors.

  • Least significant byte is stored or transmitted first
  • When bytes of large values are stored in memory “Least Significant Byte” is placed first in memory at lowest memory address
  • Numeric significance or positional value is increased with increasing memory address
  • Easier mathematical computation. Mathematical operations mostly work from LSB

Little Endian

Big Endian

Like Big-Endians crack egg on big end, most significant byte is stored or transmitted over network first in Big-Endian format. Big endian format is also called as network byte order.

Over decades most of processors took transition to little endian at present many processors(laptops, desktops and servers) come in little endian. Yet, big endian is widely used for network transmission. So it is called “network byte order” .

Big-Endian is also known as the “Motorola convention”.Back then as it was widely used by Motorola processors.

  • Most significant byte is stored or transmitted first
  • When bytes of large values are stored in memory “Most Significant Byte” is placed first in memory at lowest memory address
  • Numeric significance or positional value is decreased with increasing memory address
  • Quick sign checking. Since MSB stores first.
  • Better human readability as it is one to one mapping. Bytes are in the same order as we write.
Big Endian

Check Native Endianness (Native Byte Order)

Check native “byte order” of system using linux command line (terminal)

You can use the command “lscpu” to get CPU information including it’s byte order

# lscpu
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              2
On-line CPU(s) list: 0,1
Thread(s) per core:  1
Core(s) per socket:  2
Socket(s):           1
NUMA node(s):        1
Vendor ID:           GenuineIntel
CPU family:          6
Model:               158
Model name:          Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
Stepping:            9
CPU MHz:             2808.002
BogoMIPS:            5616.00
Hypervisor vendor:   KVM
Virtualization type: full
L1d cache:           32K
L1i cache:           32K
L2 cache:            256K
L3 cache:            6144K
NUMA node0 CPU(s):   0,1
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single pti fsgsbase avx2 invpcid rdseed clflushopt

As you can see endianness(byte order) in the above output “Byte Order: Little Endian“.

Check native byte order using python programming language

Python sys module has attribute called “byteorder” where it’s value describes native byte order of the system .

  • sys.byteorder will be “big” if it’s big-endian (most significant byte first) platform
  • sys.byteorder will be “little” if it is little-endian (least significant byte first) platform
>>> import sys
>>> sys.byteorder

Check native byte order of system using java

In java “java.nio.ByteOrder” provides flags “BIG_ENDIAN” and “LITTLE_ENDIAN” to denote Big-Endian and Little-Endian respectively. It has the static method “nativeOrder()” to get system’s native byte order.


Check endianness of system using C program

#include <stdio.h>
int main()
unsigned int i = 1;
char *c = (char*)&i;
if (*c)
printf("Little Endian System");
printf("Big Endian System");
return 0;

Above c program prints Endianess of the system. In the above program we have used character pointer “c” to dereference integer “i“. Since size of character is 1 byte it will contain only first byte of integer.

  • *c will return 1 which is Least Significant Byte on Little-Endian machine.
  • *c will return 0 which is Most Significant Byte on Big-Endian machine

Following “c” program prints memory representation of given 32-bit integer. You can see byte order in action, order in which given 32-bit integer is stored in memory either by Little-Endian or Big-Endian.

Above program will print ” 4d 3c 2b 1a” if machine is Little-Endian (Least Significant Byte Fist) or “1a 2b 3c 4d” if machien is Big-Endian (Most Significant Byte Fist)

Convert Endianness

It is required sometimes to convert endianness. You don’t have to worry about converting endiannes if you are working application level. For data exchange between machines common format is used. Yet if machine is little endian often data sent over network is big endian. Big-Endian is standard byte order for network communication. In such a case we have to convert data that we receive in Big-Endian to host fomat little endian and when we send data back to network need to convert Little-Endian to Big-Endian. There are many utility function to perform this conversion.

Following function are available in c to help endianness conversion

htons() – Host to Network Short
htonl() – Host to Network Long
ntohs() – Network to Host Short
ntohl() – Network to Host Long

Python library NumPy provides way to convert endianness. NumPy library uses c types and low level data structures behind.

How Systems Of Different Endianness Interact

Files that are created on little-endian machine won’t be read as they were created on big-endian machines without conversion. This is the reason, some programs binary file formats which are endianness independent. Where files are endianness dependent byte order marks are used in headers of file to hint a program endianness of the file.

Example: TIFF files use MM and II in header (begning of file) for big-endian and little-endian respectively. Where MM and II are palindromes these are endianness independent, interpreted same on both little-endian and big-endian machines.

Unicode uses byte order mark in the beginning of the stream to indicated endianness of following unicode bytes.

Endianness problems. Who needs to worry about byte order

You don’t need to worry much if you work with application layer protocols or high level programming languages, but it is good to know.

Think what’s the out put of following program

#include <stdio.h>

void foo(void *d){
printf("Data = %d\n", *(char *)d);

int main(void){
unsigned int i = 1;


  • Words Little-Endian and Big-Endian came from 1726 novel Gulliver’s Travels by Jonathan Swift
  • System is said to be Little-Endian if it stores or transmits Least Significant Byte First
  • System is said to be Big-Endian if it stores or transmits Most Significant Byte First
  • Command “lscpu” on linux will print CPU information including Byte Order (Endianness)
  • Currently m
  • ost of processors are little endian
  • Big Endian is mostly used in network protocols so called network byte order .
  • You don’t have to worry about byte order (endianness) compatibility, system will take care of it behind the senses. Unless you work with assembly, c/c++ language, drivers, embedded system and low level network transports.
  • Choose One endianness and stick to it to avoid obscured bugs.

Python keyword only arguments

Python Keyword-Only Arguments

Python 3 got new feature called keyword-only arguments. Python has already a support for variables length positional arguments and variable length keyword arguments.

Jump to,

Current Syntax

By variable length positional arguments we can pass any number of positional arguments which are sucked up by a variable provided by syntax ” *varargs “. Also, by the syntax of variable length keyword arguments “*kwargs” we can pass any number of keyword arguments to function which are available as dictionary in the function.

def foo(a, b, *varargs, **kwargs):
print(a, b, varargs, kwargs)

## Call foo
foo(2, 9, 12, 34, x=3, name="bob")

Which outputs, the following. excluding 2, 9 all other positional arguments are sucked up by “vargars” and keywords are by “kwargs”

2 9 (12, 34) {'x': 3, 'name': 'bob'}

Existing syntax doesn’t allow us to use any kind of keyword arguments beyond variable length keyword arguments “**kwargs“.

You can choose to pass arguments by either position or it’s keyword.

One major limit for this syntax is that, all the regular arguments slots must be filled before the Variable Length keyword arguments slot.

Keyword-Only Arguments

This feature allows us to define a function to accept selected arguments by it’s keyword only. Which literally means you have to pass arguments to the function during function call by using it’s keyword only, otherwise it trows an exception. These keyword only arguments will never be automatically filled in by a positional argument.

How it’s useful

  • It improves the readability
  • API functions can accept the critical parameters as keyword only arguments to avoid ambiguity
  • You can create the function with all of it’s arguments as keyword only arguments
  • Functions with keyword only arguments are useful in selected use case scenarios when function is taking huge number of arguments

There are two kind of keyword only arguments. Which are,

  • Non-Default Keyword-Only Arguments
  • Default Keyword-Only Arguments

Non-Default Keyword-Only Arguments

Non-Default keyword-only arguments are keyword-only arguments without any defaults in function definition. If function is defined with non-default keyword-only arguments. Function should be called by passing corresponding arguments (with keywords). See the Syntax for Non-Default Keyword-Only Arguments

Default Keyword-Only Arguments

The name Default Keyword-Only Arguments is itself self explanatory. As it says, These are keyword-only arguments with defaults in the function definition

Keyword-Only Arguments Syntax

The syntax change is fairly very simple, it allows you to define keyword only arguments following variable length positional args *varargs .

def sortwords(*words,  case_sensitive=True):
. . .

Here the argument case_sensitive is keyword-only. Every positional arg you give goes to words argument. The argument case_sensitive is available as it’s default or should be passed by it’s keyword during function call

sortwords("bob", "jack", "rose",  case_sensitive=False)

Le’ts mix up regular positional, variable length positional and keyword-only arguments.

def add(a, b, *more, print_result=True):

According to above function definition, argument slots a and b are filled up first and then left over positional arguments are sucked up by argument more. Argument print_result will never be sucked up by vararg ‘more’. It can only be passed as keyword-only argument.

Let’s see the above function in action,

def add(a, b, *more, print_result=True):
result = a + b + sum(more)
if print_result:
print("sum of {} {} {} is {}".format(a, b, ' '.join(map(str, more)), result))
return result

Call above function

>>> add(1, 3, 9) 
sum of 1 3 9 is 13
>>> add(2, 9, 12, 3, print_result=False)

Here, did you notice ? the argument “more” will also suck up any erroneous positional arguments.

>>> add(9, 2, 5, 6, 'not sure why im here')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in add
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Since we are performing addition, expected arguments are numbers but passing any other non compatible type causes error. In this case we might need to enforce arguments by it’s type or if you choose not to take or ignore varargs which brings us the following syntax “*” .

Let’s put all together,

def compute(a, b, c, *more, print_result=False, ignore_execeptions=True, **kwargs):

Let’s mix up all kind of arguments to see the kind of arguments, order and where these keyword-only arguments fit in the function definition.

Python Function Definition, Arguments Kind and Order
Python Function Definition, Arguments Kind and Order

Let’s see simple example in action to learn how we can use these arguments in combination with others.

def compute(a, b, c, print_args=True, intype=int,  *more, operation, print_result=False, return_type, ignore_exceptions=True, **kwargs):
if print_args:
print(a, b, c, print_args, intype, more, operation, print_result, return_type, ignore_exceptions, kwargs)
result = None
arr = (a, b, c) + more
if operation == "count":
result = len(arr)
elif operation == "sum":
result = sum(arr)
print("Unknow Operation")
raise NotImplementedError("Unknow Operation. Operation Not Implemented")
import sys
print("Error performing operation: {}: {}".format(operation, sys.exc_info()))
if not ignore_exceptions:
if print_result:
print("Result is: {}".format(result))
return result and return_type(result)

Above example may not be serving any logical purpose but intention was to show you the keyword-only arguments in action along with other argument types.

Keyword-Only Arguments with no varargs

Another syntax change is that, you can omit the name for the varargs argument to take keyword-only arguments and raise exception if any extra positional arguments are passed.

def compare(a, b, *, key=None):
. . .

Non-Default Keyword-Only Arguments Syntax

It is not necessary to provide a default value for keyword-only argument in the function definition

def compare(a, b, *, key):
. . .
>>>  compare(2, 3)   # missing non-default keyword-only arg
Traceback (most recent call last):
File "", line 1, in
TypeError: compare() missing 1 required keyword-only argument: 'key'

It trows TypeError exception if keyword-only argument is not passed in the function call. Since it is not having default value given.

How to view converted SQL queries of Django ORM

View Django ORM's raw SQL queries

Django provides the ORM which is database and vendor agnostic, as a result we can perform operations on database regardless kind of database running behind.Thus, developer may not need expertise on specifiec database, all he needs is how to play with ORM But, sometimes we may want to see the how Django is converting ORM into raw SQL queries of specific database either for debugging or learning.

There are multiples ways to see converted Queries

View Queries using django.db.connection.queries

You can find queries so far that converted by ORM at django.db.connection.queries

print converted django queries

>>> from django.db  import connection
>>> print(connection.queries)

Access raw SQL query on QuerySet object

Every queryset object has query attribute, which holds the actual SQL query which will be sent to Database. query attribute return query when either print or convert it to str

>>> qs = MyModel.objects.all()
>>> print(qs.query)

Configure Django Logger to log Queries

Django logs debug messages including raw SQL queries it is executing to console. To see these message you have to configure django logger and set DEBUG True

Add following setting to settings.py file of project


** DEBUG=True is not recommended in production

Configure django logger and set it’s level DEBUG in settings.py

'django': {
'handlers': ['console'],
'propagate': False,
'level': 'DEBUG'

Simple Complete logging setting to log Django logs including queries , Copy and paste the following setting into your setting.py module , so django displays logs messages including queries being sent to DB that are converted from ORM to raw SQL on console

'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(asctime)s-%(module)s-%(levelname)s :: %(message)s'
'simple': {
'format': '%(levelname)s :: %(message)s'
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
'loggers': {
'django': {
'handlers': ['console'],
'propagate': False,
'level': 'DEBUG'

Refer more on logging configuration at Django Logging

Database general query logs

All most all DBMS software’s support the logging of queries which are being execute for both debugging and tuning. You can enable the logging for a while to monitor queries from multiple clients.

Python Operators

Python Operators

Operators are building blocks of all kind of operation we can do in any programming language. Operators are basically the symbols which operate on at least one or more operands. Operators are surrounded by literals or variables which are called operands.

For Example: If you consider the expression 9 + 3 = 10 . 9, 3 and 10 are called operands. And the symbol + is called operator.

Operator vs Operands
Operator vs Operands

Python offers several types of operators which are,

  1. Arithmetic Operators
  2. Comparison Operators
  3. Logical Operators
  4. Membership Operators
  5. Identity Operators
  6. Assignment Operators
  7. Bitwise Operators
  8. Compound Assignment Operators
  9. Miscellaneous Operators

Arithmetic Operators

Python arithmetic operators help to perform mathematical operation like, addition, subtraction, multiplication etc. You will find these similar across different programming languages.

+ AdditionAdds Both Operands x + y
SubtractionSubtracts right hand side operand from left hand side operandx – y
*MultiplicationMultiplies Both operands x * y
/DivisionDivides the left operand by right operand x / y
%ModulusDivides the left operand by right operand but returns remainderx % y
** Exponent Computes the power a ** 2
//Floor DivisionIt is like division operator /. But it returns the floored value of the result. For example 5/2 is 2.5 , but floor division results only 2.x // y

Comparison Operators

Comparison operators are also called as relational operators.  These are building blocks of branching and decision making blocks of code you write. These  operators return Boolean True or False based on the condition satisfied or not 

== Equal to operator
This is operator compares the both left and right operands and return true if equal else flase
x == y
!= Not Equal to This is opposite to equal to operator. Where this operator return if both operands are not equal x != y
<> Not Equal to This is same as not equal to != . Condition becomes true only if both operands are not equal to. This is removed in python 3 x <> y
> Greater than The condition becomes true if left hand operand is greater than right operand x > y
< Less than The condition becomes true only if left hand operand is less than right operand x < y
>= Greater than or equal to The condition becomes true only if left hand operand is greater than or equal to the right hand operand x <= y
<= Less than or equal to The condition becomes true only if left hand operand is less than or equal to the right hand operand

Logical Operators 

Like any other programming languages python offers 3 logical operators. These logical operators can be applied to any literal or variable not just limited to Boolean. Like comparison operators logical operators also return either True or False. These are like conjunction words in programming languages. Logical operators help us to combine multiple conditions to write complex conditionals. These operators are not limited to use with multiple condition they can be used as short hands  or  Short Circuit Evaluations  .  If they are used in short circuiting, they return the value instead of boolean. 

andLogical ANDCondition becomes true if both operands are true(2 > x) and (2 < y)
orLogical ORCondition becomes true if at least one operand is true (2 > x) or (2 < y)
notLogical NOTIt reverses the truth value of it’s operand. Returns True if operand evaluated as False, returns False if operand Evaluated as True not x

Membership Operators 

Membership operators in python are used to check whether an element is present in the container or not. Custom Objects can implement this operator using magic method called __contains__ . This operators can be used on container data types which includes sequence and mapping data types like string,  lists and dictionaries.  It doesn’t work with scalar data types int and float. 

Returns True if Container or sequence contains given value
2 in [1, 2, 3, 4, 5]
not innot inReturns True if Container or Sequence doesn’t contain given value 2 not in [4, 9 , 34]

Identity Operators 

Identity operator compares the id(said to be address) of both operand (objects) instead of their value and return True both are same. Same id  mean both objects are same not only by their value but also by the address they are referring to object in the memory. 

isReturns True if both variables refers to the same objectx is y
is notReturns False if both variables refers to the same objectx is not y

Assignment Operator 

Assignment operator plays a vital role in programming. It is used to assign a value to the variable. 

=Assign value of right hand operand to the variable on the left handx = 2

Bitwise Operators 

As the name says, bitwise operators work on bits. These operations are performed on integers of any number system, but operators perform operation on binary equivalent of given operands. 

&Bitwise ANDPerforms Binary AND operationx & y
|Bitwise ORPerforms Binary OR operationx | y
^ Bitwise XOR/ Exclusive ORPerforms Binary XOR.x ^ y
~BinaryOnes ComplementUnary Operator. Flipps the bits~x
<<Binary Left ShiftLeft hand operand value is changed by moving the number of bits to the left specified on rightx << 3
>>Binary Right ShiftLeft hand operand value is changed by moving the number of bits to the right specified on right x >> 3

Compound Assignment Operator 

If you both relational and assignment operators you get compound assignment operators. Compound assignment Operators are also called short hand operators 

+=Add and assignx += 1x = x+1
-=Subtract and Assignx -= 1x = x -1
*=Multiply and Assignx *= 1x = x * 1
/=Divide and Assignx /= 1x = x /1
%=Modulus and Assignx %= 2x = x % 2
**=Exponent and Assignx **= 2x = x ** 2
//=Floor Divide and Assignx //=2x = x // 2
&=Bitwise And and Assignx &= 2x = x & 2
|=Bitwise OR and Assignx |= 2 x = x | 2
^=Bitwise XOR and Assignx ^= 2x = x ^ 2
<<=Binary Left Shift and Assignx <<= 2x = x << 2
>>= Binary Right Shift and Assignx >> =2x = x>>2

Miscellaneous Operators 

This one included all other operators we use which don’t fall under any one of above category, These operators are

.dotThis operators is used to access attributes or methods of objectmath.sqrt(3)
()groupThese operators are used to group expression to increase it’s precedence(x + y) * z
[]subscript / indexUsed to access elements/ items of containers/sequencesx[1]
::SliceUsed on sequences to extract part of the sequencex[::]
EllipsisSpecial value used in extending slice syntax for user defined containers

What is python, why python, How and where it is used ?

What is Python, why python, how and where it is used?

What is Python

Python is well know popular programming language Created by Guido Van Rossum. Python is general purpose, platform independent, interpreted, strong and dynamic typed programming language.

As opposed to old school static typed programming languages like c and c++, python is strong and dynamic typed programming language. Python is basically interpreted programming language, where we don’t have  the intermediate process called compilation to generate machine readable code. If you install python, you will get command line tool python which is basically the python interpreter. Which compiles the each line from the source into byte code called byte compilation and executed by python virtual machine. Python(syntax and vm) is written in programming language “C” is  so called CPython, which is majorly used implementation across the glob. There are other implementations and dialects of python Which are IronPython, Jython, MicroPython, PyPy, Numba, Stackless Python.  Dialets Cython, RPython.

Download and Install Python

You can download python stable at it’s official website python.org  See Installing Python for more information on how to install Python.

Why Python

Well there are more than hundred programming languages out there why choose python ?

Python is Open Source

First thing is first. Python is open source it is release under the license PSF which is compatible with GPL read more at python license.  No hidden fees no subscriptions you are free to use it for any purpose. Yes, It can be used for personal, non-commercial, commercial or proprietary use. You can browse or download the source code python here at cpython.

Python is Simple  and Elegant

Python is general purpose platform independent scripting language. It is not dedicated to specific field or application unlike PHP, SQL and other domain specific programming languages. It’s Eco system is very big.  As said it is general purpose, it can be used any where from build command line applications to emerging Artificial Intelligence  and deep learning.

More than any thing it’s beauty lies in it’s elegant syntax and structure. Python offers great power with out compromising code readability, infact many people love this language because of it’s elegant syntax and code readability.  Python doesn’t use open and close curly braces { }  to define blocks instead it uses indentation ( spaces ) which makes code look clean. It won’t us line ending delimiter semicolon (;) as well. Well multiple statement in the same line can be separated by semicolon otherwise it is not required.

Here, take a look at simple function definition and function call

def printGreetings():
   print("hello world!")

Python is very productive

Python is know for it’s shorter code. You can get work done with less number of lines compared to other programming languages. Because of it’s high level abstracts and rich libraries and frameworks shorted code is required to get task done. Python development speed is rapid if you compare  with other static typed languages like C and C++. Also it is very quick to test because of no build cycle. Unlike Java, c and c++ you don’t have to compile every time you make changes to your code, as it is interpreted which happens at runtime.

Quick Prototyping

Regardless of what kind of programming language or software stack you use. Python is good and quick to prototype your concept and ideas. Its makes it even easier with jupyter notebook .   If you type just python in the shell/cmd   you will be taken to python interpreter which is playground to quickly test your chunk of code. Imagine in C language you would need to compile whole file event to  test how particular expression evaluates.

Batteries Included

Python comes with rich set modules and packages called “standard library” . Python works great out of the box with the help of standard library. Python standard library includes custom data structures, numeric and mathematical tools, compression support, cryptography tools, Concurrency, networking, libraries for internet protocols cgi, http, smtp, xmlrpc etc and many other modules are included to get work done out of the box.

Rich Community

One of  the pythons strength is it’s community. It has huge community. If you are stuck somewhere or learning something, you are not along. You can get get help from stackoverflow python, daniweb python,  python mailing list or  community from corresponding frameworks or libraries like django, celery etc.  Visit Python Community.

Python is organized

Well organized and developed  by team of core developers, contributors and officers and board of directors.

Python every where

As said python is general purpose programming language it can be used in all most all areas. Python got rich set of frameworks and libraries, with the help of them you can use python for creating any kind of stuff you want.

Python for web applications

There you go, Python can be used to create  websites(backend) we got lot of web  framework for creating web applications. Django is popular among them.

Image result for django logo

Image result for flask logo


Tornado Web Server

Python for Desktop GUI applications


Python for networking

Python can be used to deal with protocols and networking stuff. Twisted is the popular event driven network programming framework.

Python for Data Sceince

Python is now effectively  being  used in data science competing   R programming language.  Following libraries made significant contribution to bring python to this level in data science.

numpy         scipy          pandas badge

Python for Data Visualization

matplotlibBokeh Logo

Python for scientific computing

scipy      Astropy: a community Python library for Astronomy     Biopython Logo  Logo

Python for Artificial Intelligence, Machine Learning and Deep learning

Every industry is going to take big leap by using artificial intelligence, machine learning and deep learning. Python is playing active roles in these fields. Following are python frameworks helpful in these fields



Python for 3D


Python game development



Along with above mentioned areas python is used in many other fields like internet of things, embedded systems, algorithmic trading and VOIP


Awesome third party frameworks and libraries

Python not only comes batteries included but also it got amazing third party frameworks and libraries. Few frameworks are mentioned in the above section . It is highly difficult to make a website or desktop application or any computer  application without using any kind of third party framework using pure python.  As they say, don’t reinvent the wheel. These frameworks help you to speed up your development by solving most common problems. So, you can focus on development by having most out of these frameworks.

A list of available python categorized  frameworks and libraries  can be found at  awesome-python .   This is may not be definitive but for sure it would hep you.

Where and Who is using Python

Want to read more? visit Python Success Stories