Crash Dive into Python

Similar documents
Crash Dive into Python

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

ESCI 386 Scientific Programming, Analysis and Visualization with Python. Lesson 5 Program Control

arrays C Programming Language - Arrays

Introduction to Python

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

A Python Tour: Just a Brief Introduction CS 303e: Elements of Computers and Programming

Python Evaluation Rules

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Computational Mathematics with Python

Exercise 1: Python Language Basics

Simulation Tools. Python for MATLAB Users I. Claus Führer. Automn Claus Führer Simulation Tools Automn / 65

Computational Mathematics with Python

Computational Mathematics with Python

PYTHON Basics

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Introduction to Computer Science I Spring 2014 Mid-term exam Solutions

Introduction to Python

2! Multimedia Programming with! Python and SDL

Number Representation

Variables, Constants, and Data Types

Python Basics. S.R. Doty. August 27, Preliminaries What is Python? Installation and documentation... 4

Stacks. Linear data structures

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

The programming language C. sws1 1

Python Programming: An Introduction to Computer Science

Creating A Simple Dictionary With Definitions

Exercise 4 Learning Python language fundamentals

Computer Science 217

Informatica e Sistemi in Tempo Reale

Reverse engineering smart cards

Introduction to Java

Lab 4.4 Secret Messages: Indexing, Arrays, and Iteration

Welcome to Introduction to programming in Python

AQA GCSE in Computer Science Computer Science Microsoft IT Academy Mapping

Programming Languages CIS 443

CS 241 Data Organization Coding Standards

6.170 Tutorial 3 - Ruby Basics

Modbus and ION Technology

How To Write Portable Programs In C

Python Lists and Loops

CRASH COURSE PYTHON. Het begint met een idee

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

Ecma/TC39/2013/NN. 4 th Draft ECMA-XXX. 1 st Edition / July The JSON Data Interchange Format. Reference number ECMA-123:2009

Chapter 4: Computer Codes

Introduction to Data Structures

The C Programming Language course syllabus associate level

Tutorial on C Language Programming

Sources: On the Web: Slides will be available on:

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

Computer Programming Tutorial

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Symbol Tables. Introduction

PL / SQL Basics. Chapter 3

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

C++FA 5.1 PRACTICE MID-TERM EXAM

Programming in Python. Basic information. Teaching. Administration Organisation Contents of the Course. Jarkko Toivonen. Overview of Python

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Chapter 13 - The Preprocessor

The use of binary codes to represent characters

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Sample Induction Proofs

Introduction to Python for Text Analysis

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

The New IoT Standard: Any App for Any Device Using Any Data Format. Mike Weiner Product Manager, Omega DevCloud KORE Telematics

Introduction to Python

NLP Programming Tutorial 0 - Programming Basics

Python Loops and String Manipulation

Introduction to Python

Simple File Input & Output

LEARNING TO PROGRAM WITH PYTHON. Richard L. Halterman

FEEG Applied Programming 5 - Tutorial Session

Instruction Set Architecture of Mamba, a New Virtual Machine for Python

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

[1] Learned how to set up our computer for scripting with python et al. [3] Solved a simple data logistics problem using natural language/pseudocode.

This is great when speed is important and relatively few words are necessary, but Max would be a terrible language for writing a text editor.

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

Introduction to. Marty Stepp University of Washington

C++ INTERVIEW QUESTIONS

Computer Programming I

The Smalltalk Programming Language. Beatrice Åkerblom

HOMEWORK # 2 SOLUTIO

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Install Java Development Kit (JDK) 1.8

GCSE. Mark Scheme for January Computing (Pilot) General Certificate of Secondary Education Unit A451: Computer systems and programming

NLP Lab Session Week 3 Bigram Frequencies and Mutual Information Scores in NLTK September 16, 2015

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

DNA Data and Program Representation. Alexandre David

Introduction to Python

River Dell Regional School District. Computer Programming with Python Curriculum

Semantic Analysis: Types and Type Checking

Simple Image File Formats

BCD (ASCII) Arithmetic. Where and Why is BCD used? Packed BCD, ASCII, Unpacked BCD. BCD Adjustment Instructions AAA. Example

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

CS 106 Introduction to Computer Science I

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Transcription:

ECPE 170 University of the Pacific Crash Dive into Python

2 Lab Schedule Ac:vi:es Assignments Due Today Lab 11 Network Programming Due by Dec 1 st 5:00am Python Lab 12 Next Week Due by Dec 8 th 5:00am Lab 12 Network Programming Final Exam Tue Dec 9 th 8-10am

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 Fall 2014

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 To install: sudo apt-get install geany

17 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)

18 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

19 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)

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

21 The struct Module

22 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

23 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

24 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