Lecture2b Phython
Lecture2b Phython
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!
>>>
>>>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
>>>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
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