Core Elements of Program: Compilation, Interpretation, Objects, Type Casting




For a code to run on your machine, machine should first understand the code and run the script accordingly.

There are two options for modern high level programming languages. A compiled language or an interpreted language.


1. Compiled Language: Your code is first converted into Low Level instructions which in turn are executed when the script is run.
Compiler Object Code Executable
Just so you know, 'Checker' checks for static semantic errors (Refer to 3rd point, Aspects of Language of this link).

'Compiler' produces an object code, 'Interpreter' runs the object code line by line & hence produces the Output.

2. Interpreted Language: As shown in following flow chart, Interpreted Language sequentially converts each step into low level machine instruction and executes. Python belongs to this type of Programming Language.

Interpreted Language Flow chart

I recommend you to read this discussion on Whether Python is Compiled or Interpreted.

Part B:

Objects:
  • Scalar: (Discussed Below)
    • integers -  int eg.: 3 or 90756
    • decimal - float eg.: 99.5 or 0.0007
    • boolean - bool it can either True or False
  • Non Scalar: (Discissed here)
    • strings - 'abc'
    • list - [1,2,3,4,5,6]
    • dictionary - {'Jan':1, 'Feb':2, .........so on... '1':Jan, '2':Feb......so on} 
>>> a = 5 #This statement is called assignment since 5 is being assigned 
>>> a #since 5 is int, 'a' is called int variable.
5
>>> b = 3.33
>>> b
3.33
>>> type(6) #type() is an in-built function that returns 'type' of value passed in
<type 'int'>
>>> type(a)
<type 'int'>
>>> type(7.166666)
<type 'float'>
>>> type(b)
<type 'float'> 

Operations on int & float

>>> 6+3 #addition
9
>>> 5-4 #subtraction
1
>>> 6*4 #multiplication
24
>>> 18/2 #division
9
>>> 6*6 
36
>>> 6**2 #raise to the power. 6 to the power 2
36
>>> 3.14*20
62.800000000000004
>>> (2+3)*4 # parentheses has the highest priority 
20
>>> 2+3*4
14

In absence of parentheses,  priority order is: ** has highest,then * and  /, then + and -


Comaprisons 

  • i > j   -   returns True if strictly greater than
  • i >= j  -  returns True if greater than or equal
  • i < j  -  returns True if strictly less than
  • i <= j  -  returns True if less than or equal
  • i == j  -  returns True if equal
  • i != j  -  returns True if not equal    

Operators on bools

  • a and b is True if both are True 
  • a or b is True if at least one is True 
  • not a is True if a is False; it is False if a is True
Type Conversions (Type Casting)

>>> float(5) #makes 5 a float
5.0
>>> int(4.555) #it returns the integer part of the number
4
>>> int(4.444)
4
>>> round(4.555)
5.0
>>> round(4.444) 

Note the difference between int() and round() 

Note:
This is a part of what I learn in an online Open Course Ware offered by MIT on edX
Its for my personal reference & also for those who have missed the course.
You too can enroll yourself on edX (if they are still offering the course MITx 6.00x)