You are on page 1of 43

Introduction to Python

Programming
Chapter 1

Python for Informatics: Exploring Information


www.pythonlearn.com

Course Materials

Unless otherwise noted, the content of this course material is licensed under a Creative
Commons Attribution 3.0 License.
http://creativecommons.org/licenses/by/3.0/.
Copyright 2010- Charles Severance
Couse materials link: http://www.pythonlearn.com/book.php

Pre-Requisite: Please Install


Python

http://www.pythonlearn.com/install.php

tp://www.pythonlearn.com/install.php

Back to the Introduction

Summary
This is a quick overview of Chapter 1
We will revisit these concepts throughout the course
Focus on the big picture

Why be a programer?

Users .vs. Programmers


What users
see

What
programmers
see

Users .vs. Programmers

Make our
own life
easier

Programmer

Make
users life
easier
Programmi
ng
language

User

Hardware Architecture

http://upload.wikimedia.org/wikipedia/commons/3/3d/RaspberryPi.jpg

Software
Input
and
Output
Devices

Central
Processin
g
Unit

Main
Memory

What
Next?

Generic
Computer

Secondary
Memory

Definitions
Central Processing Unit: Runs the Program - The CPU is
always wondering what to do next? Not the brains
exactly - very dumb but very very fast

What
Next?

Input Devices: Keyboard, Mouse, Touch Screen


Output Devices: Screen, Speakers, Printer, DVD Burner
Main Memory: Fast small temporary storage - lost on reboot
- aka RAM

Secondary Memory: Slower large permanent storage - lasts


until deleted - disk drive / memory stick

Software
Input
and
Output
Devices

What
Next?

Central
Processin
g
Unit
if x< 3:
print

Main
Memory

Generic
Computer

Secondary
Memory

Talk to Computer

Compiler and Interpreter


High-level programming
Compile language
r
Interpret
single instruction as
output
Example: C Compiler, Python
Interprete
r
Compile
entire program as
input
Example:
Easy to BASIC
write
Transferable between different
devices

Machine
Language
01001001000101001
01000100010001000
00100010000001001
0001000111000
Tedious and
difficult
Hardware
dependent

Python as a Language

Parseltongue is the language of


serpents and those who can converse
with them. An individual who can
speak Parseltongue is known as a
Parselmouth. It is a very uncommon
skill, and may be hereditary. Nearly all
known Parselmouths are descended
from Salazar Slytherin.
http://harrypotter.wikia.com/wiki/Parseltongue

Python is the language of the Python


Interpreter and those who can
converse with it. An individual who
can speak Python is known as a
Pythonista. It is a very uncommon skill,
and may be hereditary. Nearly all
known Pythonistas use software
inititially developed by Guido van
Rossum.

Lets Talk to Python...

Talking to Python

Syntax
Symbolic expressions

Semanti
cs
Meanings

Errors
Syntax Errors: When you are speaking gibberish &*9$
%6&gfdg4%%X

What computer will do.

Errors
Logic Errors: Drink the water then open the bottle.

Semantic Errors: Semantically right but doesnt make Why it is


not giving
sense
me the
correct
results???

Elements of Python
Vocabulary / Words - Variables and Reserved words
(Chapter 2)

Sentence structure - valid syntax patterns (Chapters 35)

Story structure - constructing a program for a purpose

Reserved Words

You can not use reserved words as variable names /


identifiers

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 pr

Chapter 2

Sentences or Lines
x=2
x=x+2
print x

Variable

Operator

Assignment Statement
Assignment with expression
Print statement

Constant

Reserved Word

How to Run Python

Interactive Python and


Python Scripts
Interactive Python:
Python IDLE
You type directly to Python one
line at a time and it responds
Python Script:
Write code in a Python file and execute
the file to run all the commands in the
file

Make the Python Script

Run Python Script

Make the Script

Run Python Script

Run Python Script

Type in your
python
installation
directory
Type in your
.py file
directory

C:\Python27\python.exe "C:\Users\xueyuan\school files\ISTM 3119


Python Programming\Session 1\Demonstration Example\I hate
python.py"

Program Steps or Program


Flow
Like a recipe or installation instructions, a program is a
sequence of steps to be done in order

Some steps are conditional - they may be skipped


Sometimes a step or group of steps are to be repeated
Sometimes we store a set of steps to be used over and

over as needed several places throughout the program


(Chapter 4)

Sequential Steps
print x

Give x an initial
value of 1
Print x value

x=x+1

Increase x by 1

print x

Print xs new value

x=1

x=1
print x
x= x+1
print x

When a program is running, it flows from one step


to the next. We as programmers set up paths for
the program to follow.

Chapter 2

Conditional Steps

x=5

Yes

X < 10 ?

print
'Smaller'

Yes

X > 20 ?

print 'Bigger'

x = int(raw_input("please
input x:"))
if x<10:
print "Smaller"
elif x>20:
print "Bigger"
else:
print "Finish"

print 'Finish'

Chapter 3

n=5

No

n>0?

Repeated Steps

Yes

n=5

print n
n = n -1

print
'Blastoff'

Chapter 5

while (n>0):
print n

n = n-1
else:
print "Blastoff!"
Loops (repeated steps) have iteration
variables that change each time through a
loop. Often these iteration variables go
through a sequence of numbers.

Why Python?
A Cool name?

Job Prospects
Average Salary for Skill: Python
Job

National Salary Data

Software Developer

$72,853 (Java $69,914, VB $59,049)

Data Scientist

$95,929

Data Analyst

$64,576

Software Engineer

$81,610

From Google

Job Prospects

Easy to Learn
C++

Java
public class Test {
public static void main(String args[]) {
for( int i = 1; i < 11; i ++)

using namespace std;


int main()
{
for( int i = 1; i < 11; i = i+ 1 )

{
System.out.println(value of i is:+i); }
}}

{
cout << "value of i is:" << i << endl;
}
Return 0; }

Python
for i in range(0, 10):
print "value of i is:%i"%i

Python for Data Science


Core libraries NumPy, SciPy, pandas, matplotlib,
Ipython
A glue language CPython (C), Jython, IronPython
Object-oriented
Strength in Web development
NBA player stats https://github.com/bradleyfay/
NBAStats

You might also like