get connected with python

39
#GetConnected with Jan Kroon Hogeschool Rotterdam Workshop 1

Upload: jan-kroon

Post on 20-Jul-2015

169 views

Category:

Social Media


0 download

TRANSCRIPT

Page 1: Get connected with python

#GetConnected with

Jan Kroon Hogeschool Rotterdam

Workshop 1

Page 2: Get connected with python

–Steve Jobs

"Computers are like a bicycle for our minds."

https://www.youtube.com/watch?v=ob_GX50Za6c

Page 3: Get connected with python

Computer Programming• Programming: to build a machine that does your work

• Whenever you enter a search term in Google, you are programming Google to search for you …

• but if you need to think about AND and/or OR and "grouping of search terms" most people give up.

• A computer is a general purpose machine: a calculator, a word processor, a drawing machine, a search machine, a remember machine, an analytics machine, an information machine, a communications machine, a game machine, a navigation machine, a music machine, a video machine, a design machine …

• If you learn how to program you can build these (and many other) machines!

Page 4: Get connected with python

Programming LanguagesExpressive Power:

1 line of code in Python, Perl, R or Ruby

3 lines of code in C++, C# or Java

10 lines of code in C

50-100 lines of code in Machine Language or Assembler

Page 5: Get connected with python

Python 3.4.0

In this workshop you will learn to write Python One Liners!

Page 6: Get connected with python

Guido van Rossum Benevolent Dictator For Life

Page 7: Get connected with python
Page 8: Get connected with python

Object Oriëntation

Page 9: Get connected with python

'Edwin Jansen' is a 'Plumber'

'Edwin Jansen' has properties: • edwinJansen.phoneNumber

• edwinJansen.available

'Edwin Jansen' has skills to do things: • edwinJansen.fixTap()

• edwinJansen.repairRoof()

>>> import plumber and let a plumber do the work!

Object Class

Methods (verbs)

Properties (nouns)

Page 10: Get connected with python

Everything is an object!• Whole Numbers: int

• Broken numbers: float

• Text: str

• User Interface Elements: Windows, Buttons, Sliders, TextFields, ImageViews, MapViews.

• Containers: list, dict, tuple, …

Page 11: Get connected with python

Assignment 0

Install Python on your laptop (use Google to look for Python / IDLE download)

Page 12: Get connected with python

IDLE Integrated DeveLopment Environment

Page 13: Get connected with python

Programmer's Workbench

Page 14: Get connected with python

Console "Python chat"

Editor "Python email"

Run menu Run Module

Syntax Colour Code?

Page 15: Get connected with python

Happy Coding!

print("Hello World")

help()

help(print)

type("Hello World")

IDLE

dir()

Page 16: Get connected with python

Assignment 1

Write your first computer program, a classic:

>>> print("Hello World")

Page 17: Get connected with python

Assignment 2

Introduce yourself:

>>> myName = "…"

>>> print("Hello", myName)

New Object!

Page 18: Get connected with python

Assignment 3

Introduce your group:

>>> ourGroup = ["Komala Mazerant", …]

and say hello to everyone (except yourself!)

Page 19: Get connected with python

Iteration (Looping)

>>> for student in ourGroup:

print("Hello", student)

Page 20: Get connected with python

Condition (Skipping)

… == … means 'is equal to'

… != … means 'is not equal to'

>>> for student in ourGroup:

if student != myName:

print("Hello", student)

Page 21: Get connected with python

Assignment 4Find out the class of the objects you created:

>>> type(…)

find out their size (length)

>>> len(…)

and find out what you can do with these objects

>>> dir(…)

and if you want detailed information use

>>> help(…)

Page 22: Get connected with python

Dynamic TypingTry this:

>>> aNumber = 747

>>> aNumber + aNumber

>>> sameNumber = str(aNumber)

>>> sameNumber + sameNumber

>>> anotherNumber = int(sameNumber)

>>> anotherNumber + anotherNumber

What is going on here???

Page 23: Get connected with python

Juggle with typesMake a 'class list' object from a 'class string' object:

>>> aQuote = "Computers are like a bicycle for our minds"

>>> quoteWords = aQuote.split()

and turn the words back into a sentence"

>>> aSentence = ""

>>> for word in quoteWords:

aSentence = aSentence + " " + word

Page 24: Get connected with python

Assignment 5Copy the contents of theJungle in an object:

>>> theJungle = "…"

A. Is there a lion in the jungle?

B. Where do you encounter the first tiger?

C. How many trees in the jungle?

Page 25: Get connected with python

Selection (Slicing)

>>> aStudent = ourGroup[1]

>>> someStudents = ourGroup[2:5]

>>> lastStudent = ourGroup[-1]

Page 26: Get connected with python

Assignment 6

Find palindromes in 'words' list:

>>> words = […]

Page 27: Get connected with python

Big Data

Page 28: Get connected with python

Assignment 7Browse to: https://twitter.com/search-advanced

A. How many tweets today in Rotterdam?

B. Any tweets with tag #GetConnected?

C. What is last tweet from @BarackObama

D. More positive or more negative tweets last week about @BarackObama?

Page 29: Get connected with python

Analytics1. Collect Data

2. Clean Data

3. Wrangle, Merge, Aggregate

4. Visualize, Summarize Data

5. Interpret, Develop Insights

6. Act!

Page 30: Get connected with python

Collect Data>>> from urllib import request

>>> twitterHandle = request.urlopen("http://twitter.com")

>>> twitterData = twitterHandle.read()

>>> twitterText = str(twitterData)

>>> twitterText[0:500]

>>> len(twitterText)

Page 31: Get connected with python

Assignment 8Copy the contents of aBusinessCard and someBusinessCards in two objects:

>>> aBusinessCard = […]

>>> someBusinessCards = […]

A. How can you recognize a phone number or an email address in aBusinessCard

B. Print a list of names extracted from someBusinessCards

C. Print a list of email addresses extracted from someBusinessCards

Page 32: Get connected with python

Assignment 9Copy the contents of tweets in an object:

>>> tweets = […]

A. How many tweets?

B. How many from Rotterdam?

C. Who has sent tweets with hashtag #GetConnected?

Page 33: Get connected with python

Assignment 10Copy the contents of twitterFollowers in an object:

>>> tf = "…"

A. Is there overlap in the followers of @KomalaDubois and @jankroon?

B. What would be the possibilities if you would know all followers of all people on Twitter?

Page 34: Get connected with python

Algorithms• Dictionary attack to break password protection

• Harvesting email addresses for spamming

• Recommendations: "People who rented this movie also rented …"

• Segmentation: clustering people that are similar in some respect

• Outlier detection: discover exceptional behavior, i.e. to prevent credit card fraud

Page 35: Get connected with python

Visualize DataOur eyes are extremely good at finding patterns

• Mathematical Graphs

• Statistical Diagrams

• Maps

• Timelines

• Network Graphs

Page 36: Get connected with python

Ice Bucket Challenge

https://srogers.cartodb.com/viz/89e638ac-2a2c-11e4-9cb7-0e73339ffa50/embed_map

Page 37: Get connected with python

10.000 hours of practice

Malcom Gladwell states in his book 'Outliers' that you need to practice for at least 10.000 hours in

order to master a specific skill.

8 hours/day * 7 days/week * 52 weeks/year * 3,5 year = 10192 uur

0,1%

Page 38: Get connected with python

Backup Slides

Page 39: Get connected with python

{ "coordinates": null, "created_at": "Thu Oct 21 16:02:46 +0000 2010", "favorited": false, "truncated": false, "id_str": "28039652140", "entities": { "urls": [ { "expanded_url": null, "url": "http://gnip.com/success_stories", "indices": [ 69, 100 ] } ], "hashtags": [   ], "user_mentions": [ { "name": "Gnip, Inc.", "id_str": "16958875", "id": 16958875, "indices": [ 25, 30 ], "screen_name": "gnip" } ] }, "in_reply_to_user_id_str": null, "text": "what we've been up to at @gnip -- delivering data to happy customers http://gnip.com/success_stories", "contributors": null, "id": 28039652140, "retweet_count": null, "in_reply_to_status_id_str": null, "geo": null, "retweeted": false, "in_reply_to_user_id": null, "user": { "profile_sidebar_border_color": "C0DEED", "name": "Gnip, Inc.", "profile_sidebar_fill_color": "DDEEF6", "profile_background_tile": false, "profile_image_url": "http://a3.twimg.com/profile_images/62803643/icon_normal.png", "location": "Boulder, CO", "created_at": "Fri Oct 24 23:22:09 +0000 2008", "id_str": "16958875", "follow_request_sent": false, "profile_link_color": "0084B4", "favourites_count": 1, "url": "http://blog.gnip.com", "contributors_enabled": false, "utc_offset": -25200, "id": 16958875, "profile_use_background_image": true, "listed_count": 23, "protected": false, "lang": "en", "profile_text_color": "333333", "followers_count": 260, "time_zone": "Mountain Time (US & Canada)", "verified": false, "geo_enabled": true, "profile_background_color": "C0DEED", "notifications": false, "description": "Gnip makes it really easy for you to collect social data for your business.",