This Edureka Python Programming tutorial will help you learn python and understand the various basics of Python programming with examples in detail. Below are the topics covered in this tutorial:
1. Python Installation
2. Python Variables
3. Data types in Python
4. Operators in Python
5. Conditional Statements
6. Loops in Python
7. Functions in Python
8. Classes and Objects
4. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Scripting Languages
A scripting language supports scripts or programs written for a special run-time environment that automate the execution
of tasks
Figure: Scripting Languages
They are an alternative to programs executed one-by-one by a
human operator
They are interpreted rather than being compiled
Python is the most popular Scripting Language in the industry
Program 1 Program 2 Program 3
Program 1 Program 2 Program 3
Script Script
6. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Python is a high-level dynamic programming language
Python supports multiple programming paradigms including object-
oriented, imperative, functional programming and procedural styles
It is easy to learn and provides dynamic typing
Used by vast multitude of companies around the globe
M O Z I L L A
Python Overview
Figure: Companies using Python
8. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Simplicity Of Python
Figure: Python IDLE IDE
➢ Highly readable language
➢ Clean visual layout
➢ Less syntactic exceptions
➢ Superior string manipulation
➢ Elegant and dynamic typing
➢ Interpreted nature
➢ Ideal for scripting and rapid application
➢ Fit for many platforms
13. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Python Fundamentals
The following are the five fundamentals required to master Python
Datatypes
Flow
Control
Functions
File
Handling
Object &
Class Datatypes
Numbers
Strings
Lists
Tuples
Dictionaries
Flow Control
If Else
For
While
Continue
Functions
Definition
Function Call
Docstring
Return
File Handling
Reading
Writing
Editing
Object & Class
Variables
Functions
14. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Datatypes
Numbers
Strings
Lists
Tuples
Dictionaries
Flow Control
If Else
For
While
Continue
Functions
Definition
Function Call
Docstring
Return
File Handling
Reading
Writing
Editing
Object & Class
Variables
Functions
15. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Operations
Attributes
OOP Datatype
Features
You do not need to
declare variables before
using them, or declare
their type
The type also determines
the object’s attributes and
items (if any) and whether
the object can be altered
An object’s type determines
what operations the object
supports, or, in other words,
what operations you can
perform on the data value
Python is completely
object oriented, and
not "statically typed"
Datatypes
All data values in Python are represented by objects and each object or value has a datatype
No
Declaration
16. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
a = 5
b = 7.3
c = 2 + 3j
a = [ 1, 2.2, “Python”] t = (2, “Tuple”, “95”)
s = “This is a string” b = ‘AnBnC’
week = {‘Mon’, ‘Tue’,
‘Wed’, ‘Thu’, ‘Fri’, ‘Sat’,
‘Sun’}
d = {‘value’:5, ‘key’:125}
if (number % 2) = 0:
even = True
else:
even = False
Native Datatypes
Boolean
True / False
Dictionaries
Unordered bags of
key-value pairs
Sets
Unordered bags
of values
Tuples
Ordered immutable
sequences of values
Lists
Ordered sequences of values
Numbers
Integers, Floats, Fractions and
Complex Numbers
Strings
Sequences of Unicode
Characters
Bytes & ByteArray
Contain Single Bytes
17. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Flow Control
If Else
For
While
Continue
Datatypes
Numbers
Strings
Lists
Tuples
Dictionaries
Functions
Definition
Function Call
Docstring
Return
File Handling
Reading
Writing
Editing
Object & Class
Variables
Functions
18. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Flow Control
A program’s Flow Control is the order in which the program’s code executes
To mimic the real world closer then you need to transform real world situations into your program
For this you need to control the execution of your program statements using Flow Controls
Check
Turn
Start Task 1
Task 2 Task 3
Task 4
Figure: Controlled Flow In Python
Types of Flow Control if passcontinuefor breakwhile
19. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
if Statement
The Python compound statement ’if’ lets you
conditionally execute blocks of statements
Figure: Flowchart of if Statement
Body of if
Statement
just below if
Test
expression
True
False
if
for
while
break
continue
pass
21. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
for Statement
The for statement in Python supports
repeated execution of a statement or block
of statements that is controlled by an
iterable expression
Executing Loop Body
Update Expression
Condition
Testing
True
False
Figure: Flowchart of for Statement
Exit For Loop
Start
Expression Initialization
if
while
break
continue
pass
for
23. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
while Statement
The while statement in Python supports repeated
execution of a statement or block of statements
that is controlled by a conditional expression
Body Of
While Loop
Test
Expression
True
False
Figure: Flowchart of while Statement
Statement
Below While
if
for
break
continue
pass
while
Difference between while and for loop
• For loop is used when we know the number of
iterations beforehand.
• While is used we know the range of values but
don’t know the exact number of iterations
25. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
break Statement
The break statement is allowed only inside a
loop body. When break executes, the loop
terminates.
If a loop is nested inside other loops, break
terminates only the innermost nested loop.
Test Condition
Within Loop
True
Figure: Flowchart of break Statement
break
False
if
for
while
continue
pass
break
26. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
break Statement
if
for
while
continue
pass
break
for (Time =
Start Of
Alarm)
If Incoming
Call Occurs
till (Time =
End Of
Alarm)
break Alarm
27. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
continue Statement
The continue statement is allowed
only inside a loop body.
When continue executes, the current
iteration of the loop body
terminates, and execution continues
with the next iteration of the loop.
Test Condition
Within Loop
True
Figure: Flowchart of continue Statement
continue
False
Remaining
Part Of Loop
Normal Return
Of Loop
if
for
while
break
pass
continue
28. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
continue Statement
if
for
while
break
pass
continue
Start Of
Incoming
Call
If Alarm
Schedule
Time
End Of
Incoming
Call
Continue – Alarm To Snooze
29. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
pass Statement
The pass statement, which performs no
action, can be used as a placeholder
when a statement is syntactically required
but you have nothing specific to do.
Figure: Flowchart of pass Statement
Test Condition
Within Loop
True
Process 1
False
Normal Return
Of Loop
Else If
Condition
pass
Else
Condition
True
False
False
Process
Default
True
if
for
while
break
continue
pass
30. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Functions
Definition
Function Call
Docstring
Return
Flow Control
If Else
For
While
Continue
Datatypes
Numbers
Strings
Lists
Tuples
Dictionaries
File Handling
Reading
Writing
Editing
Object & Class
Variables
Functions
31. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Functions
Functions in Python is a group of related statements that
performs a specific task
Functions make our program more organized and help in
code reusability
Figure: Understanding Python functions
32. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Uses of Functions
Figure: Reversing a string
Figure: Function to reverse a string
Functions help in code
reusability
Functions provide
organization to the code
Functions provide
abstraction
Functions help in
extensibility
Uses of Functions
33. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
File Handling
Reading
Writing
Editing
Flow Control
If Else
For
While
Continue
Functions
Definition
Function Call
Docstring
Return
Datatypes
Numbers
Strings
Lists
Tuples
Dictionaries
Object & Class
Variables
Functions
34. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
File Handling
File Handling refers to those operations that are used to read or write a file
To perform file handling, we need to perform these steps:
1. Open File
2. Read / Write File
3. Close File
Open File
Write File
Read File
Close File
Figure: A file operation in Python takes place in the following order
35. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
File Handling – Opening A File
Opening A File
▪ Python has a built-in function open() to open a file
▪ This function returns a file object, also called a handle, as it is used to read or modify the file accordingly
36. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
File Handling – Writing To A File
Writing To A File
▪ In order to write into a file we need to open it in write 'w', append 'a' or exclusive
creation 'x' mode.
▪ We need to be careful with the 'w' mode as it will overwrite into the file if it already
exists. All previous data are erased.
▪ Writing a string or sequence of bytes (for binary files) is done using write() method.
37. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
File Handling – Reading From A File
Reading From A File
▪ To read the content of a file, we must open the file in reading mode.
▪ We can use the read(size) method to read in size number of data. If size parameter is
not specified, it reads and returns up to the end of the file
38. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
File Handling – Closing A File
Closing A File
▪ When we are done with operations to the file, we need to properly close it.
▪ Closing a file will free up the resources that were tied with the file and is
done using the close() method.
39. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Object & Class
Variables
Functions
Flow Control
If Else
For
While
Continue
Functions
Definition
Function Call
Docstring
Return
File Handling
Reading
Writing
Editing
Datatypes
Numbers
Strings
Lists
Tuples
Dictionaries
40. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Object & Class
➢ Python is an object oriented programming language.
➢ Object is simply a collection of data (variables) and methods
(functions) that act on those data.
➢ Class is a blueprint for the object.
Figure: Blueprint | Class Figure: Houses | Objects
A class is like a house’s blueprint. Objects are houses created from a blueprint.
Many houses can be created from a same blueprint.
41. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Defining Class & Creating Object
We define a class using the keyword class
The first string is called docstring and has a brief description about the class.
A class object can be used to create new object instances (instantiation) of that class.
The procedure to create an object is similar to a function call.
43. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Python Job Trends
The following is the Job Trend of
Python across the world
Python has gradually acquired a
sizable share in the industry and is
the market leader from 2014
Source: www.indeed.com
45. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Python Success Story
“Python has been an important part of Google since the beginning,
and remains so as the system grows and evolves. Today dozens of
Google engineers use Python, and we're looking for more people with
skills in this language“
Source: Peter Norvig, Director, Google
M O Z I L L A
“When a user decides to share out an Instagram photo to Twitter or
Facebook, we push that task into Gearman, a task queue system
where the whole of Instagram is written in Python”
Source: www.engineering.instagram.com
“Mozilla is moving 2 of its largest sites (addons.mozilla.org and
support.mozilla.com) from PHP to Python”
Source: www.micropipes.com
“Facebook’s Tornado is a relatively simple, non-blocking Web server
framework written in Python, designed to handle thousands of
simultaneous connections, making it ideal for real-time Web services”
Source: www.developers.facebook.com