SlideShare a Scribd company logo
scripting in Python
OUTLINE
• Why Use Python?
• Running Python
• Types and Operators
• Basic Statements
• Functions
• Some programs
• Industry applications
WHY PYTHON?
 Python is a general-purpose, interpreted high-level programming
language
 It's free (open source)
Downloading and installing Python is free and easy
Source code is easily accessible
Free doesn't mean unsupported! Online Python community is huge
 It's portable
Python runs virtually every major platform used today
It just need you to have a compatible Python interpreter installed
 It's powerful
Dynamic typing
Built-in types and tools
Library utilities
Automatic memory management
Running Python
 $ python
 print 'Hello world'
 Hello world
# Relevant output is displayed on subsequent lines without the
>>> symbol
 >>> x = [0,1,2]
# Quantities stored in memory are not displayed by default
 >>> x
# If a quantity is stored in memory, typing its name will display it
 [0,1,2]
 >>> 2+3
 5
 >>> # Type ctrl-D to exit the interpreter
 $
 Suppose the file script.py contains the following
lines:
 print 'Hello world'
 x = [0,1,2]
To execute the script
 $ python -i script.py
 Hello world
 >>> x
 [0,1,2]
 >>>
 # “Hello world” is printed, x is stored and can be called later, and the
interpreter is left open
 >>> import script
 Hello world
 >>> script.x
 [1,2,3]
Types and operators: operations on
numbers
 Basic algebraic operations
Four arithmetic operations: a+b, a-b, a*b, a/b
Exponentiation: a**b
 Comparison operators
Greater than, less than, etc.: a < b, a > b ,a <= b, a>= b
Identity tests: a == b, a != b
 Bitwise operators
Bitwise or: a | b
Bitwise exclusive or: a ^ b # Don't confuse this with
exponentiation
Bitwise and: a & b
Shift a left or right by b bits: a << b, a >> b
Types and operators: strings and
operations
 Strings are ordered blocks of text
Strings are enclosed in single or double quotation marks
Examples: 'abc', “ABC”
 Concatenation and repetition
Strings are concatenated with the + sign:
>>> 'abc'+'def'
'abcdef'
Strings are repeated with the * sign:
>>> 'abc'*3
'abcabcabc'
 Indexing and slicing, contd.
s[i:j:k] extracts every kth element starting with index i
(inclusive) and ending with index j (not inclusive)
>>> s[0:5:2]
'srn'
Python also supports negative indexes. For example, s[-1]
means extract the first element of s from the end.
>>> s[-1]
'g‘
>>> s[-2]
'n‘
>>> s.upper()
STRING
 Indexing and slicing
Python starts indexing at 0. A string s will have indexes running
from 0 to len(s)-1 (where len(s) is the length of s) in
integer quantities.
s[i] fetches the i th element in s
>>> s = ‘string'
>>> s[1] # note that Python considers 't' the first element
't' # of our string s
s[i:j] fetches elements i (inclusive) through j (not inclusive)
>>> s[1:4]
'tri'
s[:j] fetches all elements up to, but not including j
>>> s[:3]
'str'
s[i:] fetches all elements from i onward (inclusive)
>>> s[2:]
'ring'
Types and operators: Lists
 Basic properties:
Lists are contained in square brackets []
Lists can contain numbers, strings, nested sublists, or nothing
Examples: L1 = [0,1,2,3], L2 = ['zero', 'one'],
L3 = [0,1,[2,3],'three',['four,one']], L4 = []
List indexing and slicing works just like string indexing
 Some basic operations on lists:
Indexing: L1[i], L2[i][j]
Slicing: L3[i:j]
Concatenation:
>>> L1 = [0,1,2]; L2 = [3,4,5]
>>> L1+L2
[0,1,2,3,4,5]
Repetition:
>>> L1*3
[0,1,2,0,1,2,0,1,2]
Appending:
>>> L1.append(3)
[0,1,2,3]
Sorting:
>>> L3 = [2,1,4,3]
>>> L3.sort()
 More list operations:
Reversal:
>>> L4 = [4,3,2,1]
>>> L4.reverse()
>>> L4
[1,2,3,4]
Shrinking:
>>> del L4[2]
Making a list of integers:
>>> range(4)
[0,1,2,3]
>>> range(1,5)
[1,2,3,4]
Types and operators: arrays
 Similarities between arrays and lists:
Arrays and lists are indexed and sliced identically
Arrays and lists both have sort and reverse attributes
Differences between arrays and lists:
With arrays, the + and * signs do not refer to concatenation or repetition
Examples:
>>> ar1 = array([2,4,6])
>>> ar1+2 # Adding a constant to an array adds the constant to each
term
[4,6,8,] # in the array
>>> ar1*2 # Multiplying an array by a constant multiplies each term
in # the array by 2
[4,8,12,]
Contd:
Adding two arrays is just like adding two vectors
>>> ar1 = array([2,4,6]); ar2 = array([1,2,3])
>>> ar1+ar2
[3,6,9,]
Multiplying two arrays multiplies them term by term:
>>> ar1*ar2
[2,8,18,]
Same for division:
>>> ar1/ar2
[2,2,2,]
Assuming the function can take vector arguments, a function
acting on an array acts on each term in the array
>>> ar2**2
[1,4,9,]
Basic Statements: The if statement
 If statements have the following basic structure:
 # inside the interpreter # inside a script
 >>> if condition: if condition:
... action action
...
>>>
4 spaces
>>> x=1
>>> if x< 2:
…. Print ‘Hello world’
….
Hello world
Basic Statements: While statement
 >>> while x < 4 :
…. Print x**2 # square of x
…. x = x+1
….
1
4
9 # sqaure of 4 is not there as 4 is not included
Basic Statements: For statement
 For statements have the following basic structure:
for item i in set s:
action on item i
 # item and set are not statements here; they are merely intended to
clarify the relationships between i and s
 Example:
>>> for i in range(1,7):
... print i, i**2, i**3, i**4
...
1 1 1 1
2 4 8 16
3 9 27 81
4 16 64 256
5 25 125 625
6 36 216 1296
 Example 2 :
>>> L = [0,1,2,3] # or, equivalently, range(4)
>>> for i in range(len(L)):
... L[i] = L[i]**2
...
>>> L
[0,1,4,9]
Functions
 Usually, function definitions have the following basic structure:
 def func(args):
return values
 >>> def f1(x):
... return x*(x-1)
...
>>> f1(3)
 def f2(x,y):
... return x+y,x-y
...
>>> f2(3,2)
(5,1)
 def avg (a,b):
return (a+b) /2
>>> avg(1,1)
1
 def f3():
 ... print 'Hello world'
 ...
 >>> f3()
 Hello world
Note:
 >>> a = 2 # a is assigned in the
interpreter, so it's global
 >>> def f(x): # x is in the function's
argument list, so it's local
 ... y = x+a # y is only assigned inside
the function, so it's local
Programs:
# A program to covert temperature from
Celsius to Fahrenheit
def main():
... Celsius = input("What is the Celsius temperature? ")
... Fahrenheit = (9.0 / 5.0) * Celsius + 32
... print "The temperature is", Fahrenheit, "degrees
Fahrenheit."
...
>>> main()
 A program to compute the value of an investment
carried 10 years into the future
vi inv.py
def main():
print "This program calculates the future value
print "of a 10-year investment."
principal = input("Enter the initial principal: ")
apr = input("Enter the annual interest rate: ")
for i in range(10):
principal = principal * (1 + apr)
print "The value in 10 years is:", principal
$ python –i inv.py
>>> main()
Industry applications:
 web programming (client and server side)
 ad hoc programming ("scripting")
 steering scientific applications
 extension language
 database applications
 GUI applications
 education
Who is using it?
 Google (various projects)
 NASA (several projects)
 Industrial Light & Magic (everything)
 Yahoo! (Yahoo mail & groups)
 Real Networks (function and load testing)
 RedHat (Linux installation tools)
scripting in Python

More Related Content

What's hot (20)

14 static timing_analysis_5_clock_domain_crossing
14 static timing_analysis_5_clock_domain_crossing14 static timing_analysis_5_clock_domain_crossing
14 static timing_analysis_5_clock_domain_crossing
Usha Mehta
 
DRCs.pptx
DRCs.pptxDRCs.pptx
DRCs.pptx
ssuserd05d591
 
Advance Peripheral Bus
Advance Peripheral Bus Advance Peripheral Bus
Advance Peripheral Bus
SIVA NAGENDRA REDDY
 
VHDL- gate level modelling
VHDL- gate level modellingVHDL- gate level modelling
VHDL- gate level modelling
VandanaPagar1
 
An Overview of SystemVerilog for Design and Verification
An Overview of SystemVerilog  for Design and VerificationAn Overview of SystemVerilog  for Design and Verification
An Overview of SystemVerilog for Design and Verification
KapilRaghunandanTrip
 
System verilog verification building blocks
System verilog verification building blocksSystem verilog verification building blocks
System verilog verification building blocks
Nirav Desai
 
UVM Methodology Tutorial
UVM Methodology TutorialUVM Methodology Tutorial
UVM Methodology Tutorial
Arrow Devices
 
Ambha axi
Ambha axiAmbha axi
Ambha axi
HARINATH REDDY
 
Register transfer and micro-operation
Register transfer and micro-operationRegister transfer and micro-operation
Register transfer and micro-operation
Nikhil Pandit
 
Computer Architecture – An Introduction
Computer Architecture – An IntroductionComputer Architecture – An Introduction
Computer Architecture – An Introduction
Dilum Bandara
 
Divide by N clock
Divide by N clockDivide by N clock
Divide by N clock
Mantra VLSI
 
Pcie basic
Pcie basicPcie basic
Pcie basic
Saifuddin Kaijar
 
Ral by pushpa
Ral by pushpa Ral by pushpa
Ral by pushpa
Pushpa Yakkala
 
Branch prediction
Branch predictionBranch prediction
Branch prediction
Aneesh Raveendran
 
Alu design-project
Alu design-projectAlu design-project
Alu design-project
alphankg1
 
Physical design
Physical design Physical design
Physical design
Mantra VLSI
 
Concepts of Behavioral modelling in Verilog HDL
Concepts of Behavioral modelling in Verilog HDLConcepts of Behavioral modelling in Verilog HDL
Concepts of Behavioral modelling in Verilog HDL
anand hd
 
CISC & RISC Architecture
CISC & RISC Architecture CISC & RISC Architecture
CISC & RISC Architecture
Suvendu Kumar Dash
 
Introduction to System verilog
Introduction to System verilog Introduction to System verilog
Introduction to System verilog
Pushpa Yakkala
 
Pcie drivers basics
Pcie drivers basicsPcie drivers basics
Pcie drivers basics
Venkatesh Malla
 
14 static timing_analysis_5_clock_domain_crossing
14 static timing_analysis_5_clock_domain_crossing14 static timing_analysis_5_clock_domain_crossing
14 static timing_analysis_5_clock_domain_crossing
Usha Mehta
 
VHDL- gate level modelling
VHDL- gate level modellingVHDL- gate level modelling
VHDL- gate level modelling
VandanaPagar1
 
An Overview of SystemVerilog for Design and Verification
An Overview of SystemVerilog  for Design and VerificationAn Overview of SystemVerilog  for Design and Verification
An Overview of SystemVerilog for Design and Verification
KapilRaghunandanTrip
 
System verilog verification building blocks
System verilog verification building blocksSystem verilog verification building blocks
System verilog verification building blocks
Nirav Desai
 
UVM Methodology Tutorial
UVM Methodology TutorialUVM Methodology Tutorial
UVM Methodology Tutorial
Arrow Devices
 
Register transfer and micro-operation
Register transfer and micro-operationRegister transfer and micro-operation
Register transfer and micro-operation
Nikhil Pandit
 
Computer Architecture – An Introduction
Computer Architecture – An IntroductionComputer Architecture – An Introduction
Computer Architecture – An Introduction
Dilum Bandara
 
Divide by N clock
Divide by N clockDivide by N clock
Divide by N clock
Mantra VLSI
 
Alu design-project
Alu design-projectAlu design-project
Alu design-project
alphankg1
 
Physical design
Physical design Physical design
Physical design
Mantra VLSI
 
Concepts of Behavioral modelling in Verilog HDL
Concepts of Behavioral modelling in Verilog HDLConcepts of Behavioral modelling in Verilog HDL
Concepts of Behavioral modelling in Verilog HDL
anand hd
 
Introduction to System verilog
Introduction to System verilog Introduction to System verilog
Introduction to System verilog
Pushpa Yakkala
 

Viewers also liked (20)

Linux Basics
Linux BasicsLinux Basics
Linux Basics
Team-VLSI-ITMU
 
CAD: Layout Extraction
CAD: Layout ExtractionCAD: Layout Extraction
CAD: Layout Extraction
Team-VLSI-ITMU
 
Reduced ordered binary decision diagram
Reduced ordered binary decision diagramReduced ordered binary decision diagram
Reduced ordered binary decision diagram
Team-VLSI-ITMU
 
RTX Kernal
RTX KernalRTX Kernal
RTX Kernal
Team-VLSI-ITMU
 
Computer Aided Design: Global Routing
Computer Aided Design:  Global RoutingComputer Aided Design:  Global Routing
Computer Aided Design: Global Routing
Team-VLSI-ITMU
 
CAD: Floorplanning
CAD: Floorplanning CAD: Floorplanning
CAD: Floorplanning
Team-VLSI-ITMU
 
Computer Aided Design: Layout Compaction
Computer Aided Design: Layout CompactionComputer Aided Design: Layout Compaction
Computer Aided Design: Layout Compaction
Team-VLSI-ITMU
 
CAD: introduction to floorplanning
CAD:  introduction to floorplanningCAD:  introduction to floorplanning
CAD: introduction to floorplanning
Team-VLSI-ITMU
 
Ch 6 randomization
Ch 6 randomizationCh 6 randomization
Ch 6 randomization
Team-VLSI-ITMU
 
CNTFET
CNTFETCNTFET
CNTFET
Team-VLSI-ITMU
 
Placement in VLSI Design
Placement in VLSI DesignPlacement in VLSI Design
Placement in VLSI Design
Team-VLSI-ITMU
 
VLSI routing
VLSI routingVLSI routing
VLSI routing
Naveen Kumar
 
Nmos design using synopsys TCAD tool
Nmos design using synopsys TCAD toolNmos design using synopsys TCAD tool
Nmos design using synopsys TCAD tool
Team-VLSI-ITMU
 
Physical design-complete
Physical design-completePhysical design-complete
Physical design-complete
Murali Rai
 
twin well cmos fabrication steps using Synopsys TCAD
twin well cmos fabrication steps using Synopsys TCADtwin well cmos fabrication steps using Synopsys TCAD
twin well cmos fabrication steps using Synopsys TCAD
Team-VLSI-ITMU
 
floor planning
floor planningfloor planning
floor planning
Team-VLSI-ITMU
 
Floorplanning in physical design
Floorplanning in physical designFloorplanning in physical design
Floorplanning in physical design
Murali Rai
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
amiable_indian
 
VLSI-Physical Design- Tool Terminalogy
VLSI-Physical Design- Tool TerminalogyVLSI-Physical Design- Tool Terminalogy
VLSI-Physical Design- Tool Terminalogy
Murali Rai
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
Nowell Strite
 
CAD: Layout Extraction
CAD: Layout ExtractionCAD: Layout Extraction
CAD: Layout Extraction
Team-VLSI-ITMU
 
Reduced ordered binary decision diagram
Reduced ordered binary decision diagramReduced ordered binary decision diagram
Reduced ordered binary decision diagram
Team-VLSI-ITMU
 
Computer Aided Design: Global Routing
Computer Aided Design:  Global RoutingComputer Aided Design:  Global Routing
Computer Aided Design: Global Routing
Team-VLSI-ITMU
 
Computer Aided Design: Layout Compaction
Computer Aided Design: Layout CompactionComputer Aided Design: Layout Compaction
Computer Aided Design: Layout Compaction
Team-VLSI-ITMU
 
CAD: introduction to floorplanning
CAD:  introduction to floorplanningCAD:  introduction to floorplanning
CAD: introduction to floorplanning
Team-VLSI-ITMU
 
Placement in VLSI Design
Placement in VLSI DesignPlacement in VLSI Design
Placement in VLSI Design
Team-VLSI-ITMU
 
Nmos design using synopsys TCAD tool
Nmos design using synopsys TCAD toolNmos design using synopsys TCAD tool
Nmos design using synopsys TCAD tool
Team-VLSI-ITMU
 
Physical design-complete
Physical design-completePhysical design-complete
Physical design-complete
Murali Rai
 
twin well cmos fabrication steps using Synopsys TCAD
twin well cmos fabrication steps using Synopsys TCADtwin well cmos fabrication steps using Synopsys TCAD
twin well cmos fabrication steps using Synopsys TCAD
Team-VLSI-ITMU
 
Floorplanning in physical design
Floorplanning in physical designFloorplanning in physical design
Floorplanning in physical design
Murali Rai
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
amiable_indian
 
VLSI-Physical Design- Tool Terminalogy
VLSI-Physical Design- Tool TerminalogyVLSI-Physical Design- Tool Terminalogy
VLSI-Physical Design- Tool Terminalogy
Murali Rai
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
Nowell Strite
 
Ad

Similar to scripting in Python (20)

Introduction_to_Python_operators_datatypes.pptx
Introduction_to_Python_operators_datatypes.pptxIntroduction_to_Python_operators_datatypes.pptx
Introduction_to_Python_operators_datatypes.pptx
MadhusmitaSahu40
 
python_computer engineering_semester_computer_language.pptx
python_computer engineering_semester_computer_language.pptxpython_computer engineering_semester_computer_language.pptx
python_computer engineering_semester_computer_language.pptx
MadhusmitaSahu40
 
Python: An introduction A summer workshop
Python: An  introduction A summer workshopPython: An  introduction A summer workshop
Python: An introduction A summer workshop
ForrayFerenc
 
Introduction To Programming with Python
Introduction To Programming with PythonIntroduction To Programming with Python
Introduction To Programming with Python
Sushant Mane
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
Girish Khanzode
 
Python Demo.pptx
Python Demo.pptxPython Demo.pptx
Python Demo.pptx
ParveenShaik21
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptx
rohithprabhas1
 
Python Demo.pptx
Python Demo.pptxPython Demo.pptx
Python Demo.pptx
ParveenShaik21
 
python language programming presentation
python language  programming presentationpython language  programming presentation
python language programming presentation
lbisht2
 
Python course in_mumbai
Python course in_mumbaiPython course in_mumbai
Python course in_mumbai
vibrantuser
 
Python course in_mumbai
Python course in_mumbaiPython course in_mumbai
Python course in_mumbai
vibrantuser
 
ppt_pspp.pdf
ppt_pspp.pdfppt_pspp.pdf
ppt_pspp.pdf
ShereenAhmedMohamed
 
Basics of Python and Numpy_583aab2b47e30ad9647bc4aaf13b884d.pdf
Basics of Python and Numpy_583aab2b47e30ad9647bc4aaf13b884d.pdfBasics of Python and Numpy_583aab2b47e30ad9647bc4aaf13b884d.pdf
Basics of Python and Numpy_583aab2b47e30ad9647bc4aaf13b884d.pdf
RudysBeats1
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1
Giovanni Della Lunga
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
Sasidhar Kothuru
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
Sasidhar Kothuru
 
Basics of Python and Numpy jfhfkfff.pptx
Basics of Python and Numpy jfhfkfff.pptxBasics of Python and Numpy jfhfkfff.pptx
Basics of Python and Numpy jfhfkfff.pptx
24bec055
 
funadamentals of python programming language (right from scratch)
funadamentals of python programming language (right from scratch)funadamentals of python programming language (right from scratch)
funadamentals of python programming language (right from scratch)
MdFurquan7
 
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdfQ-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
Michpice
 
Practical Python.pptx Practical Python.pptx
Practical Python.pptx Practical Python.pptxPractical Python.pptx Practical Python.pptx
Practical Python.pptx Practical Python.pptx
trwdcn
 
Introduction_to_Python_operators_datatypes.pptx
Introduction_to_Python_operators_datatypes.pptxIntroduction_to_Python_operators_datatypes.pptx
Introduction_to_Python_operators_datatypes.pptx
MadhusmitaSahu40
 
python_computer engineering_semester_computer_language.pptx
python_computer engineering_semester_computer_language.pptxpython_computer engineering_semester_computer_language.pptx
python_computer engineering_semester_computer_language.pptx
MadhusmitaSahu40
 
Python: An introduction A summer workshop
Python: An  introduction A summer workshopPython: An  introduction A summer workshop
Python: An introduction A summer workshop
ForrayFerenc
 
Introduction To Programming with Python
Introduction To Programming with PythonIntroduction To Programming with Python
Introduction To Programming with Python
Sushant Mane
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptx
rohithprabhas1
 
python language programming presentation
python language  programming presentationpython language  programming presentation
python language programming presentation
lbisht2
 
Python course in_mumbai
Python course in_mumbaiPython course in_mumbai
Python course in_mumbai
vibrantuser
 
Python course in_mumbai
Python course in_mumbaiPython course in_mumbai
Python course in_mumbai
vibrantuser
 
Basics of Python and Numpy_583aab2b47e30ad9647bc4aaf13b884d.pdf
Basics of Python and Numpy_583aab2b47e30ad9647bc4aaf13b884d.pdfBasics of Python and Numpy_583aab2b47e30ad9647bc4aaf13b884d.pdf
Basics of Python and Numpy_583aab2b47e30ad9647bc4aaf13b884d.pdf
RudysBeats1
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1
Giovanni Della Lunga
 
Basics of Python and Numpy jfhfkfff.pptx
Basics of Python and Numpy jfhfkfff.pptxBasics of Python and Numpy jfhfkfff.pptx
Basics of Python and Numpy jfhfkfff.pptx
24bec055
 
funadamentals of python programming language (right from scratch)
funadamentals of python programming language (right from scratch)funadamentals of python programming language (right from scratch)
funadamentals of python programming language (right from scratch)
MdFurquan7
 
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdfQ-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
Michpice
 
Practical Python.pptx Practical Python.pptx
Practical Python.pptx Practical Python.pptxPractical Python.pptx Practical Python.pptx
Practical Python.pptx Practical Python.pptx
trwdcn
 
Ad

Recently uploaded (20)

SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptxWeek 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptxPrésentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbbTree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
RATNANITINPATIL
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for  diesel tank.docxCenter Enamel can Provide Aluminum Dome Roofs for  diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdfOCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
Engineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its ApplicationEngineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its Application
Sakthivel M
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdfHow Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
grade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quizgrade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quiz
norfapangolima
 
Flow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docxFlow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docx
rifka575530
 
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
Journal of Soft Computing in Civil Engineering
 
First Come First Serve Scheduling in real time operating system.pptx
First Come First Serve Scheduling in real time operating system.pptxFirst Come First Serve Scheduling in real time operating system.pptx
First Come First Serve Scheduling in real time operating system.pptx
KavitaBagewadi2
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,PitchNALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank
Guru Nanak Technical Institutions
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptxDevelopment of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptxFINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
kippcam
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptxWeek 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptxPrésentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbbTree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
RATNANITINPATIL
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for  diesel tank.docxCenter Enamel can Provide Aluminum Dome Roofs for  diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdfOCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
Engineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its ApplicationEngineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its Application
Sakthivel M
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdfHow Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
grade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quizgrade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quiz
norfapangolima
 
Flow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docxFlow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docx
rifka575530
 
First Come First Serve Scheduling in real time operating system.pptx
First Come First Serve Scheduling in real time operating system.pptxFirst Come First Serve Scheduling in real time operating system.pptx
First Come First Serve Scheduling in real time operating system.pptx
KavitaBagewadi2
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,PitchNALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptxDevelopment of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptxFINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
kippcam
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 

scripting in Python

  • 2. OUTLINE • Why Use Python? • Running Python • Types and Operators • Basic Statements • Functions • Some programs • Industry applications
  • 3. WHY PYTHON?  Python is a general-purpose, interpreted high-level programming language  It's free (open source) Downloading and installing Python is free and easy Source code is easily accessible Free doesn't mean unsupported! Online Python community is huge  It's portable Python runs virtually every major platform used today It just need you to have a compatible Python interpreter installed  It's powerful Dynamic typing Built-in types and tools Library utilities Automatic memory management
  • 4. Running Python  $ python  print 'Hello world'  Hello world # Relevant output is displayed on subsequent lines without the >>> symbol  >>> x = [0,1,2] # Quantities stored in memory are not displayed by default  >>> x # If a quantity is stored in memory, typing its name will display it  [0,1,2]  >>> 2+3  5  >>> # Type ctrl-D to exit the interpreter  $
  • 5.  Suppose the file script.py contains the following lines:  print 'Hello world'  x = [0,1,2] To execute the script  $ python -i script.py  Hello world  >>> x  [0,1,2]  >>>  # “Hello world” is printed, x is stored and can be called later, and the interpreter is left open  >>> import script  Hello world  >>> script.x  [1,2,3]
  • 6. Types and operators: operations on numbers  Basic algebraic operations Four arithmetic operations: a+b, a-b, a*b, a/b Exponentiation: a**b  Comparison operators Greater than, less than, etc.: a < b, a > b ,a <= b, a>= b Identity tests: a == b, a != b  Bitwise operators Bitwise or: a | b Bitwise exclusive or: a ^ b # Don't confuse this with exponentiation Bitwise and: a & b Shift a left or right by b bits: a << b, a >> b
  • 7. Types and operators: strings and operations  Strings are ordered blocks of text Strings are enclosed in single or double quotation marks Examples: 'abc', “ABC”  Concatenation and repetition Strings are concatenated with the + sign: >>> 'abc'+'def' 'abcdef' Strings are repeated with the * sign: >>> 'abc'*3 'abcabcabc'
  • 8.  Indexing and slicing, contd. s[i:j:k] extracts every kth element starting with index i (inclusive) and ending with index j (not inclusive) >>> s[0:5:2] 'srn' Python also supports negative indexes. For example, s[-1] means extract the first element of s from the end. >>> s[-1] 'g‘ >>> s[-2] 'n‘ >>> s.upper() STRING
  • 9.  Indexing and slicing Python starts indexing at 0. A string s will have indexes running from 0 to len(s)-1 (where len(s) is the length of s) in integer quantities. s[i] fetches the i th element in s >>> s = ‘string' >>> s[1] # note that Python considers 't' the first element 't' # of our string s s[i:j] fetches elements i (inclusive) through j (not inclusive) >>> s[1:4] 'tri' s[:j] fetches all elements up to, but not including j >>> s[:3] 'str' s[i:] fetches all elements from i onward (inclusive) >>> s[2:] 'ring'
  • 10. Types and operators: Lists  Basic properties: Lists are contained in square brackets [] Lists can contain numbers, strings, nested sublists, or nothing Examples: L1 = [0,1,2,3], L2 = ['zero', 'one'], L3 = [0,1,[2,3],'three',['four,one']], L4 = [] List indexing and slicing works just like string indexing
  • 11.  Some basic operations on lists: Indexing: L1[i], L2[i][j] Slicing: L3[i:j] Concatenation: >>> L1 = [0,1,2]; L2 = [3,4,5] >>> L1+L2 [0,1,2,3,4,5] Repetition: >>> L1*3 [0,1,2,0,1,2,0,1,2] Appending: >>> L1.append(3) [0,1,2,3] Sorting: >>> L3 = [2,1,4,3] >>> L3.sort()
  • 12.  More list operations: Reversal: >>> L4 = [4,3,2,1] >>> L4.reverse() >>> L4 [1,2,3,4] Shrinking: >>> del L4[2] Making a list of integers: >>> range(4) [0,1,2,3] >>> range(1,5) [1,2,3,4]
  • 13. Types and operators: arrays  Similarities between arrays and lists: Arrays and lists are indexed and sliced identically Arrays and lists both have sort and reverse attributes Differences between arrays and lists: With arrays, the + and * signs do not refer to concatenation or repetition Examples: >>> ar1 = array([2,4,6]) >>> ar1+2 # Adding a constant to an array adds the constant to each term [4,6,8,] # in the array >>> ar1*2 # Multiplying an array by a constant multiplies each term in # the array by 2 [4,8,12,]
  • 14. Contd: Adding two arrays is just like adding two vectors >>> ar1 = array([2,4,6]); ar2 = array([1,2,3]) >>> ar1+ar2 [3,6,9,] Multiplying two arrays multiplies them term by term: >>> ar1*ar2 [2,8,18,] Same for division: >>> ar1/ar2 [2,2,2,] Assuming the function can take vector arguments, a function acting on an array acts on each term in the array >>> ar2**2 [1,4,9,]
  • 15. Basic Statements: The if statement  If statements have the following basic structure:  # inside the interpreter # inside a script  >>> if condition: if condition: ... action action ... >>> 4 spaces >>> x=1 >>> if x< 2: …. Print ‘Hello world’ …. Hello world
  • 16. Basic Statements: While statement  >>> while x < 4 : …. Print x**2 # square of x …. x = x+1 …. 1 4 9 # sqaure of 4 is not there as 4 is not included
  • 17. Basic Statements: For statement  For statements have the following basic structure: for item i in set s: action on item i  # item and set are not statements here; they are merely intended to clarify the relationships between i and s  Example: >>> for i in range(1,7): ... print i, i**2, i**3, i**4 ... 1 1 1 1 2 4 8 16 3 9 27 81 4 16 64 256 5 25 125 625 6 36 216 1296
  • 18.  Example 2 : >>> L = [0,1,2,3] # or, equivalently, range(4) >>> for i in range(len(L)): ... L[i] = L[i]**2 ... >>> L [0,1,4,9]
  • 19. Functions  Usually, function definitions have the following basic structure:  def func(args): return values  >>> def f1(x): ... return x*(x-1) ... >>> f1(3)  def f2(x,y): ... return x+y,x-y ... >>> f2(3,2) (5,1)
  • 20.  def avg (a,b): return (a+b) /2 >>> avg(1,1) 1  def f3():  ... print 'Hello world'  ...  >>> f3()  Hello world
  • 21. Note:  >>> a = 2 # a is assigned in the interpreter, so it's global  >>> def f(x): # x is in the function's argument list, so it's local  ... y = x+a # y is only assigned inside the function, so it's local
  • 22. Programs: # A program to covert temperature from Celsius to Fahrenheit def main(): ... Celsius = input("What is the Celsius temperature? ") ... Fahrenheit = (9.0 / 5.0) * Celsius + 32 ... print "The temperature is", Fahrenheit, "degrees Fahrenheit." ... >>> main()
  • 23.  A program to compute the value of an investment carried 10 years into the future vi inv.py def main(): print "This program calculates the future value print "of a 10-year investment." principal = input("Enter the initial principal: ") apr = input("Enter the annual interest rate: ") for i in range(10): principal = principal * (1 + apr) print "The value in 10 years is:", principal $ python –i inv.py >>> main()
  • 24. Industry applications:  web programming (client and server side)  ad hoc programming ("scripting")  steering scientific applications  extension language  database applications  GUI applications  education
  • 25. Who is using it?  Google (various projects)  NASA (several projects)  Industrial Light & Magic (everything)  Yahoo! (Yahoo mail & groups)  Real Networks (function and load testing)  RedHat (Linux installation tools)