You are on page 1of 46

Embedded Programming

Design By:
Mr. Jatinder Verma
Chandigarh University- Gharuan

28-07-2018 1
Syllabus Content
• Introduction to Python and Basics
Unit-1: • Fundamentals of Python
• Flow Control Constructs

• Modules and Functions


Unit-2: • Object Oriented Programming
• Exception Handling
• File Handling
Unit-3: • Graphical User Interface
• Database Connectivity
28-07-2018 2
Learning Outcome
• Student will be able to understand what are the features of Python.
• Student understand how to create variables and statements in Python.
• Student will be able to use different type of comments in Python.
• Students are able to write and run a simple Python program by knowing
about syntax errors, runtime errors, and logical errors.
• Students are able to implement programs that perform simple
computations.
• Students are able understand format numbers and strings using the format
function.
28-07-2018 3
Introduction to Python
• Python was introduced in Dec 1989 by Guido Van Rossam in Netherland.
• It’s name was taken from Monty Python's Flying Circus(BBC show), since
Guido Van was a big fan of this Television show.
• Python is a general purpose programming language.
• Python 3(3.7.0) is a newer version, but it is not backward compatible with
Python 2.
• Python has a simple syntax similar to the English language.

28-07-2018 4
Python Composition

28-07-2018 5
Python Applications
• Desktop Applications (Tk,wxPython, PyQT, PyGtk)
• Web Applications (Django, Pyramid, Flask, Bottle, etc.)
• Database Applications (SQLAlchemy)
• Networking Applications (Twisted)
• Games (Pygame, Pyglet)
• Data Analysis [Data Science Applications](Numpy, matplotlib)
• Machine Learning Applications (SciPy)
• AI Applications (nltk)
• IOT Applications (Watson)
• Mobile Applications (Kivy) - Testing (nose)
28-07-2018 6
Companies Using Python
• Google
• YouTube
• Dropbox (Guido Van Rossum)
• NASA
• Instagram
• Pinrest
• BitTorrent
• Yahoo Map

28-07-2018 7
Features of Python

• Simple and Easy to learn • Interpreted


• Open Source and Free • Extensible
• Platform Independent • Embedded
• High Level Programming Language • Extensive/Rich Libraries
• Portable (run on any OS) (Big Batteries)
• Dynamically Types
• Procedure Oriented, Object Oriented and
General Purpose

28-07-2018 8
Limitations of Python

• Performance
• Run-time Errors
• Incompatibility of Two Versions
• Depends on Third-Party Frameworks and Libraries

28-07-2018 9
Flavors of Python
• CPython (C)
• Jython –[earlier version JPython](Java)
• IronPython (C#)
• Pypy (more performance - JIT compiler enabled)
• RubyPython (Ruby)
• AnacondaPython (Big Data- Hadoop)
• Stackless (Python for Concurrency)

28-07-2018 10
Rules of Identifiers

• Identifiers in Python are Case Sensitive.

• Only Alphabet symbols (all uppercase and lowercase), digits (0 to 9) and


underscore ( _ ) are allowed; no special character allowed.

• Should not start with digit.

• Keywords are not allowed as identifiers.

• No limit of length but not recommended too lengthy identifier.


28-07-2018 11
33: Keywords
• class
• True • else • yeild • def
• Flase • elif • try • pass
• None • while • except • global
• and • for • finally • nonlocal
• or • break • assert • lambda
• not • continue • import • del
• is • return • from • with
• if • in • as • raise
28-07-2018 12
Reading Keywords

• It is also possible to read keywords from the installed python.


• For that you need to use keyword module as given below:

import keyword from keyword import kwlist as d


keyword.kwlist print(d)

import keyword from keyword import kwlist

len(keyword.kwlist) print(kwlist)

28-07-2018 13
Little more on Keywords
keywords_33=[ ('function_7',
('file_2', ['with', 'as']), ['lambda','def','pass',
('module_2', ['from', 'import']), 'global','nonlocal',
('constant_3', {'bool': ['False', 'True'], 'return','yield']),
'none': ['None']}), ('loop_4', ['while', 'for', 'continue', 'break']),
('operator_5', ('condition_3', ['if', 'elif', 'else']),
{'logic': ['and', 'not', 'or'], ('exception_4', ['try', 'raise','except', 'finally']),
'relation': ['is', 'in']}), ('debug_2', ['del','assert']),
('class_1', ['class']), ]

28-07-2018 14
Statement in Python

• A statement represents an action or a sequence of actions.

• An example of statement is:

print("Welcome to Python")

• It will display the "Welcome to Python"

28-07-2018 15
Shebang

In UNIX/LINUX systems you need to mention beginning of the script in a


first line as given below:
Shebang Line
The ! is called the "bang"
#!/usr/bin/python3 The # is not called the "she", it is
just a comment marker.
print("Welcome to Python") So sometimes, shebang line is also
known as Hashbang line

28-07-2018 16
Indentation in Python

• The indentation matters in Python to maintain the structure of program.

• To indicate a block of code in Python, you must indent each line of the
block by the same amount.

• For example:

if 1 == 1:

print("Logging on ...")

28-07-2018 17
Indentation in Python

• To resolve that you need to place proper indentation.

• For example:
if 1 == 1:
print("Logging on ...")
OR
OUTPUT
if 1 == 1:
Logging on ...
print("Logging on ...")

28-07-2018 18
Installing Python
• You can download python from www.python.org

• Latest version of Python till now is 3.7.0

• If you need python documentation, you can go to: docs.python.org

• If you need python job related information, you can go to:


jobs.python.org

• Once you installed python, you will get IDLE(an IDE) by default with it.
28-07-2018 19
Python Programming
Environment
• There are two modes in Python Programming Environment:
1. Interactive Mode:
This mode is used to write and immediately execute the Python code.

2. Script Mode:
This mode is used to write Python code and save Python programs which
later on can be executed.
It is preferred to make executables.

28-07-2018 20
Interactive Mode

28-07-2018 21
Script Mode

1. Use any editor and save it.

2. If you are using normal editor you can save it and run it using CMD.

3. If you are using any IDE then there is inbuilt run option.

28-07-2018 22
Python using CMD

Type in Notepad Welcome.py

Running from the DOS prompt.

28-07-2018 23
Comments
• Comments describe the portion of a program that is hard to understand.
• If you comment a statement in program, it is called comment out or
commenting out statement.
• Comments are used for developer only which are ignored by compiler or
interpreter and not executed.
• # (hash or pound) symbol is used for making comment in python.

• Example: # This is a comment here

28-07-2018 24
Type of Comments

Inline Comments Block Comments

28-07-2018 25
Inline Comments
• Inline comments occur on the same line of a statement, following the code.
• Inline comment begins with a hash mark and only after any statement.

• Syntax:
[code] # Inline comment about the code

• Example:
print("Hi") # Hi is a String argument

28-07-2018 26
Block Comments
• Block comments can be used to explain more complicated code or code
that you don’t expect the reader to be familiar with.
• In block comments, each line must begins with the hash and after that any
statement can be there.

• Example:
# The is first line and print("Hello")
# this is second line of block comment.

28-07-2018 27
Docstrings
• A.K.A Documentation Strings
• docstrings is represented by triple quotes( """ )
• It is mostly misunderstood with multiline comments.
• It is a way of associating documentation with Python modules, functions,
classes, and methods.
• The main difference between comment and docstrings is that you can access
comments within source code only whereas you can access docstrings outside
the source code also.
28-07-2018 28
Docstrings Structure
• The doc string line begins with a capital letter and end with a period.

• The first line should be a short description.

• If there are more lines in the documentation string, the second line
should be blank, visually separating the summary from the description.

• The third line should be the description.

28-07-2018 29
Docstrings example
""" Here is the docstring for documentation.

This is description of docstring which is doing nothing

"""
OUTPUT
print("Hello")
Hello

28-07-2018 30
Accessing Docstrings
import Test OUTPUT
Help on module Test:
help(Test) NAME
Test - Here is the docstring for documentation.

DESCRIPTION
Module Name
This is description of docstring which is doing nothing

FILE
h:\1 python- embedded programming- cat-811\programs\test.py
28-07-2018 31
Intermediate Code
• This is how you can generate intermediate code:

Python import py_compile Python


Source Code Intermediate Code
( .py ) py_compile.compile('Test.py') ( .pyc )

28-07-2018 32
Variable in Python
• Variable is a reserved memory location to store any value.

• Unlike other programming languages, Python has no command for


declaring a variable.

• A variable is created the when you first assign a value to it.

• E.g.:
OUTPUT
x=5 5
print(x)
28-07-2018 33
Variable in Python
• You can assign any kind of data to a variable, even if that variable has
previously assigned a value of different data type.
• E.g.:

x=5
OUTPUT
print(x)
5
x="Hello"
Hello
print(x)
28-07-2018 34
Variable Assignment
• You can assign one or more variable at once in Python, which is called
multiple assignment.
• E.g.:

a,b = 4,6
OUTPUT
print(a)
4
print(b) 6

28-07-2018 35
Swap 2 numbers without 3rd

General Logic: My Python Logic:

a = a+b a,b = b,a

b = a-b

a = a-b

28-07-2018 36
Line continuation
• Sometimes while writing a code the statement becomes very long.

• To make it readable and understandable, we divide it into multiple lines


which is called line continuation.

28-07-2018 37
Line Continuation

Implicit Continuation Explicit Continuation

28-07-2018 38
Implicit Continuation
name = "Rahul"

age = 26

print("Your name is "

+ name OUTPUT
+"\nYour age is: " Your name is Rahul

Your age is 26
+ str(age))
28-07-2018 39
Explicit Continuation
name = "Rahul"

age = 26

print("Your name is “ + name \

+"\nYour age is: " \ OUTPUT


+ str(age)) Your name is Rahul

Your age is 26

28-07-2018 40
Constants in Python
• Constants are basically variables whose value can't change.

• It is basically used to restrict some variable values.

• Unfortunately, there are no user-defined constants in Python.

• If you want to define a constants, it is recommended to user variable


name in all uppercase characters. OUTPUT
Hello
• But it does not mean you can’t change value.

28-07-2018 41
Constants in Python
• E.g.:

PI = 3.14

print(PI)

PI = 3.15 OUTPUT
3.14
print(PI)
3.15

28-07-2018 42
Constants in Python
• E.g.:

import math

print(math.pi)
OUTPUT
math.pi = 3.15
3.141592653589793
print(math.pi)
3.15

28-07-2018 43
Constants in Python
• Constants are basically variables whose value can't change.

• Since Python is a dynamic language, almost everything can be replaced


at runtime.

• You can add/remove/modify attributes and functions of objects at


runtime/anytime.

• Unfortunately, there are no user-defined constants in Python.

28-07-2018 44
Frequently Asked Questions

• Which one is the most preferred language Python 2 or Python 3 and why?
• Python could be run in various modes?
• Is there multiline comments in Python? Justify.
• Suppose, for example, that you need to take out a student loan. Given the
loan amount, loan term, and annual interest rate, can you write a program
to compute the monthly payment and total payment?
• If you assigned a negative value for radius in ComputeArea.py of the
previous slide, the program would print an invalid result. If the radius is
negative, you don't want the program to compute the area. How can you
deal with this situation?

28-07-2018 45
Bibliography

 http://www.pitt.edu/~naraehan/python2/tutorial.html
 javatpoint.com
 Introduction to Programming Using Python by Y. Daniel Liang
 tutorialspoint.com
 Programming and Problem Solving with Python by A. Kamthane

28-07-2018 46

You might also like