For whom?
This talk is for those who want to start with Python or those hoping to see a new trick.
It’s enough, I would say!
But we have options.
Participants! Sponsors !! Helping hands !!! Organizers
of this marvellous and amazing event!
I’m not saying “Do everything in Python”
“You’ll gain a lot if you are ready for the first steps with Python and if you don’t avoid it.”
a programmer is more thought of as an allrounder. It’s expected that you understand at least several of these:
Bash, C, Cobol, Coffeescript, Css, Haml, Haskell, Html, Java, Javascript, Pascal, Perl, Php, Python, Typescript, Typoscript, Xml, and so on.
Just the other day I saw this ranking of programming languages (June 2015):
What’s most popular?
Yes!
Yes, it’s definitely worth the effort:
And, NO
Find out if Python is installed already.
Get yourself a commandline and type: python --version
Hopefully you’ll see something like this:
Go to https://www.python.org/
Go to Downloads
Go to https://www.python.org/
Go to Downloads
Python is most probably installed. If not, use the packet manager of your system:
sudo apt-get install python
Go to https://www.python.org/
Go to Downloads
Pick an installer for Windows:
Tip
C:\Python27\Scripts
and - maybe - C:\Python27\Tools\Scripts
are in the path.py
to the PATHEXT
system variable if you want to be able to start script program.py
by
just typing program
.Python opens in dialog mode:
$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> 1 + 1
2
>>> 'abc' + 'bcd'
'abcbcd'
>>> 3 * 5
15
>>> 'abc,' * 5
'abc,abc,abc,abc,abc,'
>>> ['a list'] * 3
['a list', 'a list', 'a list']
>>> exit()
>>> 2 ** 2
4
>>> 2 ** 22
4194304
>>> 2 ** 222
6739986666787659948666753771754907668409286105635143120275902562304L
>>> 2 ** 2222
7738385577878121270921913291177363957675805352044369150726746755451166643339
0225621118213792695021175190244951667322675647283977978393877115802136888227
6356848070794209358070102043443325352136545050000110534739684220468927252571
8545228637585111967234385629106091901532146690670721017896215421721054251511
3476510755470378003616234600251330288404641161878732714654916869730107991804
7772519772786759323456323386160178483395107203321577048282851869205647641463
3106982424173128829195665264713264067983600100179414810240875191907508744662
4702169205467746275821767903148850994094812935433097063308327801207416503803
3940046145844629478331113175706858291419937603163400966242304L
>>> 'string'
'string'
>>> "string"
'string'
>>> '''
... multiline string
... '''
'\nmultiline string\n'
>>> """multiline string"""
'multiline string'
>>> r'a raw string: C:\Python27'
'a raw string: C:\\Python27'
>>> u'unicode constants'
u'unicode constants'
Indentation instead!
Best practise: four blanks:
if 1 and 'say hello':
msg = "Hello World!"
print(msg)
Lists will look very much like Json.
Tuples are like lists, but immutable.
Consistent:
>>> '0123456789'[2:5] # string
'234'
>>> (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)[2:5] # tuple
(2, 3, 4)
>>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9][2:5] # list
[2, 3, 4]
There is a rather small set of builtin functions plus the builtin types with objects methods
Most of the functionality is in library function and needs to be imported. These modules have different namespaces by default:
>>> import math
>>> math
<module 'math' (built-in)>
>>> math.sqrt
<built-in function sqrt>
>>> math.sqrt(36)
6.0
>>> from math import sqrt
>>> sqrt
<built-in function sqrt>
>>> sqrt(36)
6.0
>>> exit()
def myfunc(a, b, args*, kwargs**):
pass
myfunc(a, b)
myfunc(a, b, c, .., x)
myfunc(a, b, host='localhost', db='test')
myfunc(a, b, c, ..., x, host='localhost', db='test', ..., name=value)
This is well defined in chapter Truth Value Testing.
False is:
None, False, Zero number, an empty sequence, an empty mapping and instances of classes, that return 0 by bool() oder len().
This means: You most often just need to write if thing: then ...
:
result = myfunc()
if result:
"do something"
I often write:
if 1 and 'explanation':
'do something'
Because I can turn off a whole codeblock easily:
if 0 and 'explanation':
'do something'
Easy, well readable, because highlighting remains, the compiler optimises this away.
One of the most notable changes is that print
is function
in Python 3 while it’s a statement with special syntax in Python 2.
Import “the future” to use the futureproof print function in Python 2 already:
$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> print
>>> from __future__ import print_function
>>> print
<built-in function print>
>>> print("Hello world!")
Hello world!
You cannot use uninitialised variables:
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
Type conversion never happens implicitely:
>>> zero_string = '0'
>>> zero_string == 0
False
There’s a wide range of possible applications:
Complete commandline program to list all links of a webpage:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import bs4, urllib2, sys
if len(sys.argv) != 2:
print "usage: dumplinks.py <url>"
exit(1)
url = sys.argv[1]
data = urllib2.urlopen(url).read()
soup = bs4.BeautifulSoup(data)
hits = soup.find_all('a')
for hit in hits:
if hit.attrs.has_key('href'):
print hit.attrs['href']
$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def gimme():
... for item in ['Let me', 'show you', 'something']:
... yield item
... for item in [1, 2, 3]:
... yield item
...
>>> for item in gimme():
... print item
...
Let me
show you
something
1
2
3
Pycharm from Jetbrains!
Enter search terms.