How to Think Like a Computer Programmer beginning Python - PowerPoint PPT Presentation

1 / 52
About This Presentation
Title:

How to Think Like a Computer Programmer beginning Python

Description:

A field at the intersection of Math, Engineering, etc. The Algorithm ... lesser lights, fearing ridicule, couldn't summon up the nerve to admit that ... – PowerPoint PPT presentation

Number of Views:357
Avg rating:3.0/5.0
Slides: 53
Provided by: donalds45
Category:

less

Transcript and Presenter's Notes

Title: How to Think Like a Computer Programmer beginning Python


1
How to Think Likea Computer Programmerbeginning
Python
2
Outline
  • What Is Computer Science?
  • Computer Programming
  • Languages Expressing things to the Computer
  • Fundamental Concepts
  • The Process of Programming
  • What Is Computer Science?
  • Computer Programming
  • Languages Expressing things to the Computer
  • Fundamental Concepts
  • The Process of Programming

3
What is Computer Science
  • Not using a computer
  • Not just programming
  • A field at the intersection of Math, Engineering,
    etc

4
The Algorithm
  • Fundamental inventions in formal thinking
  • Formal Logic (Athens, 4 C. AD)
  • Mathematical Proof (Greece, 585 AD)
  • Secular Observation of Nature(600 AD)
  • Arabic Numerals, including Zero(India, 8 C. AD)
  • Algebra (al-Khwarizmi, 810 AD)
  • Probability (late 1500's)
  • Scientific Method (early 1600s)
  • Now The Algorithm

5
Computer Scientists
  • Who are Computer Scientists?
  • Computer Scientists study lots of things
  • e.g., "What are Algorithms Theoretically Capable
    of?
  • But we're going to look at the applied version
    Programming

6
Computer Programmers
  • Their rumpled clothes, their unwashed and
    unshaven faces, and their uncombed hair all
    testify that they are oblivious to their bodies
    and to the world in which they move. These are
    computer bums, compulsive programmers.

7
Computer Science as a Forcing Function
  • Computers have proven immensely effective as
    aids to clear thinking. Muddled and half-baked
    ideas have sometimes survived for centuries
    because luminaries have deluded themselves as
    much as their followers or because lesser lights,
    fearing ridicule, couldn't summon up the nerve to
    admit that they didn't know what the Master was
    talking about. A test as near foolproof as one
    could get of whether you understand something as
    well as you think is to express it as a computer
    program and then see if the program does what it
    is supposed to. Computers won't make
    enthusiastic noises to ensure their promotion or
    camouflage what they don't know. What you get is
    what you said.
  • Hogan

8
Computer Science as the New Literacy
  • The modes of thought that come from CS are
    influencing a huge number of fields
  • Modeling Simulation
  • We can create all sorts of worlds inside the
    computer to work with

9
Computer Programming
  • Something everyone should learn
  • A deep understanding of programming, in
    particular the notions of successive
    decomposition as a mode of analysis and debugging
    of trial solutions, results in significant
    educational benefits in many domains of
    discourse, including those unrelated to computers
    and information technology per se. Papert

10
What does a computer do?
  • Computers internally just keep doing the same
    thing over and over
  • Get the next instruction
  • Do whatever it says
  • Go back to step 1
  • But its internal instructions are in binary --
    too hard for people

11
Simplifying
MatrixCompute(double values, int size)
for(int i0 iltsize i) for(int j0
jltsize j) if(iltj
valuesisizejlt0.0)
valuesisizej valuesjsizei
valuesisizej 2valuesisizej

High-level language
Compiler
pushl ebpmovl esp,ebpmovb hi_temp,aladdb
lo_temp,almovb 0,ahadcb 0,ahmovb
2,blidivb blmovb al,av_templeaveret
Assembly Language
Assembler
1001010101101011010101010010101010111101000011010
1001110101011101011000110101001001101010101010101
0101101111010101010100111101010101010111010101010
1101110101011011010110101110100010101000010101010
11000100001010101010111110101010101011111111
Machine Code
12
Natural Languages
  • Computers dont understand English
  • Need to deal with
  • Ambiguity
  • Redundancy
  • Literalness
  • So, we express what a computer should do in a
    formal language

13
Formal Languages
  • A program usually just a document -- a written
    list of instructions
  • You saw some of this with HTML -- A markup
    language is sort-of a programming language

Poetry Ambiguity and metaphor are important
Prose Literalness and structure important
Program Formal, precise structure
14
PBJ Robot
15
Programming Languages
  • Some simple ones are built in to programs you use
    every day
  • Excel is a programming language (and contains
    another)
  • Word Macro Language
  • There are also General Purpose Programming
    Languages

16
Types of Languages (1/2)
  • All general-purpose programming languages are
    equally powerful
  • It's all about how we organize things for people
  • In linguistics, we have
  • SVO languages (English)
  • VSO languages (Irish, Tagalog)
  • others

17
Types of Languages (2/2)
  • Procedural (BASIC, C)
  • Jump(WillNewman) Run(MikeViamari)
  • Declarative (OO Java, Python)
  • WillNewman.Jump MikeViamari.Run
  • And others (e.g., functional Excel)

18
Pseudocode
  • There are lots of details to programming
    languages (for another day)
  • Well use something called pseudocode
  • Most of the way to a programming language
  • Excludes some details
  • With some plain English mixed in

19
Programs
  • The computer programmer is a creator of
    universes for which he alone is responsible.
    Universes of virtually unlimited complexity can
    be created in the form of computer programs.

20
Core concepts
  • (A quick taste of what you'll be learning in the
    Python unit)
  • State
  • Expressions
  • Variables
  • Control
  • Abstraction
  • Reference

21
State
22
Expressions
  • gt print Hello
  • Hello
  • gt print (53)8
  • gtprint (55-2)23

23
Variables
  • Not exactly like Algebra
  • In Algebra x5 (or 5x) expresses a truth
  • In a program, x5 updates something in the
    computers memory
  • (and 5x wouldnt work its not a valid
    statement)

gt x0 gt print x 0 gt x5 gt print x 5
24
Control Structures
  • Let you make decisions
  • for, while,

if(xgt5) print x is big else print x is
small
25
Complexity
  • Controlling complexity is the essence of computer
    programming
  • -Brian Kernigan

26
Abstraction
  • This is what Computer Scientists dream about
  • Programming is building layers of abstraction
  • Reuse

27
But what do you DO?
  • Design
  • Requirements
  • Use cases
  • User Interface
  • Software
  • Static
  • Dynamic
  • Implement
  • Test
  • Debug

28
Design
  • Most programming is not done sitting in front of
    a computer
  • You need to really understand what the program
    will do
  • How it interacts with the outside world
  • And how it is structured internally

29
  • Mostly, when you see programmers, they aren't
    doing anything. One of the attractive things
    about programmers is that you cannot tell whether
    or not they are working simply by looking at
    them. Very often they're sitting there seemingly
    drinking coffee and gossiping, or just staring
    into space. What the programmer is trying to do
    is get a handle on all the individual and
    unrelated ideas that are scampering around in his
    head. Charles M Strauss

30
Implement
  • Top-down design, bottom-up implementation
  • Incremental
  • Was mostly about data structures and algorithms
  • While thats still important, now more about
    using libraries of existing functions together

31
Test
  • Testing a complicated program is difficult
  • Some people write the tests before the programs
  • Programming is like sex, one mistake and you
    have to support it for the rest of your life.

32
Debug
  • At the beginning, much of your time
  • In many ways more challenging than implementing
  • When you have eliminated the impossible, whatever
    remains, however improbable, must be the truth.
    Sherlock Holmes

33
  • That's what's cool about working with
    computers. They don't argue, they remember
    everything and they don't drink all your beer.

34
Beginning Python
35
What is Python?
  • A REAL Programming Language!
  • Developed in 1990 by Guido Van Rossum in the
    Netherlands
  • Named after Monte Pythons Flying Circus!
  • Openly available at no charge!
  • Freely distributed on the Web
  • Lots of Libraries of Python Routines available
  • Increasingly popular for writing scripts for the
    Web

36
How does Python work?
  • It is an INTERPRETED Language, (Like HTML and
    Java) so that it is easy to write and execute
    programs in an interactive mode
  • It has a very clear and easy Syntax
  • It is portable so it runs on many different
    Operating Systems and Machines (Windows, MacOS,
    Linux, Unix, etc)

37
What are we going to do with Python in CS2?
  • Learn some basic commands and capabilities of the
    language
  • Printing data, evaluating expressions, inputting
    data, variables, loops, decisions, functions,
    boolean expressions
  • Learn how to use the programming environment
  • Learn how to write simple but useful programs

38
What tools will you require?
  • You will need the Python 2.6.4 Interpreter and
    programming environment
  • Already installed on the computers in the MSLAB
  • Can be downloaded and installed on your personal
    machine from
  • www.python.org/2.6/
  • You will need the Python Tutorial
  • Found under resources on the CS2 website

39
General Conventions
  • Python is Case Sensitive!Be Careful!
  • Anything you name (variables, etc) must be
    referred to exactly as initially typed
  • Python Commands are always in lowercase!
  • Comments can be inserted anywhere by using a
    before the comment text
  • Some commands require indentation to work
    properly

40
Many of the Examples in the Lectures are taken
from the on-line tutorial, but not all of them!
41
Our First Program
  • A program that prints a greeting!
  • print Hello, World!
  • When this program runs, it outputs
    Hello, World!
  • The text inside of the quotes in the program is
    referred to as a String, or String Variable

42
Another way to achieve the sameresult
  • greeting Hello, World!
  • print greeting
  • The output is Hello, World!
  • Note that we assigned the variable name greeting
    with the value of the literal string inside of
    the quotes.

43
Expressions
  • Another more complicated program
  • print 2 plus 2 is, 22
  • print 3 times 3 is, 33
  • print 35 divided by 5 is, 35/5
  • The output will be 2 plus 2 is 4 3
    times 3 is 9 35 divided by 5 is 7

44
Operations on Numbers
  • Operation Symbol Example
  • Exponentiation 5 2 25
  • Multiplication 2 3 6
  • Division / 14 / 3 4
    14 / 3.0
    4.6666
  • Remainder 14 3 2
  • Addition 1 2 3
  • Subtraction - 4 - 3 1

45
More Variable Assignment Examples
  • value 48
  • net_price 4.56
  • repeat_word word 5
  • Variables are created and recognized dynamically,
    that is when they appear in your program with a
    value assigned to them
  • Variables must be assigned before you refer to
    them otherwise an error will occur
  • The values that are assigned to variables in
    Python are called Objects, each object having a
    reserved place in memory with a pointer
    connecting it to the assigned variable

46
Variable manipulation
  • Variables can be used in conjunction with
    themselves to affect certain operations
  • Example
  • a3
  • aa4
  • aa3
  • print a
  • WHAT is the output of this program?

47
Printing variables
  • Variables may be printed by themselves or in
    succession
  • Example
  • number 55
  • bignumber 9
  • print number
  • print bignumber
  • print number , bignumber
  • (puts them on the same line)

48
More String Operations
  • String Repetition
  • word Yo!
  • print word 5
  • Results in Yo! Yo! Yo! Yo! Yo!

49
Inputting Data into your program
  • We will focus on user input which is the simplest
    type and suitable for our purposes
  • Input command syntax is
  • speed input (enter speed )
  • Or
  • name raw_input (enter your name )
  • Whats the obvious difference between these input
    commands?

50
Strings
  • Strings are literal collections of text
    characters
  • Strings must be enclosed in either single or
    double quotes
  • Strings may be manipulated by a variety of
    operators (repetition, concatenation, etc)
  • Concatenation example
  • word abc
  • word word def
  • print word results in abcdef

51
Ok, so lets write a simple program with the
stuff weve covered so far
  • A simple program to check on our progress
  • name raw_input (enter your name )
  • speed input (enter your average speed )
  • time input (enter your exact travel time in
    hours )
  • print name your travel distance is , time
    speed, miles

52
Demo and Questions
Write a Comment
User Comments (0)
About PowerShow.com