Introduction to Programming with Python Class Notes, Computer Programming I | CSE 142, Study notes of Linux skills

Material Type: Notes; Professor: Stepp; Class: COMPUTER PRGRMNG I; Subject: Computer Science and Engineering; University: University of Washington - Seattle; Term: Unknown 2008;

Typology: Study notes

Pre 2010

Uploaded on 03/18/2009

koofers-user-h3g
koofers-user-h3g 🇺🇸

5

(1)

10 documents

1 / 43

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to
Programming with Python
Programming with Python
Marty Stepp ([email protected])
University of Washington
Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides.
Except where otherwise noted, this work is licensed under:
http://creativecommons.org/licenses/by-nc-sa/3.0
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b

Partial preview of the text

Download Introduction to Programming with Python Class Notes, Computer Programming I | CSE 142 and more Study notes Linux skills in PDF only on Docsity!

Introduction to

Programming with PythonProgramming with Python

Marty Stepp ([email protected])

University of Washington

Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides.

Except where otherwise noted, this work is licensed under:http://creativecommons.org/licenses/by-nc-sa/3.

Outline

-^

Programming languages and Python

-^

Basic programs and numeric data

-^

Control statements

-^

Control statements

-^

Text processing

-^

What's next?

/^

CSE 142 at UW

Python

-^

Created in 1991 by Guido van Rossum (now at Google)^ – Named for Monty Python

-^

Useful as a

scripting language

-^ script

: A program meant for use in small/medium projects

-^

Used by:^ – Google, Yahoo!, Youtube– Many Linux distributions– Games and apps (e.g. Eve Online)

Installing Python

Windows: •^

Download Python fromhttp://www.python.org

-^

Install Python.

-^

Run

Idle

from the Start Menu.

Mac OS X: •^

Python is already installed.

-^

Open a terminal and run

python

or run Idle from Finder. Linux: •^

Chances are you already have

-^

Chances are you already have Python installed.

To check, run

python

from the terminal.

-^

If not, install from yourdistribution's package system.

Note:

For step by step installation instructions, see the course web site.

The Python Interpreter

-^

Allows you to type commands one-at-a-time and see results

-^

A great way to explore Python's syntax

Basic Programs andBasic Programs and

Numeric Data

Comments

-^ Syntax:

#^ comment text (one line) swallows2.py^1

# Suzy Student, CSE 142, Fall 2097 1 2 3 4 5 6

# Suzy Student, CSE 142, Fall 2097 # This program prints important messages. print "Hello, world!"print

# blank line

print "Suppose two swallows "carry" it together."print 'African or "European" swallows?'

Expressions

-^

expression

: A value or operation(s) to compute a value.

Example:

+^4

-^

Arithmetic operators:^ –^

+^ -

/^

add, subtract/negate, multiply, divide

–^ **

exponentiate

–^ ** –^ %

modulus, a.k.a. remainder

-^

precedence

: Order in which operations are computed.

/^

%^ **

have a higher precedence than

^3

*^

is^

(^

+^

3)^

*^4

is

Variables

-^

variable

: A named piece of memory that stores a value.

-^

assignment

: Stores a value into a variable.

  • Syntax:

name

expression

  • Examples:

x^

=^5

gpa

=^

x^

5

gpa

  • A variable can be used in expressions.

x^

+^4

is

Exercise

-^

This program's code is redundant.

Improve it with variables:

print "Subtotal:"print 38 + 40 + 30print "Tax:"print (38 + 40 + 30) * .09 print "Tip:" print "Tip:" print (38 + 40 + 30) * .15print "Total:"print 38 + 40 + 30 + (38 + 40 + 30) * .15 + (38 + 40 + 30) *.

Parameters

-^

parameter

: A value supplied to a command as you run it.

  • Syntax:

command

(^

value

command

(^

value

,^ value

,^

value

-^

Example:

-^

Example:

print

sqrt(

print

sqrt(

+^

*^

+^6

x^ =

print

sqrt(

x^

+^ sqrt(16)

Math commands

Function name

Description

abs(

value

)^

absolute value

ceil(

value

)^

rounds up

cos(

value

)^

cosine, in radians

floor(

value

)^

rounds down

log10(

value

)^

logarithm, base 10

max(

value

,^ value

)^

larger of two values

Constant

Description e^

2.7182818...

pi^

3.1415926...

-^

To use these commands, place this line atop your program:^ from

math

import

max(

value

,^ value

)^

larger of two values

min(

value

,^ value

)^

smaller of two values

round(

value

)^

nearest whole number

sin(

value

)^

sine, in radians

sqrt(

value

)^

square root

Control StatementsControl Statements

if

-^

if

statement

: Executes a set of commands only if a certain

condition is True.

Otherwise, the commands are skipped.

  • Syntax:^ if

condition

statements

-^ Example: -^ Example:^ gpa

= input("What

is

your

GPA?

")

if gpa

>^

2.0: print

"Your

application

is

accepted."