Archives October 2019

Rules of precedence used by python to evaluate an expression

operator precedence

Python calculates expressions based on the below rules , these are generally the same as those in algebra.

Expression in Parenthesis
  Parenthesis ( ) are evaluated first , any expression between the parenthesis will evaluate the value first and results will be further processed.
Exponentiation
  are the second set of expression that are evaluated, if there are multiple expression then Python will evaluate each from right to left.
Multiplication , Division and Modulus
  are on the same level of precedence. In case of multiple occurrence Python will evaluate each from left to right.
Addition and Substraction
  are on the same level of precedence. these are evaluated last unless covered in parenthesis.In case of multiple occurrence these are evaluated from left to right.

Complete list of precedence is available at Python.org

Variable and Assignment in python

Variable

a name identifier in memory that stores the data during the execution of the program. Python is dynamic and declaring a variable is very simple. a = 10 is simply a variable assignment statement.

Assignment operator

In Python assignment operator is simply single = sign , the value on the right get assigned to the name identifier on the left. Below are some of the example assignments
a = 20
b = 40
total = a
total = a + b

Arithmetic operator

In Python arithmetic operator include + - * / %
Examples :
Addition   c = a + b
  Calculates the value of a + b and assigns to c
Substraction   c = a - b
  Substracts the value of b from a and assigns to c
Multiplication   c = a * b
  Multiplies the value of a with b and assigns to c
Exponentiation   c = a ** b
  Assigns a power b example 3 ** 2 = 9
Division   c = a / b
  Divides the value of b from a and assign the new value to c
Floor Division   c = a // b
  Divides the value of b from a and yeild the real number of higher end and assign the new value to c
Modulus   c = a % b
  gives the remainder value as the output 9 % 2 gives remainder 1

Arithmetic plus Assignment short form

Arithmetic operator can be used along with the assignment operator to form a shorter statement

Statement Full Statement
a += b a = a + b
a -= b a = a – b
a *= b a = a * b
a /= b a = a / b
a //= b a = a // b
a %= b a = a % b
a **= b a = a ** b

Python Pro and Cons : Advantages and Disadvantages of Programming Language

python pros and cons

Python is one of the most popular language today , it is dynamic, general purpose programming language, it has a long history of over few decades but in recent time it has gained enormous attention specially with cloud computing.

Python is not only used for cloud application but with huge module library it is used all sort of applications such as desktop application , scripting and automation , web application , machine learning , data analysis and many more.

One of the recent projects that has raised Python value is OpenStack Cloud , this project is written in Python and utilizes libraries to integrate different part of the Cloud. This has raised the value of Python in Enterprises such as Facebook , Microsoft , Google and Amazon to name a few.

Though Python is highly in demand there are some advantages and disadvantages of using Python and it is good to understand those before considering Python for your next project.

Pro’s

Easy Python Syntax

     Python was designed keeping simplicity in mind , its code is simple and easy to read as compared to other high level languages such as Java , C++ etc.

Object Oriented

     Python is Object oriented , one of the main concept that makes it is a great choice is concept of objects, everything is an object including all the basic data types such as strings and integers are considered object and developer can apply methods on these objects.

Libraries

     Python has a huge collection of libraries that makes the life much more easier , many of the task can be performed with very few lines of code. There are two type of libraries , Python Standard Library and 3rd Party libraries developed by Vendors for there own products.

Python is extensible

     Python code can integrate with C / C++ and that makes it very extensible language , basically Python was used mainly for system integration in the past but in recent years it has developed into a more robust platform.

Portable

     Languages like C and C++ the code varies from platform to platform example libraries for Windows and Linux might differ but that is not the case with Python , here you code once and you can run it anywhere. This is called write once and run anywhere.

Interpreted

     Python code get executed Line by Line and thus debugging is easier and simpler

Open Source , Community support

     Python is an open source language and it has a huge community support , more and more people are using Python and its support is increasing as well , many Enterprises have jumped in to adopt Python and that is increasing community greatly.

Con’s

Speed

     Python as compared to low level languages like C / C++ is slow due to the structure, Python code is compiled into byte code at runtime while the C / C++ code is compiled before run-time.

Memory Utilization

     for memory intensive application Python is not a good choice , the flexibility of data-types brings overhead in form of memory consumption.

Threading Issues

      Multi-threading is another area where Python does not really perform as compared to other High level languages like Java , this is due to Global interpreter lock (GIL) which allows only one thread to execute at a time. As a result, single-threaded application perform better with Python.

Mobile Development

      As compared to Java , Python is not greatly used for Mobile applications and thus does not have great support for Mobile devices. It is easy to use Python for mobile purposes but configuring require some extra effort.
Python is not native to mobile environment and it is seen by some programmers as a weak language for mobile computing. Android and iOS don’t support Python as an official programming language. Still, Python can be easily used for mobile purposes, but it requires some additional effort.

Conclusion

      Python is ideal for creating tools , automation and applications, it is getting more and more support at both community and enterprise level , it is the most in demand language for programming jobs currently available in the market.

Copyright © 2021 | SaitCare Hub SDN BHD