Computational Physics Python Programming

Python Programming

Computational Physics

Python Programming

Outline

¡ñ

¡ñ

Useful Programming Tools

¨C

Conditional Execution

¨C

Loops

¨C

Input/Output

Python Scripts, Modules, and Packages

Conditional Execution

¡ñ

¡ñ

Sometimes we only

want to execute a

segment of our program

when a certain condition

occurs.

Conditional Execution is

done with if statements.

Start

Is X < 0 ?

True

False

X = -X

Execute

Conditional

Code

if some_logical_condition :

statements_to_execute_if_true

statements_are_indented

if ends with end of indentation ...

no special symbols or words

Ready for More Fun

if...elif...else statement example

#

# enter

enter aa value

value to

to test

test

XX == input('Enter

input('EnterValue

Value of

of X:

X: ')')

#

# now

now we

we do

do the

the test

test with

with ifif statement

statement

ifif XX -2:

-2:

print

print 'X

'X > -2

-2 !'!'

else:

else:

print

print 'X

'X isis not

not between

between -2

-2 and

and 0.'

0.'

If X is less than 0

we do this

Otherwise, if X is

equal

To 0 we do this.

== means ¡°is equal to¡±

Otherwise, if X is

equal

To 1 we do this.

Otherwise, if X doesn't

Match any of above,

Condition

be any

Then we can

do this.

Logical Statement

Loops

¡ñ

¡ñ

¡ñ

¡ñ

For iterating_var in sequence:

statements

Loops are a special type

of conditional execution.

Loops iterate over a

sequence of items.

In python, the items can

be any items in a list.

We will often iterate

through the indices that

point to items in NumPy

arrays.

Item from

sequence

No More Items

In the Sequence

Next Item

Execute

Statement(s)

on Item

Ready for More Fun

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download