SlideShare a Scribd company logo
1 of 48
Download to read offline
Python
         OpenSource Object Oriented scripting language
         OpenSource Object Oriented scripting language




Nom du fichier – à compléter   Management Presentation        1
Agenda of workshop.


 Introduction to Python
 Features of Python
 Python in Enterprise
 Who use Python (They speak about Python)
 Rapid application Development using Python with          OpenERP and
Django.
 Installation of Python on Windows and Linux
 Setup Development Enviroments using Eclipse
 Step to Python
 String
 Number
 Statements & Control Flow


  Nom du fichier – à compléter   Management Presentation
Agenda.


     Function
     Modules
     Data Structure
      •      List, Tuple, Dict
     Sorting
     Object Oriented
      •      Class, Object, Inheritence, Polymorphism
     Errors and Exceptions Handling
     Input / Output
     Python Quiz




Nom du fichier – à compléter     Management Presentation
Introduction to Python

     Python is developed by Guido van Rossum, named the language
      after the BBC show "Monty Python's Flying Circus".




Nom du fichier – à compléter   Management Presentation
Introduction to Python.
     Python is an easy to learn, powerful programming language. It has
      efficient high-level data structures and a simple but effective
      approach to object-oriented
      programming.

     Python's elegant syntax and dynamic typing, together with its
      interpreted nature, make it an ideal language for scripting and
      rapid application development in many areas on most platforms.




Nom du fichier – à compléter   Management Presentation
Features of Python.

     Simple
     Flexible.
     Easy to Learn
     Free and Open Source
     High-level Language
     Platform Independent.
     Dynamic Type.
     Extensive Libraries.
     Object Oriented.
     Interpreted.
     Scalable.




Nom du fichier – à compléter   Management Presentation
Python in Enterprise

   Frameworks, Web Development and MNCs.




Nom du fichier – à compléter   Management Presentation
Python in Enterprise.
     Server, Social Network, shopping sites.




     Games & Graphics.




Nom du fichier – à compléter   Management Presentation
What User Says ?

    YouTube.com
      •     "Python is fast enough for our site and allows us to produce maintainable features
            in record times, with a minimum of developers," said Cuong Do, Software
            Architect, YouTube.com.


    Google
      •     "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." said Peter Norvig,
            director of search quality at Google, Inc.


    Industrial Light & Magic
      •     "Python plays a key role in our production pipeline. Without it a project the size of
            Star Wars: Episode II would have been very difficult to pull off. From crowd
            rendering to batch processing to compositing, Python binds all things together,"
            said Tommy Burnette, Senior Technical Director, Industrial Light & Magic.



Nom du fichier – à compléter   Management Presentation
What User Says ?



     University of Maryland
      •      "I have the students learn Python in our undergraduate and graduate
             Semantic Web courses. Why? Because basically there's nothing else with the
             flexibility and as many web libraries," said Prof. James A. Hendler.




Nom du fichier – à compléter   Management Presentation
Rapid application Development using Python

    • OpenERP Module




    • Web Application using Django




Nom du fichier – à compléter      Management Presentation
Starting with Python.


     Instalation on Linux
      •      If you are using a Linux distribution such as Ubuntu, Fedora, OpenSUSE it is
             most likely you already have Python installed on your system.
      •      To test if you have Python already installed on your Linux box, open a shell
             program (like console or gnome-terminal) and enter the command python -V
             as shown below.


     Instalation on Windows
      •      download the latest python version from the website and install it on your
             system.
      •      Set the environment variable path.


     Setup Development Enviroments using Eclipse.
      •      http://www.easyeclipse.org/site/distributions/python.html


Nom du fichier – à compléter   Management Presentation
First step to Python.

     Using The Interpreter Prompt
      •      Start the interpreter on the command line by entering python at the shell
             prompt.
      •      For Windows users, you can run the interpreter in the command line if you
             have set the PATH variable appropriately.

      •      Ex.
                    print('Hello World')

     Using Source file.

     Indentation



Nom du fichier – à compléter   Management Presentation
Python Literals & Numbers.

   Literal Constants
    •      An example of a literal constant is a number like 5, 1.23, 9.25e-3 or a string
           like 'This is a string' or "It's a string!". It is called a literal because it is literal -
           you use its value literally.


   Numbers
    •      Numbers in Python are of three types - integers, floating point and complex
           numbers.
    •      Ex. 3.23 and 52.3E-4




Nom du fichier – à compléter   Management Presentation
Python Strings.

     Strings

      •      Single Quotes
      •      Double Quotes
      •      Triple Quotes
      •      Escape Sequences


     Raw Strings
     Strings Are Immutable
     String Literal Concatenation




Nom du fichier – à compléter   Management Presentation
Control Flow statements

      The if statement.
        •     The if statement is used to check a condition and if the condition is true, we
              run a block of statements (called the if-block), else we process another block
              of statements (called the else-block). The else clause is optional.
        •     Example




Nom du fichier – à compléter   Management Presentation
Control Flows Cont...

      The while Statement
        •     The while statement allows you to repeatedly execute a block of statements
              as long as a condition is true. A while statement is an example of what is
              called a looping statement. A while statement can have an optional else
              clause.
        •     Example


      The for loop
        •     The for..in statement is another looping statement which iterates over a
              sequence of objects i.e. go through each item in a sequence, sequence is just
              an ordered collection of items.
        •     The for loop also have optional else statement.
        •     Example




Nom du fichier – à compléter   Management Presentation
Python Control Flow...

     The break Statement
      •      The break statement is used to break out of a loop statement i.e. stop the
             execution of a looping statement, even if the loop condition has not become
             False or the sequence of items has been completely iterated over.
      •      Example



     The continue Statement
      •      The continue statement is used to tell Python to skip the rest of the
             statements in the current loop block and to continue to the next iteration of
             the loop.
      •      Example




Nom du fichier – à compléter   Management Presentation
Python Functions


     Functions are reusable pieces of programs. They allow you to give a
      name to a block of statements and you can run that block using
      that name anywhere in your program and any number of times.

     Functions are defined using the def keyword.
     This is followed by an identifier name for the function followed by a
      pair of parentheses which may enclose some names of variables
      and the line ends with a colon.

     Example




Nom du fichier – à compléter   Management Presentation
Python Function Cont...



     Local Variables
     Using The global Statement
     Default Argument Values
     Keyword Arguments
     Variable Arguments.
     The return Statement
     Return Multiple value

     DocStrings
      •     Python has a nifty feature called documentation strings, usually referred to by its
            shorter name docstrings. DocStrings are an important tool that you should make
            use of since it helps to document the program better and makes it easier to
            understand.


Nom du fichier – à compléter   Management Presentation
Lambda Function

     Python supports the creation of anonymous functions (i.e.
      functions that are not bound to a name) at runtime, using a
      construct called "lambda".
     Example

             l=[1,2,3,4,5,6,7,8,9,10]
             print map(lambda x: x*5,l)

      •      Example
             Foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
             print filter(lambda x: x % 3 == 0, foo)
      •      We can also pass lambda functions as function parameters without assigning
             to to intermediate variables.



Nom du fichier – à compléter   Management Presentation
Map & Filter

     Map
      •      One of the common things we do with list and other sequences is applying
             an operation to each item and collect the result.

             Items = [1, 2, 3, 4, 5]
             def sqr(x): return x ** 2
             list(map(sqr, items))


     Filter
      •      As the name suggests filter extracts each element in the sequence for which
             the function returns True.

             list( filter((lambda x: x < 0), range(-5,5)))




Nom du fichier – à compléter   Management Presentation
Modules


        You have seen how you can reuse code in your program by defining
         functions once. What if you wanted to reuse a number of functions
         in other programs that you write?
        The answer is modules.
        There are various methods of writing modules, but the simplest
         way is to create a file with a .py extension that contains functions
         and variables.
        A module can be imported by another program to make use of its
         functionality.




Nom du fichier – à compléter   Management Presentation
Making Your Own Modules

     Creating your own modules is easy, you've been doing it all along!
      This is because every Python program is also a module. You just
      have to make sure it has a .py extension.
     Example of Module




Nom du fichier – à compléter     Management Presentation
Packages


   Packages are just folders of modules with a special __init__.py file
    that indicates to Python that this folder is special because it
    contains Python modules.




Nom du fichier – à compléter   Management Presentation
Python Data Structures

       Data structures are basically just that - they are structures which
        can hold some data together. In other words, they are used to
        store a collection of related data.

       List
        •     A list is a data structure that holds an ordered collection of items i.e. you can
              store a sequence of items in a list.
        •     The list of items should be enclosed in square brackets so that Python
              understands that you are specifying a list. Once you have created a list, you
              can add, remove or search for items in the list. Since we can add and remove
              items, we say that a list is a mutable data type i.e. this type can be altered.
        •     Example
              o [1,2,3, 'a']




Nom du fichier – à compléter     Management Presentation
Data Structure cont...

   Tuple

    •      Tuples are used to hold together multiple objects. Think of them as similar to
           lists, but without the extensive functionality that the list class gives you. One
           major feature of tuples is that they are immutable like strings i.e. you cannot
           modify tuples.
    •      Tuples are defined by specifying items separated by commas within an
           optional pair of parentheses.
    •      Tuples are usually used in cases where a statement or a user-defined
           function can safely assume that the collection of values i.e. the tuple of
           values used will not change.
    •      Example
           o (1,2,3)




Nom du fichier – à compléter   Management Presentation
Python Data Structure cont...

     Dictionary
      •      A dictionary is like an address-book where you can find the address or
             contact details of a person by knowing only his/her name i.e. we associate
             keys (name) with values (details).
      •      Note that the key must be unique just like you cannot find out the correct
             information if you have two persons with the exact same name.
      •      Example
             o {'a': 1, 'b':2}




Nom du fichier – à compléter   Management Presentation
Python Data Structure

   Set
    •      Sets are unordered collections of simple objects. These are used when the
           existence of an object in a collection is more important than the order or
           how many times it occurs.
    •      Example
           o bri = set(['brazil', 'russia', 'india'])




Nom du fichier – à compléter   Management Presentation
Sorting

     There are lots of way to sort the data in python.
     Each data structure have its own sorting mechanism.

     List Sort
             numlist=[1, 2.1, 2, 1.1, 1.3, 1.8, 1.9, 2.4, 2.8, 2.5, 2.8, 2.4, 2.1, 2.3, 1.1, 1.3,
             1.3, 1.2, 1.2, 3, 3.1, 2.5, 3.5]
             numlist.sort()
             print (numlist)

      •      Custom Sorting With key=

             strs = ['ccc', 'aaaa', 'd', 'bb']
             print sorted(strs, key=len)
             print sorted(strs, key=str.lower)


Nom du fichier – à compléter   Management Presentation
Sorting cont...

   sort() method

           mylist = ["b", "C", "A"]
           mylist.sort()


   Dictonary sorting

           import operator
           x = {1: 2, 3: 4, 4:3, 2:1, 0:0}
           sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1))




Nom du fichier – à compléter   Management Presentation
Object Oriented Programming with Python

     Organizing your program which is to combine data and
      functionality and wrap it inside something called an object. This is
      called the object oriented programming paradigm.
     Classes and objects are the two main aspects of object oriented
      programming.
     Class contains data and methods.

     The self
      •      Class methods have only one specific difference from ordinary functions -
             they must have an extra first name that has to be added to the beginning of
             the parameter list, but you do not give a value for this parameter when you
             call the method, Python will provide it. This particular variable refers to the
             object itself, and by convention, it is given the name self.




Nom du fichier – à compléter    Management Presentation
Classes

      The simplest class possible is shown in the following example.




Nom du fichier – à compléter      Management Presentation
Object Methods


      We have already discussed that classes/objects can have methods
       just like functions except that we have an extra self variable.
      Example




Nom du fichier – à compléter    Management Presentation
The __init__method

     There are many method names which have special significance in
      Python classes. We will see the significance of the __init__ method
      now.
     The __init__ method is run as soon as an object of a class is
      instantiated. The method is useful to do any initialization you want
      to do with your object.
     Example




Nom du fichier – à compléter       Management Presentation
Class And Object Variables

     There are two types of fields - class variables and object variables
      which are classified depending on whether the class or the object
      owns the variables respectively.

     Class variables are shared - they can be accessed by all instances
      of that class. There is only one copy of the class variable and when
      any one object makes a change to a class variable, that change will
      be seen by all the other instances.

     Object variables are owned by each individual object/instance
      of the class. In this case, each object has its own copy of the field
      i.e. they are not shared and are not related in anyway to the field
      by the same name in a different instance.
     Example

Nom du fichier – à compléter     Management Presentation
Inheritance

     One of the major benefits of object oriented programming is reuse
      of code and one of the ways this is achieved is through the
      inheritance mechanism.
     Inheritance can be best imagined as implementing a type and
      subtype relationship between classes.
     Simple inheritance.
     Multiple inheritance.
     Multi level Inheritance.




Nom du fichier – à compléter      Management Presentation
Errors and Exceptions Handling

     Exception
      •      Exceptions occur when certain exceptional situations occur in your program.
             For example, what if you are going to read a file and the file does not exist?
             Or what if you accidentally deleted it when the program was running? Such
             situations are handled using exceptions.
      •      We will try to read input from the user. Press ctrl-d and see what happens.




Nom du fichier – à compléter     Management Presentation
Exception cont...

     Errors
      •      Consider a simple print function call. What if we misspelt print as Print?




Nom du fichier – à compléter   Management Presentation
Handling Exceptions

     We can handle exceptions using the try..except statement.
     We put all the statements that might raise exceptions/errors inside
      the try block and then put handlers for the appropriate
      errors/exceptions in the except clause/block.
     The except clause can handle a single specified error or exception,
      or a parenthesized list of errors/exceptions. If no names of errors
      or exceptions are supplied, it will handle all errors and exceptions.
     If any error or wxception is not handeled then default python
      handler will called.
     You can also have an else clause associated with a
      try..except block. The else clause is executed if no
      exception occurs.




Nom du fichier – à compléter      Management Presentation
Raising Exceptions

     You can raise exceptions using the raise statement by providing the
      name of the error/exception and the exception object that is to be
      thrown.
     The error or exception that you can arise should be class which
      directly or indirectly must be a derived class of the Exception class.
     Example




Nom du fichier – à compléter       Management Presentation
Try ..Finally


       Suppose you are reading a file in your program. How do you ensure
        that the file object is closed properly whether or not an exception
        was raised? This can be done using the finally block.
       Example




Nom du fichier – à compléter      Management Presentation
Input / Output

     Up to now we have seen how to take input from user and display it
      using input, raw_input and print statements.
     Another common type of input/output is dealing with files. The
      ability to create, read and write files is essential to many programs.
     Files
      •      You can open and use files for reading or writing by creating an object of the
             file class and using its read, readline or write methods appropriately to read
             from or write to the file.
      •      The ability to read or write to the file depends on the mode you have
             specified for the file opening.
      •      Then finally, when you are finished with the file, you call the close method to
             tell Python that we are done using the file.




Nom du fichier – à compléter   Management Presentation
Input/Output.

     Methods of File object.
      •      read()
      •      read_line()
      •      readlines()
      •      write(stringToWrite)
      •      seek(seekingByte)
      •      close()




Nom du fichier – à compléter   Management Presentation
Input/Output

     Pickle
      •      when you want to save more complex data types like lists, dictionaries, or
             class instances, things get a lot more complicated.
      •      Python provides a standard module called pickle.
      •      This is an amazing module that can take almost any Python object and
             convert it to a string representation; this process is called pickling.
      •      Reconstructing the object from the string representation is called unpickling.




Nom du fichier – à compléter   Management Presentation
Input/Output

     If you have an object x, and a file object f that’s been opened for
      writing, the simplest way to pickle the object takes only one line of
      code:

      •      pickle.dump(x, f)


     To unpickle the object again, if f is a file object which has been
      opened for reading:

      •      x = pickle.load(f)




Nom du fichier – à compléter   Management Presentation
Nom du fichier – à compléter   Management Presentation
Contact Us




                               Write email at education@openerp.co.in
                                      call us on 91 79 400 500 48




Nom du fichier – à compléter      Management Presentation

More Related Content

What's hot

Learn python – for beginners
Learn python – for beginnersLearn python – for beginners
Learn python – for beginnersRajKumar Rampelli
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming LanguageLaxman Puri
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAChapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAMaulik Borsaniya
 
Python: the Project, the Language and the Style
Python: the Project, the Language and the StylePython: the Project, the Language and the Style
Python: the Project, the Language and the StyleJuan-Manuel Gimeno
 
Chapter 0 Python Overview (Python Programming Lecture)
Chapter 0 Python Overview (Python Programming Lecture)Chapter 0 Python Overview (Python Programming Lecture)
Chapter 0 Python Overview (Python Programming Lecture)IoT Code Lab
 
Python quick guide1
Python quick guide1Python quick guide1
Python quick guide1Kanchilug
 
Python for Science and Engineering: a presentation to A*STAR and the Singapor...
Python for Science and Engineering: a presentation to A*STAR and the Singapor...Python for Science and Engineering: a presentation to A*STAR and the Singapor...
Python for Science and Engineering: a presentation to A*STAR and the Singapor...pythoncharmers
 
Python教程 / Python tutorial
Python教程 / Python tutorialPython教程 / Python tutorial
Python教程 / Python tutorialee0703
 
pyconjp2015_talk_Translation of Python Program__
pyconjp2015_talk_Translation of Python Program__pyconjp2015_talk_Translation of Python Program__
pyconjp2015_talk_Translation of Python Program__Renyuan Lyu
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to PythonNowell Strite
 
Python and its Applications
Python and its ApplicationsPython and its Applications
Python and its ApplicationsAbhijeet Singh
 
Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3Chariza Pladin
 
Python - An Introduction
Python - An IntroductionPython - An Introduction
Python - An IntroductionSwarit Wadhe
 
Overview of python 2019
Overview of python 2019Overview of python 2019
Overview of python 2019Samir Mohanty
 
Introduction to Python Pandas for Data Analytics
Introduction to Python Pandas for Data AnalyticsIntroduction to Python Pandas for Data Analytics
Introduction to Python Pandas for Data AnalyticsPhoenix
 

What's hot (19)

Learn python – for beginners
Learn python – for beginnersLearn python – for beginners
Learn python – for beginners
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming Language
 
Python final ppt
Python final pptPython final ppt
Python final ppt
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAChapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
 
Python: the Project, the Language and the Style
Python: the Project, the Language and the StylePython: the Project, the Language and the Style
Python: the Project, the Language and the Style
 
Chapter 0 Python Overview (Python Programming Lecture)
Chapter 0 Python Overview (Python Programming Lecture)Chapter 0 Python Overview (Python Programming Lecture)
Chapter 0 Python Overview (Python Programming Lecture)
 
Python quick guide1
Python quick guide1Python quick guide1
Python quick guide1
 
Python for Science and Engineering: a presentation to A*STAR and the Singapor...
Python for Science and Engineering: a presentation to A*STAR and the Singapor...Python for Science and Engineering: a presentation to A*STAR and the Singapor...
Python for Science and Engineering: a presentation to A*STAR and the Singapor...
 
Python教程 / Python tutorial
Python教程 / Python tutorialPython教程 / Python tutorial
Python教程 / Python tutorial
 
pyconjp2015_talk_Translation of Python Program__
pyconjp2015_talk_Translation of Python Program__pyconjp2015_talk_Translation of Python Program__
pyconjp2015_talk_Translation of Python Program__
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Python and its Applications
Python and its ApplicationsPython and its Applications
Python and its Applications
 
Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3
 
Python - An Introduction
Python - An IntroductionPython - An Introduction
Python - An Introduction
 
Python
PythonPython
Python
 
Overview of python 2019
Overview of python 2019Overview of python 2019
Overview of python 2019
 
Beginning Python
Beginning PythonBeginning Python
Beginning Python
 
Introduction to Python Pandas for Data Analytics
Introduction to Python Pandas for Data AnalyticsIntroduction to Python Pandas for Data Analytics
Introduction to Python Pandas for Data Analytics
 

Similar to Python Workshop

20120314 changa-python-workshop
20120314 changa-python-workshop20120314 changa-python-workshop
20120314 changa-python-workshopamptiny
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythonMohammed Rafi
 
Python Programming1.ppt
Python Programming1.pptPython Programming1.ppt
Python Programming1.pptRehnawilson1
 
Python Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & stylePython Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & styleKevlin Henney
 
introduction of python in data science
introduction of python in data scienceintroduction of python in data science
introduction of python in data sciencebhavesh lande
 
python intro and installation.pptx
python intro and installation.pptxpython intro and installation.pptx
python intro and installation.pptxadityakumawat625
 
Programming with Python: Week 1
Programming with Python: Week 1Programming with Python: Week 1
Programming with Python: Week 1Ahmet Bulut
 
Python 101 for the .NET Developer
Python 101 for the .NET DeveloperPython 101 for the .NET Developer
Python 101 for the .NET DeveloperSarah Dutkiewicz
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythonRanjith kumar
 
Python Training in Pune
Python Training in PunePython Training in Pune
Python Training in PuneClassboat.com
 

Similar to Python Workshop (20)

20120314 changa-python-workshop
20120314 changa-python-workshop20120314 changa-python-workshop
20120314 changa-python-workshop
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python Programming1.ppt
Python Programming1.pptPython Programming1.ppt
Python Programming1.ppt
 
Python Course In Chandigarh
Python Course In ChandigarhPython Course In Chandigarh
Python Course In Chandigarh
 
Python Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & stylePython Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & style
 
Python Programming Part 1.pdf
Python Programming Part 1.pdfPython Programming Part 1.pdf
Python Programming Part 1.pdf
 
Python Programming Part 1.pdf
Python Programming Part 1.pdfPython Programming Part 1.pdf
Python Programming Part 1.pdf
 
Python Programming Part 1.pdf
Python Programming Part 1.pdfPython Programming Part 1.pdf
Python Programming Part 1.pdf
 
introduction of python in data science
introduction of python in data scienceintroduction of python in data science
introduction of python in data science
 
python intro and installation.pptx
python intro and installation.pptxpython intro and installation.pptx
python intro and installation.pptx
 
MODULE 1.pptx
MODULE 1.pptxMODULE 1.pptx
MODULE 1.pptx
 
Python
Python Python
Python
 
Programming with Python: Week 1
Programming with Python: Week 1Programming with Python: Week 1
Programming with Python: Week 1
 
Python 101 for the .NET Developer
Python 101 for the .NET DeveloperPython 101 for the .NET Developer
Python 101 for the .NET Developer
 
INTERNSHIP REPORT.docx
 INTERNSHIP REPORT.docx INTERNSHIP REPORT.docx
INTERNSHIP REPORT.docx
 
Python
PythonPython
Python
 
python-ppt.ppt
python-ppt.pptpython-ppt.ppt
python-ppt.ppt
 
python-ppt.ppt
python-ppt.pptpython-ppt.ppt
python-ppt.ppt
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python Training in Pune
Python Training in PunePython Training in Pune
Python Training in Pune
 

More from Mantavya Gajjar (16)

Python day2
Python day2Python day2
Python day2
 
Python Day1
Python Day1Python Day1
Python Day1
 
FDP Event Report
FDP Event Report FDP Event Report
FDP Event Report
 
Demonstrate OpenERP
Demonstrate OpenERPDemonstrate OpenERP
Demonstrate OpenERP
 
ERP Implementation cycle
ERP Implementation cycleERP Implementation cycle
ERP Implementation cycle
 
Point of Sale - OpenERP 6.1
Point of Sale - OpenERP 6.1Point of Sale - OpenERP 6.1
Point of Sale - OpenERP 6.1
 
About OpenEPR
About OpenEPRAbout OpenEPR
About OpenEPR
 
Project Management
Project ManagementProject Management
Project Management
 
Order to cash flow
Order to cash flowOrder to cash flow
Order to cash flow
 
Installation
Installation Installation
Installation
 
Education Portal
Education PortalEducation Portal
Education Portal
 
Tiny-Sugar Guide
Tiny-Sugar GuideTiny-Sugar Guide
Tiny-Sugar Guide
 
Subscription
SubscriptionSubscription
Subscription
 
Payroll
PayrollPayroll
Payroll
 
Account voucher
Account voucherAccount voucher
Account voucher
 
Send Mail
Send MailSend Mail
Send Mail
 

Recently uploaded

Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxUdaiappa Ramachandran
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Commit University
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfJamie (Taka) Wang
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPathCommunity
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IES VE
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioChristian Posta
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAshyamraj55
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsSafe Software
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureEric D. Schabell
 

Recently uploaded (20)

Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptx
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation Developers
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
20150722 - AGV
20150722 - AGV20150722 - AGV
20150722 - AGV
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and Istio
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability Adventure
 

Python Workshop

  • 1. Python OpenSource Object Oriented scripting language OpenSource Object Oriented scripting language Nom du fichier – à compléter Management Presentation 1
  • 2. Agenda of workshop.  Introduction to Python  Features of Python  Python in Enterprise  Who use Python (They speak about Python)  Rapid application Development using Python with OpenERP and Django.  Installation of Python on Windows and Linux  Setup Development Enviroments using Eclipse  Step to Python  String  Number  Statements & Control Flow Nom du fichier – à compléter Management Presentation
  • 3. Agenda.  Function  Modules  Data Structure • List, Tuple, Dict  Sorting  Object Oriented • Class, Object, Inheritence, Polymorphism  Errors and Exceptions Handling  Input / Output  Python Quiz Nom du fichier – à compléter Management Presentation
  • 4. Introduction to Python  Python is developed by Guido van Rossum, named the language after the BBC show "Monty Python's Flying Circus". Nom du fichier – à compléter Management Presentation
  • 5. Introduction to Python.  Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming.  Python's elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms. Nom du fichier – à compléter Management Presentation
  • 6. Features of Python.  Simple  Flexible.  Easy to Learn  Free and Open Source  High-level Language  Platform Independent.  Dynamic Type.  Extensive Libraries.  Object Oriented.  Interpreted.  Scalable. Nom du fichier – à compléter Management Presentation
  • 7. Python in Enterprise  Frameworks, Web Development and MNCs. Nom du fichier – à compléter Management Presentation
  • 8. Python in Enterprise.  Server, Social Network, shopping sites.  Games & Graphics. Nom du fichier – à compléter Management Presentation
  • 9. What User Says ?  YouTube.com • "Python is fast enough for our site and allows us to produce maintainable features in record times, with a minimum of developers," said Cuong Do, Software Architect, YouTube.com.  Google • "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." said Peter Norvig, director of search quality at Google, Inc.  Industrial Light & Magic • "Python plays a key role in our production pipeline. Without it a project the size of Star Wars: Episode II would have been very difficult to pull off. From crowd rendering to batch processing to compositing, Python binds all things together," said Tommy Burnette, Senior Technical Director, Industrial Light & Magic. Nom du fichier – à compléter Management Presentation
  • 10. What User Says ?  University of Maryland • "I have the students learn Python in our undergraduate and graduate Semantic Web courses. Why? Because basically there's nothing else with the flexibility and as many web libraries," said Prof. James A. Hendler. Nom du fichier – à compléter Management Presentation
  • 11. Rapid application Development using Python • OpenERP Module • Web Application using Django Nom du fichier – à compléter Management Presentation
  • 12. Starting with Python.  Instalation on Linux • If you are using a Linux distribution such as Ubuntu, Fedora, OpenSUSE it is most likely you already have Python installed on your system. • To test if you have Python already installed on your Linux box, open a shell program (like console or gnome-terminal) and enter the command python -V as shown below.  Instalation on Windows • download the latest python version from the website and install it on your system. • Set the environment variable path.  Setup Development Enviroments using Eclipse. • http://www.easyeclipse.org/site/distributions/python.html Nom du fichier – à compléter Management Presentation
  • 13. First step to Python.  Using The Interpreter Prompt • Start the interpreter on the command line by entering python at the shell prompt. • For Windows users, you can run the interpreter in the command line if you have set the PATH variable appropriately. • Ex. print('Hello World')  Using Source file.  Indentation Nom du fichier – à compléter Management Presentation
  • 14. Python Literals & Numbers.  Literal Constants • An example of a literal constant is a number like 5, 1.23, 9.25e-3 or a string like 'This is a string' or "It's a string!". It is called a literal because it is literal - you use its value literally.  Numbers • Numbers in Python are of three types - integers, floating point and complex numbers. • Ex. 3.23 and 52.3E-4 Nom du fichier – à compléter Management Presentation
  • 15. Python Strings.  Strings • Single Quotes • Double Quotes • Triple Quotes • Escape Sequences  Raw Strings  Strings Are Immutable  String Literal Concatenation Nom du fichier – à compléter Management Presentation
  • 16. Control Flow statements  The if statement. • The if statement is used to check a condition and if the condition is true, we run a block of statements (called the if-block), else we process another block of statements (called the else-block). The else clause is optional. • Example Nom du fichier – à compléter Management Presentation
  • 17. Control Flows Cont...  The while Statement • The while statement allows you to repeatedly execute a block of statements as long as a condition is true. A while statement is an example of what is called a looping statement. A while statement can have an optional else clause. • Example  The for loop • The for..in statement is another looping statement which iterates over a sequence of objects i.e. go through each item in a sequence, sequence is just an ordered collection of items. • The for loop also have optional else statement. • Example Nom du fichier – à compléter Management Presentation
  • 18. Python Control Flow...  The break Statement • The break statement is used to break out of a loop statement i.e. stop the execution of a looping statement, even if the loop condition has not become False or the sequence of items has been completely iterated over. • Example  The continue Statement • The continue statement is used to tell Python to skip the rest of the statements in the current loop block and to continue to the next iteration of the loop. • Example Nom du fichier – à compléter Management Presentation
  • 19. Python Functions  Functions are reusable pieces of programs. They allow you to give a name to a block of statements and you can run that block using that name anywhere in your program and any number of times.  Functions are defined using the def keyword.  This is followed by an identifier name for the function followed by a pair of parentheses which may enclose some names of variables and the line ends with a colon.  Example Nom du fichier – à compléter Management Presentation
  • 20. Python Function Cont...  Local Variables  Using The global Statement  Default Argument Values  Keyword Arguments  Variable Arguments.  The return Statement  Return Multiple value  DocStrings • Python has a nifty feature called documentation strings, usually referred to by its shorter name docstrings. DocStrings are an important tool that you should make use of since it helps to document the program better and makes it easier to understand. Nom du fichier – à compléter Management Presentation
  • 21. Lambda Function  Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called "lambda".  Example l=[1,2,3,4,5,6,7,8,9,10] print map(lambda x: x*5,l) • Example Foo = [2, 18, 9, 22, 17, 24, 8, 12, 27] print filter(lambda x: x % 3 == 0, foo) • We can also pass lambda functions as function parameters without assigning to to intermediate variables. Nom du fichier – à compléter Management Presentation
  • 22. Map & Filter  Map • One of the common things we do with list and other sequences is applying an operation to each item and collect the result. Items = [1, 2, 3, 4, 5] def sqr(x): return x ** 2 list(map(sqr, items))  Filter • As the name suggests filter extracts each element in the sequence for which the function returns True. list( filter((lambda x: x < 0), range(-5,5))) Nom du fichier – à compléter Management Presentation
  • 23. Modules  You have seen how you can reuse code in your program by defining functions once. What if you wanted to reuse a number of functions in other programs that you write?  The answer is modules.  There are various methods of writing modules, but the simplest way is to create a file with a .py extension that contains functions and variables.  A module can be imported by another program to make use of its functionality. Nom du fichier – à compléter Management Presentation
  • 24. Making Your Own Modules  Creating your own modules is easy, you've been doing it all along! This is because every Python program is also a module. You just have to make sure it has a .py extension.  Example of Module Nom du fichier – à compléter Management Presentation
  • 25. Packages  Packages are just folders of modules with a special __init__.py file that indicates to Python that this folder is special because it contains Python modules. Nom du fichier – à compléter Management Presentation
  • 26. Python Data Structures  Data structures are basically just that - they are structures which can hold some data together. In other words, they are used to store a collection of related data.  List • A list is a data structure that holds an ordered collection of items i.e. you can store a sequence of items in a list. • The list of items should be enclosed in square brackets so that Python understands that you are specifying a list. Once you have created a list, you can add, remove or search for items in the list. Since we can add and remove items, we say that a list is a mutable data type i.e. this type can be altered. • Example o [1,2,3, 'a'] Nom du fichier – à compléter Management Presentation
  • 27. Data Structure cont...  Tuple • Tuples are used to hold together multiple objects. Think of them as similar to lists, but without the extensive functionality that the list class gives you. One major feature of tuples is that they are immutable like strings i.e. you cannot modify tuples. • Tuples are defined by specifying items separated by commas within an optional pair of parentheses. • Tuples are usually used in cases where a statement or a user-defined function can safely assume that the collection of values i.e. the tuple of values used will not change. • Example o (1,2,3) Nom du fichier – à compléter Management Presentation
  • 28. Python Data Structure cont...  Dictionary • A dictionary is like an address-book where you can find the address or contact details of a person by knowing only his/her name i.e. we associate keys (name) with values (details). • Note that the key must be unique just like you cannot find out the correct information if you have two persons with the exact same name. • Example o {'a': 1, 'b':2} Nom du fichier – à compléter Management Presentation
  • 29. Python Data Structure  Set • Sets are unordered collections of simple objects. These are used when the existence of an object in a collection is more important than the order or how many times it occurs. • Example o bri = set(['brazil', 'russia', 'india']) Nom du fichier – à compléter Management Presentation
  • 30. Sorting  There are lots of way to sort the data in python.  Each data structure have its own sorting mechanism.  List Sort numlist=[1, 2.1, 2, 1.1, 1.3, 1.8, 1.9, 2.4, 2.8, 2.5, 2.8, 2.4, 2.1, 2.3, 1.1, 1.3, 1.3, 1.2, 1.2, 3, 3.1, 2.5, 3.5] numlist.sort() print (numlist) • Custom Sorting With key= strs = ['ccc', 'aaaa', 'd', 'bb'] print sorted(strs, key=len) print sorted(strs, key=str.lower) Nom du fichier – à compléter Management Presentation
  • 31. Sorting cont...  sort() method mylist = ["b", "C", "A"] mylist.sort()  Dictonary sorting import operator x = {1: 2, 3: 4, 4:3, 2:1, 0:0} sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1)) Nom du fichier – à compléter Management Presentation
  • 32. Object Oriented Programming with Python  Organizing your program which is to combine data and functionality and wrap it inside something called an object. This is called the object oriented programming paradigm.  Classes and objects are the two main aspects of object oriented programming.  Class contains data and methods.  The self • Class methods have only one specific difference from ordinary functions - they must have an extra first name that has to be added to the beginning of the parameter list, but you do not give a value for this parameter when you call the method, Python will provide it. This particular variable refers to the object itself, and by convention, it is given the name self. Nom du fichier – à compléter Management Presentation
  • 33. Classes  The simplest class possible is shown in the following example. Nom du fichier – à compléter Management Presentation
  • 34. Object Methods  We have already discussed that classes/objects can have methods just like functions except that we have an extra self variable.  Example Nom du fichier – à compléter Management Presentation
  • 35. The __init__method  There are many method names which have special significance in Python classes. We will see the significance of the __init__ method now.  The __init__ method is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object.  Example Nom du fichier – à compléter Management Presentation
  • 36. Class And Object Variables  There are two types of fields - class variables and object variables which are classified depending on whether the class or the object owns the variables respectively.  Class variables are shared - they can be accessed by all instances of that class. There is only one copy of the class variable and when any one object makes a change to a class variable, that change will be seen by all the other instances.  Object variables are owned by each individual object/instance of the class. In this case, each object has its own copy of the field i.e. they are not shared and are not related in anyway to the field by the same name in a different instance.  Example Nom du fichier – à compléter Management Presentation
  • 37. Inheritance  One of the major benefits of object oriented programming is reuse of code and one of the ways this is achieved is through the inheritance mechanism.  Inheritance can be best imagined as implementing a type and subtype relationship between classes.  Simple inheritance.  Multiple inheritance.  Multi level Inheritance. Nom du fichier – à compléter Management Presentation
  • 38. Errors and Exceptions Handling  Exception • Exceptions occur when certain exceptional situations occur in your program. For example, what if you are going to read a file and the file does not exist? Or what if you accidentally deleted it when the program was running? Such situations are handled using exceptions. • We will try to read input from the user. Press ctrl-d and see what happens. Nom du fichier – à compléter Management Presentation
  • 39. Exception cont...  Errors • Consider a simple print function call. What if we misspelt print as Print? Nom du fichier – à compléter Management Presentation
  • 40. Handling Exceptions  We can handle exceptions using the try..except statement.  We put all the statements that might raise exceptions/errors inside the try block and then put handlers for the appropriate errors/exceptions in the except clause/block.  The except clause can handle a single specified error or exception, or a parenthesized list of errors/exceptions. If no names of errors or exceptions are supplied, it will handle all errors and exceptions.  If any error or wxception is not handeled then default python handler will called.  You can also have an else clause associated with a try..except block. The else clause is executed if no exception occurs. Nom du fichier – à compléter Management Presentation
  • 41. Raising Exceptions  You can raise exceptions using the raise statement by providing the name of the error/exception and the exception object that is to be thrown.  The error or exception that you can arise should be class which directly or indirectly must be a derived class of the Exception class.  Example Nom du fichier – à compléter Management Presentation
  • 42. Try ..Finally  Suppose you are reading a file in your program. How do you ensure that the file object is closed properly whether or not an exception was raised? This can be done using the finally block.  Example Nom du fichier – à compléter Management Presentation
  • 43. Input / Output  Up to now we have seen how to take input from user and display it using input, raw_input and print statements.  Another common type of input/output is dealing with files. The ability to create, read and write files is essential to many programs.  Files • You can open and use files for reading or writing by creating an object of the file class and using its read, readline or write methods appropriately to read from or write to the file. • The ability to read or write to the file depends on the mode you have specified for the file opening. • Then finally, when you are finished with the file, you call the close method to tell Python that we are done using the file. Nom du fichier – à compléter Management Presentation
  • 44. Input/Output.  Methods of File object. • read() • read_line() • readlines() • write(stringToWrite) • seek(seekingByte) • close() Nom du fichier – à compléter Management Presentation
  • 45. Input/Output  Pickle • when you want to save more complex data types like lists, dictionaries, or class instances, things get a lot more complicated. • Python provides a standard module called pickle. • This is an amazing module that can take almost any Python object and convert it to a string representation; this process is called pickling. • Reconstructing the object from the string representation is called unpickling. Nom du fichier – à compléter Management Presentation
  • 46. Input/Output  If you have an object x, and a file object f that’s been opened for writing, the simplest way to pickle the object takes only one line of code: • pickle.dump(x, f)  To unpickle the object again, if f is a file object which has been opened for reading: • x = pickle.load(f) Nom du fichier – à compléter Management Presentation
  • 47. Nom du fichier – à compléter Management Presentation
  • 48. Contact Us Write email at education@openerp.co.in call us on 91 79 400 500 48 Nom du fichier – à compléter Management Presentation