You are on page 1of 32

Computing

ES112
Warm-up Exercises
Open a terminal, open IDLE
Generate the list [0,1,,100] in the console.
Write a very small program, save it, run it.
Close IDLE. Again open idle. Reopen the
program you just wrote.
Exercise
Write a python program to draw a staircase.
The number of steps will be given by the user.
Variables
Storing values
>>>message=HelloWorld!
>>>printmessage
HelloWorld!
>>>

Think of variables as the name for the container in which


the value is stored
What happens in the code below?

>>>message=HelloWorld!
>>>newmsg=message
>>>message=Anothermessage
>>>printnewmsg
Variable names
Variable names have to follow a particular format
First letter is a alphabet or _
Can include alphabets and numbers after that
Only special character is _
Which among the following are valid names?
iitgn
2014iitgn
iitIIT
Iitgn2014
iit$gn
iit_gn
_iitgn
Variable names
You get syntax error on names that are not allowed
>>>76trombones=bigparade
SyntaxError:invalidsyntax
>>>more$=1000000
SyntaxError:invalidsyntax
>>>class=ComputerScience101
SyntaxError:invalidsyntax

What is wrong with the name class?


Keywords
Python has 29 keywords

These cannot be used as variable names


Statements
Unit of execution
We have seen two different kinds of statements
Assignment and print
print1 1
x=2 2
printx

print is the most commonly used method of outputting


Expressions
Math expressions are all valid statements
Whether they produce an output will depend on
whether we are using python in the console or file
mode
>>>1+2
3 1+2
but does not produce output
test.py
Question
17
print3.2
Hello,World!
1+1

What output does the above produce in file


mode?
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
Exercise 1
Find out which operators are polymorphic and
how?
i.e., for a binary operator, which pairs of types are
allowed and what is the interpretation of the
operator
Comparisons
Comparisons

Results of comparisons is a truth value


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
>>>print1+2
3
>>>x=2
>>>printx
2
>>>print1+2
1+2
>>>print1+2,3+4
37
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


Set C to be 5*(F 32) / 9
Output C F=input()
C=5*(F32)/9
printC


Another example
What is the following piece of code doing?
x=input()
Y=input()
temp=x
x=y
y=temp
printx=,x
printy=,y

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: Very old Google interview questions


Conditionals
So far all the statements in the programs that
we wrote were executed.
What if we want only a few of them to be
executed?
Branching Programs

Score=?

score>=40

grade=Not grade=Fali
Conditionals:if..else

score=input()

ifscore>=40:
grage=pass
else:
grade=not
Conditionals:if..else

score=input()

ifscore>=40:
grage=pass
else:
t ion
grade=not
e n ta
In d
NT:
RTA
P O
IM
Indentation
Very important.
Wrong indentation may lead to error, or
unintended behavior.
Two ways to get correct indentation.
One tab (fixed number of spaces)
Take the suggestion given by IDLE: Onec you
press enter after the colon :, start from the place
where the cursor goes.
Indentation
What does the two program do?

score=input() score=input()

ifscore>=40: ifscore>=40:
grage=pass grage=pass
else: else:
grade=not grade=not
Indentation Error
What does the two program do?

score=input() score=input()

ifscore>=40: ifscore>=40:
grage=pass grage=pass
o es
else: else:
n t d
grade=not grade=not inde
r : u n t e r
r r o y ou
o nE an e l
t a ti t c h le v
e n a
m tatio n
Ind o t
n de n
in
Wrong Behavour
Counting pass/fail
score=input() score=input()

ifscore>=40: ifscore>=40:
grage=pass grage=pass
pasS=pasS+1 pasS=pasS+1
else: else:
grade=not grade=not
fail=fail+1 fail=fail+1

Will you get any error?


What is the difference?
Exercise:Temperature Conversion
Ask the user what kind of temperature
conversion she/he wants (e.g., celsius to
fahrenheit, or fahrenheit to celsius). Then ask
the temperature and convert it accordingly.
Exercise
Find the square root of a number. The number
could be any positive or negative real number.
Exercise
Write a program to solve quadratic equation
ax2+bx+c=0. The values of a, b and c will be
provided by the user. [Hint: if you want to print
the complex number 3+9i you can use
print 3,''+'',9,''i'' ]

You might also like