python beginner’s workshop · introduction first steps data types libraries control flow python...

90
Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop In Collaboration with the Pikes Peak Library District 21st Century Library Ryan E. Freckleton PySprings 2016-11-12 Beginning Python PySprings

Upload: others

Post on 21-Jun-2020

27 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Python Beginner’s WorkshopIn Collaboration with the Pikes Peak Library District 21st

Century Library

Ryan E. Freckleton

PySprings

2016-11-12

Beginning Python PySprings

Page 2: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

OutlineIntroductionFirst Steps

Running PythonExpressions

Data TypesStringsListsDictionaries

LibrariesEnvironmentsThird-Party Packages

Control FlowBooleansLooping and Branching

Beginning Python PySprings

Page 3: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Ground Rules

http://pysprings.org/pages/code-of-conduct.html

I We do not tolerate harassment of participants in any form.I All communication should be appropriate for a professional

audience including people of many different backgrounds.I Be kind to others. Do not insult or put down other attendees.

Behave professionally.I Thank you for helping make this a welcoming, friendly event

for all.I Contact the organizers at [email protected] orhttp://sayat.me/pysprings (anonymous)

Beginning Python PySprings

Page 4: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Greetings

1. “How did I get here? What is my prior experience in Pythonand Programming (if any)?”

2. “What do I want to have happen?” (write on mindmap)3. “What expertise or experience do I have that others here might

find useful?”

Beginning Python PySprings

Page 5: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Learning Goals

I Virtual environments and installing/using third-party packagesI Python Basics

I ListsI DictionariesI Control Flow

Beginning Python PySprings

Page 6: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Learning Goals

I Virtual environments and installing/using third-party packagesI Python Basics

I ListsI DictionariesI Control Flow

Beginning Python PySprings

Page 7: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Learning Goals

I Virtual environments and installing/using third-party packagesI Python Basics

I ListsI DictionariesI Control Flow

Beginning Python PySprings

Page 8: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Learning Cycle

Introduction Short lecture introducing a new concept from PythonExploration Hands-on application of the concept introduced.

Work in groups and collaborate if you prefer! Explorethe material in a hands-on manner

Invention What have we learned through our exploration? Whatsurprises did we encounter? What mysteries did weuncovered?

Application With our newly “invented” knowledge, what can wedo? This leads into a new exploration phase

Beginning Python PySprings

Page 9: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Learning Cycle

Introduction Short lecture introducing a new concept from PythonExploration Hands-on application of the concept introduced.

Work in groups and collaborate if you prefer! Explorethe material in a hands-on manner

Invention What have we learned through our exploration? Whatsurprises did we encounter? What mysteries did weuncovered?

Application With our newly “invented” knowledge, what can wedo? This leads into a new exploration phase

Beginning Python PySprings

Page 10: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Learning Cycle

Introduction Short lecture introducing a new concept from PythonExploration Hands-on application of the concept introduced.

Work in groups and collaborate if you prefer! Explorethe material in a hands-on manner

Invention What have we learned through our exploration? Whatsurprises did we encounter? What mysteries did weuncovered?

Application With our newly “invented” knowledge, what can wedo? This leads into a new exploration phase

Beginning Python PySprings

Page 11: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Learning Cycle

Introduction Short lecture introducing a new concept from PythonExploration Hands-on application of the concept introduced.

Work in groups and collaborate if you prefer! Explorethe material in a hands-on manner

Invention What have we learned through our exploration? Whatsurprises did we encounter? What mysteries did weuncovered?

Application With our newly “invented” knowledge, what can wedo? This leads into a new exploration phase

Beginning Python PySprings

Page 12: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

What is Programming?

I Programming is a creative activityI It doesn’t involve much mathI Programming is simply the act of entering instructions for the

computer to perform

Beginning Python PySprings

Page 13: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

What is Programming?

I Programming is a creative activityI It doesn’t involve much mathI Programming is simply the act of entering instructions for the

computer to perform

Beginning Python PySprings

Page 14: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

What is Programming?

I Programming is a creative activityI It doesn’t involve much mathI Programming is simply the act of entering instructions for the

computer to perform

Beginning Python PySprings

Page 15: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

An Example

1passwordFile = open(’SecretPasswordFile.txt’)2secretPassword = passwordFile.read()3print ’Enter your password.’4typedPassword = raw_input()5if typedPassword == secretPassword:6print ’Access granted’7if typedPassword == ’12345’:8print ’That one is used on luggage.’9else:10print ’Access denied’

Beginning Python PySprings

Page 16: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Running Python

OutlineIntroductionFirst Steps

Running PythonExpressions

Data TypesStringsListsDictionaries

LibrariesEnvironmentsThird-Party Packages

Control FlowBooleansLooping and Branching

Beginning Python PySprings

Page 17: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Running Python

Running Python

Examplerun python with:

$ python

enter the following into the interactive prompt:

>>> print "Hello, World!"

and

>>> import this

Beginning Python PySprings

Page 18: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Running Python

Installing Python

Let’s create script.py now

print "Hello, World!"

and run it with

$ python s c r i p t . py

Beginning Python PySprings

Page 19: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Running Python

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 20: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Running Python

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 21: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Running Python

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 22: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Running Python

Notation

When you see an example like:

>>> print "Hello, World!"

it means to type that out in the interactive prompt. When you seean example like:example.py

1print "Hello, World!"

it means to type that out into a file, in this case, named example.py.

Beginning Python PySprings

Page 23: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Expressions

OutlineIntroductionFirst Steps

Running PythonExpressions

Data TypesStringsListsDictionaries

LibrariesEnvironmentsThird-Party Packages

Control FlowBooleansLooping and Branching

Beginning Python PySprings

Page 24: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Expressions

Python as a Calculator

>>> 100 * 2200>>> (1 + 2 + 3 + 4 + 5 + 6) / 63>>> 1 - 2*100 + 3*12-163>>> abs(-163)163

Beginning Python PySprings

Page 25: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Expressions

Python Math Operations

Operators:

I + - * /I % ** //I Does python obey order of operations?

Functions:

I abs bin hex oct ord roundI divmod min max powI What’s the difference between these two lists of functions?

Beginning Python PySprings

Page 26: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Expressions

Python Math Operations

Operators:

I + - * /I % ** //I Does python obey order of operations?

Functions:

I abs bin hex oct ord roundI divmod min max powI What’s the difference between these two lists of functions?

Beginning Python PySprings

Page 27: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Expressions

Python Math Operations

Operators:

I + - * /I % ** //I Does python obey order of operations?

Functions:

I abs bin hex oct ord roundI divmod min max powI What’s the difference between these two lists of functions?

Beginning Python PySprings

Page 28: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Expressions

Python Math Operations

Operators:

I + - * /I % ** //I Does python obey order of operations?

Functions:

I abs bin hex oct ord roundI divmod min max powI What’s the difference between these two lists of functions?

Beginning Python PySprings

Page 29: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Expressions

Python Math Operations

Operators:

I + - * /I % ** //I Does python obey order of operations?

Functions:

I abs bin hex oct ord roundI divmod min max powI What’s the difference between these two lists of functions?

Beginning Python PySprings

Page 30: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Expressions

Python Math Operations

Operators:

I + - * /I % ** //I Does python obey order of operations?

Functions:

I abs bin hex oct ord roundI divmod min max powI What’s the difference between these two lists of functions?

Beginning Python PySprings

Page 31: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Expressions

Python Math Operations

Operators:

I + - * /I % ** //I Does python obey order of operations?

Functions:

I abs bin hex oct ord roundI divmod min max powI What’s the difference between these two lists of functions?

Beginning Python PySprings

Page 32: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Expressions

Python Math Operations

Operators:

I + - * /I % ** //I Does python obey order of operations?

Functions:

I abs bin hex oct ord roundI divmod min max powI What’s the difference between these two lists of functions?

Beginning Python PySprings

Page 33: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Expressions

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 34: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Expressions

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 35: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Expressions

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 36: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Expressions

Functions

1def hello():2print ’Howdy!’3print ’Howdy!!!’4print ’Hello there.’56hello()7hello()8hello()

Beginning Python PySprings

Page 37: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Expressions

Functions

1def hello(name):2print ’Hello ’ + name34hello(’Alice’)5hello(’Bob’)

Beginning Python PySprings

Page 38: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Expressions

Functions

1def add(a, b):2return a + b34print add(1,2)5print add(1,2) + add(3,4)

Beginning Python PySprings

Page 39: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Strings

OutlineIntroductionFirst Steps

Running PythonExpressions

Data TypesStringsListsDictionaries

LibrariesEnvironmentsThird-Party Packages

Control FlowBooleansLooping and Branching

Beginning Python PySprings

Page 40: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Strings

Strings

Examples

"Thi s i s a s t r i n g . "’ Th i s i s a l s o a s t r i n g . ’" Th i s i s ’ a ’ s t r i n g "’ Th i s i s "a" s t r i n g ’

We can also get more information from python:

>>> help(str)

Beginning Python PySprings

Page 41: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Strings

Strings

Examples

"Thi s i s a s t r i n g . "’ Th i s i s a l s o a s t r i n g . ’" Th i s i s ’ a ’ s t r i n g "’ Th i s i s "a" s t r i n g ’

We can also get more information from python:

>>> help(str)

Beginning Python PySprings

Page 42: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Strings

Strings

Examples

"Thi s i s a s t r i n g . "’ Th i s i s a l s o a s t r i n g . ’" Th i s i s ’ a ’ s t r i n g "’ Th i s i s "a" s t r i n g ’

We can also get more information from python:

>>> help(str)

Beginning Python PySprings

Page 43: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Strings

StringsMore Examples

>>> ’this is a string’.title()’This Is A String’>>> ’this is a string’.upper()’THIS IS A STRING’>>> ’what ARE you doing!?’.lower()’what are you doing!?’>>> " there’s whitespace in this ".strip()"there’s whitespace in this string."

Beginning Python PySprings

Page 44: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Strings

Hello again

hello.py

name = raw_input(’What is your name? ’)print ’Hello, ’ + name + ’!’

let’s try it!

$ python h e l l o . py

Beginning Python PySprings

Page 45: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Strings

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 46: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Strings

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 47: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Strings

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 48: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Strings

Indexing

>>> s = ’We are the Knights who say ni!’>>> s[0]’W’>>> s[-1]’!’>>> s[7:10]’the’>>> s[-7:-4]’say’

Beginning Python PySprings

Page 49: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Strings

Indexing

+---+---+---+---+---+---+| P | y | t | h | o | n |+---+---+---+---+---+---+0 1 2 3 4 5 6-6 -5 -4 -3 -2 -1

Beginning Python PySprings

Page 50: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Strings

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 51: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Strings

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 52: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Strings

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 53: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Lists

OutlineIntroductionFirst Steps

Running PythonExpressions

Data TypesStringsListsDictionaries

LibrariesEnvironmentsThird-Party Packages

Control FlowBooleansLooping and Branching

Beginning Python PySprings

Page 54: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Lists

Lists

>>> mylist = [1, 2, ’three’, "4", 5.3]>>> s = "What are the words in this string?">>> s.split()[’What’, ’are’, ’the’, ’words’, ’in’, ’this’,

’string?’]>>> words = s.split()>>> words.sort>>> words[’What’, ’are’, ’in’, ’string?’, ’the’, ’this’

, ’words’]

Beginning Python PySprings

Page 55: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Lists

ListsWhat are the methods of list?

Remember:

>>> help(list)

and try out:

>>> dir(list)

Beginning Python PySprings

Page 56: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Lists

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 57: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Lists

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 58: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Lists

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 59: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Dictionaries

OutlineIntroductionFirst Steps

Running PythonExpressions

Data TypesStringsListsDictionaries

LibrariesEnvironmentsThird-Party Packages

Control FlowBooleansLooping and Branching

Beginning Python PySprings

Page 60: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Dictionaries

Dictionaries

>>> myCat = {’size’: ’fat’, ’color’: ’gray’,... ’disposition’: ’loud’}>>> myCat[’size’]’fat’>>> ’My cat has ’ + myCat[’color’] + ’ fur.’’My cat has gray fur.’

Beginning Python PySprings

Page 61: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Dictionaries

DictionariesWhat are the methods of list?

Remember:

>>> help(dict)>>> dir(dict)

Beginning Python PySprings

Page 62: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Dictionaries

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 63: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Dictionaries

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 64: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Dictionaries

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 65: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Environments

OutlineIntroductionFirst Steps

Running PythonExpressions

Data TypesStringsListsDictionaries

LibrariesEnvironmentsThird-Party Packages

Control FlowBooleansLooping and Branching

Beginning Python PySprings

Page 66: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Environments

Environments

virtualenv

$ v i r t u a l e n v r a i n d r o p$ . r a i n d r o p / sou r c e / b i n / a c t i v a t e # Linux and OSX$ randrop \ S c r i p t s \ a c t i v a t e # Windows

Beginning Python PySprings

Page 67: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Third-Party Packages

OutlineIntroductionFirst Steps

Running PythonExpressions

Data TypesStringsListsDictionaries

LibrariesEnvironmentsThird-Party Packages

Control FlowBooleansLooping and Branching

Beginning Python PySprings

Page 68: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Third-Party Packages

Installing Third-Party Packages

$ p i p i n s t a l l r e q u e s t s

Beginning Python PySprings

Page 69: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Third-Party Packages

Finding Third-Party Packages

http://pypi.python.org

Beginning Python PySprings

Page 70: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Third-Party Packages

Requests Example

requests_script.py

1import requests2resp = requests.get(’http://httpbin.org/ip’)3print resp.json()

Beginning Python PySprings

Page 71: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Booleans

OutlineIntroductionFirst Steps

Running PythonExpressions

Data TypesStringsListsDictionaries

LibrariesEnvironmentsThird-Party Packages

Control FlowBooleansLooping and Branching

Beginning Python PySprings

Page 72: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Booleans

Booleans

>>> bool(1)True>>> bool(0)False>>> bool("no")True>>> bool("")False>>> bool([])False>>> bool([42])True

Beginning Python PySprings

Page 73: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

OutlineIntroductionFirst Steps

Running PythonExpressions

Data TypesStringsListsDictionaries

LibrariesEnvironmentsThird-Party Packages

Control FlowBooleansLooping and Branching

Beginning Python PySprings

Page 74: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Looping and Branching

>>> words = ’this is a list of words’.split()>>> for word in words:... print word.title()...ThisIsAListOfWords

Beginning Python PySprings

Page 75: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Looping and Branching

password = raw_input("Enter the secret word: "

)if password == "sesame":

print "Access granted."else:

print "Access denied!"

Boolean operators:I == != <= >= > < in

Beginning Python PySprings

Page 76: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Looping and Branching

password = raw_input("Enter the secret word: "

)if password == "sesame":

print "Access granted."else:

print "Access denied!"

Boolean operators:I == != <= >= > < in

Beginning Python PySprings

Page 77: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 78: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 79: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 80: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Looping and BranchingWhile Loop

while True:password = raw_input("Enter the secret word: ")if password == "sesame":

print "Access granted."break

else:print "Access denied!"

Beginning Python PySprings

Page 81: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 82: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 83: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Invention

I What problems, if any, did you encounter?I What mysteries, if any, did you encounter?I What other take-aways are there from this session, what could

you use from it in the future?

Beginning Python PySprings

Page 84: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Looping and Branchingelif

age = int(raw_input("How old are you? "))if age < 18:

print "You’re not old enough dance."elif age == 18:

print "Welcome, is it your first time here?"else:

print "You can dance if you want to, you can leave your friends behind."

Beginning Python PySprings

Page 85: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Practice Problems

I Write code that prints Hello if 1 is stored in spam, printsHowdy if 2 is stored in spam, and prints Greetings! if anythingelse is stored in spam.

I Write a short program that prints the numbers 1 to 10 using afor loop. Then write an equivalent program that prints thenumbers 1 to 10 using a while loop.

I Write a function named collatz() that has one parameternamed number. If number is even, then collatz() should printnumber // 2 and return this value. If number is odd, thencollatz() should print and return 3 * number + 1.

Beginning Python PySprings

Page 86: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Practice Problems

I Say you have a list value like this:

spam = [ ’ app l e s ’ , ’ bananas ’ , ’ to fu ’ , ’ ca t s ’ ]

Write a function that takes a list value as an argument andreturns a string with all the items separated by a comma and aspace, with and inserted before the last item. For example,passing the previous spam list to the function would return’apples, bananas, tofu, and cats’. But your function should beable to work with any list value passed to it.

Beginning Python PySprings

Page 87: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Practice Problems

I You are creating a fantasy video game. The data structure tomodel the player’s inventory will be a dictionary where thekeys are string values describing the item in the inventory andthe value is an integer value detailing how many of that itemthe player has. For example, the dictionary value

{ ’ rope ’ : 1 , ’ to rch ’ : 6 , ’ go l d co in ’ : 42 ,’ dagger ’ : 1 , ’ arrow ’ : 12}

means the player has 1 rope, 6 torches, 42 gold coins, and soon.

Beginning Python PySprings

Page 88: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Practice Problems

Write a function named displayInventory() that would take anypossible “inventory” and display it like the following:

I n v e n t o r y :12 arrow42 go ld co i n1 rope6 t o r ch1 daggerTota l number o f i t ems : 62

Beginning Python PySprings

Page 89: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Conclusion

I Final TakeawaysI Survey https://goo.gl/forms/ZpNl0z8pw5J8J8Rv1I Feedback – http://sayat.me/pyspringsI Based on https://automatetheboringstuff.com/

released under

Beginning Python PySprings

Page 90: Python Beginner’s Workshop · Introduction First Steps Data Types Libraries Control Flow Python Beginner’s Workshop InCollaborationwiththePikesPeakLibraryDistrict21st CenturyLibrary

Introduction First Steps Data Types Libraries Control Flow

Looping and Branching

Projects!

I Colorwallhttps://github.com/jesstess/ColorWall

I WordPlay https://github.com/jesstess/WordplayI Twitterhttps://github.com/jesstess/TwitterAPI

I General Projects Page http://wiki.openhatch.org/Boston_Python_Workshop_6/Saturday_projects

I Daily Programmerhttps://www.reddit.com/r/dailyprogrammer/

Beginning Python PySprings