Boost Fertility New Invention Ups Success Rates.pdf
Introduction to python 3
1. Introduction to Python 3
“Your first scripting in Python 3.x” workshop
Youhei Sakurai, TC, CSS, YSJ
APAC-EU time zone, 30th September, 2016
2. 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.
3. 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.
4. 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
5. 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.
7. What’s Python
• One of programing languages like Java, C, Perl, Ruby, etc
• No compilation, no type definitions, no magic, no callback hell
• Well documented, cross platform, multi-purpose glue language
• Used in Amazon, Google, Yahoo, Dropbox, NASA, etc
OK, but why Python?
8. Why Python
• Batteries included
• Web server, database, GUI, networking, regex, XML, JSON, archiving, C/C++
integration, package manager, async I/O, serialization, etc.
• Not a toy, powerful enough
• Can write anything from one tiny script to large scaled enterprise application.
• Defacto standard language in IT industry
• Even I can write Python codes better than English email.
Sounds like none of my business…
9. Why Python for you
• Python runs anywhere
• You can carry the most professional toolset everywhere
• Python makes sense very well
So, is Python perfect?
10. A few disadvantages in Python
• Slower than C/C++ and assembly
• No JIT (Just-In-Time) compiler equipped in CPython
• GIL (Global Interpreter Lock) prevents real parallelism in CPython
• Jython and IronPython falls much behind CPython
Who cares?
12. A bit more about Python
History of Python
• Created in 1989 by Guido Van Rossum
• Python 1.0 released in 1994
• Python 2.0 released in 2000
• Python 3.0 released in 2008
• Python 2.7 released in 2010
• Python 3.5 released in 2015
• No Python 2.8 planned (*1)
• EOL of Python 2.7 planed in 2020 (*2)
*1 … PEP 404, *2 … PEP 373
Zen of Python (by Tim Peters)
• Beautiful is better than ugly.
• Explicit is better than implicit.
• Simple is better than complex.
• Complex is better than complicated.
• Flat is better than nested.
• Sparse is better than dense.
• Readability counts.
• Special cases aren't special
enough to break the rules.
…
14. How Python looks like
Interactive shell - Windows
• Just run `python`
Interactive shell - Unix
• Just run `python` or `python3`
Version infoType any codes you want to run
15. How Python looks like
Script
1. Create e.g. `script.py` file
2. Save it as UTF-8 text file
Command line interface
• Run `python script.py`
No logo output by default
16. What’s Python
How Python looks like
Let’s set up Python
Make Python ready
Make yourself ready
Get breadcrumb list
17. Let’s set up Python
Windows / Mac OS X
1. Download installer
2. Double-click installer
Windows
-> Add Python to PATH
Mac OS X
-> Do NEVER fix system Python
3. Complete installation
Linux (Debian/Ubuntu)
1. Retrieve packages list
apt-get update
2. Install Python 3.x
apt-get install python3
3. Install pip
apt-get install python3-pip
19. 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
• `?` -> help
• `??` -> see source
• `_` -> last output
• Auto indentation, [Ctrl]+c capturing, search ([Ctrl]+r), etc
20. Let’s install ipython using pip
1. Ensure connectivity to the Internet
2. Type `pip install ipython`
3. Hit [Enter]
…
21. What’s Python
How Python looks like
Let’s set up Python
“Hello world!”
✓Make Python ready
Make yourself ready
Get breadcrumb list
22. “Hello world!”
1. Run `python` or `ipython`
2. Type `print(“Hello world!”)`
3. Hit [Enter]
23. How Python looks like
Let’s set up Python
“Hello world!”
Do basic practices
✓Make Python ready
Make yourself ready
Get breadcrumb list
24. Differentiations from C style
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
25. Do basic practices – if … elif … else
1. Run `ipython`
2. Type code(s)
3. Hit [Enter]
26. Do basic practices – list, len(…)
1. Run `ipython`
2. Type code(s)
3. Hit [Enter]
27. Do basic practices – for … in …, range(…)
1. Run `ipython`
2. Type code(s)
3. Hit [Enter]
28. Do basic practices – while …, import
1. Run `ipython`
2. Type code(s)
3. Hit [Enter]
29. Do basic practices – open, “…”, b”…”
1. Run `ipython`
2. Type code(s)
3. Hit [Enter]
31. Do basic practices – open, “…”, b”…”
1. Run `ipython`
2. Type code(s)
3. Hit [Enter]
32. Do basic practices – urlopen
1. Run `ipython`
2. Type code(s)
3. Hit [Enter]
33. Let’s set up Python
“Hello world!”
Do basic practices
Your first scripting
✓Make Python ready
✓Make yourself ready
Get breadcrumb list
34. 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?
35. “Hello world!”
Do basic practices
Your first scripting
Study materials
✓Make Python ready
✓Make yourself ready
Get breadcrumb list
36. 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