This Edureka Python tutorial will help you in understanding the various fundamentals of Python programming with examples in detail. This Python tutorial helps you to learn following topics:
1. Introduction to Python
2. Who uses Python
3. Features of Python
4. Operators in Python
5. Datatypes in Python
6. Flow Control
7. Functions in Python
8. File Handling in Python
2. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Agenda
➢ Python Introduction
➢ Who uses Python?
➢ Python Features
➢ Operators in Python
➢ Datatypes in Python
➢ Flow Control
➢ Functions in Python
➢ File Handling in Python
4. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Python Introduction
➢ Python is an interpreted, object-oriented, high-level programming language
with dynamic semantics
➢ Python was created by Guido Rossum in 1989 and is very easy to learn
Procedure
Oriented
Object
Oriented
Easy to
Learn
High Level
Language
6. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Who Uses Python?
The popular YouTube video
sharing service is largely
written in Python.
C O M P A N I E S U S I N G P Y T H O N
Google makes extensive use of
Python in its web search
systems.
The Raspberry Pi single-
board computer promotes
Python as its educational
language.
Dropbox storage service
codes both its server and
desktop client software
primarily in Python.
BitTorrent peer-to-peer file
sharing system began its life
as a Python program.
NASA, Los Alamos, Fermilab,
JPL, and others use Python
for scientific programming
tasks.
The NSA uses Python for
cryptography and
intelligence analysis.
Netflix and Yelp have both
documented the role of
Python in their software
infrastructures.
8. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Python Features
Simple and Easy to Learn
Free and Open Source
Python is a simple and easy to learn, read & write
Python is an example of a FLOSS (Free/Libre and Open Source
Software) which means one can freely distribute copies of this
software, read it's source code, modify it, etc.
9. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Python Features
High-level Language
Portable
One does not need to bother about the low-level details
like memory allocation, etc. while writing Python
script
Supported by many platforms like Linux, Windows, FreeBSD,
Macintosh, Solaris, OS/2, Amiga, AROS, AS/400, BeOS, OS/390,
PlayStation, Windows CE, etc.
a=3
b=5
sum=a+b
print(sum)
01001010110110
11101100001101
10110101100101
0101011010
compile
10. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Python Features
Python code can invoke C and C++ libraries, can be called from and C++
programs, can integrate with Java
and .NET components
Python supports procedure-oriented programming as well as
object-oriented programming.
Supports different Programming Paradigm
Object
Oriented
Procedure
Oriented
C/C++ Extensible
19. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Bitwise Operators
Identity Operators
Special Operators
Operators in Python
1
2
3
4
5
6
7
20. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Arithmetic Operators
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Bitwise Operators
Identity Operators
Special Operators
1
2
3
4
5
6
7
+
Add two operands or unary plus
-
Subtract two operands or unary subtract
*
Multiply two operands
/
Divide left operand with the right and result
is in float
>> 2 + 3
5
>> +2
>> 3 – 1
2
>> -2
>> 2 * 3
6
>> 6 / 3
2.0
21. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Arithmetic Operators
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Bitwise Operators
Identity Operators
Special Operators
1
2
3
4
5
6
7
**
Left operand raised to the power of right
% Remainder of the division of left operand
by the right
// division that results into whole number
adjusted to the left in the number line
>> 2 + 3
5
>> +2
>> 3 – 1
2
>> -2
>> 2 * 3
6
26. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Comparison Operators
Logical Operators
Bitwise Operators
Identity Operators
Arithmetic Operators1
Assignment Operators2
Comparison Operators3
4
5
6
Special Operators7
>
True if left operand is greater than the right
<
True if left operand is less than the right
==
True if left operand is equal to right
!=
True if left operand is not equal to the right
>> 2 > 3
False
>> 2 < 3
True
>>2 == 2
True
>> x >>=
2
>>print(x)
1
28. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Logical Operators
Bitwise Operators
Identity Operators
Arithmetic Operators1
Assignment Operators2
Comparison Operators3
Logical Operators4
5
6
Special Operators7
and
Returns x if x is False , y otherwise
or
Returns y if x is False, x otherwise
not
Returns True if x is True, False otherwise
>> 2 and
3
3
>> 2 or 3
2
>> not 1
False
30. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Bitwise Operators
Identity Operators
Arithmetic Operators1
Assignment Operators2
Comparison Operators3
Logical Operators4
Bitwise Operators5
6
Membership Operators7
a | b
Perform OR operation on each bit of the no.
a & b Perform AND operation on each bit of the
number
a ^ b Perform XOR operation on each bit of
the number
111 7
101 5
111 7
111 7
101 5
111 5
111 7
101 5
111 2
31. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Bitwise Operators
Identity Operators
Arithmetic Operators1
Assignment Operators2
Comparison Operators3
Logical Operators4
6
Membership Operators7
a >> b
Shift a right by b bits
a << b
Shift a left by b bits
3 >> 2 = 0
0011 0000
3 << 2 = 12
0011 1100Bitwise Operators5
33. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Identity Operators
Arithmetic Operators1
Assignment Operators2
Comparison Operators3
Logical Operators4
Bitwise Operators5
Identity Operators6
Membership Operators7
is
True if the operands are identical (refer to
the same object)
is not True if the operands are not identical (do
not refer to the same object)
>> x = 5
>> x is 5
True
>> x = 5
>> x is not 5
False
35. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Membership Operators
Arithmetic Operators1
Assignment Operators2
Comparison Operators3
Logical Operators4
Bitwise Operators5
Identity Operators6
Membership Operators7
is
True if the operands are identical (refer to the
same object)
is not True if the operands are not identical (do not
refer to the same object)
>> 3 in x
True
>> 3 not in x
False
X = [1, 2, 3, 4, 5]
37. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Datatype
C/C++
➢ No need to declare variables before using them
➢ Python is a loosely typed language. Therefore, no need to define the datatype of variables
Datatype
Numbers Strings Tuples Lists Dictionaries Sets
Immutable Mutable
39. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Datatype
Tuples
Numbers
Strings
Lists
Dictionaries
Sets
Mutable
Immutable
➢ Strings are sequences of one-character strings
Example:
sample = ‘Welcome to Python Tutorial’
or
sample = “Welcome to Python Tutorial”
➢ Multi-line strings can be denoted using triple quotes, ''' or ""“
Example:
sample = “””Don’t Go Gentle into the good Night
Rage! Rage, against the dying light”””
42. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Datatype
Tuples
Numbers
Strings
Lists
Dictionaries
Sets
Mutable
Immutable
Type Specific Method:
➢ upper():
➢ max()
➢ min()
➢ isalpha()
str = ‘edureka’ str.upper () ‘EDUREKA’
str = ‘Edureka’ max (str) ‘u’
min ( str ) ‘a’
str = ‘Edureka’
str = ‘Edureka’
str.isalpha() True
43. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Datatype
Tuples
Numbers
Strings
Lists
Dictionaries
Sets
Mutable
Immutable ➢ A tuple is a sequence of immutable Python objects like floating number, string
literals, etc.
➢ The tuples can’t be changed unlike lists
➢ Tuples are defined using curve braces
myTuple = ( ‘Edureka’ , 2.4, 5, ‘Python’ )
45. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Datatype
Tuples
Numbers
Strings
Lists
Dictionaries
Sets
Mutable
Immutable ➢ A list is a sequence of mutable Python objects like floating number, string literals,
etc.
➢ The lists can be modified
➢ Tuples are defined using square braces
myList = [ ‘Edureka’ , 2.4, 5, ‘Python’ ]
48. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Datatype
Tuples
Numbers
Strings
Lists
Dictionaries
Sets
Mutable
Immutable
➢ Dictionaries are perhaps the most flexible built-in data type in Python
➢ Dictionaries, items are stored and fetched by key, instead of by positional
offset
Key
Value
myDict = { 1: ‘Josh’ , 2: ‘Bob’, 3: ‘James’ }
49. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Datatype
Tuples
Numbers
Strings
Lists
Dictionaries
Sets
Mutable
Immutable
➢ empty dictionary
➢ dictionary with integer keys
➢ dictionary with mixed keys
➢ from sequence having each item as a pair
myDict = {}
myDict = {1: 'apple', 2: 'ball'}
myDict = {'name': 'John', 1: [2, 4, 3]}
myDict = dict([(1,'apple'), (2,'ball')])
Dictionary Examples
52. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Datatype
Tuples
Numbers
Strings
Lists
Dictionaries
Sets
Mutable
Immutable
➢ A set is an unordered collection of items
➢ Every element is unique (no duplicates) and must be immutable (which cannot
be changed)
mySet = {1, 2, 3}
58. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Flow Control
LOOPS REPEAT ACTIONS
SO YOU DON’T HAVE TO ...
For
While
Repeat things until the
loop condition is true
Repeat things till the
given number of times
60. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Flow Control
4. for statement
check if
Count < 10
Count = 0
START
Syntax:
for iterator name in iterating sequence:
execute statements…
Add 1 to Count
Execute Statements
Exit loop
repeat
64. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Function
A function is a block of organized, reusable sets of instructions that is used
to perform some related actions.
Why do we use functions?
➢ Re – usability of code minimizes redundancy
➢ Procedural decomposition makes things organized
Function
Built-in
Function
User Defined
Function
65. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Function
User Defined Function
Syntax:
def func_name (arg1, arg2, arg3, ….):
statements…
return [expression]
Example:
def add ( a, b )
sum = a + b
return sum
adding two number
defining function
Input variable
print variable
output
66. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Function – Pass By Reference
Call by Object Reference:
Python supports call by value where the value is always an object reference, not the value of the object
defining function
print the modified list
output
Modifying list
calling the function
both list are pointing to different
object reference
print the initial list
67. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Function
Call by Object Reference
defining function
print the list
output
appending elements to list
calling the function
both input_list & myList pointing
to same list object reference
69. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
File Handling & I/O Methods
A file or a computer file is a chunk
of logically related data or
information which can be used by
computer programs.
Search Quality
open ( filename, mode )
write( )
close( )
read( )
File Operations in Python
Opening a file
Reading a file
Writing a file
Closing a file