Intro To Computing

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Intro to Computing

ES102

Lecture 3

Some drawings and material from CS101 course pages


and lectures of various universities, esp. IITB and
OCW@MIT
Python math operations
Order of operations
● PEMDAS
– Operations having same precedence are executed
left to right
– Important to keep order in mind when doing
integer division
– e.g. 4 / 6 * 3 vs 4 * 3 / 6 vs (4 * 3) / 6 vs
4 * ( 3 / 6)
Polymorphic operators

● Polymorphism = the meaning of the math


operators e.g. + , * , / depend on the types of
the input
>>> 1 + 2

>>> 1.0 + 2

>>> 1.0 + 2.0

>>> 1 + “2”

>>> “1” + “2”
Comparisons
Comparisons
● Results of comparisons is a truth value, boolean
expression
– True or False
>>> x = 2
>>> x >= 2
True
>>> x <= 1
False
>>> 1.2 * 6.0 == 7.2
False

● Note the difference between “=” and “==”


● Note also the floating point equality comparison
– “==” should be avoided with floating point
Printing
● Print command executes the expressions before printing
● Each print would appear in a new line
print 1 + 2  
x = 2
print x
print “1 + 2”
print 1+2, 3+4
print “x =“,x+1 
Printing
● Print command executes the expressions before printing
● Each print would appear in a new line
print 1 + 2 3
x = 2 2
print x 1 + 2
print “1 + 2” 3, 7
print 1+2, 3+4 3
print “x =“,x+1 
General framework of a “simple”
program

Calculate C = 5*(F – 32) / 9


General framework of a “simple”
program

Calculate C = 5*(F – 32) / 9

● Input F from keyboard


F = input()
● Set C to be 5*(F – 32) / 9 C = 5*(F – 32)/9
print C
● Output C   
 
Puzzle
● Suppose we have two variables x and y that are
numbers
● x and y contain some value initially
– Say x = 1, y = 2
● We want to write a code such that the values of x and y
swap at the end of it
– i.e. should have y = 1 and x = 2
● We are not allowed to use any other variable
● What would you do?

Courtesy: Google interview questions


Another example
● What is the following piece of code doing?
x = input()
Y = input()
X = x + y
y = x ­ y
print “x = “, x   
print “y = “, y
 
Functions
● What functions have we seen?
– input()
– type()
● Functions to convert from one type to another
>>> x = 3.45
>>> int(x)
3
>>> str(x)
'3.45'
>>> float(3)
3.0
>>> float('3.141')
3.141

 
Math functions
● In order to use math functions, we have to use
the module
– in python, useful functions are stored in packages
known as modules

>>> import math
>>> a = math.log10(2)
>>> print a Notice the math.log10
0.301029995664 notation.

This is how the functions


inside the module math
need to be called
 
Try these too
>>> decibel = math.log10 (17.0)
>>> angle = 1.5
>>> height = math.sin(angle)
>>> degrees = 45
>>> angle = degrees * 2 * math.pi / 360.0
>>> math.sin(angle)
>>> math.sqrt(2)/2 Notice this, this is not a function
but uses dot notation.
Why?
Composing functions
● Any math expression can be used as the
argument of a function that takes a number
>>> x = math.cos(angle + math.pi/2)

● The results can be passed to another directly


>>> x = math.exp(math.log(10.0))

– What is the above doing?


Creating new functions
● Why?
Creating new functions – syntax
def  <functionname>  ( <parameters> ):
   <statements>
spaces

● Note that we needed to give 2 spaces before writing the


statement --- this is a MUST in python.

● This is indentation – python's way of recognizing when the


function ends
● Function block ends in the line when the indentation finishes
Function Block
def print_lyrics():
  print “Papa kehte hain”
  print “Beta engineer banega” } Function block

print “Lyrics of an all­time favorite”

● The last print statement is not part of the function


Indentation
def print_lyrics():
  print “Papa kehte hain”
    print “Beta engineer banega”

print “Lyrics of an all­time favorite”

IndentationError: expected an indented block

● If you do not keep the indentation consistent, you will get


this error
Calling a function
def print_lyrics():
  print “Papa kehte hain”
  print “Beta engineer banega”
print_lyrics()

● After a function has been defined it has to be called


● The code inside the function block is executed only when
it is called

You might also like