Professional Documents
Culture Documents
languages
Goal:
Need
a
way
to
describe
algorithmic
steps
such
that
computer
can
use
them
to
execute
process
Programming
language
denes
syntax
and
seman;cs
needed
to
translate
our
computa;onal
ideas
into
mechanical
steps
Checker
Interpreter
Input
Output
Result
Compiler
Object code
Computation Process
Checker
Interpreter
Output
Checker
Compiler
Interpreter
Output
Object code
Checker
Interpreter
Output
Python
programs
Program
(or
script)
is
a
sequence
of
deni&ons
and
commands
Deni9ons
evaluated
and
commands
executed
by
Python
interpreter
in
a
shell
Can
be
typed
directly
into
a
shell,
or
stored
in
a
le
that
is
read
into
the
shell
and
evaluated
Objects
At
heart,
programs
will
manipulate
data
objects
Each
object
has
a
type
that
denes
the
kinds
of
things
programs
can
do
to
it
Objects
are:
Scalar
(i.e.
cannot
be
subdivided),
or
Non-scalar
(i.e.
have
internal
structure
that
can
be
accessed)
Scalar
objects
int
used
to
represent
integers,
e.g.,
5 or
10082!
float
used
to
represent
real
numbers,
e.g.,
3.14
or
27.0!
bool
used
to
represent
Boolean
values
True and
False!
The
built
in
Python
func9on
type returns
the
type
of
an
object
>>> type(3)!
<type int>!
>>> type(3.0)!
<type float>!
Expressions
Objects
and
operators
can
be
combined
to
form
expressions,
each
of
which
denotes
an
object
of
some
type
The
syntax
for
most
simple
expressions
is:
<object>
<operator>
<object>
Operator precedence:
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
Changing
bindings
Variable
names
can
be
rebound,
by
invoking
new
assignments
statements.
For
example,
if
we
now
execute:
radius = 11.2!14.3
Non-scalar
objects
We
will
see
many
dierent
kinds
of
compound
objects
The
simplest
of
these
are
strings,
objects
of
type
str!
Literals
of
type
string
can
be
wri?en
using
single
or
double
quotes
abc!
abc!
123
this
is
a
string
of
characters,
not
the
number
just sequence of characters that are all concatenated together
Operators
on
strings
>>>
3
*
a
aaa
>>>
a
+
a
aa
>>>
a
+
str(3)
a3
>>>
len(abc)
3
operator overloading
using same operator to do different things
letting operator decide on basis of type of object
Slicing:
Providing
input
If
we
are
going
to
write
programs
or
scripts,
we
will
need
a
way
to
incorporate
input
from
a
user.
We
use
the
Python
func@on
raw_input,
as
in:
>>> name = raw_input(Enter your name: )!
Enter your name: Eric Grimson!
>>> print(Are you + name + ?)!
Are you Eric Grimson?!
use typecasting to get int type input. ex. int(raw_input('Enter a no: '))
Some
observa@ons
Comments
appear
aLer
a
#
These
are
very
valuable,
as
they
help
a
user
understand
decisions
the
programmer
has
made
in
crea@ng
the
program
Well
commented
code
should
be
very
readable
by
a
user
Branching
programs
The
simplest
branching
statement
is
a
condi&onal
A
test
(expression
that
evaluates
to
True or
False)
A
block
of
code
to
execute
if
the
test
is
True!
An
op<onal
block
of
code
to
execute
if
the
test
is
False!
A
simple
example
x = int(raw_input('Enter an integer: '))!
if x%2 == 0:!
print()!
notice the indentation
indentation matters
print('Even')!
else:!
print()!
print('Odd')!
print(Done with conditional')!
Some
observa<ons
The
expression
x%2 == 0 evaluates
to
True
when
the
remainder
of
x divided
by
2 is
0!
Note
that
== is
used
for
comparison,
since
= is
reserved
for
assignment
The
indenta<on
is
important
each
indented
set
of
expressions
denotes
a
block
of
instruc<ons
x < z:!
is least)!
is least)!
is least)