Download Python - Bioinformatics Research Group

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Programming
Computers and Programs
Python Programming, 1/e
1
The Universal Machine
What is a computer program?
A detailed, step-by-step set of instructions telling a computer
what to do.
If we change the program, the computer performs a
different set of actions or a different task.
The machine stays the same, but the program changes!
Programs are executed, or carried out.
All computers have the same power, with suitable
programming, i.e. each computer can do the things
any other computer can do.
2
Program Power
Software (programs) rule the hardware
(the physical machine).
The process of creating this software is
called programming.
Why learn to program?
Fundamental part of computer science
Having an understanding of programming
helps you have an understanding of the
strengths and limitations of computers.
3
Types of Programs
Information Systems- UI portal, payroll
Embedded Systems – Dedicated systems- programs
built for specific devicesA program embedded in an elevator to open and close the
door
Heart monitors- tracks patient heartbeat and reports status
Critical SystemsSafety Critical-Aircraft control towers
Economic critical- program controlling All ATM
should crash.
Health critical – Program controlling insulin pump4
gives overdose and patient dies
Programming Languages
Natural language has ambiguity and
imprecision problems when used to
describe complex algorithms.
Programs expressed in an unambiguous ,
precise way using programming languages.
Every structure in programming language
has a precise form, called its syntax
Every structure in programming language
has a precise meaning, called its
semantics.
5
Programming Languages
Programming language like a code for
writing the instructions the computer
will follow.
Programmers will often refer to their
program as computer code.
Process of writing an algorithm in a
programming language often called coding.
6
Machine Languages, Assembly Languages,
and High-level Languages
Three types of programming languages
Machine languages
Strings of numbers giving machine specific instructions
Example:
+10001110 (these would really be in binary)
+10011111
+11101101
Assembly languages
English-like abbreviations representing elementary computer
operations (translated via assemblers)
Example:
LOAD
ADD
STORE
BASEPAY
OVERPAY
GROSSPAY
7
7
Machine Languages, Assembly
Languages, and High-level
Languages
High-level languages
Instructions closer to everyday English
English is a natural language. Although high level
programming languages are closer to natural languages,
it is difficult to get too close due to the ambiguities in
natural languages (a statement in English can mean
different things to different people – obviously that is
unacceptable for computer programming). However, this
is a big research area of computer science.
Use mathematical notations (translated via compilers)
Example:
grossPay = basePay + overTimePay
Interpreter – Executes high level language programs without
compilation.
8
8
Programming Languages
High-level language
c=a+b
This needs to be translated into
machine language that the computer
can execute.
Compilers convert programs written in a
high-level language into the machine
language of some computer.
9
Programming Languages
Interpreters simulate a computer that
understands a high-level language.
The source program is not translated
into machine language all at once.
An interpreter analyzes and executes
the source code instruction by
instruction.
10
Programming Languages
Compiling vs. Interpreting
Once program is compiled, it can be
executed over and over without the source
code or compiler. If it is interpreted, the
source code and interpreter are needed
each time the program runs
Compiled programs generally run faster
since the translation of the source code
happens only once.
11
Programming Languages
Interpreted languages are part of a more
flexible programming environment since
they can be developed and run
interactively
Interpreted programs are more portable,
meaning the executable code produced
from a compiler for a Pentium won’t run on
a Mac, without recompiling. If a suitable
interpreter already exists, the interpreted
code can be run with no modifications.
12
The Key Software Trend:
Procedural High-level Languages
A procedural programming paradigm is a
programming technique that works in a
step by step method. In order for this
method to work you have to create a
sequence of instructions of steps, thus
telling the computer what to do first and
then second. Typical procedural
programming languages are C, FORTRAN,
pascal, and BASIC
13
Procedural Languages
14
Questions
ISP stands for --------
A) Internetwork Standard Procedure
B) Intranet Secure Protocol C) Intranet Service Provider
D) Internet Service Provider
As useful as internet is to us, the following enumerates the
disadvantages except
A) Lack of data security
B)It can become addictive
C)Research information for school projects and business
D)Moral abuse through pornographic pictures
15
The Key Software Trend:
Object-Oriented programming
object oriented programming is a
programming language model organized
around objects rather than actions and around
data rather than logic. Object is best described
as its own class in programming and it has
two key areas, state and behaviour. Oriented
programming is also a type of programming,
not only does it describe the database of a
data structure but it also describes the types
of operations. Most languages are actually
developed using the OOP concept. C++ and16
Java are examples of OOP
Oriented programming
Objects
Reusable software components that model
items in the real world
Meaningful software units e.g ui portal
Student objects, course objects, objects, invoice objects, audio
objects, video objects, file objectsetc.
Any noun can be represented as an object
Very reusable
More understandable, better organized,
and easier to maintain than procedural
programming
Favor modularity
17
17
OOP
Student portal
Student is an object – attributes are name,
matno, dob
Examples: Java, C++, and Python
18
The Process Of Creating A
Computer Program
Translation
• A special computer program (translator) translates
the program that was just written by the
programmer into the only form that the computer
can understand (machine language/binary)
Program Creation
• A person (programmer) writes a
computer program (series of
instructions).
• The program is written and
saved using a text editor.
• The instructions in the
programming language are high
level (look much like a human
language).
Execution
• The machine language
instructions can now be
directly executed by the
computer.
19
Recap: Software development
method
1.
2.
3.
4.
5.
6.
Specification of needs/requirements
specification
Problem analysis
Design and algorithmic representation
Implementation
Testing and verification
Documentation
20
The Software Development
Process
Implement the Design
Translate the design into a computer
language.
In this course we will use Python.
21
21
Debug the Program
Test/Debug the Program
Try out your program to see if it worked.
If there are any errors (bugs), they need to be
located and fixed. This process is called
debugging.
Your goal is to find errors, so try everything that
might “break” your program! Hacking
Competition
Types of Programming Errors
22
22
Types Of Programming Errors
1.Syntax/translation errors
2.Runtime errors
3.Logic errors
23
Syntax/ Translation Errors
1.Syntax/ Translation Errors
•Each language has rules about how statements are to be structured.
•An English sentence is structured by the grammar of the English
language:
-The cat sleeps the sofa.
Grammatically incorrect:
a preposition is missing . --the sofa‟.
Begin
if score =‘80’
Then print grade=‘7’
end
24
Runtime Errors
Occur as a program is executing (running).
•The syntax of the language has not been violated (each statement
follows the rules/syntax).
•During execution a serious error is encountered that causes the
execution (running) of the program to cease.
•With a language like Python where translation occurs just before
execution (interpreted) the timing of when runtime errors appear
won‟t seem different from a syntax error.
•But for languages where translation occurs well before execution
(compiled) the difference will be quite noticeable.
•A common example of a runtime error is a division by zero error.
25
Logic Errors
•The program has no syntax errors.
•The program runs from beginning to end with no runtime errors.
•But the logic of the program is incorrect (it doesn‟t do what it‟s
supposed to and may produce an incorrect result).
•Program name: error_logic.py
print ("This program will calculate the area of a rectangle“)
length = int(input("Enter the length: "))
width = int(input("Enter the width: "))
area = length + width
print ("Area: ", area)
26
Components of Programs:
Functions Vs Procedures
Usually we want to execute several statements together
that solve a common problem. One way to do this is to
use a functions or procedures.
Put the statements in a function and call the function in
a main program to use it
Functions must return a value, procedures are called to
execute a series of instructions, they may not return a
value.
Each programing language has a syntax to implement
functions and procedures
27
Variables
Set aside a location in memory.
Used to store information (temporary).
This location can store one ‘piece’ of
information.
At most the information will be accessible as
long as the program runs.
Some of the types of information which can be
stored in variables: studentname=‘Bola’
DOB = 10/10/2010
28
Variable Naming Conventions
Style requirement: The name should be meaningful.
Style and Python requirement: Names must start with a
letter (Python requirement) and should not begin with
an underscore i.e ( _ ) (style requirement).
Python requirement: Can't be a keyword (see next
slide).
Style requirement: Names are case sensitive but avoid
distinguishing variable names only by case.
Style requirement: Variable names should generally be
all lower case.
Style requirement: For variable names composed of
29
multiple words separate each word by capitalizing the
Questions
What are the two major categories of software
A)Operating and disk operating software B)System and operating software
C)Utility and application software
D)System and application software
What are the three phases of computer operations? A)
Hardware, software, middleware
B) Input, processing, output (C) Data , communication,
processing D) Analog, Digital, Hybrid
30
Python: The Basics
31
History
Python was created by Guido van Rossum in the late 1980’s at
the National Research Institute for Mathematics and Computer
Science in the Netherlands.
Python is derived from many other languages, including
Modula-3, C, C++, Algol-68, SmallTalk, and Unix shell and other
scripting languages.
Python is a general purpose interpreted, interactive, objectoriented, high-level programming language.
Current release version is version 2.6.? available for UNIX, PC
and Mac. Version 3 is under development.
32
32
What is Python
Python is a high-level, interpreted, interactive and object orientedscripting language, designed to be highly readable, commonly uses
English keywords.
Python is Interpreted: This means that it is processed at runtime
by the interpreter and you do not need to compile your program
before executing it. This is similar to PERL and PHP.
Python is Interactive: This means that you can actually sit at a
Python prompt and interact it directly to write your programs.
Python is Object-Oriented: This means that Python supports
Object-Oriented style or technique of programming that
encapsulates code within objects.
Python is Beginner's Language: Python is a great language for
the beginner programmers and supports the development of a
wide range of applications.
33
33
Python Features
Feature highlights include:
Easy-to-learn: Python has relatively few keywords, simple
structure, and a clearly defined syntax. This allows the student
to pick up the language in a relatively short period of time.
Easy-to-read: Python code is clearly defined and if well written
visually simple to read and understand.
Easy-to-maintain: Python's success is that its source code is
fairly easy-to-maintain.
A broad standard library: One of Python's greatest strengths is
the bulk of the library is very portable and cross-platform
compatible on UNIX, Windows, and Macintosh.
Interactive Mode: Support for an interactive mode in which you
can enter results from a terminal right to the language,
allowing interactive testing and debugging of snippets of code.
34
34
Python Features
Portable: Python can run on a wide variety of hardware
platforms and has the same interface on all platforms.
Extendable: You can add low-level modules to the Python
interpreter. These modules enable programmers to add to or
customize their tools to be more efficient.
Database Aware: Python provides interfaces to all major
commercial databases.
GUI Programming: Python supports GUI applications that can
be created and ported to many system calls, libraries, and
windows systems, such as Windows MFC, Macintosh, and the X
Window system of Unix.
Scalable: Python provides a better structure and support for
large programs than shell scripting.
35
35
Important Features
Apart from the above mentioned features, Python has a big list of
important structural features that make it an efficient programming
tool, few are listed below:
Built-in high level data types: strings, lists, dictionaries, etc.
The usual control structures if, if-else, if-elif-else, while plus a
powerful (for) iterator.
It can be used as a scripting language or can be compiled to
byte-code for building large applications.
Supports automatic garbage collection – memory managementit claims memory no longer in use and utilizes it for other
processes.
36
It can be easily integrated with Fortran, C, C++, CORBA, and
Java, etc……
36
Getting Python & Help
Getting Python:
The most up-to-date and current source code, binaries,
documentation, news, etc. is available at the official website
of Python:Python Official Website : http://www.python.org/
Documentation
You can download the Python documentation from the
following site. The documentation is available in HTML, PDF,
and PostScript formats: http://docs.python.org/index.html
Tutorial
You should definitely check out the tutorial on the Internet
at: http://docs.python.org/tutorial/.
37
37
38
Python Identifiers
A Python identifier is a name used to identify a variable,
function, class, module, or other object. An identifier starts with
a letter A to Z or a to z or an underscore (_) followed by zero or
more letters, underscores, and digits (0 to 9).Python does not
allow punctuation characters such as @, $, and % within
identifiers.
Python is a case sensitive programming language. Thus Variable
and variable are two different identifiers in Python.
39
39
Variables
Set aside a location in memory.
Used to store information (temporary).
This location can store one ‘piece’ of
information.
At most the information will be accessible as
long as the program runs.
Some of the types of information which can be
stored in variables: studentname=‘Bola’
DOB = 10/10/2010
40
Variable Naming Conventions
Style requirement: The name should be meaningful.
Style and Python requirement: Names must start with a
letter (Python requirement) and should not begin with
an underscore i.e ( _ ) (style requirement).
Python requirement: Can't be a keyword (see next
slide).
Style requirement: Names are case sensitive but avoid
distinguishing variable names only by case.
Style requirement: Variable names should generally be
all lower case.
Style requirement: For variable names composed of
41
multiple words separate each word by capitalizing the
Python Reserved Words or
keyword
The following list shows the reserved words in
Python. These reserved words may not be used
as constant or variable or any other identifier
names. Reserved words contain lowercase letters
only.
and, exec, not, assert, finally, or, break, for, pass,
class, from, print, continue, global, raise, def, if,
return, del, import, try, elif, in, while, else, is,
with, except, lambda, yield.
42
42
summary
General programming concepts
Programming Paradyms
Introduction to python
43
Running Python
NEXT CLASS WE SHALL RUN OUR FIRST PYTHON PROGRAM!
44
44
Python Interfaces
IDLE – Integrated DeveLopment
Environment or Integrated Development
and Learning Environment- a crossplatform Python development environment
PythonWin – a Windows only interface to
Python
Python Shell – running 'python' from the
Command Line opens this interactive shell
For this course, we'll use IDLE, but you
can try them all and pick a favorite
45
IDLE – Development
Environment
IDLE helps you
program in Python
by:
color-coding your
program code
debugging
auto-indent
interactive shell
46
Example Python
Hello World
print “hello world”
Prints hello world to
standard out
Open IDLE and try it
out yourself
Follow along using
IDLE
47