1 Python K. Naik, M. Raju and S. Bhatkar December 3, 2002 CMSC 631.

Slides:



Advertisements
Similar presentations
Why I Invented Python EuroPython – June 27, 2005 Guido van Rossum Elemental Security, Inc.
Advertisements

Python Whats in a name? Snake logos and mascot notwithstanding, its named after Monty Pythons Flying Circus Humor-impaired can safely.
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Introduction to Python Week 15. Try It Out! Download Python from Any version will do for this class – By and large they are all mutually.
Programming Languages and Paradigms
Java Script Session1 INTRODUCTION.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
Introduction to Python. Python is a high-level programming language Open source and community driven “Batteries Included” – a standard distribution includes.
What is a scripting language? What is Python?
Python: a modern hybrid A language for scripting and prototyping Balance between extensibility and powerful built-in data structures genealogy: –Setl (NYU,
Python By Steve Wright. What is Python? Simple, powerful, GP scripting language Simple, powerful, GP scripting language Object oriented Object oriented.
Introduction to Python. Outline Python IS ……. History Installation Data Structures Flow of Control Functions Modules References.
Scripting Languages CS351 – Programming Paradigms.
Squirrel Programming Language By Nandini Bhatta CS537 Summer 2008.
1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.
Python.
Programming 101 with Python: an open-source, cross-platform, and fun language By J. Burton Browning, Ed.D. Copyright © J. Burton Browning All rights reserved.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 35 – Python Outline 35.1 Introduction First Python Program Python Keywords 35.2 Basic.
Introduction to Python Guido van Rossum Director of PythonLabs at Zope Corporation
Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - Extended.
And PyLNP for the RCX By: Eric Cranmer and Nick Blake.
Introduction to Python John Reiser May 5 th, 2010.
Session 2 Wharton Summer Tech Camp 1: Basic Python 2: Start Regex.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Python 101 Dr. Bernard Chen University of Central Arkansas IT Academic.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Computer Science 111 Fundamentals of Programming I Overview of Programming.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
1 Python CIS*2450 Advanced Programming Concepts Material for this lecture was developed by Dr. D. Calvert.
CS1022 Computer Programming & Principles Lecture 1.2 A brief introduction to Python.
Python Modules An Introduction. Introduction A module is a file containing Python definitions and statements. The file name is the module name with the.
The Python Programming Language Jeff Myers Programming Language Concepts, 01/14/2002
Chapter 4 Numbers. Python Program Structure Python programs consist of: Modules Statements Expressions Objects.
August 29, 2005ICP: Chapter 1: Introduction to Python Programming 1 Introduction to Computer Programming Chapter 1: Introduction to Python Programming.
Introduction to Python I CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth January 2005.
CIT 590 Intro to Programming First lecture on Java.
An Introduction to Python Blake Brogdon. What is Python?  Python is an interpreted, interactive, object-oriented programming language. (from python.org)
Guide to Programming with Python Chapter One Getting Started: The Game Over Program.
Chapter Function Basics CSC1310 Fall Function function (subroutine, procedure)a set of statements different inputs (parameters) outputs In.
Built-in Data Structures in Python An Introduction.
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
Introduction to Information Security Python. Python motivation Python is to a Hacker what Matlab is to an engineer Lots of built-in modules Lots of 3.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
An Introduction. What is Python? Interpreted language Created by Guido Van Rossum – early 90s Named after Monty Python
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
RUBY by Ryan Chase.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
PHP vs. Python. Similarities are interpreted, high level languages with dynamic typing are Open Source are supported by large developer communities are.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
Introduction to Python Developed by Dutch programmer Guido van Rossum Named after Monty Python Open source development project Simple, readable language.
Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - Extended.
Presented By P.SRIVIDYA 085D1A0552 Programming Language.
Fundamentals of Programming I Overview of Programming
CST 1101 Problem Solving Using Computers
CS1022 Computer Programming & Principles
Introduction to Python
Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.
Intro To Pete Alonzi University of Virginia Library
Principles of Computing – UFCFA3-30-1
What Is a Program? A program is like an algorithm, but describes a process that is ready or can be made ready to run on a real computer Retrieved from:
Introduction to Python
Introduction to Python
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

1 Python K. Naik, M. Raju and S. Bhatkar December 3, 2002 CMSC 631

2 Outline  Introduction  Installation and Use  Distinct Features  Python basics  A detail example  Comparison with other languages  Areas of application  References

3 Introduction  What is Python? InterpretedInterpreted InteractiveInteractive PortablePortable Object-Oriented programming languageObject-Oriented programming language

4 Introduction  A brief History Invented in 1990 by Guido Van RossumInvented in 1990 by Guido Van Rossum The name PythonThe name Python Intended to be a scripting language on Amoeba OSIntended to be a scripting language on Amoeba OS Python was influenced by ABC and Modula-3Python was influenced by ABC and Modula-3 First public release was in 1991First public release was in 1991

5 Introduction  Goals Designed to be simple yet powerfulDesigned to be simple yet powerful Allow modular programmingAllow modular programming Great emphasis on readabilityGreat emphasis on readability Rapid application developmentRapid application development Easy to embed in and extend with other languagesEasy to embed in and extend with other languages

6 Installation and Use  Freely available at  Download the appropriate installation for your computer  Can be used in both interactive and batch mode  IDLE is the editor for writing and running python programs

7 Distinct features  Extensible (c, c++, fortran, java)  Embeddable in applications  Object Oriented without being Object- centric  Rapid Prototyping  Great for readability  White space is significant  Low maintenance costs  Exception handling  Free (open source)

8 Python Basics In-built data structures  Numbers decimal e.g. 631, 3.14decimal e.g. 631, 3.14 octal e.g. O631octal e.g. O631 hexadecimale.g. oxABChexadecimale.g. oxABC complexe.g jcomplexe.g j longe.g Llonge.g L Normal Arithmetic and Bit operatorsNormal Arithmetic and Bit operators Integer division truncates e.g. ½ = 0Integer division truncates e.g. ½ = 0

9 Python Basics  Strings ConcatenationConcatenation  “Hello” + “World”-> “HelloWorld” RepetitionRepetition  “UMBC” * 3-> “UMBCUMBCUMBC” IndexingIndexing  “UMBC”[0]-> “U” SlicingSlicing  “UMBC”[1:3]-> “MB” SizeSize  len(“UMBC”)-> 4

10 Python Basics ComparisonComparison  “UMBC” 0 SearchSearch  “M” in “UMBC”-> 1 Can also be enclosed in single quotesCan also be enclosed in single quotes  e.g. ‘UMBC’

11 Python Basic  Lists e.g.e.g. aList = [631, “Programming languages”,[331, “programming languages”]] List items need not have the same typeList items need not have the same type Flexible arrays not Lisp-like linked listFlexible arrays not Lisp-like linked list Same operators as for stringsSame operators as for strings More operations append(), insert(), pop(), reverse() and sort()More operations append(), insert(), pop(), reverse() and sort()

12 Python Basics  Tuples E.g.E.g. aTuple = (631, “Programming Languages”,611, “Computer Architecture”) Nesting is PossibleNesting is Possible Outer Parenthesis is optionalOuter Parenthesis is optional Unlike Lists and like strings tuples are immutableUnlike Lists and like strings tuples are immutable

13 Python Basics  Dictionaries E.g.E.g. Map = {“Guido”: “Python”, “Ullman”: “ML”} InsertMap[“Ritchie”] = “C”InsertMap[“Ritchie”] = “C” Lookup Map[“Guido”]Lookup Map[“Guido”] Delete del Map[“Ullman”]Delete del Map[“Ullman”] Iterations keys() values() items()Iterations keys() values() items() Presence has_key(“Guido”)Presence has_key(“Guido”) Values could be anythingValues could be anything Keys must be immutableKeys must be immutable

14 Python Basics  Variables No Need to declareNo Need to declare Not typedNot typed E.g. F = 2 * 4.5 E.g. F = 2 * 4.5 Need to initializeNeed to initialize Everything is a variableEverything is a variable (functions, modules, classes)

15 Python Basics  References a = b does not make copy of ba = b does not make copy of b b = a, a and b refer to the same objectb = a, a and b refer to the same objectE.g. >>> a = [1,2,3] >>> b = a >>> a.append(4) >>> print b [1, 2, 3, 4]

16 Python Basics  Flow Control if condition : statementsif condition : statements [elif condition : statement] [else : statement] while condition : statementswhile condition : statements for var in sequence : statementsfor var in sequence : statements breakbreak continuecontinue

17 Python Basics  An Example (Fibonacci series ) >>> a = 0 >>> b = 1 >>> while b >> while b < 1000 …print b …a, b = b, a + b

18 Python Basics  Functions and Procedures General FormGeneral Form def(arg1, arg2, …) Statements return# from procedureOR return expression# from function e.g. >>> def fib(n): # write Fibonacci series up to n>>> def fib(n): # write Fibonacci series up to n... """Print a Fibonacci series up to n."""... """Print a Fibonacci series up to n."""... a, b = 0, 1... a, b = 0, 1... while b < n:... while b < n:... print b,... print b,... a, b = b, a+b... a, b = b, a+b......

19 Python Basics  Modules A module is a file containing Python definitions and statementsA module is a file containing Python definitions and statements File should have suffix.pyFile should have suffix.py Within a module, the module’s name is available as through global variable _name_.Within a module, the module’s name is available as through global variable _name_. Use “import module-name” to import the functions in this moduleUse “import module-name” to import the functions in this module It is not required to place all import statements at the beginning of a moduleIt is not required to place all import statements at the beginning of a module Some modules are built-in e.g. sysSome modules are built-in e.g. sys

20 Python Basics  Packages Structure Python’s module namespace using dotted module namesStructure Python’s module namespace using dotted module names E.g. A.B.C refers to the submodule C of module B in package AE.g. A.B.C refers to the submodule C of module B in package A To import module C ->To import module C ->  “import A.B.C” and use the fully qualified name OR  “from A.B import C” and use only the module name Subpackages need to use fully qualified names to refer to each otherSubpackages need to use fully qualified names to refer to each other

21 Python Basics  Classes E.g. class ClassName: statements OR statements OR class ClassName(BaseClass1, BaseClass2…) statements  Objects x = ClassName() creates a new instance of class ClassName and assigns it to the variable x

22 Python Basics  An Example class stack: “A well known data structure.” def __init__(self) :#constructor self.items = [] def push(self, x) : self.items.append(x) def pop(self) : x = self.items[-1] del self.items[-1] return x def empty(self) return len(self.items) == 0

23 Python Basics  Exceptions E.g.try: Print 1/x except ZeroDivisionError, message: print “Can’t divide by zero” print message f = open(file) try:process_file(f) finally : f.close() print “OK”

24 Python Basics  Raising Exceptions Raise ZeroDivisionExceptionRaise ZeroDivisionException Raise ZeroDivisionException(“can’t divide by zero”)Raise ZeroDivisionException(“can’t divide by zero”) Raise ZeroDivisionException, “can’t divide by zero”Raise ZeroDivisionException, “can’t divide by zero” Python allows user-defined exceptionsPython allows user-defined exceptions

25 Example  Example def binarySearch(data, item): min = 0; max = len(data) - 1 min = 0; max = len(data) - 1 while 1: while 1: if max < min: if max < min: return -1 return -1 m = (min + max) / 2 m = (min + max) / 2

26 Example if data[m] < item: min = m + 1 min = m + 1 elif data[m] > item: max = m - 1 max = m - 1else: return m return m

27 Comparisons  Vs perl Easier to learnEasier to learn More readableMore readable Fewer side effectsFewer side effects Less Unix biasLess Unix bias  Vs Tcl Much fasterMuch faster Less need for C extensionsLess need for C extensions Better java integrationBetter java integration

28 Comparison  Vs java More concise codeMore concise code Dynamic typingDynamic typing Runs slower but development is fastRuns slower but development is fast No compilationNo compilation Can be integrated with java using JPythonCan be integrated with java using JPython

29 Areas of application  As a glue language  For developing graphical applications  For writing Internet protocol applications  Database applications  Web scripting applications  Multimedia applications

30 References  Python Homepage  Python Tutorial  Python documentation