SlideShare a Scribd company logo
What Is Python?
Created in 1990 by Guido van Rossum
While at CWI, Amsterdam
Now hosted by centre for national research initiatives,
Reston, VA, USA
Free, open source
And with an amazing community
Object oriented language
“Everything is an object”
Why Python?Designed to be easy to learn and master
Clean, clear syntax
Very few keywords
Highly portable
Runs almost anywhere - high end servers and
workstations, down to windows CE
Uses machine independent byte-codes
Extensible
Designed to be extensible using C/C++, allowing
access to many external libraries
Python: a modern hybrid
A language for scripting and prototyping
Balance between extensibility and powerful built-in
data structures
genealogy:
Setl (NYU, J.Schwartz et al. 1969-1980)
ABC (Amsterdam, Meertens et al. 1980-)
Python (Van Rossum et all. 1996-)
Very active open-source community
Prototyping
Emphasis on experimental programming:
Interactive (like LISP, ML, etc).
Translation to bytecode (like Java)
Dynamic typing (like LISP, SETL, APL)
Higher-order function (LISP, ML)
Garbage-collected, no ptrs
(LISP, SNOBOL4)
Prototyping
Emphasis on experimental programming:
Uniform treatment of indexable structures (like
SETL)
Built-in associative structures (like SETL,
SNOBOL4, Postscript)
Light syntax, indentation is significant (from ABC)
Most obvious and notorious
features
Clean syntax plus high-level data types
Leads to fast coding
Uses white-space to delimit blocks
Humans generally do, so why not the language?
Try it, you will end up liking it
Variables do not need declaration
Although not a type-less language
A Digression on Block Structure
There are three ways of dealing with IF structures
Sequences of statements with explicit end (Algol-68,
Ada, COBOL)
Single statement
(Algol-60, Pascal, C)
Indentation (ABC, Python)
Sequence of Statements
IF condition THEN
stm;
stm;
..
ELSIF condition THEN
stm;
..
ELSE
stm;
..
END IF;
next statement;
Single Statement
IF condition THEN
BEGIN
stm;
stm;
END ..
ELSE IF condition THEN
BEGIN
stm;
..
END;
ELSE
BEGIN
stm;
..
END;
next-statement;
Indentation
IF condition:
stm;
stm;
..
ELSIF condition:
stm;
..
ELSE:
stm;
..
next-statement
Pythonwin
These examples use Pythonwin
Only available on Windows
GUI toolkit using Tkinter available for most platforms
Standard console Python available on all platforms
Has interactive mode for quick testing of code
Includes debugger and Python editor
Interactive Python
Starting Python.exe, or any of the GUI environments
present an interactive mode
>>>prompt indicates start of a statement or
expression
If incomplete, ...prompt indicates second and
subsequent lines
All expression results printed back to interactive
console
Variables and Types(1 of 3)
Variables need no declaration
>>> a=1
>>>
As a variable assignment is a statement, there is no
printed result
>>> a
1
Variable name alone is an expression, so the result is
printed
Variables and Types (2 of 3)
Variables must be created before they can be used
>>> b
Traceback (innermost last):
File "<interactive input>", line
1, in ?
NameError: b
>>>
Python uses exceptions - more detail later
Variables and Types (3 of 3)
Objects always have a type
>>> a = 1
>>> type(a)
<type 'int'>
>>> a = "Hello"
>>> type(a)
<type 'string'>
>>> type(1.0)
<type 'float'>
Assignment versus Equality Testing
Assignment performed with single =
Equality testing done with double = (==)
Sensible type promotions are defined
Identity tested with is operator.
>>> 1==1
1
>>> 1.0==1
1
>>> "1"==1
0
Simple Data Types
Strings
May hold any data, including embedded NULLs
Declared using either single, double, or triple quotes
>>> s = "Hi there"
>>> s
'Hi there'
>>> s = "Embedded 'quote'"
>>> s
"Embedded 'quote'"
Simple Data Types
Triple quotes useful for multi-line strings
>>> s = """ a long
... string with "quotes" or anything
else"""
>>> s
' a long012string with "quotes" or
anything else'
>>> len(s)
45
Simple Data TypesInteger objects implemented using C longs
Like C, integer division returns the floor
>>> 5/2
2
Float types implemented using C doubles
No point in having single precision since execution
overhead is large anyway
Simple Data Types
Long Integers have unlimited size
Limited only by available memory
>>> long = 1L << 64
>>> long ** 5
21359870359209100823950217061695521146027045223
56652769947041607822219725780640550022962086936
576L
High Level Data Types
Lists hold a sequence of items
May hold any object
Declared using square brackets
>>> l = []# An empty list
>>> l.append(1)
>>> l.append("Hi there")
>>> len(l)
2
High Level Data Types
>>> l
[1, 'Hi there']
>>>
>>> l = ["Hi there", 1, 2]
>>> l
['Hi there', 1, 2]
>>> l.sort()
>>> l
[1, 2, 'Hi there']
High Level Data Types
Tuples are similar to lists
Sequence of items
Key difference is they are immutable
Often used in place of simple structures
Automatic unpacking
>>> point = 2,3
>>> x, y = point
>>> x
2
High Level Data Types
Tuples are particularly useful to return multiple
values from a function
>>> x, y = GetPoint()
As Python has no concept of byref parameters, this
technique is used widely
High Level Data Types
Dictionaries hold key-value pairs
Often called maps or hashes. Implemented using hash-
tables
Keys may be any immutable object, values may be any
object
Declared using braces
>>> d={}
>>> d[0] = "Hi there"
>>> d["foo"] = 1
High Level Data Types
Dictionaries (cont.)
>>> len(d)
2
>>> d[0]
'Hi there'
>>> d = {0 : "Hi there", 1 :
"Hello"}
>>> len(d)
2
Blocks
Blocks are delimited by indentation
Colon used to start a block
Tabs or spaces may be used
Mixing tabs and spaces works, but is discouraged
>>> if 1:
... print "True"
...
True
>>>
Blocks
Many hate this when they first see it
Most Python programmers come to love it
Humans use indentation when reading code to
determine block structure
Ever been bitten by the C code?:
if (1)
printf("True");
CallSomething();
Looping
The for statement loops over sequences
>>> for ch in "Hello":
... print ch
...
H
e
l
l
o
>>>
Looping
Built-in function range() used to build sequences of
integers
>>> for i in range(3):
... print i
...
0
1
2
>>>
Looping
while statement for more traditional loops
>>> i = 0
>>> while i < 2:
... print i
... i = i + 1
...
0
1
>>>
Functions
Functions are defined with the def statement:
>>> def foo(bar):
... return bar
>>>
This defines a trivial function named foo that takes a
single parameter bar
Functions
A function definition simply places a function object
in the namespace
>>> foo
<function foo at fac680>
>>>
And the function object can obviously be called:
>>> foo(3)
3
>>>
Classes
Classes are defined using the class statement
>>> class Foo:
... def __init__(self):
... self.member = 1
... def GetMember(self):
... return self.member
...
>>>
Classes
A few things are worth pointing out in the previous
example:
The constructor has a special name __init__, while a
destructor (not shown) uses __del__
The self parameter is the instance (ie, the this in C+
+). In Python, the self parameter is explicit (c.f. C++,
where it is implicit)
The name self is not required - simply a convention
Classes
Like functions, a class statement simply adds a class
object to the namespace
>>> Foo
<class __main__.Foo at 1000960>
>>>
Classes are instantiated using call syntax
>>> f=Foo()
>>> f.GetMember()
1
Modules
Most of Python’s power comes from modules
Modules can be implemented either in Python, or in
C/C++
import statement makes a module available
>>> import string
>>> string.join( ["Hi", "there"] )
'Hi there'
>>>
Exceptions
Python uses exceptions for errors
try / except block can handle exceptions
>>> try:
... 1/0
... except ZeroDivisionError:
... print "Eeek"
...
Eeek
>>>
Exceptions
try / finally block can guarantee execute of code
even in the face of exceptions
>>> try:
... 1/0
... finally:
... print "Doing this anyway"
...
Doing this anyway
Traceback (innermost last): File "<interactive
input>", line 2, in ?
ZeroDivisionError: integer division or modulo
>>>
Threads
Number of ways to implement threads
Highest level interface modelled after Java
>>> class DemoThread(threading.Thread):
... def run(self):
... for i in range(3):
... time.sleep(3)
... print i
...
>>> t = DemoThread()
>>> t.start()
>>> t.join()
0
1 <etc>
Standard Library
Python comes standard with a set of modules, known
as the “standard library”
Incredibly rich and diverse functionality available
from the standard library
All common internet protocols, sockets, CGI, OS
services, GUI services (via Tcl/Tk), database, Berkeley
style databases, calendar, Python parser, file
globbing/searching, debugger, profiler, threading and
synchronisation, persistency, etc
External library
Many modules are available externally covering
almost every piece of functionality you could ever
desire
Imaging, numerical analysis, OS specific functionality,
SQL databases, Fortran interfaces, XML, Corba, COM,
Win32 API, etc
Way too many to give the list any justice
Python ProgramsPython programs and modules are written as text files
with traditionally a .py extension
Each Python module has its own discrete namespace
Name space within a Python module is a global one.
Python ProgramsPython modules and programs are differentiated only
by the way they are called
.py files executed directly are programs (often referred
to as scripts)
.py files referenced via the import statement are
modules
Python Programs
Thus, the same .py file can be a program/script, or a
module
This feature is often used to provide regression tests
for modules
When module is executed as a program, the regression
test is executed
When module is imported, test functionality is not
executed
More Information on Python
Can’t do Python justice in this short time frame
But hopefully have given you a taste of the language
Comes with extensive documentation, including
tutorials and library reference
Also a number of Python books available
Visit www.python.org for more details
Can find python tutorial and reference manual
Scripting Languages
What are they?
Beats me 
Apparently they are programming languages used for
building the equivalent of shell scripts, i.e. doing the
sort of things that shell scripts have traditionally been
used for.
But any language can be used this way
So it is a matter of convenience
Characteristics of Scripting Languages
Typically interpretive
But that’s an implementation detail
Typically have high level data structures
But rich libraries can substitute for this
For example, look at GNAT.Spitbol
Powerful flexible string handling
Typically have rich libraries
But any language can meet this requirement
Is Python A Scripting Language?
Usually thought of as one
But this is mainly a marketing issue
People think of scripting languages as being easy to
learn, and useful.
But Python is a well worked out coherent dynamic
programming language
And there is no reason not to use it for a wide range of
applications.

More Related Content

What's hot

Python language data types
Python language data typesPython language data types
Python language data types
Hoang Nguyen
 
Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
Panimalar Engineering College
 
Standard data-types-in-py
Standard data-types-in-pyStandard data-types-in-py
Standard data-types-in-py
Priyanshu Sengar
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
nitamhaske
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
Praveen M Jigajinni
 
Python course in_mumbai
Python course in_mumbaiPython course in_mumbai
Python course in_mumbai
vibrantuser
 
Basic data types in python
Basic data types in pythonBasic data types in python
Basic data types in python
sunilchute1
 
Python Cheat Sheet
Python Cheat SheetPython Cheat Sheet
Python Cheat Sheet
Muthu Vinayagam
 
Python strings
Python stringsPython strings
Python strings
Mohammed Sikander
 
Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)
Pedro Rodrigues
 
Notes3
Notes3Notes3
Notes3hccit
 
Iteration
IterationIteration
Iteration
Pooja B S
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arraysphanleson
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 

What's hot (18)

Python language data types
Python language data typesPython language data types
Python language data types
 
Variables In Php 1
Variables In Php 1Variables In Php 1
Variables In Php 1
 
Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
 
Standard data-types-in-py
Standard data-types-in-pyStandard data-types-in-py
Standard data-types-in-py
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
 
Python course in_mumbai
Python course in_mumbaiPython course in_mumbai
Python course in_mumbai
 
Python course
Python coursePython course
Python course
 
scripting in Python
scripting in Pythonscripting in Python
scripting in Python
 
Basic data types in python
Basic data types in pythonBasic data types in python
Basic data types in python
 
Python Cheat Sheet
Python Cheat SheetPython Cheat Sheet
Python Cheat Sheet
 
Python strings
Python stringsPython strings
Python strings
 
Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
Notes3
Notes3Notes3
Notes3
 
Iteration
IterationIteration
Iteration
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 

Similar to Pythonintroduction

FALLSEM2022-23_ITA3007_ETH_VL2022230100613_Reference_Material_I_23-09-2022_py...
FALLSEM2022-23_ITA3007_ETH_VL2022230100613_Reference_Material_I_23-09-2022_py...FALLSEM2022-23_ITA3007_ETH_VL2022230100613_Reference_Material_I_23-09-2022_py...
FALLSEM2022-23_ITA3007_ETH_VL2022230100613_Reference_Material_I_23-09-2022_py...
admin369652
 
Python PPT by Sushil Sir.pptx
Python PPT by Sushil Sir.pptxPython PPT by Sushil Sir.pptx
Python PPT by Sushil Sir.pptx
sushil155005
 
01-Python-Basics.ppt
01-Python-Basics.ppt01-Python-Basics.ppt
01-Python-Basics.ppt
VicVic56
 
PYTHON
PYTHONPYTHON
PYTHON
JOHNYAMSON
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
arivukarasi2
 
Python for Beginners(v1)
Python for Beginners(v1)Python for Beginners(v1)
Python for Beginners(v1)
Panimalar Engineering College
 
Introduction to Python for Bioinformatics
Introduction to Python for BioinformaticsIntroduction to Python for Bioinformatics
Introduction to Python for Bioinformatics
José Héctor Gálvez
 
Python1
Python1Python1
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
Girish Khanzode
 
manish python.pptx
manish python.pptxmanish python.pptx
manish python.pptx
ssuser92d141
 
Kavitha_python.ppt
Kavitha_python.pptKavitha_python.ppt
Kavitha_python.ppt
KavithaMuralidharan2
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
ALOK52916
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
VishwasKumar58
 
Python Basics
Python BasicsPython Basics
Python Basics
MobeenAhmed25
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
RajPurohit33
 
Lenguaje Python
Lenguaje PythonLenguaje Python
Lenguaje Python
RalAnteloJurado
 
Learn Python in Three Hours - Presentation
Learn Python in Three Hours - PresentationLearn Python in Three Hours - Presentation
Learn Python in Three Hours - Presentation
Naseer-ul-Hassan Rehman
 

Similar to Pythonintroduction (20)

FALLSEM2022-23_ITA3007_ETH_VL2022230100613_Reference_Material_I_23-09-2022_py...
FALLSEM2022-23_ITA3007_ETH_VL2022230100613_Reference_Material_I_23-09-2022_py...FALLSEM2022-23_ITA3007_ETH_VL2022230100613_Reference_Material_I_23-09-2022_py...
FALLSEM2022-23_ITA3007_ETH_VL2022230100613_Reference_Material_I_23-09-2022_py...
 
Python PPT by Sushil Sir.pptx
Python PPT by Sushil Sir.pptxPython PPT by Sushil Sir.pptx
Python PPT by Sushil Sir.pptx
 
01-Python-Basics.ppt
01-Python-Basics.ppt01-Python-Basics.ppt
01-Python-Basics.ppt
 
PYTHON
PYTHONPYTHON
PYTHON
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
Python for Beginners(v1)
Python for Beginners(v1)Python for Beginners(v1)
Python for Beginners(v1)
 
Python
PythonPython
Python
 
ENGLISH PYTHON.ppt
ENGLISH PYTHON.pptENGLISH PYTHON.ppt
ENGLISH PYTHON.ppt
 
Introduction to Python for Bioinformatics
Introduction to Python for BioinformaticsIntroduction to Python for Bioinformatics
Introduction to Python for Bioinformatics
 
Python1
Python1Python1
Python1
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
 
manish python.pptx
manish python.pptxmanish python.pptx
manish python.pptx
 
Kavitha_python.ppt
Kavitha_python.pptKavitha_python.ppt
Kavitha_python.ppt
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
Python Basics
Python BasicsPython Basics
Python Basics
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
Lenguaje Python
Lenguaje PythonLenguaje Python
Lenguaje Python
 
Learn Python in Three Hours - Presentation
Learn Python in Three Hours - PresentationLearn Python in Three Hours - Presentation
Learn Python in Three Hours - Presentation
 

More from -jyothish kumar sirigidi

Webtechnologies
Webtechnologies Webtechnologies
Webtechnologies
-jyothish kumar sirigidi
 
Open source software
Open source softwareOpen source software
Open source software
-jyothish kumar sirigidi
 
Html
HtmlHtml
Authenticating with our minds
Authenticating with our mindsAuthenticating with our minds
Authenticating with our minds
-jyothish kumar sirigidi
 
Google chrome OS
Google chrome OSGoogle chrome OS
Google chrome OS
-jyothish kumar sirigidi
 
Blue eye technology ppt
Blue eye technology pptBlue eye technology ppt
Blue eye technology ppt
-jyothish kumar sirigidi
 
mobile application security
mobile application securitymobile application security
mobile application security
-jyothish kumar sirigidi
 
Android
AndroidAndroid
Network security
Network securityNetwork security
Network security
-jyothish kumar sirigidi
 
Applications of computer graphics
Applications of computer graphicsApplications of computer graphics
Applications of computer graphics
-jyothish kumar sirigidi
 

More from -jyothish kumar sirigidi (11)

Webtechnologies
Webtechnologies Webtechnologies
Webtechnologies
 
Open source software
Open source softwareOpen source software
Open source software
 
Html
HtmlHtml
Html
 
Authenticating with our minds
Authenticating with our mindsAuthenticating with our minds
Authenticating with our minds
 
Google chrome OS
Google chrome OSGoogle chrome OS
Google chrome OS
 
Blue eye technology ppt
Blue eye technology pptBlue eye technology ppt
Blue eye technology ppt
 
mobile application security
mobile application securitymobile application security
mobile application security
 
Android
AndroidAndroid
Android
 
Network security
Network securityNetwork security
Network security
 
CLOUD COMPUTING
CLOUD COMPUTINGCLOUD COMPUTING
CLOUD COMPUTING
 
Applications of computer graphics
Applications of computer graphicsApplications of computer graphics
Applications of computer graphics
 

Recently uploaded

CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 

Recently uploaded (20)

CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 

Pythonintroduction

  • 1. What Is Python? Created in 1990 by Guido van Rossum While at CWI, Amsterdam Now hosted by centre for national research initiatives, Reston, VA, USA Free, open source And with an amazing community Object oriented language “Everything is an object”
  • 2. Why Python?Designed to be easy to learn and master Clean, clear syntax Very few keywords Highly portable Runs almost anywhere - high end servers and workstations, down to windows CE Uses machine independent byte-codes Extensible Designed to be extensible using C/C++, allowing access to many external libraries
  • 3. Python: a modern hybrid A language for scripting and prototyping Balance between extensibility and powerful built-in data structures genealogy: Setl (NYU, J.Schwartz et al. 1969-1980) ABC (Amsterdam, Meertens et al. 1980-) Python (Van Rossum et all. 1996-) Very active open-source community
  • 4. Prototyping Emphasis on experimental programming: Interactive (like LISP, ML, etc). Translation to bytecode (like Java) Dynamic typing (like LISP, SETL, APL) Higher-order function (LISP, ML) Garbage-collected, no ptrs (LISP, SNOBOL4)
  • 5. Prototyping Emphasis on experimental programming: Uniform treatment of indexable structures (like SETL) Built-in associative structures (like SETL, SNOBOL4, Postscript) Light syntax, indentation is significant (from ABC)
  • 6. Most obvious and notorious features Clean syntax plus high-level data types Leads to fast coding Uses white-space to delimit blocks Humans generally do, so why not the language? Try it, you will end up liking it Variables do not need declaration Although not a type-less language
  • 7. A Digression on Block Structure There are three ways of dealing with IF structures Sequences of statements with explicit end (Algol-68, Ada, COBOL) Single statement (Algol-60, Pascal, C) Indentation (ABC, Python)
  • 8. Sequence of Statements IF condition THEN stm; stm; .. ELSIF condition THEN stm; .. ELSE stm; .. END IF; next statement;
  • 9. Single Statement IF condition THEN BEGIN stm; stm; END .. ELSE IF condition THEN BEGIN stm; .. END; ELSE BEGIN stm; .. END; next-statement;
  • 11. Pythonwin These examples use Pythonwin Only available on Windows GUI toolkit using Tkinter available for most platforms Standard console Python available on all platforms Has interactive mode for quick testing of code Includes debugger and Python editor
  • 12. Interactive Python Starting Python.exe, or any of the GUI environments present an interactive mode >>>prompt indicates start of a statement or expression If incomplete, ...prompt indicates second and subsequent lines All expression results printed back to interactive console
  • 13. Variables and Types(1 of 3) Variables need no declaration >>> a=1 >>> As a variable assignment is a statement, there is no printed result >>> a 1 Variable name alone is an expression, so the result is printed
  • 14. Variables and Types (2 of 3) Variables must be created before they can be used >>> b Traceback (innermost last): File "<interactive input>", line 1, in ? NameError: b >>> Python uses exceptions - more detail later
  • 15. Variables and Types (3 of 3) Objects always have a type >>> a = 1 >>> type(a) <type 'int'> >>> a = "Hello" >>> type(a) <type 'string'> >>> type(1.0) <type 'float'>
  • 16. Assignment versus Equality Testing Assignment performed with single = Equality testing done with double = (==) Sensible type promotions are defined Identity tested with is operator. >>> 1==1 1 >>> 1.0==1 1 >>> "1"==1 0
  • 17. Simple Data Types Strings May hold any data, including embedded NULLs Declared using either single, double, or triple quotes >>> s = "Hi there" >>> s 'Hi there' >>> s = "Embedded 'quote'" >>> s "Embedded 'quote'"
  • 18. Simple Data Types Triple quotes useful for multi-line strings >>> s = """ a long ... string with "quotes" or anything else""" >>> s ' a long012string with "quotes" or anything else' >>> len(s) 45
  • 19. Simple Data TypesInteger objects implemented using C longs Like C, integer division returns the floor >>> 5/2 2 Float types implemented using C doubles No point in having single precision since execution overhead is large anyway
  • 20. Simple Data Types Long Integers have unlimited size Limited only by available memory >>> long = 1L << 64 >>> long ** 5 21359870359209100823950217061695521146027045223 56652769947041607822219725780640550022962086936 576L
  • 21. High Level Data Types Lists hold a sequence of items May hold any object Declared using square brackets >>> l = []# An empty list >>> l.append(1) >>> l.append("Hi there") >>> len(l) 2
  • 22. High Level Data Types >>> l [1, 'Hi there'] >>> >>> l = ["Hi there", 1, 2] >>> l ['Hi there', 1, 2] >>> l.sort() >>> l [1, 2, 'Hi there']
  • 23. High Level Data Types Tuples are similar to lists Sequence of items Key difference is they are immutable Often used in place of simple structures Automatic unpacking >>> point = 2,3 >>> x, y = point >>> x 2
  • 24. High Level Data Types Tuples are particularly useful to return multiple values from a function >>> x, y = GetPoint() As Python has no concept of byref parameters, this technique is used widely
  • 25. High Level Data Types Dictionaries hold key-value pairs Often called maps or hashes. Implemented using hash- tables Keys may be any immutable object, values may be any object Declared using braces >>> d={} >>> d[0] = "Hi there" >>> d["foo"] = 1
  • 26. High Level Data Types Dictionaries (cont.) >>> len(d) 2 >>> d[0] 'Hi there' >>> d = {0 : "Hi there", 1 : "Hello"} >>> len(d) 2
  • 27. Blocks Blocks are delimited by indentation Colon used to start a block Tabs or spaces may be used Mixing tabs and spaces works, but is discouraged >>> if 1: ... print "True" ... True >>>
  • 28. Blocks Many hate this when they first see it Most Python programmers come to love it Humans use indentation when reading code to determine block structure Ever been bitten by the C code?: if (1) printf("True"); CallSomething();
  • 29. Looping The for statement loops over sequences >>> for ch in "Hello": ... print ch ... H e l l o >>>
  • 30. Looping Built-in function range() used to build sequences of integers >>> for i in range(3): ... print i ... 0 1 2 >>>
  • 31. Looping while statement for more traditional loops >>> i = 0 >>> while i < 2: ... print i ... i = i + 1 ... 0 1 >>>
  • 32. Functions Functions are defined with the def statement: >>> def foo(bar): ... return bar >>> This defines a trivial function named foo that takes a single parameter bar
  • 33. Functions A function definition simply places a function object in the namespace >>> foo <function foo at fac680> >>> And the function object can obviously be called: >>> foo(3) 3 >>>
  • 34. Classes Classes are defined using the class statement >>> class Foo: ... def __init__(self): ... self.member = 1 ... def GetMember(self): ... return self.member ... >>>
  • 35. Classes A few things are worth pointing out in the previous example: The constructor has a special name __init__, while a destructor (not shown) uses __del__ The self parameter is the instance (ie, the this in C+ +). In Python, the self parameter is explicit (c.f. C++, where it is implicit) The name self is not required - simply a convention
  • 36. Classes Like functions, a class statement simply adds a class object to the namespace >>> Foo <class __main__.Foo at 1000960> >>> Classes are instantiated using call syntax >>> f=Foo() >>> f.GetMember() 1
  • 37. Modules Most of Python’s power comes from modules Modules can be implemented either in Python, or in C/C++ import statement makes a module available >>> import string >>> string.join( ["Hi", "there"] ) 'Hi there' >>>
  • 38. Exceptions Python uses exceptions for errors try / except block can handle exceptions >>> try: ... 1/0 ... except ZeroDivisionError: ... print "Eeek" ... Eeek >>>
  • 39. Exceptions try / finally block can guarantee execute of code even in the face of exceptions >>> try: ... 1/0 ... finally: ... print "Doing this anyway" ... Doing this anyway Traceback (innermost last): File "<interactive input>", line 2, in ? ZeroDivisionError: integer division or modulo >>>
  • 40. Threads Number of ways to implement threads Highest level interface modelled after Java >>> class DemoThread(threading.Thread): ... def run(self): ... for i in range(3): ... time.sleep(3) ... print i ... >>> t = DemoThread() >>> t.start() >>> t.join() 0 1 <etc>
  • 41. Standard Library Python comes standard with a set of modules, known as the “standard library” Incredibly rich and diverse functionality available from the standard library All common internet protocols, sockets, CGI, OS services, GUI services (via Tcl/Tk), database, Berkeley style databases, calendar, Python parser, file globbing/searching, debugger, profiler, threading and synchronisation, persistency, etc
  • 42. External library Many modules are available externally covering almost every piece of functionality you could ever desire Imaging, numerical analysis, OS specific functionality, SQL databases, Fortran interfaces, XML, Corba, COM, Win32 API, etc Way too many to give the list any justice
  • 43. Python ProgramsPython programs and modules are written as text files with traditionally a .py extension Each Python module has its own discrete namespace Name space within a Python module is a global one.
  • 44. Python ProgramsPython modules and programs are differentiated only by the way they are called .py files executed directly are programs (often referred to as scripts) .py files referenced via the import statement are modules
  • 45. Python Programs Thus, the same .py file can be a program/script, or a module This feature is often used to provide regression tests for modules When module is executed as a program, the regression test is executed When module is imported, test functionality is not executed
  • 46. More Information on Python Can’t do Python justice in this short time frame But hopefully have given you a taste of the language Comes with extensive documentation, including tutorials and library reference Also a number of Python books available Visit www.python.org for more details Can find python tutorial and reference manual
  • 47. Scripting Languages What are they? Beats me  Apparently they are programming languages used for building the equivalent of shell scripts, i.e. doing the sort of things that shell scripts have traditionally been used for. But any language can be used this way So it is a matter of convenience
  • 48. Characteristics of Scripting Languages Typically interpretive But that’s an implementation detail Typically have high level data structures But rich libraries can substitute for this For example, look at GNAT.Spitbol Powerful flexible string handling Typically have rich libraries But any language can meet this requirement
  • 49. Is Python A Scripting Language? Usually thought of as one But this is mainly a marketing issue People think of scripting languages as being easy to learn, and useful. But Python is a well worked out coherent dynamic programming language And there is no reason not to use it for a wide range of applications.

Editor's Notes

  1. Newsgroup comp.lang.python, with a mailing list mirror available at www.python.org Available on almost every OS in any sort of common use - all Unixs and variants, Redhat linux includes RPMs, mainframes, CE devices, etc No separate compilation step - compiled version is cached when used.
  2. Many people new to Python have trouble seeing any significant different between tuples and lists. Lists are mutable, so can not be used as dictionary keys Tuples are immutable, so are suited to be used in the place of structures. Lists are generally used to hold variable length sequences - as tuples are immutable, they are generally used to hold sequences whose length is known in advance.
  3. Note
  4. The number printed in the function object representation is simply the address is memory of the object. Objects can define their own printed representation.