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. 

inin
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]
y[‘name’]
::SliceUsed on sequences to extract part of the sequencex[::]
y[2:]
EllipsisSpecial value used in extending slice syntax for user defined containers

Difference between JDK, JRE and JVM explained – Java

JDK vs JM vs JRE

If you are new to programming language, you might me wondering what is JVM, JRE and JDK.  Often many people are confused with these terms, lets what are exactly these.

JDK vs JRE vs JVM

JDK

JDK stands for Java Development Kit it is the subset of SDK which stands for Software Development Kit. JDK is what used by developer to write, compile, test, debug and trouble shoot the programs he is creating in java.  It is comprised of development tools + JRE.

JDK is required for the developer to write code,  compile, test, debug and monitor.

It contains tools to create distributable  jar files. It is nothing but a set of tools .

JRE

JRE stands for Java Runtime Environment. It’s job is to provide runtime environment and libraries to the program which is getting interpreted by JVM. JRE Contains JVM.  Java is  platform independent, infact JRE is what making it possible to compile once and execute anywhere. To execute java one should need JRE. If user wants run java program he should need JRE installed in his machine but JDK is not necessary.  JRE is available to download at Java SE downloads .

JVM

JVM stands for Java Virtual Machine. It is the critical part, it is part of JRE. If you install JRE, it includes JVM.  If you install JDK for development, which includes JRM which includes JVM.

JVM is the interpreter, which  executes the java (JVM) bytecode. Java source code is compiled into bytecode by java (javac) compiler. These compiled files typically have .class extension.  These .class files can be share across different computers and executed by Java Virtual Machine.

As explained above unlike C/C++,  java  class (compiled/bytecode) files are not directly executed on processor. JVM takes care of hardware specifics and platform. So, we don’t need to worry about recompiling code again and again for each targeted platform and hardware. Java source code if often compiled into .class files and all of those files and resource are aggregated into one  called jar (Java Archive) to distribute.

 

 

Understand JDK vs JRE vs JVM
Understand JDK vs JRE vs JVM

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!")

printGreetings()
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

Logo

Tornado Web Server

Python for Desktop GUI applications

   PySide

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

     Panda3D

 

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