3. 3
What is Python?
Created by Guido Van Rossum as a successor
to the ABC programming language
Conceived in 1980, started development in
1989, v1.0 out in 1994
Name is derived from Monty Python’s Flying
Circus
4. 4
Python in the real world
Uses millions of lines of Python code for ad
management to build automation
Employs the creator of Python himself
5. 5
Python in the real world
Industrial Light & Magic
Uses Python to automate visual effects
6. 6
Python in the real world
Uses Python for their desktop application
Python automates the synchronization and file
sending over the internet to Dropbox’s cloud
storage servers
7. 7
Python in the real world
Django web framework is built using Python
Notable users of Django are some of the
biggest websites today
8. 8
Python in the real world
Blender, a 3D Imaging program uses Python
as its main scripting language
9. 9
Python in the real world
Minecraft uses Python as its scripting language
to automate building
10. 10
Python in the real world
Google
Industrial Light and Magic (Star Wars)
Django web framework (used in Pinterest, Instagram, Mozilla)
Dropbox
Reddit
LibreOffice
Games (Battlefield, EVE Online, Civilization IV, Minecraft, etc.)
...and many more!
11. 11
Why Python?
Simple, like speaking English
It’s FOSS! Lots of resources available
High level programming language
Object-oriented, but not enforced
Portable, works on a lot of different systems
Can be embedded in programs for scripting capabilities
Extensive libraries available
Extensible
12. 12
Getting Started
Go to https://www.python.org and download
the latest version of Python for your OS
15. 15
Basic Characteristics
Organized syntax
Indentation is strictly enforced
Encourages proper programming practices
Dynamic variable typing
Variables are simply names that refer to objects
No defined data type during compile time
16. 16
Simple and Readable
Python programs look like pseudo-code
compared to other prominent languages
Sample code – helloworld.py
17. 17
Indentation
Proper Indentation is enforced to identify
sections within your program (functions,
procedures, classes)
Sample code – guessgame.py
19. 19
Built-in Data Types
Boolean
Numbers (integers, real, complex)
Strings
Sequence Types
Tuples
Lists (resize-able arrays)
Range
Set Types
Set, FrozenSet
Dictionary
20. 20
Data Types – Tuples
class tuple([iterable])
Immutable sequence of data – once assigned,
elements within cannot be changed
Using a pair of parentheses to denote the empty tuple:
()
Using a trailing comma for a singleton tuple: a, or (a,)
Separating items with commas: a, b, c or (a, b, c)
Using the tuple() built-in: tuple() or tuple(iterable)
22. 22
Data Types – Tuples
Python Expression Result Description
Len((1,2,3)) 3 Length
(1,2,3) + (4,5,6) (1,2,3,4,5,6) Concatenation
('Hi!',) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!') Repetition
3 in (1, 2, 3) True Membership
for x in (1, 2, 3):
print(x)
1 2 3 Iteration
Basic tuple operations
23. 23
Data Types – List
class list([iterable])
Mutable sequence of data – once assigned,
elements within cannot be changed
Using a pair of square brackets to denote the empty
list: []
Using square brackets, separating items with commas:
[a], [a, b, c]
Using a list comprehension: [x for x in iterable]
Using the type constructor: list() or list(iterable)
25. 25
Data Types – List
Basic list operations
Python Expression Result Description
Len([1,2,3]) 3 Length
[1,2,3] + [4,5,6] [1,2,3,4,5,6] Concatenation
['Hi!',] * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] Repetition
3 in [1, 2, 3] True Membership
for x in [1, 2, 3]:
print(x)
1 2 3 Iteration
26. 26
Built-in Functions
Some built-in functions that work with tuples
and lists
min() - Return the smallest item
max() - Return the largest item
len() - Return the length of the object
sorted() - Sorts an iterable object
27. 27
When to use Tuple and List
A tuple’s contents cannot be changed
individually once assigned
Each element of a list can be dynamically
assigned
28. 28
Data Types – Range
class range(stop)
class range(start, stop[, step])
Represents an immutable sequence of numbers and is commonly
used for looping a specific number of times
start - The value of the start parameter (or 0 if the parameter was not
supplied)
stop - The value of the stop parameter
step - The value of the step parameter (or 1 if the parameter was not
supplied)
It only stores the start, stop and step values, calculating
individual items and subranges as needed
30. 30
Data Types – Set
class set([iterable])
class frozenset([iterable])
An unordered collection of distinct hashable
(non-dynamic) objects
Common use includes membership testing,
removing duplicates from a sequence,
mathematical operations (union, intersection,
difference, symmetric difference)
32. 32
Data Types – Dictionary
class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)
Immutable sequence of data – once assigned,
elements within cannot be changed
Can be created by placing a comma-separated list of
key: value pairs within braces
Example: {'apple': 10, 'orange': 12} or {10: 'apple', 12:
'orange'}, or by the dict() constructor
34. 34
Defining Functions
You can define functions to provide the required functionality. Here are
simple rules to define a function in Python.
Function blocks begin with the keyword def followed by the function name and
parentheses ( ( ) ).
Any input parameters or arguments should be placed within these parentheses.
You can also define parameters inside these parentheses.
The first statement of a function can be an optional statement - the
documentation string of the function or docstring.
The code block within every function starts with a colon (:) and is indented.
The statement return [expression] exits a function. A return statement with no
arguments is the same as return None.
36. 36
Object-oriented Programming
Object: A unique instance of a data structure that's defined by its class. An
object comprises both data members (class variables and instance
variables) and methods.
Class: A user-defined prototype for an object that defines a set of attributes
that characterize any object of the class. The attributes are data members
(class variables and instance variables) and methods, accessed via dot
notation.
Class variable: A variable that is shared by all instances of a class. Class
variables are defined within a class but outside any of the class's methods.
Class variables are not used as frequently as instance variables are.
Data member: A class variable or instance variable that holds data
associated with a class and its objects.
37. 37
Object-oriented Programming
Instance variable: A variable that is defined inside a method and
belongs only to the current instance of a class.
Inheritance: The transfer of the characteristics of a class to other
classes that are derived from it.
Instance: An individual object of a certain class. An object obj that
belongs to a class Circle, for example, is an instance of the class
Circle.
Instantiation: The creation of an instance of a class.
Method : A special kind of function that is defined in a class
definition.
38. 38
Object-oriented Programming
In this example, we can see a basic
demonstration of using Classes and
Object-Oriented programming
Class → Employee
Class variable → empCount
Data member → name, salary
Instance → emp1 and emp2
Inhertiance → emp1 and emp2
inherits the properties of the class
Employee
Method → displayCount(),
displayEmployee()
39. 39
Commonly Used Standard
Modules
time – provides objects and functions for working with Time
calendar - provides objects and functions for working with Dates
os, shutil – contains functions that allow Python to interact with the operating
system and the shell
sys – common utility scripts needed to process command line arguments
re – regular expression tools for advanced string processing
math – gives access to floating point Mathematical functions and operations
urllib – access internet and processing internet protocols
Smtplib – sending email