Overview of Programming and Python
- Computers can be programmed
- Designed to do any job that a program tells them to.
- Program: set of instructions that a computer follows to
perform a task
- Commonly referred to as Software
- Programmer: person who can design, create, and test
computer programs
- Also known as software developer
Main Components of a computer
- CPU - Central Processing Unit: The "brain" of the computer
- ISA - Instruction Set Architecture: the specific set of
low-level instructions available to a CPU. Differs for various
CPU types (Intel Pentium, Mac G4, etc)
- ALU - Arithmetic and Logic Unit
- responsible for performing arithmetic calculations, as
well as logical operations (comparisons for equality, inequality,
for instance).
- Main Memory (RAM - Random Access Memory)
- storage close to CPU
- Faster to access than hard disk
- stores executing programs and data being currently worked
on
- Secondary Memory
- hard disk, floppy disk, CD, DVD, etc.
- Input devices
- mouse, keyboard, scanner, network card, etc.
- Output devices
- screen/console, printer, network card, etc.
- Operating System
- Examples: Mac OS, Windows XP, Linux
- Controls computer operations
- Manages allocation of resources for currently running
applications
How Computers Store Data: Memory Concepts
- bit: a binary digit
- Stores the value 0 or 1
- Smallest unit of storage in a computer
- byte: 8 bits
- Smallest addressable unit of storage in a computer
- Storage units (variables) in a program are 1 or more
bytes
- Each byte in memory has an address (a number that
identifies the location)
- Data stored in computer must be stored as binary number
- Characters are converted to numeric code, numeric code
stored in memory
- Most important coding scheme is ASCII
- ASCII is limited: defines codes for only 128 characters
- Unicode coding scheme becoming standard
- Compatible with ASCII
- Can represent characters for other languages
Programming, and Programming Languages
- How a program works
- Program - a set of instructions for a computer to
execute
- Program must be copied from secondary memory to RAM each
time CPU executes it.
- CPU executes program in cycle:
- Evolution of Programming languages
- Machine Language
- CPU designed to perform simple operations on pieces of
data
- Examples: reading data, adding, subtracting,
multiplying, and dividing numbers
- Based on machine's core instruction
set
- Needed by computer, hard for humans to read (1's and
0's)
- Example: 1110110101010110001101010
- To carry out meaningful calculation, CPU must perform
many operations
- Assembly Language
- translation of machine instructions to symbols,
slightly easier for humans to read
- Example: ADD $R1, $R2, $R3
- Well, we know it has something to do with addition!
- High-level procedural languages
- Abstraction of concepts into more human-readable terms
- Closer to "natural language" (i.e. what we speak)
- Easy to write and design, but must be translated for
computer
- Examples include C, Pascal, Fortran
- Object-oriented languages
- Abstraction taken farther than procedural languages
- Objects model real-world objects, not only storing data
(attributes), but having inherent behaviors (operations,
functions)
- Easier to design and write good, portable, maintainable
code
- Examples include Smalltalk, C++, Java, Python
- Bridging the gap between high-level code and machine code
- Interpreted languages
- Translates and executes instructions in high-level
language program
- Interprets one instruction at a time
- No separate machine language program
- Python is an interpreted
language
- Compiled Languages
- A compiler program translates
source code (what the programmer writes) to machine language (object
code)
- A linker program puts various
object code files together into an executable program (or other
target type, like a DLL)
- C and C++ are compiled languages
- Java is a mix of both!
- Key Words, Operators, and Syntax: an Overview
- Key words: predefined words
used to write program in high-level language: has specific
meaning
- Operators: perform operations
on data: math operators for arithmetic operations
- Syntax: set of rules to be
followed when writing program
- Syntax error: prevents code from being translated
- Statement: individual instruction used in high-level
language
Building Programs
Software development
Involves more than just writing code
- Analysis and problem definition
- Design - includes design of program or system structure,
algorithms, user-interfaces, and more
- Implementation (coding)
- Testing - can be done during design, during implementation,
and after implementation
- Maintenance - usually the major cost of a software system.
Not part of "development", but definitely part of the software life
cycle
The Python Language
Basic Creation and Execution of a Python program
- Python must be installed and configured prior to use.
- One of the items installed is the Python interpreter
- Python interpreter can be used in two modes:
- Interactive mode: enter statements on keyboard
- Script mode: save statements in Python script
Interactive mode
- When you start Python in interactive mode, you will see a prompt.
- Indicates the interpreter is waiting for a Python statement to be typed
- Prompt reappears after previous statement is executed
- Error message displayed If you incorrectly type a statement
- Good way to learn new parts of Python
Script mode
- Statements entered in interactive mode are not saved as a program
- To have a program use script mode:
- Save a set of Python statements in a file.
- The filename should have the .py extension
- To run the file, or script, type
python filename
at the operating system command line
Integrated Development Environments
- IDLE (Integrated Development Program): single program that provides tools to write, execute and test a program
- Automatically installed when Python language is installed
- Runs in interactive mode
- Has built-in text editor with features designed to help write Python programs
- An Integrated Development Environment (IDE) is a software
package that includes all necessary tools for creating a program.
Typically includes:
- Text editor
- Compiler
- Loader
- Debugger
- ability to launch and run applications from within IDE
environment
- Other useful tools
Programming is about Problem Solving
- Algorithm - a finite sequence of steps to perform a
specific task
- To solve a problem, you have to come up with the
necessary step-by-step process before you can code it
- This is often the trickiest part of programming
- Some useful tools and techniques for formulating an
algorithm
- Top-down Refinement: Decomposing a task into
smaller and simpler steps, then breaking down each step into
smaller steps, etc
- Pseudocode: Writing algorithms informally in a
mixture of natural language and general types of code statements
- Flowcharting: If you can visualize it, it's often
easier to follow and understand!
- Testing - algorithms must also be tested!
- Does it do what is required?
- Does it handle all possible situations?
- Syntax vs. Semantics
- Syntax -- the grammar of a language.
A syntax error: "I is a programmer."
- Semantics -- the meaning of language
constructs
Correct syntax, but a semantic error: "The
car ate the lemur."
A sample Python program
def main(): # main function, start of program
number = input('Enter a number: ') # Read input from the user: password.
print 'The number you entered is:', number # Output the result with a message.
print 'Thank you, have a good day!' # Output a Good bye message.
main() # This line results in the def main(): line to be executed.
A link to this code file