Introduction to Python Programming

foremanyellowΛογισμικό & κατασκευή λογ/κού

7 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

61 εμφανίσεις

Introduction to
Python Programming
CSE 110: Introduction to Computer Science

Labs begin on Wednesday

Labs will meet in CS 2129, NOT the CS
SINC site!

Homework 1 is due in class on Friday

Check Blackboard for your Academic
Honesty form check-off
What is Python?

Python is a programming language

A notation for describing algorithms

Used to instruct a computer to do something

Python is a high-level language

It’s much more readable than 1s and 0s

Python was named for Monty Python, not the snake!
Why Python?

Python is interpreted

Get immediate feedback as you program

Python has simple syntax

It has very clear rules for writing code

Enforces good programming style

Python is free and runs everywhere
Getting Started

Download Python from the Python Home Page:

Python programs can be written live in the
Python interpreter, or they can be written in a
text editor and then executed as scripts

We’ll do both this semester

The IDLE program lets us work both ways
Our First Program

Traditionally, the first program anyone
writes in a new language is “Hello, world!”

In Python, this can be done in one line:
print “Hello, world!”
Commands and Expressions

is a Python command

It tells Python to do something

All Python commands are lowercase

“Hello, world!”
is an expression

It has a value

In this case, it’s a string (a sequence of characters)
More Expressions

Numbers are also expressions

5 is a integer expression

12.36 is a floating-point expression

Expressions can consist of other expressions

2 * 9 is an expression

Python also has Boolean expressions

are equivalent to 1 and 0
Arithmetic in Python

  addition

-  subtraction

*  multiplication

/  division

%  remainder (modulus)

**  exponentiation (raising to a power)
Division in Python

Dividing two floating-point numbers works
as you would expect

4.5 / 3 gives you 1.5

Dividing two integers gives you the integer
part of the division (the quotient)

6 / 4 gives you 1, not 1.5!

Use modulus (%) to get the remainder

6 / 4 gives you 2 (the remainder)
More on Printing

moves to the next line when done

We can combine

Use commas to separate values:
print 2, “squared is”, 2*2

Spaces are added between elements

Comments are used to describe code

Should tell
something is being done,
it’s being done (we can see that)

Comments provide a record for the author
and others who read the code later

Python comments begin with a  and run
to the end of the line

Variables are names that refer to values

Storing a value in a variable means you
don’t have to rewrite it every time

To create a variable in Python, just assign it
a value

number_of_guesses  5

Now, we can use number_of_guesses
Basic Input

We can read from the keyboard using a
command called raw_input()

Put a string in the parentheses to serve as a

Save the result in a variable
response = raw_input(“What’s your name?”)
Loops in Python

Loops are used to repeat groups of statements

Python has two kinds of loops: for and while

for loops execute a fixed number of times

while loops execute a variable number of times

Basic syntax

statements to be repeated

Note the indentation!

Python uses indentation (spaces) to indicate
that several statements are part of a group

A sequence is just a collection of values

Ex. characters in a string

loop sets its loop variable to each
value in turn

The loop executes one time for each
value in the sequence
Sequences of Integers

Python uses the range() command to
generate a list of integer values

List does not include supplied end value

range() takes 1, 2, or 3 arguments

range(3) produces [0, 1, 2]

range(1, 5) produces [1, 2, 3, 4]

range(1,6,2) produces [1,3,5]
Loop Example
 Print the characters in a string
for i in “This is an ex-parrot”:
print i
Another Example
 Print the values 1-9, all on the same line
for q in range(1, 10):
print q,  Print the next value in the range
Putting It All Together

We can combine variables and loops:
 A simple countdown
max  10
for i in range(max, 0, -1) :  count down by 1s
print i
Next Time


conditional (
) statements

Read through Chapter 4 of Dawson