Lecture 02 | Core Elements of a Program

Lecture 02 | Core Elements of a Program


IDLE is the IDE (Integrated Development Environment) built for Python. So what does make a IDE:-
* Specialized Text Editor
* Provides Highlighting
* Auto Completion
* Smart Indent
* It also includes a Shell which is the environment that actually interprets the python code.
* Integrated Debugger, print statements are our friend.


Everything in python in Object, in fact Python code itself is an object. Each Object have a type which tells us
* Kind of object it is
* What can be done with the object

Type of Objects

There is a built in function called type, to find the “typeof” an object. There are two fundamental type of Object:-
* Scalar:  These are indivisible, i.e. basic type which cannot be created from any other type.
* Non Scalar


The following are the scalar type.

  • Integer
    Integers in Python are represented as int,  for every type we write, we call the value assigned to it as literal.

So when we check the type of 3 as shown below.


it will give the output as

<type 'int'>

So the type of literal 3 is int

Float’s correspond to what we call real numbers.


check the type of 3.2 as shown


it will give the output as

<type 'float'>

So in python 3 is an int but 3.0 is a float.

**NOTE:- **
* Do not float same as real number, they are similar but not the same.

Boolean have only two values.
* True, so type(True) gives &lt;type 'bool'&gt;
* False

There is also a value called None, whose type is &lt;type 'NoneType'&gt;. It is used when we want to fill the value of something temporarily.

Non Scalar

  • String
    Python does not have a fundamental type for characters called char, as available in other programming languages, In its place we have str.

when we check the type of 'a' we get

<type 'str'>

Literal of type str can be written with either ' ' or "".


  • Expression:- Sequence of operator and operands objects.



3 + 2

gives an output as 5, Now consider these two example.


/ is the divide operator, gives the output as 1 but consider this example.


gives the output as 1.5.

* Divide operator / on integer returns a literal with passing it by a  floor operator, which is not allowed in Python version 3.0
* User floating point number to get result as same as real number division.


Now consider this example,

'a' + 'b'

returns "ab"

By this we can deduce that the operator + is overloadedOverloaded Operators have a meaning that depends on the type of the operands.

Type Conversion

For the below code

'a' + 3

We get a Static Semantic Error, because it follows the grammar or syntax i.e. operand operator operand, but there is no meaning on adding a string with a int, the exact error we get is.

>>> 'a' + 3
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects

Type checking is actually very good for a programming languages, because it tells the programmer that he is trying to do something which will be error prone later.

The above code can also be changed to a valid syntax by doing this.

'a' + '3'   //here '3' is a string

also we can do this.

'a' + str(3)    //any type name can be used to convert on type to other, so integer 3 is converted to string '3'

int('3')        //string 3 is converted to integer 3

but this below code will give an static semantic error


error is

>>> int('0.0')
Traceback (most recent call last):
File "<string>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '0.0'

also this is a valid conversion.

int(2.1)        //will give a output 2


Program or Scripts: These are sequence of commands, each commands telling the interpreter to do something.

print type(5.6)



Consider this example,

x = 3

The above expression is an assignment expression, and x is a variable. In Python, a variable is just a name for an object.

An assignment expression, binds a variable with a value/object.


To receive input from a keyboard we use raw_input
There are two type of input statement in Python2,

NOTE:- Be careful python shell does print the quotes around the strings in shell.

Raw Input

raw_input, only one present in Python 3,  it outputs all the input as strings.


input does not do any type conversion.

Straight line and branching programs

All the program seen till now are Straight Line Program, which is executing a sequence of commands one after another without making any deviations.

Conditional Statement

Conditional statements are used to make decision about the flow of a programs. using this words.
* if
* else
* elif : else if


if x%2 == 0:
print "Even"
print "Odd"

The operator == is used to do a comparison.


We can also nest one condition inside another condition as shown below…:-

if x%2 == 0:
print "Even"
if x % 3 != 0
print "Hello"
print "Odd"

Here is what an if constructs does:-
* Checks the condition, if it is true executes the if block,
* Else block is executed
* It will either execute the if block or the else block but never both.


The indentation is very important in python.  Because of this the visual structure is following the semantic structure.

Looping constructs

When we add loops to a programming language, these programing language belongs to a class of programming language called Turing Complete. The concept of Loops are called iteration.
With loops, we can execute a same instructions multiple times.

Here is a simple loops.

while ans*ans*ans < abs(x):
ans = ans + 1



  1. MIT OCW
  2. Lecture code handout (PDF)
  3. Lecture code (PY)

Problem Sets

  1. Problem Set 0: Introduction to Python and IDLE (Due)
  2. Problem Set 0
  3. Problem Set 1 (Assigned)
  4. Problem Set 1 Due on Lecture 4

Check Yourself

What is a ‘type’?

The type tells the classification of an object, so that we can determine what operation can be performed on them.

What is an ‘expression’?

An expression is a combination of operand operator, which computes to give a value.

What is a type conversion?

When we have type 3 which is an int, we can convert its type to string using str(3) for operation or in an expression. So basically it converts one type to another.

What is a keyword?

Keyword are string which have special meaning in a programming language.

What is the difference between a straight line program and a branching program?

Straight line program have a single flow of control, where as a branching program have multiple flow of control depending on the no of branches.

What is a conditional?

A conditional statement is if, elif or else


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s