Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

Slides:



Advertisements
Similar presentations
"The State of the Python Union" Python10 - Alexandria, VA - February 7, 2002 Guido van Rossum Director, PythonLabs at Zope Corporation
Advertisements

Why I Invented Python EuroPython – June 27, 2005 Guido van Rossum Elemental Security, Inc.
Python Regrets OSCON, July 25, 2002
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.
Jonathan Huelman CSC 415 – Programming Languages
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Python Control of Flow.
Intro to Python Programming (Resources) Pamela A. Moore Zenia C. Bahorski Eastern Michigan University March 7, 2012 A language to swear by, not at. 1.
“Everything Else”. Find all substrings We’ve learned how to find the first location of a string in another string with find. What about finding all matches?
Lists in Python.
1 Python CIS*2450 Advanced Programming Concepts Material for this lecture was developed by Dr. D. Calvert.
Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar.
Modules and Decomposition UW CSE 190p Summer 2012 download examples from the calendar.
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.
Built-in Data Structures in Python An Introduction.
Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global.
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.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
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
Course A201: Introduction to Programming 09/30/2010.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
Working With Objects Tonga Institute of Higher Education.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
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.
Python Let’s get started!.
8 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Python  Monty or Snake?. Monty?  Spam, spam, spam and eggs  Dead parrots  Eric Idle, John Cleese, Michael Palin, etc.
Chapter 10 Loops: while and for CSC1310 Fall 2009.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )
Python Programing: An Introduction to Computer Science
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Python Programming Language by Vasu Chetty. Origins of Python Created by: Guido van Rossum, a Dutch Programmer Created during: Christmas Break, 1989 Created.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
Programming For Big Data
Whatcha doin'? Aims: To start using Python. To understand loops.
Python Let’s get started!.
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
Variables, Expressions, and IO
CSC 108H: Introduction to Computer Programming
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.
Shell scripts on Pegasus 2
Arithmetic operations, decisions and looping
Introduction to Python
Introduction to Python
Programming Tips GS540 January 10, 2011.
CSC1018F: Intermediate Python
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Python Primer 1: Types and Operators
(more) Python.
CISC101 Reminders Assignment 2 due today.
Python Review
Class code for pythonroom.com cchsp2cs
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
Ruth Anderson UW CSE 160 Winter 2016
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Introduction to Python (for C++ programmers)

Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed language – no int, char, long nonsense Object Oriented Current versions are and – not backwards compatible

Terms Pythonic – hard to define – programming in a way that lends itself well to the Python language Pythonista – a person who codes in a pythonic way

The Zen of Python Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. …

Syntax Indentation Colon on the line before indented block No Braces

Types of Quotes Double quote – “string” Single quotes – ‘string’ Triple quotes –

Let’s Dive In! Hello World A more interesting Hello World

Data Types Numeric Types — int, float, long, complexintfloatlongcomplex List – [] Tuple – () More About TuplesMore About Tuples Dictionary – {} dictdict string set, frozenset Mutable vs. Immutable Indexing (slicing, step slicing) Heterogeneous

C++ Python C++Python NULLNone trueTrue falseFalse !not &&and ||or while(true);while(True): pass

for iterate through stuff for var in iterable: do_something

in Check for membership Iterating through lists (as in the ‘for’ example)

is vs. == Remember Python is an OOPL? The ‘is’ keyword checks if two variable names point to the same memory. == checks the value. Uses the __eq__ function in classes.

The idiomatic way to perform an operation on all items in a list in C looks like this: for (i=0; i < mylist_length; i++) { do_something(mylist[i]); } The direct equivalent in Python would be this: i = 0 while i < mylist_length: do_something(mylist[i]) i += 1 That, however, while it works, is not considered Pythonic. It's not an idiom the Python language encourages. We could improve it. A typical idiom in Python to generate all numbers in a list would be to use something like the built-in range() function: for i in range(mylist_length): do_something(mylist[i]) This is however not Pythonic either. Here is the Pythonic way, encouraged by the language itself: for element in mylist: do_something(element) Pythonic Programming

Ternary Operator a if b else c b ? a : c Frowned upon in Python just like in C

Functions Functions are objects too Can return multiple values using tuple unpacking Can pass in arbitrary number of elements

Useful Builtin Functions len() range() and xrange() enumerate() map() zip() any() and all() dir() ml ml

List Comprehension and Generators List comprehension makes a list (subscriptable, iterable) Generators not a tuple iterable calculation gets done on demand (when iterated) can’t subscript

Python 2 vs. Python 3 print range and xrange raw_input and input division – 2 does integer division. Use // to do floating – 3 does floating point. Use // to do integer str and bytes Unicode __future__ Full changes at

Classes Everything is public. Python provides a way to ‘hide’ things, but it’s STILL public. self

Modules (Importing)

Gotchas Other languages have "variables“ Mixing tabs and spaces or inconsistent indentation no i++ notation; use i += 1 tml tml

File I/O source code / demo Python 3 introduced ‘with’ can be accessed in __future__ in 2.6

Exceptions and Classes for another day just know they exist

Tips and Tricks Swap Values Chained comparisons a < b < c is the same as a < b and b < c easy_install - setuptools 0.6c11setuptools 0.6c11 – pdb - The Python Debugger profile

Learning Python – Great documentation for every version! help() ipython – python shell – tab-completion – ? and ??

TODO lambda functions dictionaries ‘’.join() zip() list splicing