Python Power

What we'll cover

  • Basics
  • Language Features
    • Operator Overloading
    • Duck Typing
    • Multiple Inheritance
    • Comprehensions
  • PIP
  • Popular applications of Python
    • Pandas/numpy
    • Django

The Basics: Numbers

# Dynamic typing means different number types based on syntax

my_num = 1    # integer 
my_num = 1.0  # float (all floats are doubles)
my_num = 1.   # also float!

# Most conversion can be done automatically

>>> 1.1 * 2
2.2

# But some can be tricky

>>> 1/2
0

>>> 1./2
0.5
  • All floats are double percision
  • In Python3, integers have no defined limit

The Basics: Strings

>>> "Hello world"[0] # iterable!
"H"

>>> "Hello world"[:5] # iterable!
"Hello"

>>> for letter in "yams": # iterable!
...    print letter.upper()
Y
A
M
S

# Most operators work with strings

>>> "wow" * 3
"wowwowwow"

>>> "hello " + "world"
"hello world"

# And unlike JavaScript, things make sense!
>>> "hello" + 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects

The Basics: Functions

The Basics: Objects

Operator Overloading

  • Python has many magic methods and names
  • Magic methods begin and end with two underscores
    • __init__, __add__, __name__
  • We can override magic methods to change behaviour

Operator Overloading

Operator Overloading

Duck Typing

  • Duck typing is almost the absence of actual typing
    • Objects are typed based on their attributes
    • If the attributes match, the types match
    • An object is of any type until... it isn't
  • Python uses duck typing, but objects still have types
    • You shouldn't care what type of object you have

    • Care if you can do the required action with your object

  • If types don't match, a TypeError or AttributeError is raised

Duck Typing

>>> a = [0,1 2, 3]
>>> print a[0]
0
>>> b = {'a': 0, 'b': 1}
>>> print b['a']
0

# Is the same as

>>> a =  [0,1 2, 3]
>>> print list.__getitem__(a, 0)
0
>>> b = {'a': 0, 'b': 1}
>>> print dict._getitem__(b, 'a')
0


# Both lists and dictionaries are supported by this method:
def getit(item, key):
    return item[key]

Multiple Inheritance

Multiple Inheritance

Comprehensions

PIP

  • the npm of Python
  • dope

Pandas/NumPy

  • Math!
  • Popular in data science, ML
  • Slower than something like C, but not if you include the time it takes to write it
  • Loosely types languages are great for data science
  • Python's built in number support helps power other libraries
    • Fraction, Decimal types
    • ints with no max size
    • Simple syntax for integer operations

Django

  • The most popular Python web framework
  • Other include:
    • Flask
    • Twisted
  • Highly pragmatic, convention based framework
  • Built in admin panel for working with models
  • Decent ORM
  • Control all database migrations in Python, most autogenerated

thanks yo

Python Power

By Jamie Counsell

Python Power

  • 1,221