Python by Tcs
Python by Tcs
Session – Objective
- Python Basics
-Versions
-Variable and Types
-Conditional and Looping structures
-Module based Programming
-Container Objects – Data Storage Objects
-OOP Introduction
-File and Exception handling
INTERNAL
Python
Overview:
-High level , Interpreted, Object Oriented and scripting language
2.Version -Desktop:
Stable: 2.7
Latest: 3.x
Link:https://www.python.org/downloads/release/python-2710/
Desktop Modes:
-IDE
-Command Mode
Supports to code in interactive and scripting style
3.Online Editors:
https://www.onlinegdb.com/online_python_compiler
http://www.codeskulptor.org/
-3- INTERNAL
Variables – Whats New in Python?
• Variables are nothing but reserved memory locations to store values. This means
that when you create a variable you reserve some space in memory.
#!/usr/bin/python
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string
print counter
print miles
print name
INTERNAL
Python - I/O
Syntax: Example:
if expression: #!/usr/bin/python
statement(s) var1,var2 = 1,100
elif expression: if var1:
statement(s) print("var1 value is:",var1)
else: elif var1:
statement(s) print("var2 value is:",var2)
else:
print("var1 and var2 are not
defined");
INTERNAL
Nested IF
Syntax: Example:
if expression1: #!/usr/bin/python
statement(s) Var1,var2 = 100,200
if expression2: if var1:
statement(s) print ("var1 value is:”,var1)
elif expression3: if var2:
statement(s) print ("var2 value is:”,var2)
else: else:
statement(s) print ("var2 is not defined“)
elif expression4: else:
statement(s) print ("var1 is not defined“)
else:
statement(s) OUTPUT:
var1 value is:100
var2 value is:200
INTERNAL
How Block is identified - Whats new in Python?
INTERNAL
Looping statements
While: Example:
#!/usr/bin/python
while expression: count = 0
statement(s) while count < 9:
count = count + 1
else:
while expression: print “count is not less than 9”
statement(s) print 'The count is:', count
else: print "Good bye!"
statement(s)
OUTPUT:
count is not less than 9
The count is:8
Good bye!
INTERNAL
Looping Statements - Contnd...
For: Example:
#!/usr/bin/python
for iterating_var in sequence: for letter in 'TCSHYD':
statements(s) print( 'Current Letter :', letter)
print ("Good bye!“)
OUTPUT:
for iterating_var in sequence:
Current Letter :T
statements(s)
Current Letter :C
else:
Current Letter :S
statement(s)
Current Letter :H
Current Letter :Y
Current Letter :D
Good bye!
INTERNAL
Loop control statements
Break: Continue:
#!/usr/bin/python #!/usr/bin/python
for letter in 'Python': for letter in 'Python':
if letter == 'h': if letter == 'h':
break ; continue
print 'Current Letter :', letter ; print ('Current Letter :', letter ;)
print "over"; print( "over“)
OUTPUT: OUTPUT:
Current Letter :P Current Letter : P
Current Letter :y Current Letter : y
Current Letter :t Current Letter : t
over Current Letter : o
Current Letter : n
over INTERNAL
Show Loops Ex – with User defined types
INTERNAL
Functions -
Brief the concept with Example
A function is a block of organized, reusable code that is used
to perform a single, related action.
Python gives you many built-in functions like print(), etc. but
you can also create your own functions. These functions are
called user-defined functions
INTERNAL
Functions
Defining a Function:
Function blocks begin with the keyword def followed by the function name and
parentheses ( ( ) ).
The code block within every function starts with a colon (:) and is indented.
INTERNAL
Functions
Syntax:
Example:
INTERNAL
Functions
Calling a Function:
Defining a function only gives it a name, specifies the parameters that are to
be included in the function and structures the blocks of code.
#!/usr/bin/python
# Function definition is here
def sample( str ):
“This is my first function”
print (str);
return;
INTERNAL
Functions
Pass by reference:
INTERNAL
Functions
#!/usr/bin/python
mylist = [10,20,30];
changeme( mylist );
print ("Values outside the function: ", mylist)
Output:
Values inside the function: [10, 20, 30, [1, 2, 3, 4]]
Values outside the function: [10, 20, 30, [1, 2, 3, 4]]
INTERNAL
Functions
Function Arguments:
You can call a function by using the following types of formal arguments:
Required arguments
Keyword arguments
Default arguments
INTERNAL
Functions
Required arguments:
#!/usr/bin/python
def printme( str ):
print (str);
return;
INTERNAL
Functions
INTERNAL
Functions
Keyword arguments:
Keyword arguments are related to the function calls. When you use
Keyword arguments in a function call, the caller identifies the arguments by
the parameter name.
This allows you to skip arguments or place them out of order because the
Python interpreter is able to use the keywords provided to match the values
with parameters.
#!/usr/bin/python
def printme( str ):
print (str);
return;
printme( str = "My string");
INTERNAL
Functions
#!/usr/bin/python
INTERNAL
Functions
Default arguments:
#!/usr/bin/python
def printinfo( name, age = 35 ):
print "Name: ", name;
print "Age ", age;
return;
INTERNAL
Functions
The return Statement:
#!/usr/bin/python
➢ Lists
➢ Tuples
➢ Dictionary
➢ Set
- 27
INTERNAL
-
Lists
The list type is a container that holds a number of other objects, in a given
order.
Ex:-
L=[1,2,3,4,5,6,7,8]
L3=[1,2,[3,4,5],4,5]
- 28
INTERNAL
-
Lists – Ex
INTERNAL
Lists – How to Apply in Real Time –
Show Ex -
INTERNAL
Tuple
Creating Tuples:-
T = ()
T = (expression, …)
Ex:-
T=(1,2,3,4,5,6,7,8)
T1 = ('Java', 'Python', 2015, 2016)
T2=(1,2,[3,4,5],4,5)
Note:
Explore the functions on tuple with examples?
- 31
INTERNAL
-
Tuple - Ex
INTERNAL
Tuple – How to Apply in Real Time ?
Show Ex
INTERNAL
Set
A set is a dictionary with no values. It has only unique keys. Its syntax is similar to that for
a dictionary. But we omit the values.
Ex:-
s={1,2,3,4,5,6,7,8}
S1 = {'Java', 'Python', 2015, 2016}
S2=set([1,2,3])
- 34
INTERNAL
-
Set – How to Apply in Real Time ?
INTERNAL
Dictionary
A dictionary is mutable that can store any number of objects. Dictionaries consist
of pairs (called items) of keys and their corresponding values.
Keys of a particular dictionary are unique while values may not be.
The values of a dictionary can be of any type, but the keys must be of an
immutable data type such as strings, numbers, or tuples.
Creating Dictionaries:-
D={}
D={1:'Java',2:'Python',3:'HTML'}
Create a dictionary in such a way that, if I give the week day in number I should
be in a position to get Week Day Name, Similarly for Month
- 36
INTERNAL
-
Example
INTERNAL
Dictionary – Ex2
INTERNAL
Dictionary – How to Apply in Real Time ?
INTERNAL
Exception Handling
What is an Exception?
INTERNAL
Exception Handling - Ex:
INTERNAL
File Handling - Ex:
INTERNAL
INTERNAL
OOPs Intro
Why OOP Programming?
OOP Features?
-Abstraction
-Technical Implementation of Abstraction :-Encapsulation
-Inheritance
-Polymorphism etc..
INTERNAL
OOPs Intro with Classes/Objects
Python has been an object-oriented language from day one. Because of
this, creating and using classes and objects are downright easy.
Creating Classes:
The class statement creates a new class definition. The name of the class
immediately follows the keyword class followed by a colon as follows:
class ClassName:
'Optional class documentation string'
class_suite
Creating Classes:
The class statement creates a new class definition. The name of the class
immediately follows the keyword class followed by a colon as follows:
class ClassName:
'Optional class documentation string'
class_suite
def displayCount(self):
print "Total Employee %d" % Employee.empCount
def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary
INTERNAL
Classes/Objects
The variable empCount is a class variable whose value would be shared
among all instances of a this class. This can be accessed as
Employee.empCount from inside the class or outside the class.
You declare other class methods like normal functions with the exception
that the first argument to each method is self. Python adds the self
argument to the list for you; you don't need to include it when you call the
methods.
INTERNAL
Classes/Objects
To create instances of a class, you call the class using class name and
pass in whatever arguments its __init__ method accepts.
INTERNAL
Classes/Objects
Show Example?
INTERNAL
https://dev.mysN.com/downloads/connecto
Python My SQL connectors – Database connectivity
r/python/?os=31
https://dev.mysql.com/downloads/connector/python/?os=31
INTERNAL
https://dev.mysN.com/downloads/connecto
Python My SQL connectors – Database connectivity
r/python/?os=31
https://dev.mysql.com/downloads/connector/python/?os=31
INTERNAL
Python My SQL connectors – Database connectivity
INTERNAL
Applications - Snapshot
INTERNAL
Applications – Snapshot2
INTERNAL
Applications – Snapshot3
INTERNAL
Applications – Snapshot4
INTERNAL
Applications – Snapshot5
INTERNAL
Thank You