Cover page images (keyboard)

Introduction to Computer Science Using Python Part II

Patti Ordóñez & Sue Evans

Adapted from the CS1 Course at Swarthmore College by Lisa Meeden


Hit the space bar for next slide

Learning Outcomes

Programming Languages

High-Level Languages

Compiled Languages


Compiler

Example Compiled Language

Compiled

Interpreted Languages


Interpreter

Example Interpreted Language

Interpreted

where JRE stands for Java Runtime Environment

Python Language



Python

Python Reserved Words

and del for is raise
assert elif from lambda return
break else global not try
class except if or while
continue exec import pass yield
def finally in print  

Assignment

You can assign a value to a variable using the equals sign.

Syntax

<variable> = <expression>

An <expression> is anything that equates to a value



Variable

Semantics

Technically, a variable name is a reference to a particular location in the computer's memory. An analogy is like the term 'President'. It is a reference to a position. The person that the term refers to varies according to who is in the position.

Variable Before/After

Whenever you use a variable name on the left-hand side of an assignment statement, you are asking the interpreter to store the value of the right-hand side into the computer's memory.

Whenever you use a variable name anywhere else, you are asking the interpreter to retrieve the value from the computer's memory.

For example:

x = 5 
stores the value 5 into a memory location called x

y = x + 1 
retrieve the value of x, add 1 and store the result into a memory
location called y

Design Patterns

There are many common problem solving patterns in computer science. As you are designing a solution, it is often helpful to consider which pattern would be appropriate.

The most basic pattern is called: Input, Process, Output

In other words, request input from the user, process it in some way, and then produce some output to display the result.

When we are designing solutions we often use pseudocode, a more precise form of English. It is not as precise as a program, but more precise than prose.

Let's write the pseudocode to ask the user's name and then say hello.

In Class Exercise

Write the pseudocode to solve the following problem.

Have the user enter the current year and his or her graduation year, and then print how many more years they have left of college.

Programs

A program is a named collection of statements that can be re-used again and again.

What is involved in creating and running a program in Python?

The emacs text editor that we'll be using for this class indents the code automatically and displays color-coded text based on the syntax of the language. The ".py" extension lets it know that you're coding in Python.

The Chaos Program

# File:        chaos.py
# Written by:  John Zelle
# Date:        ?/?/04
# Section:     2
# Email:       zelle@umbc.edu (not really)
# Description: A simple program executing chaotic behavior
#              Inline comments have been intentionally omitted

print "This program illustrates a chaotic function"

x = input("Enter a number between 0 and 1: ")

for i in range(10):
	x = 3.9 * x * (1-x)
	print x

What would you expect this program to output?