Crash Dive into Python

Similar documents
Introduction to Python. Fang (Cherry) Liu Ph.D. Scien5fic Compu5ng Consultant PACE GATECH

pybdg Documentation Release 1.0.dev2 Outernet Inc

Python Tutorial. CSE 3461: Computer Networking

Introduction to Python for IBM i

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

CPSC 217 Midterm (Python 3 version)

[Software Development] Python (Part A) Davide Balzarotti. Eurecom Sophia Antipolis, France

The current topic: Python. Announcements. Python. Python


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

Computer Systems and Networks

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

Introduction to Python

Programming in Python

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Outline. Simple types in Python Collections Processing collections Strings Tips. 1 On Python language. 2 How to use Python. 3 Syntax of Python

CIS192: Python Programming

Programming Languages: Part 1. Robert M. Dondero, Ph.D. Princeton University

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

Python Input, output and variables

CSCE 110 Programming I

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

CIS192 Python Programming. Robert Rand. August 27, 2015

Control Structures 1 / 17

Programming Languages Part 1. Copyright 2017 by Robert M. Dondero, Ph.D. Princeton University

Babu Madhav Institute of Information Technology, UTU 2015

Course Introduction and Python Basics

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Welcome to Python 3. Some history

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

MUTABLE LISTS AND DICTIONARIES 4

PYTHON FOR MEDICAL PHYSICISTS. Radiation Oncology Medical Physics Cancer Care Services, Royal Brisbane & Women s Hospital

Some material adapted from Upenn cmpe391 slides and other sources

Networking: UDP & DNS

Introduction to Python! Lecture 2

Senthil Kumaran S

Introduction to Python. Prof. Steven Ludtke

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

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

Programming to Python

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

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Python Workshop. January 18, Chaitanya Talnikar. Saket Choudhary

Programming in Python 3

Python Development with PyDev and Eclipse -

PYTHON NUMPY TUTORIAL CIS 581

Brush up Python. December 20, 2018

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

Python Tutorial. Day 1

Computer Science 217

python 01 September 16, 2016

COMP519 Web Programming Autumn A Brief Intro to Python

Data Handing in Python

Spring Semester 09 Exam #1 Dr. Dillon. (02/19)

Python Basics. Nakul Gopalan With help from Cam Allen-Lloyd

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

Short, Unique and Mysterious

Data Structures. Lists, Tuples, Sets, Dictionaries

Python BASICS. Introduction to Python programming, basic concepts: formatting, naming conventions, variables, etc.

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

ENGR 101 Engineering Design Workshop

CS 1301 CS1 with Robots Summer 2007 Exam 1

Table of Contents EVALUATION COPY

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

Networking: UDP & DNS

Beyond Blocks: Python Session #1

Introduction to Python (All the Basic Stuff)

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

D R S H YA M N C H AW D A

egrapher Language Reference Manual

Python Basics. Lecture and Lab 5 Day Course. Python Basics

Lists How lists are like strings

Lists, loops and decisions

TEXT MINING INTRO TO PYTHON

CS Programming In C

And Parallelism. Parallelism in Prolog. OR Parallelism

Accelerating Information Technology Innovation

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

Python for ArcGIS. Lab 1.

Programming Environments

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

STSCI Python Introduction. Class URL

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

python Introduction Rich Macfarlane, 2015

Accelerating Information Technology Innovation

ECE 364 Software Engineering Tools Lab. Lecture 3 Python: Introduction

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

ECE 364 Software Engineering Tools Laboratory. Lecture 4 Python: Collections I

Python Programming, bridging course 2011

Python in 10 (50) minutes

Computing with Numbers

Chapter 8 SETS AND DICTIONARIES

CS 2316 Exam 1 Spring 2014

Winter 2019 CISC101 1/17/2019

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

CPSC 217 Midterm (Python 3 version)

ENGR 102 Engineering Lab I - Computation

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

Lessons on Python Functions

Transcription:

ECPE 170 University of the Pacific Crash Dive into Python

2 Lab Schedule Today Ac:vi:es Endianness Python Thursday Network programming Lab 8 Network Programming Lab 8 Assignments Due Due by Mar 30 th 5:00am Lab 9 Due by Apr 6 th 5:00am

3 Person of the Day: Guido van Rossum ì Author of the Python programming language ì Self- appointed Benevolent Dictator For Life ì Chose the name because he was in a slightly irreverent mood (and a big fan of Monty Python's Flying Circus) ì Has worked in numerous organiza:ons, including NIST, Google and Dropbox Computer Systems and Networks Spring 2015

4 Python

5 What is Python? Interpreted language for scrip:ng and many other uses Features: Objects Dynamic types A rich set of libraries Extensibility through C (for speed cri:cal code) It is most notorious for its indenta:on rules, using whitespace or tabs (and it is very picky)

6 Python Datatypes Python supports many datatypes from C or C++: Integers, floats, strings, booleans Recent Python versions support other useful types: Complex numbers Sequences (tuples, lists) Dic:onaries Sets Bytes and bytearrays

7 Runtime evaluation Python is interpreted and has dynamic typing Implica:ons: Syntax is checked when code is first encountered Variable types (or even their existence) aren t checked un:l the code is executed Result: Code can execute correctly for a while un:l either an undefined variable is encountered, or it is used incorrectly (i.e., trying to access an integer as a sequence)

8 Python Tuples A tuple is an immutable collec:on of objects Tuples are denoted by parenthesis t = (1,2,3) The objects in a tuple do not need to be of the same type

9 Python Lists A list is an mutable collec:on of objects Lists are denoted by square brackets l = [1.5, a, (3,True)]

10 Python Sequences Tuples and lists are both types of sequences: individual items can be accessed in various ways To access a par:cular item in a sequence: t = (1,2,3) l = [1.5, a, (3,True)] print(t[0],l[1]) Output: 1 a

11 Python Sequences Sequences can also be accessed from the end (instead of beginning) using nega9ve indices t = (1,2,3) l = [1.5, a, (3,True)] print(t[-2],l[-1]) Output: 2 (3, True)

12 Python Sequences Slices (subsets of sequences) are accessed by using a : Note that the second index (if supplied) is one greater than actual last object in the slice t = (1,2,3) l = [1.5, a, (3,True)] print(t[0:2]) print(l[1:]) Output: (1,2) ('a, (3, True)]

13 Python Dictionaries A dic9onary is an associa:ve array of keys and value pairs d={'a':1, 'b':2, 3:'c'} print(d) print(d.keys()) print(d.values()) print(d['a']) print(d['c']) Output: {'a': 1, 3: 'c', 'b': 2} dict_keys(['a', 3, 'b']) dict_values([1, 'c', 2]) 1 KeyError: 'c

14 Python Error Handling Python handles errors using the try and except statements try: d['c'] except: print("key 'c' is not present") Output: Key 'c' is not present

15 Python Blocks Python uses whitespace and : to denote blocks Note: tabs and spaces are not interchangeable! Within a block, all lines are indented exactly the same amount print(l) print(l) Output: [1.5, 'a', (3, True)] IndentationError: unexpected indent

16 Python Statements and Flow Control Python supports these statements: if elif else for while if 1 > 2: print(a) elif 3 > 2: print(t) else: print("neither") Output: (1, 2, 3)

17 Python Statements and Flow Control The for statement takes a sequence as its input This works for any sequence type Tuples, lists, strings, etc for x in (1,3,5,'a'): print(x) Output: 1 3 5 a

18 Python Statements and Flow Control For the equivalent of a C for loop, use the range class for i in range(0,9,3): print(i) Output: 0 3 6 This is equivalent to: for (int i=0; i < 9; i += 3)

19 Using Python Libraries Libraries (modules) are accessed using the import statement import math print(math.sin(2)) Output: 0.9092974268256817

20 The struct Module

21 The struct Module Since the details of variables are hidden in Python (for example, how many bytes is an integer?), there are no built- in ways to store values into files along with their encoding A typical Python file would contain just ASCII or Unicode values The struct module deals with binary data In reality, it performs conversions between basic Python datatypes and binary strings

22 The struct Module Two main func:ons in the struct module pack: convert a group of variables into a string unpack: convert a string into a group of variables Similar to C s printf and scanf Each func:on requires a format string to describe how to pack or unpack the arguments See hbps://docs.python.org/3/library/struct.html

23 The struct Module Endianness must be considered when doing file or network I/O with fields greater than one byte The first character of the format string determines the endianness Character Byte order Size Alignment @ Na:ve Na:ve Na:ve = Na:ve Standard None < Lible Standard None > Big Standard None! Network (Big) standard None