Unit 1 PPT PDF Applied Programing S01 12
Unit 1 PPT PDF Applied Programing S01 12
Applied Programming
Course Instructor:
Dr.V.Sumitra
Dr.S.Sunithamani
18ECS301J-Applied Programming 1
18ECS301J
Applied Programming
Unit 1
Session 1
18ECS301J-Applied Programming 2
18ECS301J Applied Programming
18ECS301J-Applied Programming 3
18ECS301J Applied Programming
Session plan-Unit 1(12 hrs)
18ECS301J-Applied Programming 4
18ECS301J Applied Programming
18ECS301J-Applied Programming 5
18ECS301J Applied Programming
18ECS301J-Applied Programming 6
Unit I Learning Objectives
Introduction, Types and Operation
• Introduction to Python,
• Lists, Dictionaries,
18ECS301J-Applied Programming 7
Required software
• Python Version 2.7 and above: http://python.org
• Install Anaconda Navigator and Launch Jupyter notebook
• Numerical Python (NumPy): http://numpy.org
• Matplotlib for plotting: http://matplotlib.org
• Ipython for interactive computing: http://ipython.org
• Scitools for addons to NumPy: http://code.google.com/scitools
18ECS301J-Applied Programming 8
Installation
• Installing Anaconda on windows
• https://problemsolvingwithpython.com/01-Orientation/01.03-Installing-Anaconda-on-Windows/
18ECS301J-Applied Programming 9
Challenges faced with raw data
• Data quality does not
conform to set standards
• Data integration is a
complex task
• Data is distributed into large
clusters which are difficult
to integrate and analyze.
• Unstructructured and
semistructured data are
hard to analyze.
18ECS301J-Applied Programming 10
Purpose of Python
• Python deals with each stage of data analytics efficiently
by applying different libraries and packages.
18ECS301J-Applied Programming 11
Python creator Guido van Rossum named it after the BBC
comedy series Monty Python’s Flying Circus. He is a big fan of
Monty Python.
18ECS301J-Applied Programming 12
Why Program?
What is a Program?
“Everybody in this country should learn how to program a computer...
because it teaches you how to think”.- Steve Jobs, 1955-2011.
What is a Program?
• A sequence of instructions to the computer, written in a programming
language, somewhat like English, but very much simpler - and very
much stricter.
• This course teaches Python Program
18ECS301J-Applied Programming 13
Introduction to Python
18ECS301J-Applied Programming 14
Introduction to Python
Why do people use Python?
The primary factors cited by Python users seem to be these:
• Software quality
• Python code is designed to be readable, and hence reusable and maintainable—much more so than traditional
scripting languages.
• Developer productivity
• Python boosts developer productivity many times beyond compiled or statically typed languages such as C, C++, and
Java.
• Python code is typically one-third to one-fifth the size of equivalent C++ or Java code.
• That means there is less to type, 3 less to debug, and less to maintain after the fact.
• Program portability
• Most Python programs run unchanged on all major computer platforms.
• Porting Python code between Linux and Windows, for example, is usually just a matter of copying a script’s code
between machines
• Support libraries
• Python comes with a large collection of prebuilt and portable functionality, known as the standard library.
• This library supports an array of application-level programming tasks, from text pattern matching to network scripting.
• Component integration
• Python scripts can easily communicate with other parts of an application, using a variety of integration mechanisms.
• Such integrations allow Python to be used as a product customization and extension tool.
• Enjoyment
• Because of Python’s ease of use and built-in toolset, it can make the act of programming more pleasure than chore.
18ECS301J-Applied Programming 15
Introduction to Python
18ECS301J-Applied Programming 16
Introduction to Python
18ECS301J-Applied Programming 17
Introduction to Python
Technical strengths of Python?
1. Python is object-oriented Structure supports such concepts as polymorphism, operation overloading and
multiple inheritance.
2. Indentation Indentation is one of the greatest feature in python
3. It’s free (open source) Downloading python and installing python is free and easy
4. It’s Powerful
• Dynamic typing Built-in types and tools
• Library utilities Third party utilities (e.g. Numeric, NumPy, sciPy)
• Automatic memory management
5. It’s Portable
• Python runs virtually every major platform used today
• As long as you have a compaitable python interpreter installed, python programs will run in exactly the same manner,
irrespective of platform.
6. It’s easy to use and learn
• No intermediate compile
• Python Programs are compiled automatically to an intermediate form called byte code, which the interpreter then reads.
• This gives python the development speed of an interpreter without the performance loss inherent in purely interpreted
languages.
• Structure and syntax are pretty intuitive and easy to grasp.
7. Interpreted Language Python is processed at runtime by python Interpreter
8. Interactive Programming Language Users can interact with the python interpreter directly for writing the
programs
9. Straight forward syntax The formation of python syntax is simple and straight forward which also makes it
popular
18ECS301J-Applied Programming 18
Frequently used terms:
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
18ECS301J-Applied Programming 19
Operating System
• An operating system (OS) is a set of programs managing hardware
and software resources on a computer
18ECS301J-Applied Programming 20
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
18ECS301J Applied Programming
Summary:
• Introduction to python.
• What are the six main reasons that people choose to use Python.
• List few notable companies or organizations using Python today.
• Why might you not want to use Python in an application?
• What can you do with Python?
• Installation of python software.
18ECS301J-Applied Programming 21
Thank you
18ECS301J-Applied Programming 22
18ECS301J-Applied Programming 23
18ECS301J
Applied Programming
Unit 1
Session 2
18ECS301J-Applied Programming 24
Data Types
18ECS301J-Applied Programming 25
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
• A program consists of statements
18ECS301J-Applied Programming 26
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Example program
18ECS301J-Applied Programming 27
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Illegal Variable Names
• Program files can have a freely chosen name, but stay away
from names that coincide with keywords or module names in
Python.
• For instance, do not use
math.py time.py random.py
os.py sys.py, while.py
for.py if.py class.py
def.py
18ECS301J-Applied Programming 28
printf syntax
18ECS301J-Applied Programming 29
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
printf syntax
18ECS301J-Applied Programming 30
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Examples on different printf formats
18ECS301J-Applied Programming 31
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
• A program takes some known input data and computes
some output data
18ECS301J-Applied Programming 32
Answer?????
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
The error is caused by (unintended) integer division
18ECS301J-Applied Programming 33
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Everything in Python is an Object
18ECS301J-Applied Programming 34
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Arithmetic expressions
18ECS301J-Applied Programming 35
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Using standard Mathematical modules
18ECS301J-Applied Programming 36
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Using standard Mathematical modules
18ECS301J-Applied Programming 37
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Rounding Errors
18ECS301J-Applied Programming 38
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Type Conversion
18ECS301J-Applied Programming 39
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
support for complex numbers
18ECS301J-Applied Programming 40
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Symbolic Computing
18ECS301J-Applied Programming 41
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Interactive Computing
• The environments where you work interactively with programming
are commonly known as Python shells.
• The simplest Python shell is invoked by just typing python at the
command line in a terminal window.
18ECS301J-Applied Programming 42
Let’s find out
why the following programs do not work
C=A+B
A=3
B=2
print C
x=1
Print ‘sin(%g)=%g’ % (x, sin(x))
18ECS301J-Applied Programming 43
Corrected code
A=3
B=2
C=A+B
print(C)
Corrected code
18ECS301J-Applied Programming 45
Corrected code
• ***************************** ******************************
• from math import tan, pi address = '[email protected]'
and_var = 'duck'
• print(tan(pi))
class_var = "INF1100, gr 2"
• pi = "3.14159" continue_var = x > 0
• print(tan(float(pi))) rev = fox = True
• **************************** Norwegian = ['a human language']
• c = 4 ** 3 ** 2 ** 3 true_var = fox is rev in Norwegian
• _ = ((c - 78564) / c + 32) *******************************
• ****************************
• discount = 0.12 # 12% as a decimal
• AMOUNT = 120.0 # Note: Use a decimal point for floating-
point numbers
• amount = 120.0 # Similar to AMOUNT
• ***************************
18ECS301J-Applied Programming 46
Summary
18ECS301J-Applied Programming 47
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
THANK YOU
18ECS301J-Applied Programming 48
18ECS301J
APPLIED PROGAMMING
Unit 1
Session 5 : User Input and error Handling
18ECS301J-Applied Programming 49
18ECS301J-Applied Programming 50
Session 5
Control Flow Statements Sequences
18ECS301J-Applied Programming 51
Hard code Data
18ECS301J-Applied Programming 52
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
How to specify input data
18ECS301J-Applied Programming 53
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Getting input
18ECS301J-Applied Programming 54
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Getting input
18ECS301J-Applied Programming 55
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Python Program
• In concrete perspective, Python programs can be decomposed into
modules, statements, expressions, and objects, as follows:
1. Programs are composed of modules.
2. Modules contain statements.
3. Statements contain expressions.
4. Expressions create and process objects.
18ECS301J-Applied Programming 56
Python Collections (Arrays)
• There are four collection data types in the Python programming
language:
18ECS301J-Applied Programming 58
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Reading from Command line
18ECS301J-Applied Programming 59
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Reading data from file
18ECS301J-Applied Programming 60
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Reading data from file
18ECS301J-Applied Programming 61
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Alternative ways to read a file
18ECS301J-Applied Programming 62
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
File reading Demo
18ECS301J-Applied Programming 63
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
18ECS301J-Applied Programming 64
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Reading a mixture of text and numbers
18ECS301J-Applied Programming 65
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Reading a mixture of text and numbers
18ECS301J-Applied Programming 66
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Complete Program
18ECS301J-Applied Programming 67
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Writing Data to a file
18ECS301J-Applied Programming 68
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Writing a table
18ECS301J-Applied Programming 69
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
What is a file?
18ECS301J-Applied Programming 70
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Wrong Input from User
18ECS301J-Applied Programming 71
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Handle errors at the input
18ECS301J-Applied Programming 72
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
18ECS301J-Applied Programming 73
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Summary of Session 5
18ECS301J-Applied Programming 74
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Summary of session 5
Reading and Writing
18ECS301J-Applied Programming 75
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Practice program
• r: Opens a file for reading only r+: Opens a file for both reading and
writing w: Opens a file for writing only w+: Open a file for writing
and reading. a: Opens a file for appending a+: Opens a file for both
appending and reading.
18ECS301J-Applied Programming 76
18ECE201J
Python and Scientific Python
Unit 1
Session 6 : Making Modules
18ECS301J-Applied Programming 77
Python Program
• In concrete perspective, Python programs can be decomposed into
modules, statements, expressions, and objects, as follows:
1. Programs are composed of modules.
2. Modules contain statements.
3. Statements contain expressions.
4. Expressions create and process objects.
18ECS301J-Applied Programming 78
Making Own Modules
18ECS301J-Applied Programming 79
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Making Own Modules
18ECS301J-Applied Programming 80
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
18ECS301J-Applied Programming 81
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Adding test block in a module
18ECS301J-Applied Programming 82
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Test block in functions
18ECS301J-Applied Programming 83
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
How Python can find new module
18ECS301J-Applied Programming 84
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Example
18ECS301J-Applied Programming 85
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
18ECE201J
Python and Scientific Python
Unit 1
Session 9 : While and for loops,
Lists manipulation, Loops with list indices
18ECS301J-Applied Programming 86
Loops
• Repetitive tasks in a program can be automated by
loops
18ECS301J-Applied Programming 87
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
While Loop
18ECS301J-Applied Programming 88
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
While Loop – Program Flow
18ECS301J-Applied Programming 89
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Boolean expression – T or F
18ECS301J-Applied Programming 90
Lists
• Lists are objects for storing a sequence of things (objects)
18ECS301J-Applied Programming 91
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
List Operations
indexing, append, extend, insert, delete
18ECS301J-Applied Programming 92
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
List Opeations: search
18ECS301J-Applied Programming 93
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
for Loop
18ECS301J-Applied Programming 94
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Making a table with for loop
18ECS301J-Applied Programming 95
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Translate for loop to while loop
18ECS301J-Applied Programming 96
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Mathematical sum using loops
18ECS301J-Applied Programming 97
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Storing Table columns as Lists
18ECS301J-Applied Programming 98
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
for loop with list indices
18ECS301J-Applied Programming 99
Courtesy: https://hplgit.github.io/scipro-primer/slides/index.html
Change elements in a list