SlideShare una empresa de Scribd logo
1 de 50
Python Tutorial

Haitham El-Ghareeb, Ph.D.
        May, 2012
  Twitter: @helghareeb
Modules
• A module is a file containing Python
  definitions and statements.
• The file name is the module name with the
  suffix .py appended.
• Within a module, the module’s name (as a
  string) is available as the value of the global
  variable __name__.
Assign Local Name
Compiled Python Files
• As an important speed-up of the start-up time for
  short programs that use a lot of standard
  modules,
• if a file called spam.pyc exists in the directory
  where spam.py is found, this is assumed to
  contain an already-“byte-compiled” version of
  the module spam.
• The modification time of the version of spam.py
  used to create spam.pyc is recorded in spam.pyc,
  and the .pyc file is ignored if these don’t match.
Compiled Python Files
• Normally, you don’t need to do anything to create the
  spam.pyc file.
• Whenever spam.py is successfully compiled, an
  attempt is made to write the compiled version to
  spam.pyc.
• It is not an error if this attempt fails; if for any reason
  the file is not written completely, the resulting
  spam.pyc file will be recognized as invalid and thus
  ignored later.
• The contents of the spam.pyc file are platform
  independent, so a Python module directory can be
  shared by machines of different architectures.
Some Tips for Experts
• When the Python interpreter is invoked with
  the -O flag, optimized code is generated and
  stored in .pyo files.
• The optimizer currently doesn’t help much; it
  only removes assert statements.
• When -O is used, all bytecode is optimized;
  .pyc files are ignored and .py files are
  compiled to optimized bytecode.
Some Tips for Experts
• Passing two -O flags to the Python interpreter (-
  OO) will cause the bytecode compiler to perform
  optimizations that could in some rare cases result
  in malfunctioning programs.
• Currently only __doc__ strings are removed from
  the bytecode, resulting in more compact .pyo
  files.
• Since some programs may rely on having these
  available, you should only use this option if you
  know what you’re doing.
Some Tips for Experts
• A program doesn’t run any faster when it is
  read from a .pyc or .pyo file than when it is
  read from a .py file; the only thing that’s faster
  about .pyc or .pyo files is the speed with
  which they are loaded.
Some Tips for Experts
• When a script is run by giving its name on the
  command line, the bytecode for the script is
  never written to a .pyc or .pyo file.
• Thus, the startup time of a script may be
  reduced by moving most of its code to a
  module and having a small bootstrap script
  that imports that module. It is also possible to
  name a .pyc or .pyo file directly on the
  command line.
Some Tips for Experts
• It is possible to have a file called spam.pyc (or
  spam.pyo when -O is used) without a file
  spam.py for the same module.
• This can be used to distribute a library of
  Python code in a form that is moderately hard
  to reverse engineer.
Some Tips for Experts
• The module compileall can create .pyc files (or
  .pyo files when -O is used) for all modules in a
  directory.
Standard Modules
Dir()
• The built-in function dir() is used to find out
  which names a module defines. It returns a
  sorted list of strings.
Packages
• Packages are a way of structuring Python’s module
  namespace by using “dotted module names”.
• For example, the module name A.B designates a
  submodule named B in a package named A.
• Just like the use of modules saves the authors of
  different modules from having to worry about each
  other’s global variable names, the use of dotted
  module names saves the authors of multi-module
  packages like NumPy or the Python Imaging Library
  from having to worry about each other’s module
  names.
Input and Output
• There are several ways to present the output
  of a program; data can be printed in a human-
  readable form, or written to a file for future
  use. This chapter will discuss some of the
  possibilities.
• How do you convert values to strings?
Representation
• The str() function is meant to return
  representations of values which are fairly
  human-readable.
• repr() is meant to generate representations
  which can be read by the interpreter (or will
  force a SyntaxError if there is not equivalent
  syntax).
Str() and Repr()
Formatting Output
Formatting Output
I don’t think so!

MORE ON STRING FORMATTING?
Reading and Writing Files
• Open() returns a file object
Reading and Writing Files
Alternative Method
Write to File
Close
Pickle Module
Errors and Exceptions
• Syntax Errors
Exceptions
Handling Exceptions
Else?!
Another Exception
Raise Exception
Catching it!
User Defined Exception
Clean Up Actions
Classes
Class Definition Syntax
Class Objects
Inheritance
Multiple Inheritance
Iterators
Style of Access
Make your Class Iterable
Len()
• Implement __len__
Unit Testing
• Hopefully!

Más contenido relacionado

La actualidad más candente

Python programming introduction
Python programming introductionPython programming introduction
Python programming introductionSiddique Ibrahim
 
Python presentation by Monu Sharma
Python presentation by Monu SharmaPython presentation by Monu Sharma
Python presentation by Monu SharmaMayank Sharma
 
Presentation on python
Presentation on pythonPresentation on python
Presentation on pythonwilliam john
 
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Python Tutorial | Python Tutorial for Beginners | Python Training | EdurekaPython Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Python Tutorial | Python Tutorial for Beginners | Python Training | EdurekaEdureka!
 
Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming pptismailmrribi
 
Fundamentals of Python Programming
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python ProgrammingKamal Acharya
 
Python quick guide1
Python quick guide1Python quick guide1
Python quick guide1Kanchilug
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythonAgung Wahyudi
 
Variables in python
Variables in pythonVariables in python
Variables in pythonJaya Kumari
 

La actualidad más candente (20)

Intro to Python
Intro to PythonIntro to Python
Intro to Python
 
Python programming introduction
Python programming introductionPython programming introduction
Python programming introduction
 
Python programming
Python  programmingPython  programming
Python programming
 
Python Presentation
Python PresentationPython Presentation
Python Presentation
 
Python presentation by Monu Sharma
Python presentation by Monu SharmaPython presentation by Monu Sharma
Python presentation by Monu Sharma
 
Presentation on python
Presentation on pythonPresentation on python
Presentation on python
 
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Python Tutorial | Python Tutorial for Beginners | Python Training | EdurekaPython Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
 
Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming ppt
 
Fundamentals of Python Programming
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python Programming
 
Python Basics
Python BasicsPython Basics
Python Basics
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
Python ppt
Python pptPython ppt
Python ppt
 
Python training
Python trainingPython training
Python training
 
Basics of python
Basics of pythonBasics of python
Basics of python
 
Python quick guide1
Python quick guide1Python quick guide1
Python quick guide1
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python Intro
Python IntroPython Intro
Python Intro
 
Python
PythonPython
Python
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
Python ppt
Python pptPython ppt
Python ppt
 

Destacado

Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Pedro Rodrigues
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)Pedro Rodrigues
 
Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Pedro Rodrigues
 
Python Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List ComprehensionsPython Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List ComprehensionsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and IndentationPython Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and IndentationP3 InfoTech Solutions Pvt. Ltd.
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basicsLuigi De Russis
 
Python教程 / Python tutorial
Python教程 / Python tutorialPython教程 / Python tutorial
Python教程 / Python tutorialee0703
 
AmI 2016 - Python basics
AmI 2016 - Python basicsAmI 2016 - Python basics
AmI 2016 - Python basicsLuigi De Russis
 
Blisstering drupal module development ppt v1.2
Blisstering drupal module development ppt v1.2Blisstering drupal module development ppt v1.2
Blisstering drupal module development ppt v1.2Anil Sagar
 
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...Alessandro Molina
 
FLTK Summer Course - Part I - First Impact - Exercises
FLTK Summer Course - Part I - First Impact - ExercisesFLTK Summer Course - Part I - First Impact - Exercises
FLTK Summer Course - Part I - First Impact - ExercisesMichel Alves
 
FLTK Summer Course - Part VIII - Eighth Impact
FLTK Summer Course - Part VIII - Eighth ImpactFLTK Summer Course - Part VIII - Eighth Impact
FLTK Summer Course - Part VIII - Eighth ImpactMichel Alves
 

Destacado (20)

Python tutorial
Python tutorialPython tutorial
Python tutorial
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
 
Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)
 
Python Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List ComprehensionsPython Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List Comprehensions
 
Python - basics
Python - basicsPython - basics
Python - basics
 
PythonIntro
PythonIntroPythonIntro
PythonIntro
 
Python Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and IndentationPython Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and Indentation
 
Python Tutorial
Python TutorialPython Tutorial
Python Tutorial
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basics
 
Python教程 / Python tutorial
Python教程 / Python tutorialPython教程 / Python tutorial
Python教程 / Python tutorial
 
Python Workshop
Python WorkshopPython Workshop
Python Workshop
 
python codes
python codespython codes
python codes
 
Python basics
Python basicsPython basics
Python basics
 
AmI 2016 - Python basics
AmI 2016 - Python basicsAmI 2016 - Python basics
AmI 2016 - Python basics
 
Blisstering drupal module development ppt v1.2
Blisstering drupal module development ppt v1.2Blisstering drupal module development ppt v1.2
Blisstering drupal module development ppt v1.2
 
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
 
FLTK Summer Course - Part I - First Impact - Exercises
FLTK Summer Course - Part I - First Impact - ExercisesFLTK Summer Course - Part I - First Impact - Exercises
FLTK Summer Course - Part I - First Impact - Exercises
 
Advanced Git
Advanced GitAdvanced Git
Advanced Git
 
FLTK Summer Course - Part VIII - Eighth Impact
FLTK Summer Course - Part VIII - Eighth ImpactFLTK Summer Course - Part VIII - Eighth Impact
FLTK Summer Course - Part VIII - Eighth Impact
 

Similar a Python Tutorial Part 2

PyCourse - Self driving python course
PyCourse - Self driving python coursePyCourse - Self driving python course
PyCourse - Self driving python courseEran Shlomo
 
20120314 changa-python-workshop
20120314 changa-python-workshop20120314 changa-python-workshop
20120314 changa-python-workshopamptiny
 
Modules and Packages in Python Programming Language.pptx
Modules and Packages in Python Programming Language.pptxModules and Packages in Python Programming Language.pptx
Modules and Packages in Python Programming Language.pptxarunavamukherjee9999
 
Introduction to Python and Django
Introduction to Python and DjangoIntroduction to Python and Django
Introduction to Python and Djangosolutionstreet
 
Python-Yesterday Today Tomorrow(What's new?)
Python-Yesterday Today Tomorrow(What's new?)Python-Yesterday Today Tomorrow(What's new?)
Python-Yesterday Today Tomorrow(What's new?)Mohan Arumugam
 
Interesting Presentation on Python Modules and packages
Interesting Presentation on Python Modules and packagesInteresting Presentation on Python Modules and packages
Interesting Presentation on Python Modules and packagesarunavamukherjee9999
 
Introduction to Python for Security Professionals
Introduction to Python for Security ProfessionalsIntroduction to Python for Security Professionals
Introduction to Python for Security ProfessionalsAndrew McNicol
 
Python Programming1.ppt
Python Programming1.pptPython Programming1.ppt
Python Programming1.pptRehnawilson1
 
Python Programming for ArcGIS: Part I
Python Programming for ArcGIS: Part IPython Programming for ArcGIS: Part I
Python Programming for ArcGIS: Part IDUSPviz
 
How to write a well-behaved Python command line application
How to write a well-behaved Python command line applicationHow to write a well-behaved Python command line application
How to write a well-behaved Python command line applicationgjcross
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythonMohammed Rafi
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unitmichaelaaron25322
 
web programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Malothweb programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh MalothBhavsingh Maloth
 
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 to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python ProgrammingAkhil Kaushik
 

Similar a Python Tutorial Part 2 (20)

PyCourse - Self driving python course
PyCourse - Self driving python coursePyCourse - Self driving python course
PyCourse - Self driving python course
 
Python libraries
Python librariesPython libraries
Python libraries
 
20120314 changa-python-workshop
20120314 changa-python-workshop20120314 changa-python-workshop
20120314 changa-python-workshop
 
Modules and Packages in Python Programming Language.pptx
Modules and Packages in Python Programming Language.pptxModules and Packages in Python Programming Language.pptx
Modules and Packages in Python Programming Language.pptx
 
Basics of python
Basics of pythonBasics of python
Basics of python
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Introduction to Python and Django
Introduction to Python and DjangoIntroduction to Python and Django
Introduction to Python and Django
 
Python-Yesterday Today Tomorrow(What's new?)
Python-Yesterday Today Tomorrow(What's new?)Python-Yesterday Today Tomorrow(What's new?)
Python-Yesterday Today Tomorrow(What's new?)
 
Interesting Presentation on Python Modules and packages
Interesting Presentation on Python Modules and packagesInteresting Presentation on Python Modules and packages
Interesting Presentation on Python Modules and packages
 
Introduction to Python for Security Professionals
Introduction to Python for Security ProfessionalsIntroduction to Python for Security Professionals
Introduction to Python for Security Professionals
 
Python Programming1.ppt
Python Programming1.pptPython Programming1.ppt
Python Programming1.ppt
 
C- language Lecture 8
C- language Lecture 8C- language Lecture 8
C- language Lecture 8
 
Python Programming for ArcGIS: Part I
Python Programming for ArcGIS: Part IPython Programming for ArcGIS: Part I
Python Programming for ArcGIS: Part I
 
How to write a well-behaved Python command line application
How to write a well-behaved Python command line applicationHow to write a well-behaved Python command line application
How to write a well-behaved Python command line application
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
 
web programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Malothweb programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Maloth
 
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
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
Python for Delphi Developers - Part 2
Python for Delphi Developers - Part 2Python for Delphi Developers - Part 2
Python for Delphi Developers - Part 2
 

Más de Haitham El-Ghareeb (20)

مختصر وحدة التعلم الذاتي 2015
مختصر وحدة التعلم الذاتي 2015مختصر وحدة التعلم الذاتي 2015
مختصر وحدة التعلم الذاتي 2015
 
وحدة التعلم الذاتي 2015
وحدة التعلم الذاتي 2015وحدة التعلم الذاتي 2015
وحدة التعلم الذاتي 2015
 
NoSQL Databases, Not just a Buzzword
NoSQL Databases, Not just a Buzzword NoSQL Databases, Not just a Buzzword
NoSQL Databases, Not just a Buzzword
 
EMC Academic Alliance Presentation
EMC Academic Alliance PresentationEMC Academic Alliance Presentation
EMC Academic Alliance Presentation
 
DSA - 2012 - Conclusion
DSA - 2012 - ConclusionDSA - 2012 - Conclusion
DSA - 2012 - Conclusion
 
Lecture 9 - DSA - Python Data Structures
Lecture 9 - DSA - Python Data StructuresLecture 9 - DSA - Python Data Structures
Lecture 9 - DSA - Python Data Structures
 
Data Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesData Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study Notes
 
Lect07
Lect07Lect07
Lect07
 
Lecture 07 Data Structures - Basic Sorting
Lecture 07 Data Structures - Basic SortingLecture 07 Data Structures - Basic Sorting
Lecture 07 Data Structures - Basic Sorting
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
LectureNotes-05-DSA
LectureNotes-05-DSALectureNotes-05-DSA
LectureNotes-05-DSA
 
LectureNotes-04-DSA
LectureNotes-04-DSALectureNotes-04-DSA
LectureNotes-04-DSA
 
LectureNotes-03-DSA
LectureNotes-03-DSALectureNotes-03-DSA
LectureNotes-03-DSA
 
LectureNotes-02-DSA
LectureNotes-02-DSALectureNotes-02-DSA
LectureNotes-02-DSA
 
LectureNotes-01-DSA
LectureNotes-01-DSALectureNotes-01-DSA
LectureNotes-01-DSA
 
Lecture-05-DSA
Lecture-05-DSALecture-05-DSA
Lecture-05-DSA
 
Learn Latex
Learn LatexLearn Latex
Learn Latex
 
Research Methodologies - Lecture 02
Research Methodologies - Lecture 02Research Methodologies - Lecture 02
Research Methodologies - Lecture 02
 
DSA-Lecture-05
DSA-Lecture-05DSA-Lecture-05
DSA-Lecture-05
 
DSA - Lecture 04
DSA - Lecture 04DSA - Lecture 04
DSA - Lecture 04
 

Último

Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 

Último (20)

Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 

Python Tutorial Part 2

  • 1. Python Tutorial Haitham El-Ghareeb, Ph.D. May, 2012 Twitter: @helghareeb
  • 2. Modules • A module is a file containing Python definitions and statements. • The file name is the module name with the suffix .py appended. • Within a module, the module’s name (as a string) is available as the value of the global variable __name__.
  • 3.
  • 4.
  • 6.
  • 7. Compiled Python Files • As an important speed-up of the start-up time for short programs that use a lot of standard modules, • if a file called spam.pyc exists in the directory where spam.py is found, this is assumed to contain an already-“byte-compiled” version of the module spam. • The modification time of the version of spam.py used to create spam.pyc is recorded in spam.pyc, and the .pyc file is ignored if these don’t match.
  • 8. Compiled Python Files • Normally, you don’t need to do anything to create the spam.pyc file. • Whenever spam.py is successfully compiled, an attempt is made to write the compiled version to spam.pyc. • It is not an error if this attempt fails; if for any reason the file is not written completely, the resulting spam.pyc file will be recognized as invalid and thus ignored later. • The contents of the spam.pyc file are platform independent, so a Python module directory can be shared by machines of different architectures.
  • 9. Some Tips for Experts • When the Python interpreter is invoked with the -O flag, optimized code is generated and stored in .pyo files. • The optimizer currently doesn’t help much; it only removes assert statements. • When -O is used, all bytecode is optimized; .pyc files are ignored and .py files are compiled to optimized bytecode.
  • 10. Some Tips for Experts • Passing two -O flags to the Python interpreter (- OO) will cause the bytecode compiler to perform optimizations that could in some rare cases result in malfunctioning programs. • Currently only __doc__ strings are removed from the bytecode, resulting in more compact .pyo files. • Since some programs may rely on having these available, you should only use this option if you know what you’re doing.
  • 11. Some Tips for Experts • A program doesn’t run any faster when it is read from a .pyc or .pyo file than when it is read from a .py file; the only thing that’s faster about .pyc or .pyo files is the speed with which they are loaded.
  • 12. Some Tips for Experts • When a script is run by giving its name on the command line, the bytecode for the script is never written to a .pyc or .pyo file. • Thus, the startup time of a script may be reduced by moving most of its code to a module and having a small bootstrap script that imports that module. It is also possible to name a .pyc or .pyo file directly on the command line.
  • 13. Some Tips for Experts • It is possible to have a file called spam.pyc (or spam.pyo when -O is used) without a file spam.py for the same module. • This can be used to distribute a library of Python code in a form that is moderately hard to reverse engineer.
  • 14. Some Tips for Experts • The module compileall can create .pyc files (or .pyo files when -O is used) for all modules in a directory.
  • 16. Dir() • The built-in function dir() is used to find out which names a module defines. It returns a sorted list of strings.
  • 17. Packages • Packages are a way of structuring Python’s module namespace by using “dotted module names”. • For example, the module name A.B designates a submodule named B in a package named A. • Just like the use of modules saves the authors of different modules from having to worry about each other’s global variable names, the use of dotted module names saves the authors of multi-module packages like NumPy or the Python Imaging Library from having to worry about each other’s module names.
  • 18.
  • 19. Input and Output • There are several ways to present the output of a program; data can be printed in a human- readable form, or written to a file for future use. This chapter will discuss some of the possibilities. • How do you convert values to strings?
  • 20. Representation • The str() function is meant to return representations of values which are fairly human-readable. • repr() is meant to generate representations which can be read by the interpreter (or will force a SyntaxError if there is not equivalent syntax).
  • 24. I don’t think so! MORE ON STRING FORMATTING?
  • 25. Reading and Writing Files • Open() returns a file object
  • 29. Close
  • 31. Errors and Exceptions • Syntax Errors
  • 45.
  • 48. Make your Class Iterable

Notas del editor

  1. This does not enter the names of the functions defined in fibo directly in the current symbol table; it only enters the module name fibo there. Using the module name you can access the functions
  2. This imports all names except those beginning with an underscore (_).Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.
  3. Without arguments, dir() lists the names you have defined currently
  4. The __init__.py files are required to make Python treat the directories as containing packages
  5. Luckily, Python has ways to convert any value to a string: pass it to the repr() or str() functions.