38
Introduction to Python 3 “Your first scripting in Python 3.x” workshop Youhei Sakurai, TC, CSS, YSJ EU-US time zone, 31 October, 2016

Introduction to python 3 2nd round

Embed Size (px)

Citation preview

Page 1: Introduction to python 3   2nd round

Introduction to Python 3“Your first scripting in Python 3.x” workshop

Youhei Sakurai, TC, CSS, YSJ

EU-US time zone, 31 October, 2016

Page 2: Introduction to python 3   2nd round

Youhei Sakurai

• Intermediate Pythonista• Started to learn Python in 2010.

• Have been introducing Python several times since 2011.

• Publishing one package on PyPI – PyMemoryModule.

• My name is written in change log of lxml 3.6.0.

Page 3: Introduction to python 3   2nd round

Rules

• Will finish on time even if some of topics are not completed.

• Turn on your web-cam to enhance your active involvement.

• Raise your questions to drive bi-directional communication.

• If it is beyond beginner’s level, I’d suggest to have separate call.

• If there’re too many, I’d select rather important ones for everyone.

• Stay sharp with your text editor and Python installer.

Page 4: Introduction to python 3   2nd round

Agenda

• What’s Python

• How Python looks like

• Let’s set up Python

• “Hello world!”

• Do basic practices

• Your first scripting

• Study materials

• Kahoot quiz

Page 5: Introduction to python 3   2nd round

Goals

• Make Python ready on your workstation.

• Make yourself ready in the Python 3.x Ocean.

• Get breadcrumb list to learn Python 3.x.

• And... Let you get high score at Kahoot quiz.

Page 6: Introduction to python 3   2nd round

What’s PythonMake Python ready

Make yourself ready

Get breadcrumb list

Page 7: Introduction to python 3   2nd round

Python is,,,

• Programing language like Java, C, Perl, Ruby, etc

• Easy to run because of cross platform scripting language

• Clear look & feel thanks to language design and culture hating magics

• Able do anything (multi-purpose glue language) except writing OS/drivers

• Used everywhere e.g. in Amazon, Google, Yahoo, Dropbox, NASA, etc

Page 8: Introduction to python 3   2nd round

Most attractive features

•Batteries included• Web server, database, GUI, networking, regex, XML, JSON, archiving, C/C++

integration, thread/process pooling, async I/O, serialization, etc.

•More batteries invented• Intelligences are innovating technologies all over the world day by day.• You can try the innovations using pip through the Internet.

•Easier than English• Even I can write Python codes better than English email. • All you need is not LOVE but intelligence with a little knowledge about Python.

Page 9: Introduction to python 3   2nd round

My favorites

• Python runs everywhere

• Python allows me to explain things very clearly

Page 10: Introduction to python 3   2nd round

A few disadvantages

• Slower than C/C++ and assembly

• No JIT (Just-In-Time) compiler equipped in CPython

• Jython and IronPython falls much behind CPython

• Weaker multithreading due to GIL in CPython

Who cares?

Page 11: Introduction to python 3   2nd round

So Python could be also,,,

Something special making your professional life much more brilliant never ever before!!

Page 12: Introduction to python 3   2nd round

A bit more about Python

History of Python• Created in 1989 by Guido Van Rossum

• Python 2.7 released in 2010

• Python 3.5 released in 2015

PEP 404 - No 2.8 planned

PEP 373 - EOL of 2.7 planed in 2020

Zen of Python (by Tim Peters)• Beautiful is better than ugly.

• Explicit is better than implicit.

• Simple is better than complex.

Page 13: Introduction to python 3   2nd round

What’s Python

How Python looks likeMake Python ready

Make yourself ready

Get breadcrumb list

Page 14: Introduction to python 3   2nd round

How Python looks like

Interactive shell

• Just run `python` or `python3`

Script

1. Create e.g. `script.py` file

2. Save it as UTF-8 text file

3. Run `python script.py`

Version info

Type any codes

Page 15: Introduction to python 3   2nd round

Differentiations from C style code

1. `:` + 4x space instead of `{ … }`

2. Pascal-like operators -> `and` `or` `not`

3. Bash-like comment -> starting with `#`

4. No difference between `’` and `”`

5. No need to put `;` at the end of line

sample.py sample.c

Page 16: Introduction to python 3   2nd round

What’s PythonHow Python looks like

Let’s set up PythonMake Python ready

Make yourself ready

Get breadcrumb list

Page 17: Introduction to python 3   2nd round

Let’s set up Python

Windows / Mac OS X

1. Download installer

2. Double-click installerWindows

-> Add Python to PATH

Mac OS X

-> Do NEVER fix system Python

3. Complete installation

Linux (Debian/Ubuntu)

1. Retrieve packages listapt-get update

2. Install Python 3.xapt-get install python3

3. Install pipapt-get install python3-pip

Page 18: Introduction to python 3   2nd round

Where to install?

`C:¥Python35` and `C:¥Python35-x64` IMHO

Page 19: Introduction to python 3   2nd round

Let’s install ipython using pip

1. Ensure connectivity to the Internet

2. Type `pip install ipython`

3. Hit [Enter]

Page 20: Introduction to python 3   2nd round

pip and ipython

•pip … Package manager in Python

• Packages are on public repository similarly with apt, ppm, npm, etc

• ipython … Enhanced interactive shell

• [TAB] -> completion

• `?` -> show help

• `??` -> show source

• `_` -> refer last output

• Auto indentation, [Ctrl]+c capturing, search ([Ctrl]+r), etc

Page 21: Introduction to python 3   2nd round

What’s PythonHow Python looks like

Let’s set up Python

“Hello world!”✓Make Python ready

Make yourself ready

Get breadcrumb list

Page 22: Introduction to python 3   2nd round

“Hello world!”

1. Run `python` or `ipython`

2. Type `print(“Hello world!”)`

3. Hit [Enter]

Page 23: Introduction to python 3   2nd round

How Python looks likeLet’s set up Python

“Hello world!”

Do basic practices✓Make Python ready

Make yourself ready

Get breadcrumb list

Page 24: Introduction to python 3   2nd round

Differentiations from C style code

1. `:` + indentation instead of `{ … }`

2. Pascal-like operators -> `and` `or` `not`

3. Bash-like comment -> starting with `#`

4. No difference between `’` and `”`

5. No need to put `;` at the end of line

sample.py sample.c

Page 25: Introduction to python 3   2nd round

Do basic practices – if … elif … else

1. Run `ipython`

2. Type code(s)

3. Hit [Enter]

Page 26: Introduction to python 3   2nd round

Do basic practices – list, len(…)

1. Run `ipython`

2. Type code(s)

3. Hit [Enter]

Page 27: Introduction to python 3   2nd round

Do basic practices – for … in …, range(…)

1. Run `ipython`

2. Type code(s)

3. Hit [Enter]

Page 28: Introduction to python 3   2nd round

Do basic practices – while …, import

1. Run `ipython`

2. Type code(s)

3. Hit [Enter]

Page 29: Introduction to python 3   2nd round

Do basic practices – open, “…”, b”…”

1. Run `ipython`

2. Type code(s)

3. Hit [Enter]

Page 30: Introduction to python 3   2nd round

str object v.s. bytes object

str – String

"text"

bytes – Binary data

b"¥x74¥x65¥x78¥x74"

(= b"text")

"text".encode()

b"text".decode()

Page 31: Introduction to python 3   2nd round

Do basic practices – open, “…”, b”…”

1. Run `ipython`

2. Type code(s)

3. Hit [Enter]

Page 32: Introduction to python 3   2nd round

Do basic practices – urlopen

1. Run `ipython`

2. Type code(s)

3. Hit [Enter]

Page 33: Introduction to python 3   2nd round

Let’s set up Python“Hello world!”

Do basic practices

Your first scripting✓Make Python ready

✓Make yourself ready

Get breadcrumb list

Page 34: Introduction to python 3   2nd round

Your first scripting

• I am going to write dummy Web server.• My script will do:

• Listen on 80/tcp.• Accept incoming connections.• Respond random number of words.

• You are going to write HTTP client.• Your script will do:

• Access server via HTTP.• Read content from response object. <- How to decode binary data?• Split content by white space. <- How to split string?• Print how many words in response. <- How to count list of strings?

Page 35: Introduction to python 3   2nd round

“Hello world!”Do basic practices

Your first scripting

Study materials✓Make Python ready

✓Make yourself ready

Get breadcrumb list

Page 36: Introduction to python 3   2nd round

Study materials

•The Python Tutorial• Describes all you need to know about Python

•Learn Python the Hard Way• Gives practical tasks to make you able to write Python codes

•GitHub• Guides how you should write well-mannered Python codes

Page 37: Introduction to python 3   2nd round

Congratulations!! ✓Make Python ready

✓Make yourself ready

✓Get breadcrumb list

Page 38: Introduction to python 3   2nd round

Thank you for your participation.Open Kahoot! - https://kahoot.it