

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















Identifiers
Identifiers
Print statements
Variables and assignments
Variables and assignments
Definite loopsB
i^
b
(i
d fl
asic types: numbers (int and float)
Math library
Accumulator problem
Back to the robots
Built-in names
Built in names
Can’t use as regular identifiers
Python reserved words:
Python reserved words:
and
del
for
is^
raise
assert
elif
from
lambda
return
assert
elif
from
lambda
return
break
else
global
not
try
class
except
if^
or
while
continue
exec
import
pass
with
def
finally
in^
yield
Q1b
Fragments of code that produce or calculate new
Fragments of code that produce or calculate new^ data values
Examples
Examples
Literals
: indicate a specific value
Identifiers
: evaluate to their assigned value
Identifiers
: evaluate to their assigned value
Compound
expressions using
operators
: +, -, *, /, **
Can use parentheses to group
Can use parentheses to group
Q2-
Syntax:
A “ l t” t
b
fill d with
i
Syntax:
print
A “slot” to be filled with any expression
Repeat indefinitely
expr
print , , …, ,
p
rint , , …,
Note: trailing comma
p
p
,^
p
,^
,^
p
Semantics?
Is this allowed?
Is this allowed?
print “The answer is:”, 7 * 3 * 2
Q
Variable
Variable
Identifier that stores a value
A value must be
assigned
to the variable
A value must be
assigned
to the variable
=
(syntax)
Assignment
Assignment
Process of giving a value to a variable
Uses
= (equal sign)
Uses
(equa s g )
x = 0. x = 3.9 * x * (1 – x)
A list of things
A list of things
For example:
[2 3 5 7]
[2, 3, 5, 7]
[“My”, “dog”, “has”, “fleas”]
Some can be generated by the range function:
Some can be generated by the range function:
range()
range( )
range(, )
range(, , )
Definition
Definition
Loop: a control structure for executing a portion of aprogram multiple times
Definite: Python
knows
how many times to iterate the
body of the loop
Syntax:for in :
Trace this by hand:
More items in
no
a = 0for i in range(4):
a = a + i
yes
a
a + i
print a
= next item
An
accumulator combines parts of alist using looping
list
using looping. We’ll use this ideaoften this term!often this term!
Q
Data
Data
Information stored and manipulated on a computer
Different kinds of data will be stored and manipulated
Different kinds of data will be stored and manipulatedin different ways
Data type
yp
A particular way of interpreting bits
Determines the possible values an item can have
Determines the operations supported on items
Built-in function
type()
returns the data type of
Built in function
type()
returns the data type of
any value
Find the types of:
3, 3.0, -32, 64.0, “Shrubbery”, [2, 3]
Find the types of:
3, 3.0,
32, 64.0,
Shrubbery , [2, 3]
Why do we need different numerical types?
Operations on int are more efficient
Operations on int are more efficient^
Compute algorithm for operations on int are simple and fast
Counting requires int
g
q
Floats provide approximate values when we need realnumbers
Q
dir()
dir()
dir()
help()
help()T
hi h f
i^
b il
i
o see which functions are built-in, type: ^
dir(builtins_) F^
f h
or usage of them, type: ^ help(builtins)
Q12a
Quadratic formula to find real roots for quadraticQuadratic formula to find real roots for quadratic
equations of the form ax
2
Solution:
2
2
Solution:
a
ac
b
b
x
2
4 2
−
a
ac
b
b
x^
2
4 2
−
Write out the Python expression for the firstformulaformula.
If you have time, test it IDLE
Q
Python
Mathematics
English
pi
π^
Approximation of piA^
i^
i^
f
e^
e^
Approximation of
e
sin(x)
sin x
The sine of x
cos(x)
cos x
The cosine of x
tan(x)
tan x
The tangent of x
log(x)
ln x
The natural (base e) log of x
l^
0( )
l^
h^
b^
0 l
f
log10(x)
log
10
x^
The base 10 log of x
exp(x)
xe
The exponential of x