The Python Programming Language Matt Campbell | Steve Losh.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Lecture 2 Introduction to C Programming
Title An Introduction to Python. What is Python exactly? Python is a modern rapid development language. Code is very clean and easy to read. Emphasizes.
‘And Now For Something Completely Different’ Python Programming David Hartwell Clements.
Intro to Python Paul Martin. History Designed by Guido van Rossum Goal: “Combine remarkable power with very clear syntax” Very popular in science labs.
JavaScript, Third Edition
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
CSC 9010: Natural Language Processing
Python quick start guide
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
And PyLNP for the RCX By: Eric Cranmer and Nick Blake.
CIS Computer Programming Logic
Introduction to Python
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
Computer Science 101 Introduction to Programming.
An Introduction to Python Blake Brogdon. What is Python?  Python is an interpreted, interactive, object-oriented programming language. (from python.org)
Python – May 11 Briefing Course overview Introduction to the language Lab.
Programming Languages and Paradigms Imperative Programming.
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.
Python Functions.
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
An Introduction. What is Python? Interpreted language Created by Guido Van Rossum – early 90s Named after Monty Python
Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Python Let’s get started!.
By Mr. Muhammad Pervez Akhtar
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )
Introduction to Python Sajal Desai. What is Python? Versitile, simple, high level language No linking and compilation “By the way, the language is named.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Fun Fact: Python is not named for the snake Rather, it is named for Monty Python’s Flying Circus.
Python Programming Language by Vasu Chetty. Origins of Python Created by: Guido van Rossum, a Dutch Programmer Created during: Christmas Break, 1989 Created.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
Rajkumar Jayachandran.  Classes for python are not much different than those of other languages  Not much new syntax or semantics  Python classes are.
Fundamentals of Programming I Overview of Programming
Definition of the Programming Language CPRL
CS1022 Computer Programming & Principles
Ruby: An Introduction Created by Yukihiro Matsumoto in 1993 (named after his birthstone) Pure OO language (even the number 1 is an instance of a class)
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Python Let’s get started!.
Introduction to Python
CSC 458– Predictive Analytics I, Fall 2017, Intro. 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
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Arithmetic operations, decisions and looping
Introduction to Python
WEB PROGRAMMING JavaScript.
Introduction to Python
Lecture 2 Python Programming & Data Types
CSC 458– Predictive Analytics I, Fall 2018, Intro. To Python
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Lecture 2 Python Programming & Data Types
Python Primer 1: Types and Operators
Introduction to Programming with Python
Chapter 2: Introduction to C++.
12th Computer Science – Unit 5
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
Python fundamental.
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

The Python Programming Language Matt Campbell | Steve Losh

From the Creators… “The language is named after the BBC show ``Monty Python's Flying Circus'' and has nothing to do with nasty reptiles. Making references to Monty Python skits in documentation is not only allowed, it is encouraged! “

Origins Created in the early 1990s by Guido van Rossum at Stichting Mathematisch Centrum (CWI ) in the Netherlands Successor language to ABC Rossum remains the principle author of the language today

Overview of the Language Python is an interpreted language Like Scheme, it is an interactive language Very high-level data types Code is very human readable

Extensibility Python is a very extensible language You can write modules in C to link python to other binary libraries You can even link the interpreter itself into an application written in C and use python as an extension or command language for that application

Lexical Aspects Input Format: –Line oriented –White space is not ignored Comments: –Denoted by hash mark (#) to end of line Delimiters: –End of line Keywords: –Reserved Names: –Case sensitive –Variable names can consist of letters, numbers, and/or underscores –Underscores sometimes have special meaning, so their use is not highly recommended

Data Types Scalars: –Integer, Float, Boolean Aggregate Types –Complex Number, String, List, Dictionary, Tuple, File, Set Python is not strongly typed Python does not require declaration of variables before their use

Literals Integers: 2, 4, -3 Floats: 2.0e10, 3.5,.03 Boolean: True, False Strings: ‘cat’, “cat” Lists: [12, 3.4, ‘cat’, lambda x: x+3] Sets: set([12, 3.4, ‘cat’, lambda x: x+3]) Dictionaries: dict = {‘cat': 2, 6: ‘dog’} Functions: Can be mapped to names via ‘def’ and ‘lambda’ just as in Scheme. They can be returned by functions, placed in lists, etc. Files: open('/path/file', ‘r+') Null: None ‘_’: holds the most recently returned value

Variable Typing Variables in Python do not need to be declared as a specific type –Example: A, B = 3, ‘cat’ A variable’s type is dynamic, and will changed whenever it is reassigned –Example: a, b = 1, ‘cat’ a, b =.3, lambda x: x*x No such thing as “const” in Python

Quick & Dirty Input >>> x = int(raw_input("Please enter an integer: "))

Slicing Aggregate slicing syntax is similar to ICON Think of indices as pointing between elements in a list. [ ‘cat’, ‘dog’, 3, 4.5 ] >>> animals = [‘cat’, ‘dog’, ‘mouse’, ‘bird’] >>> print animals[0:1] [‘cat’, ‘dog’] >> print animals[1:] [‘dog’, ‘mouse’, ‘bird’] >>> tmp = list(“Shrubbery”) >>> tmp[:1] = tmp[-7:] >>> tmp [‘r’, ’u’, ’b’, ’b’, ’e’, ’r’, ’y’, ’S’, ’h’, ’r’, ’u’, ’b’, ’b’, ’e’, ’r’, ’y’]

Ranges Python has a range function to easily form lists of integers. >>> range(5) [0, 1, 2, 3, 4] >>> range(2,5) [2, 3, 4] >>> range(0, 10, 2) [0, 2, 4, 6, 8] >>> range(5, 0, -1) [5, 4, 3, 2, 1]

in The in keyword checks if the given object is contained within the aggregate. >>> p = “cat” >>> j = [‘cat’, ‘dog’] >>> p in j True >>> ‘a’ in p True >>> ‘t’ in p[:2] False

Subroutines Python supports both procedures and functions –Procedure: def proc1(): print ‘Hi!’ –Function: def func1(): return ‘Hi!’

Subroutines (continued) Python does not support name mangling as in C++ Anything can be returned from a function, including None and other functions Recursion is allowed Python has support for calling subroutines in modules written in C Parameters are passed by value

Scope Lexical Global/local scope Similar to Scheme No names need to be declared before use

Lifetime / Actions Variables are alive as long as they can be referenced, similar to Scheme Python supports standard arithmetic precedence and association with ()’s Result type is defined the more descriptive of the operands

Control Structures if statements work as expected >>> if x < 0: …print ‘Negative’ … elif x == 0: …print ‘Zero’ … else: …print “Positive” …

Control Structures continued for loops differ from c++ and/or java. They iterate over an aggregate. >>> animals = [‘cat’, ‘dog’, ‘horse’] >>> for x in animals: …print x …

Control Structures Continued for loops can iterate over multiple lists at the same time >>> questions = ['name', 'quest', 'favorite color'] >>> answers = ['lancelot', 'the holy grail', 'blue'] >>> for q, a in zip (questions, answers):... print 'What is your %s? It is %s.' % (q, a)... What is your name? It is lancelot. What is your quest? It is the holy grail. What is your favorite color? It is blue.

Pass The pass command does nothing.

Functions >>> def fib(n):...a, b = 0, 1... while b < n:... print b,... a, b = b, a+b...

Functions continued >>> def makeIncFunc ( n = 1 ) …return lambda x: x + n … >>> tmp = makeIncFunc() >>> print tmp(3) 4 >>> tmp = makeIncFunc(2) >>> print tmp(3) 5

Default Value Side Effects >>> def f(a, L=[]): …L.append(a) …return L … >>> print f(1) [1] >>> print f(2) [1, 2] >>> print f(3) [1, 2, 3]

Classes Python implements classes in a similar way to Java and C++ >>> class Complex:...def __init__(self, realpart, imagpart):... self.r = realpart... self.i = imagpart... >>> x = Complex(3.0, -4.5) >>> x.r, x.i (3.0, -4.5)

Inheritance “Of course, a language feature would not be worthy of the name ``class'' without supporting inheritance. “ class DerivedClassName(BaseClassName):...

Multiple Inheritance! class DerivedClassName(Base1, Base2, Base3):...

Odds and Ends class Employee: pass john = Employee() john.name = 'John Doe‘ john.dept = 'computer lab‘ john.salary = 1000

Pickling Python’s equivalent to Serialization >>> pickle.dump( anyobject, fileopenedforwriting ) >>> objecttoloadto = pickle.load( fileopenedforreading )

What this has to do with Legos A python library calls Pylnp which allows remote control of your robot through the IR tower import lnp lnp.iwrite('hello') lnp.iread() 'world'