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

PPTX
Advanced Pipelining in ARM Processors.pptx
JoyChowdhury30
 
PDF
UVM TUTORIAL;
Azad Mishra
 
PDF
Pcie basic
Saifuddin Kaijar
 
PPTX
ARM Processor
Aniket Thakur
 
PDF
3.1 structure of a wireless communicaiton link
JAIGANESH SEKAR
 
DOC
Darshan Dehuniya - Resume - ASIC Verification Engineer (1)
Darshan Dehuniya
 
PDF
Digital modulation basics(nnm)
nnmaurya
 
PPTX
ARM- Programmer's Model
Ravikumar Tiwari
 
Advanced Pipelining in ARM Processors.pptx
JoyChowdhury30
 
UVM TUTORIAL;
Azad Mishra
 
Pcie basic
Saifuddin Kaijar
 
ARM Processor
Aniket Thakur
 
3.1 structure of a wireless communicaiton link
JAIGANESH SEKAR
 
Darshan Dehuniya - Resume - ASIC Verification Engineer (1)
Darshan Dehuniya
 
Digital modulation basics(nnm)
nnmaurya
 
ARM- Programmer's Model
Ravikumar Tiwari
 

What's hot (20)

PPT
Spi master core verification
Maulik Suthar
 
PPTX
PCIe
ChiaYang Tsai
 
PPT
8051 serial communication-UART
Pantech ProLabs India Pvt Ltd
 
PPTX
Slideshare - PCIe
Jin Wu
 
PPTX
Ambha axi
HARINATH REDDY
 
PPT
Lecture13
INDHULEKSHMI M.C
 
PPTX
8051 MICROCONTROLLER ARCHITECTURE.pptx
MemonaMemon1
 
PDF
8051 micro controllers Instruction set
Nitin Ahire
 
PDF
Pci express technology 3.0
Biddika Manjusree
 
PDF
CPU Verification
Ramdas Mozhikunnath
 
PPT
Spread spectrum techniques
Faisal Ch
 
PDF
Verification Strategy for PCI-Express
DVClub
 
PPT
Coding style for good synthesis
Vinchipsytm Vlsitraining
 
ODP
axi protocol
Azad Mishra
 
PPTX
Uart
Aditee Apurvaa
 
PPTX
SATA Protocol
Nirav Desai
 
PDF
1 RB sensitivity at middle RBs poor than other RBs
Pei-Che Chang
 
PPT
Equalization
bhabendu
 
PPTX
UART(universal asynchronous receiver transmitter ) PPT
Sai_praneeth
 
PDF
Verification of amba axi bus protocol implementing incr and wrap burst using ...
eSAT Journals
 
Spi master core verification
Maulik Suthar
 
8051 serial communication-UART
Pantech ProLabs India Pvt Ltd
 
Slideshare - PCIe
Jin Wu
 
Ambha axi
HARINATH REDDY
 
Lecture13
INDHULEKSHMI M.C
 
8051 MICROCONTROLLER ARCHITECTURE.pptx
MemonaMemon1
 
8051 micro controllers Instruction set
Nitin Ahire
 
Pci express technology 3.0
Biddika Manjusree
 
CPU Verification
Ramdas Mozhikunnath
 
Spread spectrum techniques
Faisal Ch
 
Verification Strategy for PCI-Express
DVClub
 
Coding style for good synthesis
Vinchipsytm Vlsitraining
 
axi protocol
Azad Mishra
 
SATA Protocol
Nirav Desai
 
1 RB sensitivity at middle RBs poor than other RBs
Pei-Che Chang
 
Equalization
bhabendu
 
UART(universal asynchronous receiver transmitter ) PPT
Sai_praneeth
 
Verification of amba axi bus protocol implementing incr and wrap burst using ...
eSAT Journals
 
Ad

Viewers also liked (20)

PDF
Linux Basics
Team-VLSI-ITMU
 
PPTX
CAD: Layout Extraction
Team-VLSI-ITMU
 
PPTX
Reduced ordered binary decision diagram
Team-VLSI-ITMU
 
PPTX
RTX Kernal
Team-VLSI-ITMU
 
PPT
Computer Aided Design: Global Routing
Team-VLSI-ITMU
 
PPTX
CAD: Floorplanning
Team-VLSI-ITMU
 
PPTX
Computer Aided Design: Layout Compaction
Team-VLSI-ITMU
 
PPTX
CAD: introduction to floorplanning
Team-VLSI-ITMU
 
PPTX
Ch 6 randomization
Team-VLSI-ITMU
 
PPT
CNTFET
Team-VLSI-ITMU
 
PPTX
Placement in VLSI Design
Team-VLSI-ITMU
 
PPT
VLSI routing
Naveen Kumar
 
PPTX
Nmos design using synopsys TCAD tool
Team-VLSI-ITMU
 
PDF
Physical design-complete
Murali Rai
 
PPTX
twin well cmos fabrication steps using Synopsys TCAD
Team-VLSI-ITMU
 
PPT
floor planning
Team-VLSI-ITMU
 
PDF
Floorplanning in physical design
Murali Rai
 
PPT
Introduction to Python
amiable_indian
 
PDF
VLSI-Physical Design- Tool Terminalogy
Murali Rai
 
PPT
Introduction to Python
Nowell Strite
 
Linux Basics
Team-VLSI-ITMU
 
CAD: Layout Extraction
Team-VLSI-ITMU
 
Reduced ordered binary decision diagram
Team-VLSI-ITMU
 
RTX Kernal
Team-VLSI-ITMU
 
Computer Aided Design: Global Routing
Team-VLSI-ITMU
 
CAD: Floorplanning
Team-VLSI-ITMU
 
Computer Aided Design: Layout Compaction
Team-VLSI-ITMU
 
CAD: introduction to floorplanning
Team-VLSI-ITMU
 
Ch 6 randomization
Team-VLSI-ITMU
 
Placement in VLSI Design
Team-VLSI-ITMU
 
VLSI routing
Naveen Kumar
 
Nmos design using synopsys TCAD tool
Team-VLSI-ITMU
 
Physical design-complete
Murali Rai
 
twin well cmos fabrication steps using Synopsys TCAD
Team-VLSI-ITMU
 
floor planning
Team-VLSI-ITMU
 
Floorplanning in physical design
Murali Rai
 
Introduction to Python
amiable_indian
 
VLSI-Physical Design- Tool Terminalogy
Murali Rai
 
Introduction to Python
Nowell Strite
 
Ad

Similar to scripting in Python (20)

PDF
An overview of Python 2.7
decoupled
 
PDF
A tour of Python
Aleksandar Veselinovic
 
PPT
Data types usually used in python for coding
PriyankaRajaboina
 
PPT
02python.ppt
ssuser492e7f
 
PPT
02python.ppt
rehanafarheenece
 
PPTX
Python Scipy Numpy
Girish Khanzode
 
PPTX
Python language data types
Harry Potter
 
PPTX
Python language data types
Young Alista
 
PPTX
Python language data types
Tony Nguyen
 
PPTX
Python language data types
James Wong
 
PPTX
Python language data types
Luis Goldster
 
PPTX
Python language data types
Hoang Nguyen
 
PPTX
Python language data types
Fraboni Ec
 
PPTX
009 Data Handling class 11 -converted.pptx
adityakumar123456112
 
PPTX
009 Data Handling class 11 -converted.pptx
adityakumar123456112
 
PPT
Python lab basics
Abi_Kasi
 
PPTX
Functions in advanced programming
VisnuDharsini
 
PPT
sonam Kumari python.ppt
ssuserd64918
 
PPT
ComandosDePython_ComponentesBasicosImpl.ppt
oscarJulianPerdomoCh1
 
PDF
Python lecture 05
Tanwir Zaman
 
An overview of Python 2.7
decoupled
 
A tour of Python
Aleksandar Veselinovic
 
Data types usually used in python for coding
PriyankaRajaboina
 
02python.ppt
ssuser492e7f
 
02python.ppt
rehanafarheenece
 
Python Scipy Numpy
Girish Khanzode
 
Python language data types
Harry Potter
 
Python language data types
Young Alista
 
Python language data types
Tony Nguyen
 
Python language data types
James Wong
 
Python language data types
Luis Goldster
 
Python language data types
Hoang Nguyen
 
Python language data types
Fraboni Ec
 
009 Data Handling class 11 -converted.pptx
adityakumar123456112
 
009 Data Handling class 11 -converted.pptx
adityakumar123456112
 
Python lab basics
Abi_Kasi
 
Functions in advanced programming
VisnuDharsini
 
sonam Kumari python.ppt
ssuserd64918
 
ComandosDePython_ComponentesBasicosImpl.ppt
oscarJulianPerdomoCh1
 
Python lecture 05
Tanwir Zaman
 

Recently uploaded (20)

PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PPT
SCOPE_~1- technology of green house and poyhouse
bala464780
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PDF
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
PDF
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
PPTX
22PCOAM21 Data Quality Session 3 Data Quality.pptx
Guru Nanak Technical Institutions
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
PPT
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
PDF
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PPTX
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
PDF
Introduction to Data Science: data science process
ShivarkarSandip
 
PPTX
Color Model in Textile ( RGB, CMYK).pptx
auladhossain191
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
SCOPE_~1- technology of green house and poyhouse
bala464780
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
Inventory management chapter in automation and robotics.
atisht0104
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
22PCOAM21 Data Quality Session 3 Data Quality.pptx
Guru Nanak Technical Institutions
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
Introduction to Data Science: data science process
ShivarkarSandip
 
Color Model in Textile ( RGB, CMYK).pptx
auladhossain191
 

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)