0% found this document useful (0 votes)
4 views28 pages

Python Programming Lecture1

Python has gained significant popularity in the last five years, particularly in the fields of scientific computing and machine learning. It supports multiple programming paradigms, has a simple syntax, and is dynamically typed, making it versatile for various applications including web development, data analysis, and game development. Python's interpreted nature and inherent portability across platforms contribute to its widespread use, despite some performance trade-offs compared to compiled languages.

Uploaded by

ph25c013
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views28 pages

Python Programming Lecture1

Python has gained significant popularity in the last five years, particularly in the fields of scientific computing and machine learning. It supports multiple programming paradigms, has a simple syntax, and is dynamically typed, making it versatile for various applications including web development, data analysis, and game development. Python's interpreted nature and inherent portability across platforms contribute to its widespread use, despite some performance trade-offs compared to compiled languages.

Uploaded by

ph25c013
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python's place in the

computing world

2
Major Programming Languages
in the Scientific World
Byte Compiled
Compiled (partly compiled + Interpreted
interpreted)

Fortran Tcl
t ive l ibraries)
n A PI (ac cess na
Extensio
C
g i n side C/C++
n
Embeddi

C++

● dynamic type
● static type ● portable
● fast ● relatively slow
● source level portability ● scripting/embedding
3
● interface to C/C++/Java
Major Programming Languages
in the Scientific World
Byte Compiled
Compiled (partly compiled + Interpreted
interpreted)

Fortran Tcl
t ive l ibraries) Perl
n A PI (ac cess na
Extensio
C
g i n side C/C++
n
Embeddi

C++

● dynamic type
● static type ● portable
● fast ● relatively slow
● source level portability ● scripting/embedding
4
● interface to C/C++/Java
Major Programming Languages
in the Scientific World
Byte Compiled
Compiled (partly compiled + Interpreted
interpreted)

Fortran Tcl
t ive l ibraries) Perl
n A PI (ac cess na
Extensio
C Python byte compiled
g i n side C/C++
n
Embeddi

C++

● dynamic type
● static type ● portable
● fast ● relatively slow
● source level portability ● scripting/embedding
5
● interface to C/C++/Java
Major Programming Languages
in the Scientific World
Byte Compiled
Compiled (partly compiled + Interpreted
interpreted)

Fortran Tcl
t ive l ibraries) Perl
n A PI (ac cess na
Exten sio
C Python byte compiled
g i n side C/C++
n
Embeddi
Jav
aN
ativ
C++ e Int
erf
ace Java BeanShell
Groovy
Jython

● dynamic type
● static type ● static type ● portable
● fast ● secure ● relatively slow
● source level portability ● portable ● scripting/embedding
6
● interface to C/C++/Java
Major Programming Languages
in the Scientific World
Byte Compiled
Compiled (partly compiled + Interpreted
interpreted)

Fortran Tcl
t ive l ibraries) Perl
n A PI (ac cess na
Exten sio
C Python byte compiled
g i n side C/C++
n
Embeddi
Jav
aN R
ativ
C++ e Int
erf PHP
ace Java BeanShell
JavaScript
Groovy
Jython

● dynamic type
● static type ● static type ● portable
● fast ● secure ● relatively slow
● source level portability ● portable ● scripting/embedding
7
● interface to C/C++/Java
Major Programming Languages
in the Scientific World
Byte Compiled
Compiled (partly compiled + Interpreted
interpreted)

Fortran Tcl
t ive l ibraries) Perl
n A PI (ac cess na
Exten sio
C Python byte compiled
g i n side C/C++
n
Embeddi
Jav
aN R
ativ
C++ e Int
erf PHP
Julia (JIT ace Java BeanShell
JavaScript
Groovy
compiled) Jython

● dynamic type
● static type ● static type ● portable
● fast ● secure ● relatively slow
● source level portability ● portable ● scripting/embedding
8
● interface to C/C++/Java
Major Programming Languages
in the Scientific World
Byte Compiled
Compiled (partly compiled + Interpreted
interpreted)

Fortran Tcl
t ive l ibraries) Perl
n A PI (ac cess na
Exten sio numba (JIT)
C Python byte compiled
g i n side C/C++
n
Go, Rust Ja Embeddi
va
Na
R
C++ tive
Int
erf PHP
Julia (JIT ace Java BeanShell
JavaScript
Groovy
compiled) Jython

● dynamic type
● static type ● static type ● portable
● fast ● secure ● relatively slow
● source level portability ● portable ● scripting/embedding
9
● interface to C/C++/Java
Popularity of Python

10
● Python has slowly become the numero uno in the last 5 years, thanks to its ubiquitous role in Machine Learning!
Python at a glance

11
Python Design Highlights
● Supports all major programming paradigms
○ procedural, object oriented and functional
○ imperative and declarative
● Simple syntax, white space important, pass
by-reference
● Dynamically typed, loosely bound
● Scripting & driver language
○ binds different components together
○ seamlessly supports multi-language environment
[Link]

● One can programmatically build Python code as string and execute on-the-fly, all inside a
running Python program
● Object Orientation from ground up (no data hiding, though!)
● A very high level language (VHLL)
○ built-in support for exception
○ introspection is an integral part of the language
● High level API to seamlessly access C/C++/Java/Fortran libraries
○ opens up infinite possibilities
○ Python objects can be directly created from the native library (.so) 12
Integrated Development Environment

● Interactive environment
○ python shell
○ ipython ([Link]
○ jupyter notebook - [Link]
○ IDLE, spyder, PyCharm
○ SWAN - [Link] - a platform
to perform interactive data analysis in the
cloud

13
Python application domain
● Scientific computing, data science
○ NumPy, pandas, PyTables,
PyROOT, Machine Learning
frameworks (classical, quantum)
● System administration
● Web frameworks
○ CGI, CherryPy, Flask, Django,
TurboGears and a hell lot more
● Interface to databases, XML and
json parsers
● GUI development
○ PyGtk, PyQT/PySide, wxPython
● Game development

[Link] 14
Python as an interpreted language
● A Python interpreter source code
○ parses a Python
program/source file (.py)
■ compiles into parse external
byte-code (.pyc) libraries

○ executes byte-code
edit
■ interprets on a virtual byte
machine (VM) compile
○ no need for compilation test
and linking phases
■ no binary executable
run

[Link] 15
Portability
● Python source code is inherently portable
across platforms/architectures
○ the underlying virtual machine (VM) instance
takes care of architecture dependence
○ in most cases you pay a performance penalty
■ vis-a-vis Fortran/C/C++
■ benefits outweigh lack of performance
■ there are alternative implementation of Python,
e.g Cython, numba, PyPy to address
performance issues

16
Interactive Python

17
Interactive Python
$ python
>>> credits
>>> copyright
>>> help(1) # help on integer Get information,
>>> help() extensive help
help> keywords
help> quit
>>>

There are more than one ways to exit an interactive session


● >>> CTRL-D
● >>> quit()
● >>> raise SystemExit
● >>> import sys
● >>> [Link]() 18
Python as a calculator
$ python3
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + 2
4
>>> a = 2
>>> b = 2
>>> c = a * b
>>> print(c)
4
>>> bin(16)
'0b10000'
>>> pow(2,3) Python does not load the full
8 math library in memory
>>> log(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'log' is not defined

● Two major releases in use: python2 and python3


● python3 is the default on modern OSes, breaks backward compatibility
● python3 is used in this course 19
Python as a calculator
>>> import math # load a module
>>> dir(math) # see what it offers
[... 'log', 'log10', 'log1p', 'modf', 'pi',
'pow', 'radians', 'sin', 'sinh', 'sqrt',
'tan', 'tanh', 'trunc']
>>> [Link]
3.141592653589793
>>> from math import sqrt,pow
>>> pow(2,3)
8.0
>>> sqrt(10)
3.1622776601683795
>>> _ # result of the last expression
3.1622776601683795
20
Code Execution: script, execfile, eval, exec

21
Python script (on Linux)
#!/usr/bin/env python3

# [Link] Find the default


for i in range(10): Python interpreter
print(i, end=" ")
[1]$ python [Link]
0 1 2 3 4 5 6 7 8 9

[2]$ chmod a+x [Link]


Turn on execution bit
[3]$ ./[Link]
0 1 2 3 4 5 6 7 8 9 22
execfile, eval
$ python3
>>> execfile('[Link]')
0 1 2 3 4 5 6 7 8 9
>>> result = eval('1.0 + 1.0')
>>> print(result, type(result))
2.0 <class 'float'>
>>> print(eval("len('hello world!')"))
12
>>> print(eval("__import__('os').getcwd()"))
/home/sarkar/python/examples

23
exec
● one can construct Python code as string and execute
on-the-fly from within a running Python program

>>> code = 'print("hello world!")'


>>> exec(code)
hello world!

24
Data type, variable scope,
namespace

25
Built-in Data Types
Data type Example
Numbers 3.1415, 1234, 999L, 3+4j

String “Hello”, “guido v”

List [1, [2,'three'], 4]


{'compiled': 'Fortran,C,C++',
Dictionary
'interpreted': 'Perl, Python, Ruby'}
Tuple 1,'world!',3,4 or (1,'world!', 2, 3)

File f = open('myfi[Link]', 'r').readlines()

26
Variable Scope & Lifetime
● Scope
○ part of a program where a variable is accessible
● Lifetime
○ duration for which a variable is accessible
● In general, variables defined
○ in the main body of a program have global scope
■ visible throughout the file, and also inside any file
which imports that file
■ global scope is usually a bad idea
○ inside a function have local scope
○ in a class have local scope, but can be accessed
from outside through the object (no data hiding) 27
Variable Scope & Lifetime
# global # local
a = 0 def test_scope(c):
d = 3
if a == 0: print(c, d)
b = 1
# call the function
# what do we expect? d = 5
print(b) test_scope(7)

# what do we expect?
print(c, d)

28
Python - dynamically typed, loosely bound
● Name (refer) a variable, no need to declare the type
○ variable type is context sensitive
● Python keeps track of type of an object referenced by a variable
name during it's lifetime i.e Python is NOT weakly typed

>>> a = False >>> a = [1,2,3,4]


>>> print(type(a)) >>> print(type(a))
<class 'bool'> <class 'list'>

>>> a = 5 >>> a = 1,2,3,4


>>> print(type(a)) >>> print(type(a))
<class 'int'> <class 'tuple'>

>>> a = "Hello World!" >>> a = None


>>> print(type(a)) >>> print(type(a))
<class 'str'> <class 'NoneType'> 29

You might also like