SlideShare una empresa de Scribd logo
1 de 48
16IT205 & OBJECT
ORIENTED
PROGRAMMING
CONCEPTS
1
COURSE OUTCOMES:
 On successful completion of the course, students will be
able to:
 CO1: Illustrate the basics of Python environment, data
types and operations.
 CO2: Develop simple Python scripts using control
statements and data types.
 CO3: Construct object oriented programs using functions.
 CO4: Outline the concepts of Inheritance and
Polymorphism.
 CO5: Apply overloading concepts in various applications.h
 CO6: Outline the concept of handling the exception
through handlers.
2
Syllabus
3
Books & References
4
Introduction to Python
 Python is a high-level programming language
 Open source and community driven
 “Batteries Included”
 a standard distribution includes many modules
 Dynamic typed
 Source can be compiled or run just-in-time
 Similar to perl, tcl, ruby
5
Why Python?
 Unlike AML and Avenue, there is a considerable base of
developers already using the language
 “Tried and true” language that has been in development
since 1991
 Can interface with the Component Object Model (COM)
used by Windows
 Can interface with Open Source GIS toolsets
6
Python!
 Created in 1991 by Guido van Rossum (now at Google)
 Named for Monty Python
 Useful as a scripting language
 script: A small program meant for one-time use
 Targeted towards small to medium sized projects
 Used by:
 Google, Yahoo!, Youtube
 Many Linux distributions
 Games and apps (e.g. Eve Online)
Installing Python
Windows:
 Download Python from
http://www.python.org
 Install Python.
 Run Idle from the Start Menu.
Mac OS X:
 Python is already installed.
 Open a terminal and run
python or run Idle from Finder.
Linux:
 Chances are you already have
Python installed. To check, run
python from the terminal.
 If not, install from your
distribution's package system.Note: For step by step installation
instructions, see the course web site.
Interpreted Languages
 interpreted
 Not compiled like Java
 Code is written and then directly executed by an
interpreter
 Type commands into interpreter and see immediate results
Computer
Runtime
Environment
CompilerCodeJava:
ComputerInterpreterCodePython:
The Python Interpreter
 Allows you to type commands one-at-a-time and see
results
 A great way to explore Python's syntax
 Repeat previous command: Alt+P
Our First Python Program
 Python does not have a main method like Java
 The program's main code is just written directly in the file
 Python statements do not end with semicolons
hello.py
1 print("Hello, world!")
The print Statement
print("text")
print() (a blank line)
 Escape sequences such as " are the same as in Java
 Strings can also start/end with '
swallows.py
1
2
3
4
print("Hello, world!")
print()
print("Suppose two swallows "carry" it together.")
print('African or "European" swallows?')
Comments
 Syntax:
# comment text (one line)
swallows2.py
1
2
3
4
5
6
# Suzy Student, CSE 142, Fall 2097
# This program prints important messages.
print("Hello, world!")
print() # blank line
print("Suppose two swallows "carry" it together.")
print('African or "European" swallows?')
IDLE – Development
Environment
 IDLE helps you program
in Python by:
 color-coding your
program code
 debugging
 auto-indent
 interactive shell
14
More than just printing
 Python is an object oriented language
 Practically everything can be treated as an object
 “hello world” is a string
 Strings, as objects, have methods that return the result
of a function on the string
15
What Is a Program?
 Usually, one or more algorithms written in a
programming language that can be translated to run on
a real machine
 We sometimes call programs software
What Is a Programming
Language?
 A programming language is somewhat like a
natural language, but with a very limited set
of statements and strict syntax rules.
 Has statements to implement sequential,
conditional and iterative processing -
algorithms
 Examples: FORTRAN, COBOL, Lisp, Basic,
Pascal, C, C++, Java, C#, Python, …
Compiler
 A compiler is a program that converts a program written
in a programming language into a program in the native
language, called machine language, of the machine that
is to execute the program.
From Algorithms to Hardware
(with compiler)
Algorithm
Program
A real computer
Translate (by a human being)
Translate (by compiler program)
The Program Development
Process (Data Flow)
Editor
Compiler
A real computer
Algorithm
Program in programming language
Program in machine’s language
Input Output
The Program Development
Process (Control Flow)
Edit
Compile
Run
Syntax errors
Input Output
Runtime errors
Three kinds of errors
 Syntax error : Some statement in the program is not a legal statement in the
language.
 Runtime error : An error occurs while the program is executing, causing the
program to terminate (divide by zero, etc.)
 Logic error : The program executes to completion, but gives incorrect
results.
Interpreter
 An alternative to a compiler is a program called an
interpreter. Rather than convert our program to the
language of the computer, the interpreter takes our
program one statement at a time and executes a
corresponding set of machine instructions.
Interpreter
Edit
Interpreter
Syntax or runtime errors
Input
Output
Python
 Python is a real-world, production language that is freely
available for most computers.
http:www.python.org
 If you want a copy of Python to use with this course, go to
http://code.google.com/p/mediacomp-jes/ .
We are using JES (Jython Environment for Students) which has
a lot of special multimedia functionality.
 Note: Our textbook covers a limited amount of Python. There
are many excellent online tutorials. For example, see
http://en.wikibooks.org/wiki/Non-Programmer's_Tutorial_for_Python/Contents
Python
 Python uses an interpreter. Not only can we write complete
programs, we can work with the interpreter in a statement
by statement mode enabling us to experiment quite easily.
 Python is especially good for our purposes in that it does
not have a lot of “overhead” before getting started.
 It is easy to jump in and experiment with Python in an
interactive fashion.
Language terminology
 Syntax: The formal rules for legal statements in the
language.
 Semantics: The meaning of the statements - what
happens when the statement is executed.
Three major control constructs
of programming
(Execution flow of instructions)
 Sequential: Simply do steps one after the other in order they are listed.
 Conditional: Decide which statement to do next based on some
true/false test.
 Iterative: A set of statements is repeated over and over until some
condition is met.
Sequential Operations
“Atomic”
 Input
 Computation
 Output
The Big Plan
 We want to get some experience of programming simple
algorithms in a real programming language. This gives us
an understanding of how software is written and allows us
to test our algorithms to see if they work.
 We’ll first work with programs where the variables have
numbers as values.
 Later we’ll work with programs dealing with pictures and
sound.
 In lab we’ll work with some simple statements and small
programs.
The Basic Pattern
 Most of our programs will use the basic pattern of
 Get some user input
 Perform some algorithm on the input
 Provide results as output
Identifiers
 Identifiers are names of various program
elements in the code that uniquely identify the
elements. They are the names of things like
variables or functions to be performed. They're
specified by the programmer and should have
names that indicate their purpose.
 In Python, identifiers
 Are made of letters, digits and underscores
 Must begin with a letter or an underscore
 Examples: temperature, myPayrate, score2
Keywords
 Keywords are reserved words that have special meaning in the Python
language. Because they are reserved, they can not be used as identifiers.
Examples of keywords are if, while, class, import.
Variables in Python
 A variable has
 A name – identifier
 A data type - int, float, str, etc.
 Storage space sufficient for the type.
Numeric Data Types
 int
This type is for whole numbers, positive or negative.
Examples: 23, -1756
 float
This type is for numbers with possible fraction parts.
Examples: 23.0, -14.561
Integer operators
The operations for integers are:
+ for addition
- for subtraction
* for multiplication
/ for integer division: The result of 14/5 is 2
% for remainder: The result of 14 % 5 is 4
 *, /, % take precedence over +, -
x + y * z will do y*z first
 Use parentheses to dictate order you want.
(x+y) * z will do x+y first.
Integer Expressions
 Integer expressions are formed using
 Integer Constants
 Integer Variables
 Integer Operators
 Parentheses
Python Assignment
Statements
 In Python, = is called the assignment operator
and an assignment statement has the form
<variable> = <expression>
 Here
 <variable> would be replaced by an actual variable
 <expression> would be replaced by an expression
 Python: age = 19
Python Assignment Statement
 Syntax: <variable> = <expression>
 Note that variable is on left
 Semantics:
Compute value of expression
Store this as new value of the variable
 Example: Pay = PayRate * Hours
Payrate
10
Hours
40
Pay
400
Assignment Example
Before
X
3
Z
12
Y
5
After
X
3
Z
11
Y
5
Execute
Z = X * 3 + Z / Y
Python Session
Python Session
What about floats?
 When computing with floats, / will indicate regular division with
fractional results.
 Constants will have a decimal point.
 14.0/5.0 will give 2.8 while 14/5 gives 2.
Comments
 Often we want to put some documentation in our program. These are
comments for explanation, but not executed by the computer.
 If we have # anywhere on a line, everything following this on the line is a
comment – ignored
Numerical Input
 To get numerical input from the user, we use an
assignment statement of the form
<variable> = input(<prompt>)
 Here
 <prompt> would be replaced by a prompt for the user inside quotation marks
 If there is no prompt, the parentheses are still needed
 Semantics
 The prompt will be displayed
 User enters number
 Value entered is stored as the value of the variable
Print Statement
 For output we use statements of the form
print <expression>
 Semantics
 Value of expression is computed
 This value is displayed
 Several expressions can be printed – separate them by
commas
Example - Fahrenheit to
Centigrade
 We want to convert a Fahrenheit temperature to
Centigrade.
 The formula is C = (F -32) x 5/9
 We use type float for the temperatures.
Python Session

Más contenido relacionado

La actualidad más candente

La actualidad más candente (19)

Python Crash Course
Python Crash CoursePython Crash Course
Python Crash Course
 
Core python programming tutorial
Core python programming tutorialCore python programming tutorial
Core python programming tutorial
 
Python programming
Python programmingPython programming
Python programming
 
Introduction of Python
Introduction of PythonIntroduction of Python
Introduction of Python
 
What is Python? An overview of Python for science.
What is Python? An overview of Python for science.What is Python? An overview of Python for science.
What is Python? An overview of Python for science.
 
Introduction to Python Basics Programming
Introduction to Python Basics ProgrammingIntroduction to Python Basics Programming
Introduction to Python Basics Programming
 
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
 
Learn python
Learn pythonLearn python
Learn python
 
Chapter 0 Python Overview (Python Programming Lecture)
Chapter 0 Python Overview (Python Programming Lecture)Chapter 0 Python Overview (Python Programming Lecture)
Chapter 0 Python Overview (Python Programming Lecture)
 
Python - the basics
Python - the basicsPython - the basics
Python - the basics
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Python: the Project, the Language and the Style
Python: the Project, the Language and the StylePython: the Project, the Language and the Style
Python: the Project, the Language and the Style
 
PYTHON CURRENT TREND APPLICATIONS- AN OVERVIEW
PYTHON CURRENT TREND APPLICATIONS- AN OVERVIEWPYTHON CURRENT TREND APPLICATIONS- AN OVERVIEW
PYTHON CURRENT TREND APPLICATIONS- AN OVERVIEW
 
Benefits & features of python |Advantages & disadvantages of python
Benefits & features of python |Advantages & disadvantages of pythonBenefits & features of python |Advantages & disadvantages of python
Benefits & features of python |Advantages & disadvantages of python
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAChapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
 
PYTHON NOTES
PYTHON NOTESPYTHON NOTES
PYTHON NOTES
 
introduction of python in data science
introduction of python in data scienceintroduction of python in data science
introduction of python in data science
 
summer training report on python
summer training report on pythonsummer training report on python
summer training report on python
 

Similar a Introduction to python

Python Programming-1.pptx of python by computer
Python Programming-1.pptx of python by computerPython Programming-1.pptx of python by computer
Python Programming-1.pptx of python by computer
sharanyarashmir5
 
a9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptx
a9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptxa9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptx
a9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptx
cigogag569
 
Python Programming and ApplicationsUnit-1.docx
Python Programming and ApplicationsUnit-1.docxPython Programming and ApplicationsUnit-1.docx
Python Programming and ApplicationsUnit-1.docx
Manohar k
 

Similar a Introduction to python (20)

Python Programming-1.pptx of python by computer
Python Programming-1.pptx of python by computerPython Programming-1.pptx of python by computer
Python Programming-1.pptx of python by computer
 
a9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptx
a9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptxa9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptx
a9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptx
 
python-handbook.pdf
python-handbook.pdfpython-handbook.pdf
python-handbook.pdf
 
Python Programming and ApplicationsUnit-1.docx
Python Programming and ApplicationsUnit-1.docxPython Programming and ApplicationsUnit-1.docx
Python Programming and ApplicationsUnit-1.docx
 
Fundamentals of python
Fundamentals of pythonFundamentals of python
Fundamentals of python
 
CSC2308 - PRINCIPLE OF PROGRAMMING II.pdf
CSC2308 - PRINCIPLE OF PROGRAMMING II.pdfCSC2308 - PRINCIPLE OF PROGRAMMING II.pdf
CSC2308 - PRINCIPLE OF PROGRAMMING II.pdf
 
MODULE 1.pptx
MODULE 1.pptxMODULE 1.pptx
MODULE 1.pptx
 
Python fundamentals
Python fundamentalsPython fundamentals
Python fundamentals
 
01 python introduction
01 python introduction 01 python introduction
01 python introduction
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdf
 
Introduction to Python Unit -1 Part .pdf
Introduction to Python Unit -1 Part .pdfIntroduction to Python Unit -1 Part .pdf
Introduction to Python Unit -1 Part .pdf
 
Pyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdfPyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdf
 
python-ppt.ppt
python-ppt.pptpython-ppt.ppt
python-ppt.ppt
 
python-ppt.ppt
python-ppt.pptpython-ppt.ppt
python-ppt.ppt
 
INTERNSHIP REPORT.docx
 INTERNSHIP REPORT.docx INTERNSHIP REPORT.docx
INTERNSHIP REPORT.docx
 
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
 
Python Training in Pune - Ethans Tech Pune
Python Training in Pune - Ethans Tech PunePython Training in Pune - Ethans Tech Pune
Python Training in Pune - Ethans Tech Pune
 
Introduction python
Introduction pythonIntroduction python
Introduction python
 
PYTHON FEATURES.pptx
PYTHON FEATURES.pptxPYTHON FEATURES.pptx
PYTHON FEATURES.pptx
 
Python Tutorial | Python Programming Language
Python Tutorial | Python Programming LanguagePython Tutorial | Python Programming Language
Python Tutorial | Python Programming Language
 

Último

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 

Último (20)

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 

Introduction to python

  • 2. COURSE OUTCOMES:  On successful completion of the course, students will be able to:  CO1: Illustrate the basics of Python environment, data types and operations.  CO2: Develop simple Python scripts using control statements and data types.  CO3: Construct object oriented programs using functions.  CO4: Outline the concepts of Inheritance and Polymorphism.  CO5: Apply overloading concepts in various applications.h  CO6: Outline the concept of handling the exception through handlers. 2
  • 5. Introduction to Python  Python is a high-level programming language  Open source and community driven  “Batteries Included”  a standard distribution includes many modules  Dynamic typed  Source can be compiled or run just-in-time  Similar to perl, tcl, ruby 5
  • 6. Why Python?  Unlike AML and Avenue, there is a considerable base of developers already using the language  “Tried and true” language that has been in development since 1991  Can interface with the Component Object Model (COM) used by Windows  Can interface with Open Source GIS toolsets 6
  • 7. Python!  Created in 1991 by Guido van Rossum (now at Google)  Named for Monty Python  Useful as a scripting language  script: A small program meant for one-time use  Targeted towards small to medium sized projects  Used by:  Google, Yahoo!, Youtube  Many Linux distributions  Games and apps (e.g. Eve Online)
  • 8. Installing Python Windows:  Download Python from http://www.python.org  Install Python.  Run Idle from the Start Menu. Mac OS X:  Python is already installed.  Open a terminal and run python or run Idle from Finder. Linux:  Chances are you already have Python installed. To check, run python from the terminal.  If not, install from your distribution's package system.Note: For step by step installation instructions, see the course web site.
  • 9. Interpreted Languages  interpreted  Not compiled like Java  Code is written and then directly executed by an interpreter  Type commands into interpreter and see immediate results Computer Runtime Environment CompilerCodeJava: ComputerInterpreterCodePython:
  • 10. The Python Interpreter  Allows you to type commands one-at-a-time and see results  A great way to explore Python's syntax  Repeat previous command: Alt+P
  • 11. Our First Python Program  Python does not have a main method like Java  The program's main code is just written directly in the file  Python statements do not end with semicolons hello.py 1 print("Hello, world!")
  • 12. The print Statement print("text") print() (a blank line)  Escape sequences such as " are the same as in Java  Strings can also start/end with ' swallows.py 1 2 3 4 print("Hello, world!") print() print("Suppose two swallows "carry" it together.") print('African or "European" swallows?')
  • 13. Comments  Syntax: # comment text (one line) swallows2.py 1 2 3 4 5 6 # Suzy Student, CSE 142, Fall 2097 # This program prints important messages. print("Hello, world!") print() # blank line print("Suppose two swallows "carry" it together.") print('African or "European" swallows?')
  • 14. IDLE – Development Environment  IDLE helps you program in Python by:  color-coding your program code  debugging  auto-indent  interactive shell 14
  • 15. More than just printing  Python is an object oriented language  Practically everything can be treated as an object  “hello world” is a string  Strings, as objects, have methods that return the result of a function on the string 15
  • 16. What Is a Program?  Usually, one or more algorithms written in a programming language that can be translated to run on a real machine  We sometimes call programs software
  • 17. What Is a Programming Language?  A programming language is somewhat like a natural language, but with a very limited set of statements and strict syntax rules.  Has statements to implement sequential, conditional and iterative processing - algorithms  Examples: FORTRAN, COBOL, Lisp, Basic, Pascal, C, C++, Java, C#, Python, …
  • 18. Compiler  A compiler is a program that converts a program written in a programming language into a program in the native language, called machine language, of the machine that is to execute the program.
  • 19. From Algorithms to Hardware (with compiler) Algorithm Program A real computer Translate (by a human being) Translate (by compiler program)
  • 20. The Program Development Process (Data Flow) Editor Compiler A real computer Algorithm Program in programming language Program in machine’s language Input Output
  • 21. The Program Development Process (Control Flow) Edit Compile Run Syntax errors Input Output Runtime errors
  • 22. Three kinds of errors  Syntax error : Some statement in the program is not a legal statement in the language.  Runtime error : An error occurs while the program is executing, causing the program to terminate (divide by zero, etc.)  Logic error : The program executes to completion, but gives incorrect results.
  • 23. Interpreter  An alternative to a compiler is a program called an interpreter. Rather than convert our program to the language of the computer, the interpreter takes our program one statement at a time and executes a corresponding set of machine instructions.
  • 25. Python  Python is a real-world, production language that is freely available for most computers. http:www.python.org  If you want a copy of Python to use with this course, go to http://code.google.com/p/mediacomp-jes/ . We are using JES (Jython Environment for Students) which has a lot of special multimedia functionality.  Note: Our textbook covers a limited amount of Python. There are many excellent online tutorials. For example, see http://en.wikibooks.org/wiki/Non-Programmer's_Tutorial_for_Python/Contents
  • 26. Python  Python uses an interpreter. Not only can we write complete programs, we can work with the interpreter in a statement by statement mode enabling us to experiment quite easily.  Python is especially good for our purposes in that it does not have a lot of “overhead” before getting started.  It is easy to jump in and experiment with Python in an interactive fashion.
  • 27. Language terminology  Syntax: The formal rules for legal statements in the language.  Semantics: The meaning of the statements - what happens when the statement is executed.
  • 28. Three major control constructs of programming (Execution flow of instructions)  Sequential: Simply do steps one after the other in order they are listed.  Conditional: Decide which statement to do next based on some true/false test.  Iterative: A set of statements is repeated over and over until some condition is met.
  • 30. The Big Plan  We want to get some experience of programming simple algorithms in a real programming language. This gives us an understanding of how software is written and allows us to test our algorithms to see if they work.  We’ll first work with programs where the variables have numbers as values.  Later we’ll work with programs dealing with pictures and sound.  In lab we’ll work with some simple statements and small programs.
  • 31. The Basic Pattern  Most of our programs will use the basic pattern of  Get some user input  Perform some algorithm on the input  Provide results as output
  • 32. Identifiers  Identifiers are names of various program elements in the code that uniquely identify the elements. They are the names of things like variables or functions to be performed. They're specified by the programmer and should have names that indicate their purpose.  In Python, identifiers  Are made of letters, digits and underscores  Must begin with a letter or an underscore  Examples: temperature, myPayrate, score2
  • 33. Keywords  Keywords are reserved words that have special meaning in the Python language. Because they are reserved, they can not be used as identifiers. Examples of keywords are if, while, class, import.
  • 34. Variables in Python  A variable has  A name – identifier  A data type - int, float, str, etc.  Storage space sufficient for the type.
  • 35. Numeric Data Types  int This type is for whole numbers, positive or negative. Examples: 23, -1756  float This type is for numbers with possible fraction parts. Examples: 23.0, -14.561
  • 36. Integer operators The operations for integers are: + for addition - for subtraction * for multiplication / for integer division: The result of 14/5 is 2 % for remainder: The result of 14 % 5 is 4  *, /, % take precedence over +, - x + y * z will do y*z first  Use parentheses to dictate order you want. (x+y) * z will do x+y first.
  • 37. Integer Expressions  Integer expressions are formed using  Integer Constants  Integer Variables  Integer Operators  Parentheses
  • 38. Python Assignment Statements  In Python, = is called the assignment operator and an assignment statement has the form <variable> = <expression>  Here  <variable> would be replaced by an actual variable  <expression> would be replaced by an expression  Python: age = 19
  • 39. Python Assignment Statement  Syntax: <variable> = <expression>  Note that variable is on left  Semantics: Compute value of expression Store this as new value of the variable  Example: Pay = PayRate * Hours Payrate 10 Hours 40 Pay 400
  • 43. What about floats?  When computing with floats, / will indicate regular division with fractional results.  Constants will have a decimal point.  14.0/5.0 will give 2.8 while 14/5 gives 2.
  • 44. Comments  Often we want to put some documentation in our program. These are comments for explanation, but not executed by the computer.  If we have # anywhere on a line, everything following this on the line is a comment – ignored
  • 45. Numerical Input  To get numerical input from the user, we use an assignment statement of the form <variable> = input(<prompt>)  Here  <prompt> would be replaced by a prompt for the user inside quotation marks  If there is no prompt, the parentheses are still needed  Semantics  The prompt will be displayed  User enters number  Value entered is stored as the value of the variable
  • 46. Print Statement  For output we use statements of the form print <expression>  Semantics  Value of expression is computed  This value is displayed  Several expressions can be printed – separate them by commas
  • 47. Example - Fahrenheit to Centigrade  We want to convert a Fahrenheit temperature to Centigrade.  The formula is C = (F -32) x 5/9  We use type float for the temperatures.