SlideShare una empresa de Scribd logo
1 de 76
Python Tutorial

Haitham El-Ghareeb, Ph.D.
        May 2012
  Twitter: @helghareeb
Session One - Agenda
•   Whetting Your Appetite
•   Using Python Interpreter
•   Informal Introduction to Python
•   More Control Flows
Session Two - Agenda
•   Data Structures
•   Modules
•   Input and Output
•   Errors and Exceptions
•   Classes
Bonus Session - Agenda
• Unit Testing
  – Hopefully!
Python
• easy to learn, powerful programming
  language.
• has efficient high-level data structures and a
  simple but effective approach to object-
  oriented programming.
  – elegant syntax
  – dynamic typing
  – interpreted nature,
Whetting Your Appetite
• You could write a Unix shell script or Windows
  batch files for some tasks, but:
  – Shell scripts are best at moving around files and
    changing text data, not well-suited for GUI
    applications or games.
• You could write a C/C++/Java program, but:
  – It can take a lot of development time to get even a
    first-draft program.
Python
•   Simpler to use
•   Available on Windows, Mac OS X, and Unix
•   Help you get the job done more quickly
•   Split your program into modules that can be reused
• Python is an interpreted language:
    – Save you considerable time during program
      development because no compilation and linking
      is necessary.
    – Interpreter can be used interactively
Python
• Python is extensible: if you know how to
  program in C it is easy to add a new built-in
  function or module to the interpreter,
• Named after the BBC show “Monty Python’s
  Flying Circus” and has nothing to do with
  reptiles.
Using the Python Interpreter
• Python interpreter is usually installed as
  /usr/local/bin/python
• Invoking the Interpreter
  – A second way of starting the interpreter is python
    -c command [arg] ..., which executes the
    statement(s) in command
  – Some Python modules are also useful as scripts.
    These can be invoked using python -m module
    [arg] ..., which executes the source file for module
Using Python Interpreter
• Argument Passing
• Interactive Mode




• Error Handling
Executable Python Scripts
Source Code Encoding
• It is possible to use encodings different than
  ASCII in Python source files.
• The best way to do it is to put one more
  special comment line right after the #! line to
  define the source file encoding:
Informal Introduction
Using Python as a Calculator
Numbers
Strings
Strings
String Concatenation
String Indexing
String Slicing
Strings are Immutable!
Indices May be Negative
How Come?!
Lists
• compound data type
• used to group together other values
• The most versatile
• can be written as a list of comma-separated
  values (items) between square brackets.
• List items need not all have the same type.
Shallow Copy
First Steps toward Programming
More Control Flow Tools
If Statement
For Statement
For Statement with Shallow Copy
Range Function
Break, Continue, Else
Pass
• Does Nothing!
Functions
Return Statement
Default Argument Value
Keyword Arguments
Keyword Arguments
• When a final formal parameter of the form
  **name is present, it receives a dictionary
  containing all keyword arguments except for
  those corresponding to a formal parameter.
• This may be combined with a formal
  parameter of the form *name which receives
  a tuple containing the positional arguments
  beyond the formal parameter list.
• (*name must occur before **name.)
Declaration
Calling
Output
Lambda Strings
Documentation String
Coding Style - PEP8
• Use 4-space indentation, and no tabs.
• 4 spaces are a good compromise between small
  indentation (allows greater nesting depth) and large
  indentation (easier to read). Tabs introduce
  confusion, and are best left out.
• Wrap lines so that they don’t exceed 79 characters.
• This helps users with small displays and makes it
  possible to have several code files side-by-side on
  larger displays.
• Use blank lines to separate functions and classes, and
  larger blocks of code inside functions.
Coding Style - PEP8
• When possible, put comments on a line of their own.
• Use docstrings.
• Use spaces around operators and after commas, but not
  directly inside bracketing constructs: a = f(1, 2) + g(3, 4).
• Name your classes and functions consistently; the
  convention is to use CamelCase for classes and
  lower_case_with_underscores for functions and methods.
  Always use self as the name for the first method argument.
• Don’t use fancy encodings if your code is meant to be used
  in international environments. Plain ASCII works best in any
  case.
Data Structures - Lists
• list.append(x) Add an item to the end of the list.
• list.extend(L) Extend the list by appending all the
  items in the given list.
• list.insert(i, x) Insert an item at a given position.
  The first argument is the index of the element
  before which to insert, so a.insert(0, x) inserts at
  the front of the list, and a.insert(len(a), x) is
  equivalent to a.append(x).
• list.remove(x) Remove the first item from the list
  whose value is x. It is an error if there is no such
  item.
Data Structures - Lists
• list.pop([i]) Remove the item at the given position in
  the list, and return it. If no index is specified, a.pop()
  removes and returns the last item in the list.
• list.index(x) Return the index in the list of the first item
  whose value is x. It is an error if there is no such item.
• list.count(x) Return the number of times x appears in
  the list.
• list.sort() Sort the items of the list, in place.
• list.reverse() Reverse the elements of the list, in place.
Lists Example
Using Lists as Stack
Using List as a Queue
Functional Programming Tools
• There are three built-in functions that are very
  useful when used with lists: filter(), map(), and
  reduce().
Filter()
• filter(function, sequence) returns a sequence
  consisting of those items from the sequence
  for which function(item) is true.
• If sequence is a string or tuple, the result will
  be of the same type; otherwise, it is always a
  list.
• For example, to compute a sequence of
  numbers not divisible by 2 and 3:
Filter()
Map()
• map(function, sequence) calls function(item)
  for each of the sequence’s items and returns a
  list of the return values.
• For example, to compute some cubes:
Map()
• More than one sequence may be passed; the
  function must then have as many arguments
  as there are sequences and is called with the
  corresponding item from each sequence (or
  None if some sequence is shorter than
  another). For example:
Reduce()
• reduce(function, sequence) returns a single
  value constructed by calling the binary
  function function on the first two items of the
  sequence, then on the result and the next
  item, and so on.
• For example, to compute the sum of the
  numbers 1 through 10:
List Comprehension
• List comprehensions provide a concise way to
  create lists.
• Common applications are to make new lists
  where each element is the result of some
  operations applied to each member of
  another sequence or iterable, or to create a
  subsequence of those elements that satisfy a
  certain condition.
List of Squares
List Comprehension
• A list comprehension consists of brackets
  containing an expression followed by a for
  clause, then zero or more for or if clauses. The
  result will be a new list resulting from
  evaluating the expression in the context of the
  for and if clauses which follow it.
• For example, this listcomp combines the
  elements of two lists if they are not equal:
Del()
Tuples and Sequences
Comma at the end!
Sets
• A set is an unordered collection with no
  duplicate elements.
• Basic uses include membership testing and
  eliminating duplicate entries.
• Set objects also support mathematical
  operations like
  union, intersection, difference, and symmetric
  difference.
Dictionaries
• Dictionaries are sometimes found in other
  languages as “associative memories” or
  “associative arrays”.
• Unlike sequences, which are indexed by a range
  of numbers, dictionaries are indexed by
  keys, which can be any immutable type; strings
  and numbers can always be keys.
• Tuples can be used as keys if they contain only
  strings, numbers, or tuples; if a tuple contains any
  mutable object either directly or indirectly, it
  cannot be used as a key.
Dictionaries
• You can’t use lists as keys, since lists can be modified in
  place using index assignments, slice assignments, or
  methods like append() and extend().
• It is best to think of a dictionary as an unordered set of
  key: value pairs, with the requirement that the keys are
  unique (within one dictionary).
• A pair of braces creates an empty dictionary: {}. Placing
  a comma-separated list of key:value pairs within the
  braces adds initial key:value pairs to the dictionary; this
  is also the way dictionaries are written on output.
Dictionaries
• The main operations on a dictionary are storing a value
  with some key and extracting the value given the key.
• It is also possible to delete a key:value pair with del. If
  you store using a key that is already in use, the old
  value associated with that key is forgotten. It is an error
  to extract a value using a non-existent key.
• The keys() method of a dictionary object returns a list
  of all the keys used in the dictionary, in arbitrary order
  (if you want it sorted, just apply the sorted() function
  to it).
• To check whether a single key is in the dictionary, use
  the in keyword.
Dict() Constructor
Looping Techniques
Looping over Two Sequences
Loop in Reverse
More on Conditions
• Include and, or
Comparing Objects of Different Types

Más contenido relacionado

La actualidad más candente

Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming pptismailmrribi
 
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 Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaEdureka!
 
Python Tutorial For Beginners | Python Crash Course - Python Programming Lang...
Python Tutorial For Beginners | Python Crash Course - Python Programming Lang...Python Tutorial For Beginners | Python Crash Course - Python Programming Lang...
Python Tutorial For Beginners | Python Crash Course - Python Programming Lang...Edureka!
 
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 Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | EdurekaPython Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | EdurekaEdureka!
 
Python programming | Fundamentals of Python programming
Python programming | Fundamentals of Python programming Python programming | Fundamentals of Python programming
Python programming | Fundamentals of Python programming KrishnaMildain
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming LanguageDipankar Achinta
 
Python GUI Programming
Python GUI ProgrammingPython GUI Programming
Python GUI ProgrammingRTS Tech
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaEdureka!
 

La actualidad más candente (20)

Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming ppt
 
Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3
 
Python
PythonPython
Python
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | Edureka
 
Python
PythonPython
Python
 
Python Tutorial For Beginners | Python Crash Course - Python Programming Lang...
Python Tutorial For Beginners | Python Crash Course - Python Programming Lang...Python Tutorial For Beginners | Python Crash Course - Python Programming Lang...
Python Tutorial For Beginners | Python Crash Course - Python Programming Lang...
 
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 final ppt
Python final pptPython final ppt
Python final ppt
 
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | EdurekaPython Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
 
Python programming | Fundamentals of Python programming
Python programming | Fundamentals of Python programming Python programming | Fundamentals of Python programming
Python programming | Fundamentals of Python programming
 
Python Basics
Python BasicsPython Basics
Python Basics
 
Python basics
Python basicsPython basics
Python basics
 
Beginning Python Programming
Beginning Python ProgrammingBeginning Python Programming
Beginning Python Programming
 
Python - the basics
Python - the basicsPython - the basics
Python - the basics
 
Python cheat-sheet
Python cheat-sheetPython cheat-sheet
Python cheat-sheet
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Python GUI Programming
Python GUI ProgrammingPython GUI Programming
Python GUI Programming
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | Edureka
 
Python basic
Python basicPython basic
Python basic
 
Python ppt
Python pptPython ppt
Python ppt
 

Destacado

Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesMatt Harrison
 
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
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programmingKiran Vadakkath
 
Classification of computers
Classification of computersClassification of computers
Classification of computerssunil kumar
 
Introduction to python 3 2nd round
Introduction to python 3   2nd roundIntroduction to python 3   2nd round
Introduction to python 3 2nd roundYouhei Sakurai
 

Destacado (20)

Python Tutorial Part 2
Python Tutorial Part 2Python Tutorial Part 2
Python Tutorial Part 2
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
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
 
PythonIntro
PythonIntroPythonIntro
PythonIntro
 
Python - basics
Python - basicsPython - basics
Python - basics
 
Python Tutorial
Python TutorialPython Tutorial
Python Tutorial
 
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
 
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 Basics
Python BasicsPython Basics
Python Basics
 
python codes
python codespython codes
python codes
 
AmI 2016 - Python basics
AmI 2016 - Python basicsAmI 2016 - Python basics
AmI 2016 - Python basics
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
Python basics
Python basicsPython basics
Python basics
 
Classification of computers
Classification of computersClassification of computers
Classification of computers
 
Introduction to python 3 2nd round
Introduction to python 3   2nd roundIntroduction to python 3   2nd round
Introduction to python 3 2nd round
 

Similar a Python Tutorial Part 1

Engineering CS 5th Sem Python Module -2.pptx
Engineering CS 5th Sem Python Module -2.pptxEngineering CS 5th Sem Python Module -2.pptx
Engineering CS 5th Sem Python Module -2.pptxhardii0991
 
Python first day
Python first dayPython first day
Python first dayfarkhand
 
Module 3,4.pptx
Module 3,4.pptxModule 3,4.pptx
Module 3,4.pptxSandeepR95
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3Ahmet Bulut
 
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxQ-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxkalai75
 
Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...gjcross
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way PresentationAmira ElSharkawy
 
Python programming
Python programmingPython programming
Python programmingsirikeshava
 
MODULE-2.pptx
MODULE-2.pptxMODULE-2.pptx
MODULE-2.pptxASRPANDEY
 
Python for katana
Python for katanaPython for katana
Python for katanakedar nath
 

Similar a Python Tutorial Part 1 (20)

Engineering CS 5th Sem Python Module -2.pptx
Engineering CS 5th Sem Python Module -2.pptxEngineering CS 5th Sem Python Module -2.pptx
Engineering CS 5th Sem Python Module -2.pptx
 
Pa1 session 2
Pa1 session 2 Pa1 session 2
Pa1 session 2
 
Pa2 session 1
Pa2 session 1Pa2 session 1
Pa2 session 1
 
Python first day
Python first dayPython first day
Python first day
 
Python first day
Python first dayPython first day
Python first day
 
Module 3,4.pptx
Module 3,4.pptxModule 3,4.pptx
Module 3,4.pptx
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
 
Python-Basics.pptx
Python-Basics.pptxPython-Basics.pptx
Python-Basics.pptx
 
Python Demo.pptx
Python Demo.pptxPython Demo.pptx
Python Demo.pptx
 
Python Demo.pptx
Python Demo.pptxPython Demo.pptx
Python Demo.pptx
 
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxQ-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
 
Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...
 
intro to python.pptx
intro to python.pptxintro to python.pptx
intro to python.pptx
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
 
Python programming
Python programmingPython programming
Python programming
 
MODULE-2.pptx
MODULE-2.pptxMODULE-2.pptx
MODULE-2.pptx
 
Python for katana
Python for katanaPython for katana
Python for katana
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
Python Basics
Python BasicsPython Basics
Python Basics
 

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

SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
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
 
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
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
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
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
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
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
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
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 

Último (20)

SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
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
 
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
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
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.
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
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
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
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
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 

Python Tutorial Part 1

  • 1. Python Tutorial Haitham El-Ghareeb, Ph.D. May 2012 Twitter: @helghareeb
  • 2. Session One - Agenda • Whetting Your Appetite • Using Python Interpreter • Informal Introduction to Python • More Control Flows
  • 3. Session Two - Agenda • Data Structures • Modules • Input and Output • Errors and Exceptions • Classes
  • 4. Bonus Session - Agenda • Unit Testing – Hopefully!
  • 5. Python • easy to learn, powerful programming language. • has efficient high-level data structures and a simple but effective approach to object- oriented programming. – elegant syntax – dynamic typing – interpreted nature,
  • 6. Whetting Your Appetite • You could write a Unix shell script or Windows batch files for some tasks, but: – Shell scripts are best at moving around files and changing text data, not well-suited for GUI applications or games. • You could write a C/C++/Java program, but: – It can take a lot of development time to get even a first-draft program.
  • 7. Python • Simpler to use • Available on Windows, Mac OS X, and Unix • Help you get the job done more quickly • Split your program into modules that can be reused • Python is an interpreted language: – Save you considerable time during program development because no compilation and linking is necessary. – Interpreter can be used interactively
  • 8. Python • Python is extensible: if you know how to program in C it is easy to add a new built-in function or module to the interpreter, • Named after the BBC show “Monty Python’s Flying Circus” and has nothing to do with reptiles.
  • 9. Using the Python Interpreter • Python interpreter is usually installed as /usr/local/bin/python • Invoking the Interpreter – A second way of starting the interpreter is python -c command [arg] ..., which executes the statement(s) in command – Some Python modules are also useful as scripts. These can be invoked using python -m module [arg] ..., which executes the source file for module
  • 10. Using Python Interpreter • Argument Passing • Interactive Mode • Error Handling
  • 12. Source Code Encoding • It is possible to use encodings different than ASCII in Python source files. • The best way to do it is to put one more special comment line right after the #! line to define the source file encoding:
  • 14. Using Python as a Calculator
  • 22. Indices May be Negative
  • 24. Lists • compound data type • used to group together other values • The most versatile • can be written as a list of comma-separated values (items) between square brackets. • List items need not all have the same type.
  • 26. First Steps toward Programming
  • 30. For Statement with Shallow Copy
  • 38. Keyword Arguments • When a final formal parameter of the form **name is present, it receives a dictionary containing all keyword arguments except for those corresponding to a formal parameter. • This may be combined with a formal parameter of the form *name which receives a tuple containing the positional arguments beyond the formal parameter list. • (*name must occur before **name.)
  • 44. Coding Style - PEP8 • Use 4-space indentation, and no tabs. • 4 spaces are a good compromise between small indentation (allows greater nesting depth) and large indentation (easier to read). Tabs introduce confusion, and are best left out. • Wrap lines so that they don’t exceed 79 characters. • This helps users with small displays and makes it possible to have several code files side-by-side on larger displays. • Use blank lines to separate functions and classes, and larger blocks of code inside functions.
  • 45. Coding Style - PEP8 • When possible, put comments on a line of their own. • Use docstrings. • Use spaces around operators and after commas, but not directly inside bracketing constructs: a = f(1, 2) + g(3, 4). • Name your classes and functions consistently; the convention is to use CamelCase for classes and lower_case_with_underscores for functions and methods. Always use self as the name for the first method argument. • Don’t use fancy encodings if your code is meant to be used in international environments. Plain ASCII works best in any case.
  • 46. Data Structures - Lists • list.append(x) Add an item to the end of the list. • list.extend(L) Extend the list by appending all the items in the given list. • list.insert(i, x) Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x). • list.remove(x) Remove the first item from the list whose value is x. It is an error if there is no such item.
  • 47. Data Structures - Lists • list.pop([i]) Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. • list.index(x) Return the index in the list of the first item whose value is x. It is an error if there is no such item. • list.count(x) Return the number of times x appears in the list. • list.sort() Sort the items of the list, in place. • list.reverse() Reverse the elements of the list, in place.
  • 49. Using Lists as Stack
  • 50. Using List as a Queue
  • 51. Functional Programming Tools • There are three built-in functions that are very useful when used with lists: filter(), map(), and reduce().
  • 52. Filter() • filter(function, sequence) returns a sequence consisting of those items from the sequence for which function(item) is true. • If sequence is a string or tuple, the result will be of the same type; otherwise, it is always a list. • For example, to compute a sequence of numbers not divisible by 2 and 3:
  • 54. Map() • map(function, sequence) calls function(item) for each of the sequence’s items and returns a list of the return values. • For example, to compute some cubes:
  • 55. Map() • More than one sequence may be passed; the function must then have as many arguments as there are sequences and is called with the corresponding item from each sequence (or None if some sequence is shorter than another). For example:
  • 56. Reduce() • reduce(function, sequence) returns a single value constructed by calling the binary function function on the first two items of the sequence, then on the result and the next item, and so on. • For example, to compute the sum of the numbers 1 through 10:
  • 57. List Comprehension • List comprehensions provide a concise way to create lists. • Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.
  • 59. List Comprehension • A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it. • For example, this listcomp combines the elements of two lists if they are not equal:
  • 60.
  • 61.
  • 62. Del()
  • 64. Comma at the end!
  • 65. Sets • A set is an unordered collection with no duplicate elements. • Basic uses include membership testing and eliminating duplicate entries. • Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
  • 66.
  • 67. Dictionaries • Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. • Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys. • Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key.
  • 68. Dictionaries • You can’t use lists as keys, since lists can be modified in place using index assignments, slice assignments, or methods like append() and extend(). • It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary). • A pair of braces creates an empty dictionary: {}. Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs to the dictionary; this is also the way dictionaries are written on output.
  • 69. Dictionaries • The main operations on a dictionary are storing a value with some key and extracting the value given the key. • It is also possible to delete a key:value pair with del. If you store using a key that is already in use, the old value associated with that key is forgotten. It is an error to extract a value using a non-existent key. • The keys() method of a dictionary object returns a list of all the keys used in the dictionary, in arbitrary order (if you want it sorted, just apply the sorted() function to it). • To check whether a single key is in the dictionary, use the in keyword.
  • 70.
  • 73. Looping over Two Sequences
  • 75. More on Conditions • Include and, or
  • 76. Comparing Objects of Different Types

Notas del editor

  1. Interpreter can be used interactively, which makes it easy to experiment with features of the language, to write throw-away programs, or to test functions during bottom-up program development. It is also a handy desk calculator.
  2. Using r in front of string means: Raw String – string will not be processed
  3. Unlike Strings, Lists are MutableWe can Apply len() on listsIt is possible to Nest Lists
  4. The break statement, like in C, breaks out of the smallest enclosing for or while loop.The continue statement, also borrowed from C, continues with the next iteration of the loop.Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.
  5. First Block: Function DeclarationSecond Block: ValidThird Block: Non-Valid
  6. By popular demand, a few features commonly found in functional programming languages like Lisp have been added to Python. With the lambda keyword, small anonymous functions can be created. Here’s a function that returns the sum of its two arguments: lambda a, b: a+b. Lambda forms can be used wherever function objects are required. They are syntactically restricted to a single expression. Semantically, they are just syntactic sugar for a normal function definition. Like nested function definitions, lambda forms can reference variables from the containing scope:
  7. First in Last Out, The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index.
  8. It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the other elements have to be shifted by one).
  9. Pythonic StyleAnother way: squares = map(lambda x: x**2, range(10))
  10. How can this be Pythonic?
  11. We can del a
  12. A = [1,2,3]B = [4,5,6]C = zip(A,B)F,G = zip(*C) #this is unzipping
  13. Note that comparing objects of different types is legal. The outcome is deterministic but arbitrary: the types are ordered by their name. Thus, a list is always smaller than a string, a string is always smaller than a tuple, etc. [1] Mixed numeric types are compared according to their numeric value, so 0 equals 0.0, etc.