Python Introduction 2 Lab Schedule Activities Assignments Due - - PowerPoint PPT Presentation

python introduction
SMART_READER_LITE
LIVE PREVIEW

Python Introduction 2 Lab Schedule Activities Assignments Due - - PowerPoint PPT Presentation

Computer Systems and Networks ECPE 170 University of the Pacific Python Introduction 2 Lab Schedule Activities Assignments Due This Week Lab 7 Due by Mar 19 th 5:00am Python introduction Networking introduction


slide-1
SLIDE 1

ì

Computer Systems and Networks

ECPE 170 – University of the Pacific

Python Introduction

slide-2
SLIDE 2

Lab Schedule

Activities

ì

This Week

ì

Python introduction

ì

Networking introduction

ì

Endianness (Thursday)

ì

Lab 8 (HTTP, TCP sockets)

Assignments Due

ì

Lab 7

ì

Due by Mar 19th 5:00am ì

Lab 8

ì

Due by Mar 26th 5:00am

Spring 2019 Computer Systems and Networks

2

slide-3
SLIDE 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

  • rganizations , including NIST,

Google and Dropbox

Spring 2019 Computer Systems and Networks

3

slide-4
SLIDE 4

ì

Python

Spring 2019 Computer Systems and Networks

4

slide-5
SLIDE 5

What is Python?

ì Interpreted language for scripting and many other

uses

ì Features:

ì

Objects

ì

Dynamic types

ì

A rich set of libraries

ì

Extensibility through C (for speed critical code) ì It is most notorious for its indentation rules, using

whitespace or tabs (and it is very picky)

Spring 2019 Computer Systems and Networks

5

slide-6
SLIDE 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)

ì

Dictionaries

ì

Sets

ì

Bytes and bytearrays

Spring 2019 Computer Systems and Networks

6

slide-7
SLIDE 7

Runtime evaluation

ì Python is interpreted and has dynamic typing ì Implications:

ì

Syntax is checked when code is first encountered

ì

Variable types (or even their existence) aren’t checked until the code is executed ì Result: Code can execute correctly for a while until

either an undefined variable is encountered, or it is used incorrectly (i.e., trying to access an integer as a sequence)

Spring 2019 Computer Systems and Networks

7

slide-8
SLIDE 8

Python Tuples

ì A tuple is an immutable collection of objects in a

sequence

ì Tuples are denoted by parenthesis ì The objects in a tuple do not need to be of the

same type

Spring 2019 Computer Systems and Networks

8

t = (1,2,3) t = (1,2,3, 'ECPE 170’, 3.1415)

slide-9
SLIDE 9

Problem 1 – Indexing Tuples

Open the terminal. Type python3 to open the interpreter. Create a tuple: Write the output for: >>>print(t[0]) #Pound is for comment, btw >>>print(t[3]) >>>print(t[7]) >>>print(t[-3]) # Access sequence from end using neg indices >>>print(t[-8]) >>>t[2]=t[3]

t = (1,2,3,’ECPE 170 rocks!’, ‘bye’)

Spring 2019 Computer Systems and Networks

9

P1

slide-10
SLIDE 10

Problem 2 – Slicing Tuples

Slices (subsets of sequences) are accessed by using a “:” What does the following print? >>>t[2:4] >>>t[0:4:2]

Spring 2019 Computer Systems and Networks

10

P2

slide-11
SLIDE 11

Python Lists

ì A list is an mutable collection of objects in a

sequence

ì Lists are denoted by square brackets

Spring 2019 Computer Systems and Networks

11

l = [1.5, 'a', (3,True)]

slide-12
SLIDE 12

Problem 3 - Lists

Declare the list: Write the output for the following operations:

  • a. >>>list.append('Hello!')

>>>print(list)

  • b. Try slicing like in tuples to see if it works:

>>>list[1:3]

  • c. >>>list.insert(4,'Scarlett Popapill')

>>>print(list)

  • d. >>>list.pop(-2)

list = [1.5, 'a', (3,True)]

Spring 2019 Computer Systems and Networks

12

P3

slide-13
SLIDE 13

Python Dictionaries

ì

A dictionary is an associative array of keys and value pairs

Spring 2019 Computer Systems and Networks

13

d={'a':1, 'b':2, 3:'c'} print(d) print(d.keys()) print(d.values()) print(d['a']) print(d['c']) {'a': 1, 3: 'c', 'b': 2} dict_keys(['a', 3, 'b']) dict_values([1, 'c', 2]) 1 KeyError: 'c’

Output:

P4

slide-14
SLIDE 14

Problem 5 – Strings Sequences

ì String sequences are versatile with many built-in

  • perations.

Perform the following and write the output: a. >>>string="Programming in C is " >>>print(string) b. >>>string=string + "a lot of fun!" #concatenation >>>print(string)

Spring 2019 Computer Systems and Networks

14

P5

slide-15
SLIDE 15

Problem 6 – String Splitting

ì ‘Split’ a string to divide it into a list based on a

delimiter

<name of string>.split(delimiter,maxsplits)

  • Returns a list of separated items
  • delimiter is the delimiting sequence about which you would

like to split.

  • maxsplits is the number of splits to perform. The output list

will have maxsplits+1 items

What is the output: >>>string=“Python is the best language, ever!” >>>newlist=string.split(' ',2); >>>newlist=string.split(' ');

Spring 2019 Computer Systems and Networks

15

P6

slide-16
SLIDE 16

Problem 7 – String Striping

ì ‘Strip’ a string to remove all characters in [chars]

<name of string>.strip([chars])

  • Strips the string from front and back by removing all

characters in [chars]

  • Stops strip when a character is encountered that is not

in [chars]

What is the output: >>>website=“www.pacific.edu” >>>hostname=website.strip(‘wedu.’)

Spring 2019 Computer Systems and Networks

16

P7

slide-17
SLIDE 17

Python Error Handling

ì Python handles errors using the try and except

statements

Spring 2019 Computer Systems and Networks

17

try: d['c'] except: print("Key 'c' is not present") Key 'c' is not present

Output:

slide-18
SLIDE 18

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

Spring 2019 Computer Systems and Networks

18

print(l) print(l) [1.5, 'a', (3, True)] IndentationError: unexpected indent

Output:

slide-19
SLIDE 19

Python Statements and Flow Control

ì

Python supports these statements:

ì

if

ì

elif

ì

else

ì

for

ì

while

Spring 2019 Computer Systems and Networks

19

if 1 > 2: print(a) elif 3 > 2: print(t) else: print("Neither") (1, 2, 3)

Output:

slide-20
SLIDE 20

Python Statements and Flow Control

ì

The for statement takes a sequence as its input

ì

This works for any sequence type

ì

Tuples, lists, strings, etc…

Spring 2019 Computer Systems and Networks

20

for x in (1,3,5,'a'): print(x) 1 3 5 a

Output:

slide-21
SLIDE 21

Python Statements and Flow Control

ì

For the equivalent of a C for loop, use the range class

Spring 2019 Computer Systems and Networks

21

for i in range(0,9,3): print(i) 3 6

Output:

This is equivalent to: for (int i=0; i < 9; i += 3)

slide-22
SLIDE 22

Using Python Libraries

ì Libraries (modules) are accessed using the import

statement

Spring 2019 Computer Systems and Networks

22

import math print(math.sin(2)) 0.9092974268256817

Output: