Friday, November 29, 2019

Using Python as a Calculator

Python shell can be used as mathematical calculator. Let’s take a look and understand how it works as a calculator.

First we have to start the Python shell and wait for the primary prompt “>>>” which is the symbol of three greater than sign. Once the Python Shell is ready (which shall not take long) with the prompt we can commence using some simple Python commands. 

Numbers


The Python interpreter serves as a simple mathematical calculator. We can type any expression, which will be calculated and the result will be displayed with one Enter. 

Syntax:

Operand  Operator  Operand

Example 1:

>>> 2 + 2
4
>>>

Parentheses can be used to group more than one operand

Example 2:

>>> 2 + 2
4
>>> 50 +5 * 2
60
>>> (50-5*2)/4
10.0 #Division always returns a floating point
>>> 9/5
1.8
>>>

The integer numbers like 1,2,3,4,9,5, have int data type while the numbers with the fractional part (e.g. 10.0, 1.8) are of float data type. As I have mentioned above in the code block that Division (/) always returns a float value irrespective of the decimal part in the result even if the result is whole number Python console will give a decimal point with zero. Though we can disregard the decimal part using double slash also called Floor Division (//) operator. and to calculate the reminder we can use modulus (%) operator. Below code block displays the examples of the same. Let's have a look.

Example 3:

>>> 9/5 #standard division returns a float
1.8
>>> 9//5 #floor division discards the fractional part
1
>>> 9%5 #the % operator returns the remainder of the division
4
>>> 1*5+4 #result * divisor + remainder
9
>>>

To calculate power for any number the symbol used is  double multiplication symbol **

Example 4:

>>> 5 ** 2 #Squared
25
>>>5 ** 4 #5 raise to the power 4
625
Python shell allows you to create variables on the shell and use them later on the script,

Note 1: No result will be displayed on the next line, rather next interactive prompt will be displayed.

Example 5:

>>> length =10
>>> width =5
>>> length * width
50
>>>

Note 2: if you will try to access a variable without declaring it, then an error will occur and it will be displayed on the prompt.

Example 6:


>>> m * 5 #Trying to access an undefined variable m
Traceback (most recent call last):
File "", line 1, in
m*5
NameError: name 'm' is not defined
>>>

Note 3: Mixed Data Type Operands can be used. but the int operand will be treated as float  operand because float operands always gives float result as discussed earlier.

Example 7:

>>> 4*5.5-8.1
13.9
>>>

Python shell can be used to Work with Strings. Click Here

Wednesday, November 27, 2019

Comments in Python

Comments

Comments are the statements which we don't want to be read by the compiler or the interpreter of any language.

Location
There are two types of Comments in most of the language.

1. Single Line
2. Multi Line

Single Line comments in Python begins with symbol #
Example: 

print ("I am before the comments")
#this lne is ignored by the complier
print ("I am after single line comment")

Output:

I am before the comments

I am after single line comment

Multi Line comments begins and ends with ''' three single quote symbol
print ("I am before the comments")
#this lne is ignored by the complier
print ("I am after single line comment")
''' these lines
will be
ignored by the
complier'''
print("I am after multiline comments")

Output: 

I am before the comments
I am after single line comment
I am after multiline comments

Compiler and Interpreter

Its a big confusion for the people Started working on python I have explained this hope this will clear your confusion :-
When we instruct Python to run our script, there are a few steps that Python carries out before our code actually starts crunching away:
  1. It is compiled to bytecode.
  2. Then it is routed to virtual machine.
When we execute a source code, Python compiles it into a byte code. Compilation is atranslation step, and the byte code is a low-level platform-independent representation of source code. Note that the Python byte code is not binary machine code (e.g., instructions for an Intel chip).
Actually, Python translate each statement of the source code into byte code instructions by decomposing them into individual steps. The byte code translation is performed to speedexecution.
Byte code can be run much more quickly than the original source code statements. It has.pyc extension and it will be written if it can write to our machine.
So, next time we run the same program, Python will load the .pyc file and skip the compilation step unless it's been changed. Python automatically checks the timestamps of source and byte code files to know when it must recompile. If we resave the source code, byte code is automatically created again the next time the program is run.
If Python cannot write the byte code files to our machine, our program still works. The byte code is generated in memory and simply discarded on program exit. But because .pyc files speed startup time, we may want to make sure it has been written for larger programs.
Let's summarize what happens behind the scenes.
When a Python executes a program, Python reads the .py into memory, and parses it in order to get a bytecode, then goes on to execute. For each module that is imported by the program, Python first checks to see whether there is a precompiled bytecode version, in a .pyo or .pyc, that has a timestamp which corresponds to its .py file. Python uses the bytecode version if any. Otherwise, it parses the module's .py file, saves it into a .pyc file, and uses the bytecode it just created.
Byte code files are also one way of shipping Python codes. Python will still run a program if all it can find are.pyc files, even if the original .py source files are not there.
Python Virtual Machine (PVM)
Once our program has been compiled into byte code, it is shipped off for execution to Python Virtual Machine (PVM). The PVM is not a separate program. It need not be installed by itself. Actually, the PVM is just a big loop that iterates through our byte code instruction, one by one, to carry out their operations. The PVM is the runtime engine of Python. It's always present as part of the Python system. It's the component that truly runs our scripts. Technically it's just the last step of what is called the Python interpreter.

Tuesday, November 26, 2019

Hello World! Program in Python



To print a statement in Python one has to use only Print Function:
Syntax of Print function is:

print (‘Your Text Goes Here’)
Or
print (“Your Text Goes Here”)

Note: A few extra spaces between the print function and the starting brace will not generate error

Hence this Hello World program will only contain one line

print ('Hello World!')

Output

Hello World!