Docsity

Python Programming: Loops, Identifiers, and Variables, Study notes of Software Engineering

Partial preview of the text

Download Python Programming: Loops, Identifiers, and Variables and more Study notes Software Engineering in PDF only on Docsity!

ASSIGNMENT,LOOPS ANDLOOPS, ANDBASIC TYPESCSSE 120 – Rose-Hulman Institute of Technology

Outline (ch. 2, some of ch. 3)

(^

,^

Identifiers

Identifiers

Print statements

Variables and assignments

Variables and assignments

Definite loopsB

i^

b

(i

d fl

B

asic types: numbers (int and float)

Math library

Accumulator problem

Back to the robots

Reserved Words

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^

print

yield

Q1b

Expressions

p

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-

Output Statements

p

Syntax:

A “ l t” t

b

fill d with

i

Syntax: †

print †

print

A “slot” to be filled with any expression

Repeat indefinitely

†

print

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

Variables and Assignments

g

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)

Sequences

q

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(, , )

Definite loops

p

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 :

Flowchart for a for loop

p

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 types

yp

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

Finding the Type of Data

g

yp

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

Built-in Help

p

dir()

dir()

dir()

help()

help()T

hi h f

i^

b il

i

T

o see which functions are built-in, type: †^

dir(builtins_) F^

f h

F

or usage of them, type: †^ help(builtins)

Q12a

Math library functions

y

Quadratic formula to find real roots for quadraticQuadratic formula to find real roots for quadratic

equations of the form ax

2

  • bx +c = 0

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

More math library components

y

p

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