Python in 10 (50) minutes

Similar documents
Python I. Some material adapted from Upenn cmpe391 slides and other sources

Basic Syntax - First Program 1

Introduction to Python

Basic Scripting, Syntax, and Data Types in Python. Mteor 227 Fall 2017

Babu Madhav Institute of Information Technology, UTU 2015

S206E Lecture 19, 5/24/2016, Python an overview

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Some material adapted from Upenn cmpe391 slides and other sources

Programming in Python 3

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Python Intro GIS Week 1. Jake K. Carr

Statements 2. a operator= b a = a operator b

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Key Differences Between Python and Java

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

PTN-102 Python programming

Data type built into Python. Dictionaries are sometimes found in other languages as associative memories or associative arrays.

Introduction to Bioinformatics

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

CS S-02 Python 1. Most python references use examples involving spam, parrots (deceased), silly walks, and the like

(CC)A-NC 2.5 by Randall Munroe Python

Advanced Algorithms and Computational Models (module A)

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Pace University. Fundamental Concepts of CS121 1

APT Session 2: Python

Java+- Language Reference Manual

AI Programming CS S-02 Python

Python - Variable Types. John R. Woodward

Download Python from Any version will do for this class

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

Course Title: Python + Django for Web Application

History Installing & Running Python Names & Assignment Sequences types: Lists, Tuples, and Strings Mutability

Python INTRODUCTION: Understanding the Open source Installation of python in Linux/windows. Understanding Interpreters * ipython.

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

Beyond Blocks: Python Session #1

UNIT- 3 Introduction to C++

Programming in Python

Python a modern scripting PL. Python

Introduction to Python! Lecture 2

CSC Software I: Utilities and Internals. Programming in Python

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

MUTABLE LISTS AND DICTIONARIES 4

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Language Reference Manual

Overview of the Ruby Language. By Ron Haley

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

CIS192 Python Programming. Robert Rand. August 27, 2015

Introduction to Python. Didzis Gosko

Lessons on Python Functions

Basic Python 3 Programming (Theory & Practical)

Accelerating Information Technology Innovation

PYTHON CONTENT NOTE: Almost every task is explained with an example

Python Class-Lesson1 Instructor: Yao

egrapher Language Reference Manual

ENGR 101 Engineering Design Workshop

Positional, keyword and default arguments

Python Tutorial. Day 1

Data Structures (list, dictionary, tuples, sets, strings)

Contents. Lezione 3. Reference sources. Contents

CIS192 Python Programming

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Ruby: Introduction, Basics

Variable and Data Type I

OBJECT ORIENTED PROGRAMMING

Coral Programming Language Reference Manual

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

PYTHON. Values and Variables

Variables, expressions and statements

python 01 September 16, 2016

Here n is a variable name. The value of that variable is 176.

Table of Contents. Dive Into Python...1

Introduction to Python

CS 11 Haskell track: lecture 1

Course May 18, Advanced Computational Physics. Course Hartmut Ruhl, LMU, Munich. People involved. SP in Python: 3 basic points

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Introduction to Python Part 1. Brian Gregor Research Computing Services Information Services & Technology

CNRS ANF PYTHON Objects everywhere

Overloading, Type Classes, and Algebraic Datatypes

Table of Contents EVALUATION COPY

PYTHON. Varun Jain & Senior Software Engineer. Pratap, Mysore Narasimha Raju & TEST AUTOMATION ARCHITECT. CenturyLink Technologies India PVT LTD

61A Lecture 2. Wednesday, September 4, 2013

Full file at

The current topic: Python. Announcements. Python. Python

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Accelerating Information Technology Innovation

Introduction to Programming Using Java (98-388)

Java Bytecode (binary file)

Subprogram Concept. COMP3220 Principle of Programming Languages. Zhitao Gong Spring

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

Ruby: Introduction, Basics

Introduction to Python programming, II

Variable and Data Type I

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

Functional programming in LISP

Swift. Introducing swift. Thomas Woodfin

Transcription:

Python in 10 (50) minutes https://www.stavros.io/tutorials/python/ Python for Microcontrollers Getting started with MicroPython Donald Norris, McGrawHill (2017)

Python is strongly typed (i.e. types are enforced) dynamically, implicitly typed (i.e. you don t have to declare variables), case sensitive (i.e. var and VAR are two different variables) and object-oriented (i.e. everything is an object).

Getting Help Help in Python is always available right in the interpreter. If you want to know how an object works, all you have to do is call help(<object>)! Also useful are dir(), which shows you all the object s methods, and <object>. doc, which shows you its documentation string

Syntax Python has no mandatory statement termination characters and blocks are specified by indentation. Indent to begin a block, dedent to end one. Statements that expect an indentation level end in a colon (:). Comments start with the pound (#) sign and are single-line, multi-line strings are used for multi-line comments. Values are assigned (in fact, objects are bound to names) with the equals sign ( = ), and equality testing is done using two equals signs ( == ). You can increment/decrement values using the += and -= operators respectively by the right-hand amount. This works on many datatypes, strings included. You can also use multiple variables on one line.

Data types The data structures available in python are lists, tuples and dictionaries. Lists are like one-dimensional arrays (but you can also have lists of other lists), dictionaries are associative arrays (a.k.a. hash tables) and tuples are immutable one-dimensional arrays (Python arrays can be of any type, so you can mix e.g. integers, strings, etc in lists/dictionaries/tuples). The index of the first item in all array types is 0. Negative numbers count from the end towards the beginning, -1 is the last item. Variables can point to functions.

Data types You can access array ranges using a colon (:). Leaving the start index empty assumes the first item, leaving the end index assumes the last item. Indexing is inclusive:exclusive so specifying [2:10] will return items [2] (the third item, because of 0-indexing) to [9] (the tenth item), inclusive (8 items). Negative indexes count from the last item backwards (thus -1 is the last item)

Strings Its strings can use either single or double quotation marks, and you can have quotation marks of one kind inside a string that uses the other kind (i.e. He said hello. is valid). Multiline strings are enclosed in triple double (or single) quotes ("""). Python supports Unicode out of the box, using the syntax u This is a unicode string. To fill a string with values, you use the % (modulo) operator and a tuple. Each %s gets replaced with an item from the tuple, left to right, and you can also use dictionary substitutions

Flow control statements Flow control statements are if, for, and while. There is no switch; instead, use if. Use for to enumerate through members of a list. To obtain a list of numbers, use range(<number>).

Functions Functions are declared with the def keyword. Optional arguments are set in the function declaration after the mandatory arguments by being assigned a default value. For named arguments, the name of the argument is assigned a value. Functions can return a tuple (and using tuple unpacking you can effectively return multiple values). Lambda functions are ad hoc functions that are comprised of a single statement. Parameters are passed by reference, but immutable types (tuples, ints, strings, etc) cannot be changed in the caller by the callee. This is because only the memory location of the item is passed, and binding another object to a variable discards the old one, so immutable types are replaced.

Classes Python supports a limited form of multiple inheritance in classes. Private variables and methods can be declared (by convention, this is not enforced by the language) by adding at least two leading underscores and at most one trailing one (e.g. spam). We can also bind arbitrary names to class instances.

Exceptions Exceptions in Python are handled with try-except [exceptionname] blocks

Importing External libraries are used with the import [libname] keyword. You can also use from [libname] import [funcname] for individual functions.

File I/O Python has a wide array of libraries built in. As an example, here is how serializing (converting data structures to strings using the pickle library) with file I/O is used:

Miscellaneous Conditions can be chained: 1 < a < 3 checks that a is both less than 3 and greater than 1. You can use del to delete variables or items in arrays. List comprehensions provide a powerful way to create and manipulate lists. They consist of an expression followed by a for clause followed by zero or more if or for clauses

Global variables Global variables are declared outside of functions and can be read without any special declarations, but if you want to write to them you must declare them at the beginning of the function with the global keyword, otherwise Python will bind that object to a new local variable (be careful of that, it s a small catch that can get you if you don t know it).