PYTHON
& YOU
NETWORK WEEK 2015 WORKSHOP
SIAN LERK LAU
linkedin.com/in/sianlerk
sianlerk.lau@onapp.com | kiawin@gmail.com
Software Engineer
Volunteer
Educator-in-Exile
https://twitter.com/OnApp - https://facebook.com/OnApp
WONDERFUL THINGS WE DO =)
Python, Java, Ruby, Lua, Nginx, Wowza, Puppet, Vagrant,
Docker, Debian, Cucumber, RabbitMQ, MariaDB, MongoDB,
ELK, etc.
WE’RE HIRING!
SYSADMIN as integral role in managing and
develop tools for our ecosystem
DEV as engineering role in creating bleeding edge
applications for our ecosystem
YES, INTERNS TOO!
R U
REHDY
TO PYTHON*
FIVE
DEADLY
SINS
OF COMPUTER SCIENCE
I DO EVERYTHING
EXCEPT
int main(void);
NO. 5
I DO CODING
EXCEPT
for (;;);
NO. 4
I DO EVERYTHING
EXCEPT
MATHS
NO. 3
I DO NOTHING
EXCEPT
<NAME YOUR LANGUAGE>
NO. 2
I DO NOTHING
EXCEPT
MY {WORK, STUDY}
NO. 1
EVER
CHANGING
INDUSTRY
OF COMPUTER SCIENCE
SOFTWARE
DEVELOPMENT
IS ABOUT
LEARNING
NO. 5
SOFTWARE
DEVELOPMENT
IS ABOUT
ENGINEERING
NO. 4
SOFTWARE
DEVELOPMENT
IS ABOUT
SPEED
NO. 3
SOFTWARE
DEVELOPMENT
IS ABOUT
BUSINESS
NO. 2
SOFTWARE
DEVELOPMENT
IS ABOUT
COMMUNITY
NO. 1
SOFTWARE
DEVELOPMENT
IS ABOUT
YOU
NO. 0
DO.
* WHY PYTHON
* THE ENVIRONMENT
* PYTHON 101
* CONTROL STRUCTURES
* A REAL PROGRAM
WHY*
CODE IN PYTHON
EASY TO UNDERSTAND
CONCISE SYNTAX
MULTI-PURPOSE
STRENGTH OF PYTHON
GOOGLE-ABLE!
WELL SUPPORTED LIBS
FAST TO DELIVER
STRENGTH OF PYTHON
ENV*
PYTHON ENVIRONMENT
# INSTALL PACKAGES
$ sudo apt-get install python-pip
python-virtualenv
# SETUP ENVIRONMENT
$ virtualenv workshop-env
# ENABLE ENVIRONMENT
$ source workshop-env/bin/activate
ENV* - ENVIRONMENT ISOLATION
101*
PYTHON - DATA TYPES
# DECLARE-LESS TYPED
>>> a = 1
>>> print a
1
>>> type(a)
<type 'int'>
101* - DATA TYPES
# MORE TYPES - '' and ""
>>> b = 'a'
>>> c = 'abc'
>>> d = "abc"
# QUIZ: WHAT DATA TYPES ARE b, c and d?
>>> type(b)
>>> type(c)
>>> type(d)
101* - DATA TYPES
# MORE TYPES
# bool, NoneType, float, long
>>> e = True
>>> f = False
>>> g = None
>>> h = 1.0
>>> i = 1L
# QUIZ: WHAT IS None?
101* - DATA TYPES
101*
PYTHON - COLLECTIONS
# COLLECTION 1: LIST
>>> a = [1, 2, 3]
>>> b = list()
>>> b.append(1)
>>> b.append(2)
>>> b.append(3)
# QUIZ: How do we retrieve the value?
# QUIZ: Is a and b same?
101* - DATA TYPES
# SAME - EQUALITY or IDENTITY?
>>> a == b
True
>>> a is b
False
# QUIZ: WHAT IS THE DIFF
# BETWEEN == AND is
101* - DATA TYPES
# COLLECTION 2 - TUPLE
>>> c = (1, 2, 3)
>>> d = 1, 2, 3
# QUIZ: SO AGAIN, IS c SAME with d?
101* - DATA TYPES
# COLLECTION 3: dict
>>> e = {1: 11, 2: 22}
>>> e[1]
>>> e[2]
# QUIZ: IS THIS AN array?
# QUIZ: MUST THE key BE int?
101* - DATA TYPES
# COLLECTION 4: set
>>> f = set()
>>> f.add(1)
>>> f.add(2)
>>> f.add(3)
# QUIZ: WHAT IS THE DIFF
# BETWEEN list AND set?
101* - DATA TYPES
101*
PYTHON - CONTROL STRUCTURES
101* - DATA TYPES
# if, else, elif
>>> a = 2
>>> if a == 1:
... print "hello"
... elif a == 2:
... print "world"
>>>
101* - DATA TYPES
# if, else, elif CONTINUES
>>> a = 1
>>> if a == 1:
... print "hello"
... else:
... print "world"
>>>
101* - DATA TYPES
# for LOOP
>>> for i in [1, 2, 3]:
... print i
>>> for j in range(1,3):
... print j
# QUIZ: WHAT DO YOU SEE WHEN print j
101* - DATA TYPES
# for LOOP
>>> for i in (1, 2, 3):
... print i
>>> for k,v in {1: 11, 2: 22}.iteritems():
... print k, v
# QUIZ: WHAT DO YOU SEE WHEN print k, v
101* - DATA TYPES
# list comprehension
>>> a = [1,2,3,4,5]
>>> b = [i+1 for i in a]
# QUIZ: WHAT IS THE VALUE OF b?
101* - DATA TYPES
# list comprehension
>>> a = [1,2,3,4,5]
>>> b = [i for i in a if i % 2 == 0]
# QUIZ: WHAT IS THE VALUE OF b?
PROG*
A REAL PROGRAM, NOW
# CREATE AN EXECUTABLE FILE
$ touch calculator.py
$ chmod +x calculator.py
# QUIZ: MUST YOU END THE FILENAME WITH
# A FILE EXTENSION?
PROG* - A REAL PROGRAM
# IMPROVISE IT
$ vim calculator.py
$ nano calculator.py
<or, use your favourite text editor>
# RUN IT
$ ./calculator.py
PROG* - A REAL PROGRAM
Q&A*
ASK ME ANYTHING

Python and you