Introduction To Python
Introduction To Python
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
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
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
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
print("Welcome to Python")
28-07-2018 15
Shebang
28-07-2018 16
Indentation in Python
• 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
• 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
• 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
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
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.
28-07-2018 24
Type of 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.
• If there are more lines in the documentation string, the second line
should be blank, visually separating the summary from the description.
28-07-2018 29
Docstrings example
""" Here is the docstring for documentation.
"""
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:
28-07-2018 32
Variable in Python
• Variable is a reserved memory location to store any value.
• 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
b = a-b
a = a-b
28-07-2018 36
Line continuation
• Sometimes while writing a code the statement becomes very long.
28-07-2018 37
Line Continuation
28-07-2018 38
Implicit Continuation
name = "Rahul"
age = 26
+ 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
Your age is 26
28-07-2018 40
Constants in Python
• Constants are basically variables whose value can't change.
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.
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