Introduction to Python Kris Kneubuhler SE NPUG July 17 th 2014.

Slides:



Advertisements
Similar presentations
Introduction to Python
Advertisements

Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
1 eclipse Tips. 2 What is eclipse? Eclipse is a popular IDE (Integrated Development Environment) that we will use to create, compile, execute, and test.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Programming Introduction November 9 Unit 7. What is Programming? Besides being a huge industry? Programming is the process used to write computer programs.
Introduction to a Programming Environment
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
Chapter 1: Python Basics CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Game Programming © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line L.
Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012
A First Program Using C#
1 eclipse Tips. 2 What is eclipse? Eclipse is a popular IDE (Integrated Development Environment) that we will use to create, compile, execute, and test.
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
Introduction to Python
Gator Engineering 1 Chapter 2 C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
1 Chapter 2 First Java Programs Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introduction to Programming Peggy Batchelor.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
UCSC All rights reserved. No part of this material may be reproduced and sold. 1 IT1202-Fundamentals Of Programming (Using JAVA) Interacting with.
Chapter 1 CSIS-120: Java Intro. What is Programming?  A: It is what makes computer so useful.  The flexibility of a computer is amazing  Write a term.
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
Programming With C.
Writing Scala Programs. Command Line There are three common operating systems: Windows (various flavors; I recommend Windows 7) UNIX or Linux (basically.
Python From the book “Think Python”
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Guide to Programming with Python Chapter One Getting Started: The Game Over Program.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Hello World in the Forte IDE An introduction to the Forte IDE (integrated development environment) writing the classic “Hello World” program in Java.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.
ECET – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.
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.
CPS120: Introduction to Computer Science Compiling a C++ Program From The Command Line.
CSC 1010 Programming for All Lecture 2 Introduction to Python Some material based on material from Marty Stepp, Instructor, University of Washington.
Python Let’s get started!.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
8/2/07. >>> About Me Scott Shawcroft * Junior * Computer Engineering * Third Quarter TA * Creative Commons Intern * Small-time Open Source Developer
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
CS 201 Lecture 1 (b) Using an IDE Tarik Booker CS 201: Introduction to Programming California State University, Los Angeles.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Fundamentals of Programming I Overview of Programming
Introduction to Java Import Scanner class to use in our program
The eclipse IDE IDE = “Integrated Development Environment”
Development Environment
Introduction to GIS PythonScript CGIS-NURIntroduction to ArcGIS II.
Whatcha doin'? Aims: To start using Python. To understand loops.
Introduction to Eclipse
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.
Variables, Expressions, and IO
Introduction to C Topics Compilation Using the gcc Compiler
CompSci 230 Software Construction
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Introduction to Python
PHP.
12th Computer Science – Unit 5
Chapter 1: Programming Basics, Python History and Program Components
Input and Output Python3 Beginner #3.
Introduction to Python
How to Run a Java Program
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Introduction to Python Kris Kneubuhler SE NPUG July 17 th 2014

Python 101 – Introduction Python Examples NXAPI Demo Q&A Agenda

© 2012 Cisco and/or its affiliates. All rights reserved.Cisco Confidential 3 Python: the basics

Python is an interpreted programming (scripting) language – Available for Linux, Windows, Mac – Two common versions: 2.7(x) and 3.3(x) – 2.7 more widely used than 3.3 at the time of writing Python code is not compiled into standalone binary – It’s either interpreted (interactive shell) or translated into byte code and executed by Python VM Code can be packaged as a “frozen binary” and executed on systems that don’t have Python installed – the Python VM is packaged within the frozen binary (using py2exe, pyinstaller or freeze) Basics

Python home page – Major Releases – Python 3.2.2September 4, 2011 – Python 3.1.4June 11, 2011 – Python 3.0.1February 13, 2009 – Python 2.7.2June 11, 2011 – Python 2.6.7June 3, 2011 – Python December 23, 2008 – Python December 19, 2008 – Python March 11, 2008 – Python May 30, 2003 – Python April 8, 2002 – Python June 2001 – Python September 2000 Python Distributions 2.7.x is probably the version most frequently used today; this slide deck focuses primarily on 2.7

Python is an interpreted language – no pre-compile step as in Java and C++ – each time Python code is run it is interpreted afresh – code changes can be very quickly made and tested – code can also be entered interactively – can dynamically construct Python code For example in the form of a string and execute it directly What about performance? – (much) slower than compiled C/C++.. but also much more flexible – Good enough for most cases Very large projects such as Openstack are implemented in Python – Modules to measure (profile) performance exist Python is interpreted

When executing Python instructions, Python internally compiles the source into a format called byte code – This is meant to speed up execution This process is hidden from the programmer You will now and then notice.pyc files automatically created in your folders (depending if Python has write access) – Next time you run your code, assuming it hasn’t been modified, Python skips the compilation step and directly runs the byte code inside the Python VM The Python VM is Python’s runtime engine. It always comes with any installation of Python – Note: VM does not mean a VMware VM here! Byte code compilation

If you are used to other programming languages that are compiled, you should know there is a significant difference in terms of error checking with interpreted languages Python checks errors at runtime Interpreted code: error checking

Be aware that Python is case sensitive – A and a are different variables – We’re going to explore variables in a bit Python programs typically have the.py extension – Run using python or python –i Python is case-sensitive

You MUST indent code blocks in Python – Keep this rule in mind, it will catch you almost inevitably if you are already used to other programming languages There are no curly braces to delimitate code blocks Code blocks are identified with a colon and indentation Prefer white spaces to tabs for indentation – Four white spaces is the preferred de-facto standard – Indentation is mandatory

The hash # sign indicates comments follow – Usually ok for short comments – Can start a new line or be used at the end of a line Comments included at the beginning of your code appear when invoking help(yourscript): Comments

Your code should contain comments They should be relatively short and to the point Comments should say why you are doing what you are doing The code already says how; why you did things in a certain way is more important Too many comments hurt readability though and require maintenance – find the right balance Sidenote: should you write comments?

If you are running Windows: – – Download the “Community” edition If you are running Linux: – It should be available in your path already If you are running Mac OS-X: – It’s available natively. Start a terminal and type ‘python’ – There is a also a DMG available at the Activepython URL listed above Where can I get Python?

To write Python code, you need an editor that is aware of Python’s syntax and indentation: – Windows -- do not use Notepad or Wordpad. Try the free and open source Notepad++ or the free and open source JEditNotepad++JEdit – Mac -- The built in TextEdit works, but not very well. Try the free TextWrangler or the free and open source JEditTextWranglerJEdit – Linux -- any unix text editor is fine, or try the above JEdit. Getting a proper editor

You can use a Python syntax-aware editor such as Notepad++ or vim or gedit and test all your code with python -i – Try without the -i flag and see what the difference is Don’t forget to add the.py extension to your files to ensure the editor starts highlighting Python syntax Alternatively, there’s an easy to use, small and free IDE (Integrated Development Environment) called IDLE For larger projects, you can use Eclipse with PyDev, or PyCharm – There is a PyCharm module at end of this deck – For now, IDLE is good enough How do I start Python

IDLE is a simple IDE (integrated development environment) – Provides contextual help, command completion, syntax highlighting, step-by-step debugging, etc. Simple and free – Distributed with Python since 1.5.x – Install Python and launch it now – It isn’t perfect, but it’s probably good enough for what we’re going to do Much more complex IDE: – PyDev inside Eclipse IDLE

Launch IDLE and start coding! How about print “Hello World!” ? The print statement was directly interpreted – that is because you are sitting at the interpreter’s interactive prompt – Naturally, code can be saved in files rather than entered line by line – Try that as well and invoke the script using python –i Your very first Python statement  notice that parentheses inside print are not necessary in 2.7 (not true in 3.x!)  there is no need for a semicolon (;) to end the print statement

IDLE lets you directly enter Python commands interactively with immediate execution. You also get contextual command completion: You’ll pretty much never type code directly into the interactive shell though, except when testing/debugging Your code will live inside.py files IDLE can open.py files, run and help you debug your code Exploring IDLE a bit further

Inside IDLE, go to File then select New Window This brings up a Python-aware editor that we are going to use to create our first program. You can also use a Python-friendly editor. Your first program

Once inside the new window IDLE spawned, type this: Go to File, then Save As. Save your file and make sure you explicitly specify.py at the end of the name you provided If you press F5 now, Python will run your program in the main window Your first program Don’t worry about the keywords for now …

If you’re using Windows, you can run.py files by double-clicking them or specifying how to run them: If we run our basic program, a black window will come and go very rapidly – we can trick Windows to keep the window from disappearing Other ways of executing your program

Just add a input() statement at the end of your program to prevent Windows from closing the Python window after the script has executed You can also invoke the script with python -i (“dash i”) A little trick for Windows users Once you execute your program, input() prevents Windows from closing the window

Typing help() brings up interactive help. Type topics or keywords Outside of the interactive context, use help(‘keyword’) Alternatively, if you’re inside the interactive help context, just type while to get help on the while keyword To leave the interactive help session, type quit Getting help

Literally every Python question you can come up with is answered on Stackoverflow You can also subscribe to the Python Tutor mailing list Or browse the official Python docs External help

If you type help(‘this’), you’ll see these guiding principles: “The Zen of Python”

© 2012 Cisco and/or its affiliates. All rights reserved.Cisco Confidential 26 Core data types

Python provides several built-in data types: Numbers: 1234, , 3+4j, Decimal, Fraction Strings: 'spam', “Joe's", b'a\x01c' Lists: [1, [2, 'three'], 4] Dictionaries: {'food': 'spam', 'taste': 'yum'} Tuples: (1, 'spam', 4, 'U') Files: myfile = open('eggs', 'r') Sets: set('abc'), {'a', 'b', 'c'} Other core types: Booleans, types, None Data types

There is a significant difference in Python (much like in other languages) between the = and the == operator A single equal sign assigns a value to a named variable A double equal sign tests equality between the right-hand side and the left-hand side of the operation Before we continue