L9 Python Introduction
L9 Python Introduction
Introduction to
❑ History of python (https://www.python.org)
▪ Development of python started in the late 1980s by Guido van Rossum
at Centrum Wiskunde & Informatica(CWI) in the Netherlands
▪ Its implementation began in December1989
▪ Python 1.0 was released in 1994
▪ Python 2.0 was released on 16 October 2000 with many major new
features
▪ Python 3.0 was released on 3 December 2008
❑ Python is
▪ powerful and fast
▪ plays well with others
▪ runs everywhere
▪ friendly & easy to learn
▪ Open source
1 Department of Mechanical Engineering, BUET
What is python?
❑ Python is an easy to learn, extremely usable, high-level, general
purpose, powerful interpreting programming language
❑ It has efficient high-level data structures and a simple but effective
approach to object- oriented programming
❑ Python’s elegant syntax and dynamic typing, together with its
interpreted nature, make it an ideal language for scripting and rapid
application development in many areas on most platforms
❑Easy to learn: good readability and simple to learn syntax
❑Support libraries: a large collection of prebuilt and portable
functionality, known as the standard library
❑ Third-party Modules: Python’s third-party domain offers tools for
scientific computations, website construction, game development, and
much more
2
2 Department of Mechanical Engineering, BUET
Why python?
▪ Software quality : Python’s focus on readability, coherence, and
software quality in general sets it apart from other tools in the
scripting world. The uniformity of Python code makes it easy to
understand, even if you did not write it.
3
3 Department of Mechanical Engineering, BUET
Why python?
❑ Component integration:
• Python code can invoke C and C++ libraries, can be called from C and
C++ programs, can integrate with Java and .NET components
• can communicate over frameworks such as COM, can interface with
devices over serial ports
• The main disadvantage, however, is that numerically-intensive
programs written in Python typically run slower than those in
compiled languages
❑ Free to use:
• Python and its different extensions and modules are free and easily
found from the internet.
• Python development is performed by a community that largely
coordinates its efforts over the Internet. It consists of Python’s
creator—Guido van Rossum, the officially anointed Benevolent
Dictator for Life (BDFL) of Python—plus a supporting cast of
thousands.
• Language changes must follow a formal enhancement procedure and
be scrutinized by both other developers and the BDFL. Happily, this
tends to make Python more conservative with changes than some
other languages.
4
4 Department of Mechanical Engineering, BUET
Availability of python
❑ Python is available on:
• Linux and Unix systems
• Microsoft Windows and DOS (all modern flavors)
• Mac OS (both OS X and Classic)
• BeOS, OS/2, VMS, and QNX
• Real-time systems such as VxWorks
• Cray supercomputers and IBM mainframes
• PDAs running Palm OS, PocketPC, and Linux
• Cell phones running Symbian OS and Windows Mobile
• Gaming consoles and iPods
• And more
5
5 Department of Mechanical Engineering, BUET
Python Modules: Numpy and Scipy
❑ NumPy and SciPy are open-source add-on modules to Python that
provide common mathematical and numerical routines in pre-
compiled, fast functions
❑ These have grown into highly mature packages that provide
functionality that meets, or perhaps exceeds, that associated with
common commercial software like Matlab
❑ The NumPy (Numeric Python) package provides basic routines for
manipulating large arrays and matrices of numeric data
❑ The SciPy (Scientific Python) package extends the functionality of
NumPy with a substantial collection of useful algorithms, like
minimization, Fourier transformation, regression, and other applied
mathematical techniques
6
6 Department of Mechanical Engineering, BUET
Application/use of python
❑ As python is general purpose language, it can be used for various
applications,
• Systems Programing: Python programs can search files and directory
trees, launch other programs, do parallel processing with processes and
threads, and so on.
• GUIs
• Internet Scripting: Python comes with standard Internet modules that
allow Python programs to perform a wide variety of networking tasks, in
client and server modes.
• Database Programing: For traditional database demands, there are
Python interfaces to all commonly used relational database systems—
Sybase, Oracle, Informix, ODBC, MySQL, PostgreSQL, SQLite, and more.
• Rapid Prototyping Tool: It is possible to prototype systems in Python
initially, and then move selected components to a compiled language
such as C or C++ for delivery.
• Numeric and Scientific Computing: NumPy, SciPy.
• Machine learning: analyses data and learns to predict the outcome
• Gaming, Images, Serial Ports, XML, Robots, and More
7
7 Department of Mechanical Engineering, BUET
python interpreter
❑ An interpreter is like a compiler in that it translates higher level
source code into machine language
❑ Interpreted programs can run on any platform with an
appropriate interpreter; they do not need to be recompiled to run
on a different platform
❑ While a compiler produces an executable program that may run
many times with no additional translation needed, an interpreter
translates source code statements into machine language line by
line as the program runs
❑ Python, is used mainly as an interpreted language, but compilers
for it are available
❑ Interpreted languages are better suited for dynamic, explorative
development which many people feel is ideal for beginning
programmers
8
8 Department of Mechanical Engineering, BUET
python installation
▪ To install python: https://www.python.org/downloads/
9
9 Department of Mechanical Engineering, BUET
Python: Interactive Prompt
❑ The interactive prompt runs code and echoes results as you go, but it
doesn’t save your code in a file
❑ It may not seem to you that you are writing programs, but you can
write python programs here. Each line will be executed after it is
written, no compilation is needed
10
10 Department of Mechanical Engineering, BUET
python IDLE
❑ An integrated development environment for Python, which has been
bundled with the default implementation of the language
❑ Search for IDLE in your start menu and open it You will find something
that is similar to the previous command prompt with white background
11
11 Department of Mechanical Engineering, BUET
python IDLE
12
12 Department of Mechanical Engineering, BUET
python IDEs
▪ IDE stands for Integrated Development Environment Example
of an IDE is codeblocks which is an IDE for C programming
▪ There are many IDE’s for python programming like PyCharm,
Spyder, PyScripter Eclipse, IDLE, jupyter and many more
▪ An IDE is a program dedicated to software development. As
the name implies, IDEs integrate several tools specifically
designed for software development These tools usually
include
• An editor designed to handle code ( for example, syntax
highlighting and auto completion)
• Build, execution, and debugging tools
13
13 Department of Mechanical Engineering, BUET
C vs python code
#include <stdio.h> // To print "Hello world" 5 times
int main(){
int i; for i in range(1,6,1):
// To print "Hello world" 5 times print ('Hello world',i)
for(i=1;i<=5;i++) { print('\n')
printf("Hello world %d",i);
printf("\n"); ▪ No header file is required to be
} defined
return 0; ▪ main() function doesn’t require to be
} defined explicitly
▪ Variables doesn’t require any
declaration
▪ No need add semicolon(;) at the end
of each statement
14
14 Department of Mechanical Engineering, BUET
References
▪ Beginning Python, Peter Norton, et al.
▪ Learning Python, Mark Lutz
▪ Programming Python, Mark Lutz
▪ Learning to Program with Python, Richard L Halterman
▪ https://docs.python.org/3/tutorial/
▪ An introduction to Python for scientific computing, M. Scott Shell
▪ An introduction to Numpy and Scipy, M. Scott Shell
………………and more……..
15
15 Department of Mechanical Engineering, BUET