Wednesday, July 5, 2017

Summer of code 2017: Python, Day 18 Iterables


As explained in my Summer of code 2017: Python post I decided to pick up Python

This is officially day 18. today I will take a look at iterables


Comprehensions

A comprehension is a list, a set or a dictionary that is computed via a set of looping and filtering instructions. The comprehension consists of a single expression followed by at least one for clause and zero or more for or if clauses

List Comprehensions

The basic syntax for list comprehensions looks like this

[expr(item) for item in iterable]

Here is an example in code, the code below returns the length for each item in the list

>>> words= "A comprehension is a list, a set or a dictionary that is computed via a set
 of looping and filtering instructions".split()
>>> words
['A', 'comprehension', 'is', 'a', 'list,', 'a', 'set', 'or', 'a', 'dictionary', 'that',
 'is', 'computed', 'via', 'a', 'set', 'of', 'looping', 'and', 'filtering', 'instructions']
>>> [len(word) for word in words]
[1, 13, 2, 1, 5, 1, 3, 2, 1, 10, 4, 2, 8, 3, 1, 3, 2, 7, 3, 9, 12]
>>> 

Pretty simple stuff, nothing too complicated here


Set Comprehensions

The basic syntax for set comprehensions looks like this

{expr(item) for item in iterable}

Here is an example in code, the code below returns the length as well as the max character for each item of the set

>>> x = {'abc','def','vvv','nnn'}
>>> type(x)
<class 'set'>
>>> {len(str(x)) for x in x}
{3}
>>> {max(str(x)) for x in x}
{'f', 'v', 'n', 'c'}
>>> 

As you can see the set is distinct, the number 3 is only returned once. Also the set is unordered



Dictionary Comprehensions


The basic syntax for dictionary comprehensions looks like this

{ key_expr:value_expr  for item in iterable }

Here is an example in code, the code below will swap the key value pair

>>> stocks ={
'TGT' :  '51.33', 
'AAPL': '147.48',
'MSFT':  '71.49',
'F'   :  '11.12', 
'INTC':  '34.37'}
>>> stocks
{'TGT': '51.33', 'AAPL': '147.48', 'MSFT': '71.49', 'F': '11.12', 'INTC': '34.37'}
>>> price_to_stock = {ticker: price for price, ticker in stocks.items()}
>>> price_to_stock
{'51.33': 'TGT', '147.48': 'AAPL', '71.49': 'MSFT', '11.12': 'F', '34.37': 'INTC'}
>>> 


We can make it more interesting by using a condition and only return the key value pair if the price is the max price for that ticker. I added a price of 1000 for AAPL and a price of 5000 for F. As you can see in the code below, I have used a filtering predicate if max(price)

>>> stocks ={
'TGT' :  '51.33', 
'AAPL': '147.48',
'AAPL': '1000',
'MSFT':  '71.49',
'F'   :  '11.12', 
'F'   :  '5000',
'INTC':  '34.37'}
>>> price_to_stock = {price: ticker for ticker, price in stocks.items() if max(price)}
>>> price_to_stock
{'51.33': 'TGT', '1000': 'AAPL', '71.49': 'MSFT', '5000': 'F', '34.37': 'INTC'}
>>> 

As you can see for Apple (AAPL), we return the pair with the price of 1000, for F (Ford), we return the pair with the price of 5000, the other prices for AAPL and F are not returned

Generators

Iterator

An object representing a stream of data. Repeated calls to the iterator’s __next__() method (or passing it to the built-in function next()) return successive items in the stream.

>>> i = ['a', 'b', 'c', 'd' ]
>>> iterator =iter(i)
>>> next(iterator)
'a'
>>> next(iterator)
'b'
>>> next(iterator)
'c'
>>> 

When no more data is available a StopIteration exception is raised instead. At this point, the iterator object is exhausted and any further calls to its __next__() method just raise StopIteration again.

Here is what that looks like

>>> next(iterator)
'd'
>>> next(iterator)
Traceback (most recent call last):
  File "", line 1, in 
    next(iterator)
StopIteration
>>> 

We can trap this by checking for the StopIteration exception, here is an example

>>> try:
 next(iterator)
except(StopIteration):
    print ('max value reached')

    
max value reached
>>> 



I also looked at itertools, itertools  implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. Each has been recast in a form suitable for Python.

Here are some of these iterators

count()
cycle()
repeat()

accumulate()
chain()
chain.from_iterable()
compress()
dropwhile()
filterfalse()
groupby()
islice()
starmap()
takewhile()
tee()
zip_longest()


That was all for today, next I will look at classes

Monday, July 3, 2017

Summer of code 2017: Python, Day 16 Exceptions


As explained in my Summer of code 2017: Python post I decided to pick up Python

This is officially day 16. I took a couple of days off but today I looked at exceptions in Python, here are my notes


A couple of things to keep in mind when getting into exceptions



  • You can raise an exception to interrupt the flow of your program
  • You can handle an exception to resume control instead of the program being terminated
  • If you do not handle exceptions, unhandled exceptions will terminate the program.
  • Information about exception events will be contained in exception objects



Here is a simple function that converts an input to an integer, there is no exception handling done

>>> def converter(s):


    x = int(s)
    return x


>>> converter("33")
33
>>> converter("A")
Traceback (most recent call last):
  File "", line 1, in 
    converter("A")
  File "", line 4, in converter
    x = int(s)
ValueError: invalid literal for int() with base 10: 'A'
>>> 


As you can see it blows up with a ValueError exception

To catch an exception, you wrap a try except block around your statements, here is an example


>>> def converter(s):
 try:
  x = int(s)
 except ValueError:
  x = -1
 return x

>>> converter("33")
33
>>> converter("A")
-1
>>> 


As you can see no exceptions are displayed

You can also check for more than one exceptions like shown in the code below

>>> def converter(s):
    '''Convert to an integer.'''
    x = -1
    try:
        x = int(s)
        print("Conversion successful! x =", x)
    except ValueError:
        print("Conversion doomed!")
    except TypeError:
        print("Conversion doomed!")
    return x

>>> converter(33)
Conversion succesful! x = 33
33
>>> converter("a")
Conversion doomed!
-1
>>> converter(1.5555555)
Conversion succesful! x = 1
1
>>> converter([1,2])
Conversion doomed!
-1
>>>


We can simplify the code above by combining two exceptions into one check, you can see that in the code below

>>> 
def converter(s):
    '''Convert to an integer.'''
    x = -1
    try:
        x = int(s)
        print("Conversion successful! x =", x)
    except (ValueError, TypeError):
        print("Conversion doomed!")
    return x

>>> converter(33)
Conversion succesful! x = 33
33
>>> converter("a")
Conversion doomed!
-1
>>> converter([1,2])
Conversion doomed!
-1
>>> 


You need to have a statement after except, if you were to do the following, you will get a syntax error

>>> def converter(s):
    '''Convert to an integer.'''
    x = -1
    try:
        x = int(s)
        print("Conversion succesful! x =", x)
    except (ValueError, TypeError):
    return x
SyntaxError: expected an indented block


In Python to deal with a need for placeholder statement a pass statement can be used. Pass is a null operation, when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed, for example:


>>> def converter(s):
    '''Convert to an integer.'''
    x = -1
    try:
        x = int(s)
        print("Conversion succesful! x =", x)
    except (ValueError, TypeError):
        pass
    return x

>>> 


These 3 errors should be handled during development and not at runtime

  • IndentationError
  • SyntaxError
  • NameError



If we want to print the exception that has been caught, you can assign it to a variable

Here is an example, e will hold the exception and then a print with a conversion to a string and format is used to print the exception to the console

>>> def converter(s):
    '''Convert to an integer.'''
    x = -1
    try:
        x = int(s)
        print("Conversion succesful! x =", x)
    except (ValueError, TypeError) as e:
        print("Conversion error: {}"\
        .format(str(e)),
        file=sys.stderr)
    return x

>>> import sys
>>> converter(10)
Conversion succesful! x = 10
10
>>> converter('a')
Conversion error: invalid literal for int() with base 10: 'a'
-1
>>> converter([1,2])
Conversion error: int() argument must be a string, a bytes-like object or a number, not 'list'
-1
>>> 


If you want to print the statement to the console but also raise the exception, you can use raise. Raise re-raises the last exception that was active in the current scope. Here is an example

def converter(s):
    '''Convert to an integer.'''
    x = -1
    try:
        x = int(s)
        print("Conversion succesful! x =", x)
    except (ValueError, TypeError) as e:
        print("Conversion error: {}"\
        .format(str(e)),
        file=sys.stderr)
        raise
    return x

>>> import sys
>>> converter(1)
Conversion succesful! x = 1
1
>>> converter('a')
Conversion error: invalid literal for int() with base 10: 'a'
Traceback (most recent call last):
  File "", line 1, in 
    converter('a')
  File "", line 5, in converter
    x = int(s)
ValueError: invalid literal for int() with base 10: 'a'
>>> 




If you have code that you want to run no matter what happens for example to 'cleanup' anything, you can use the finally keyword. Keep in mind that if you have multiple return statements, the one on the finally block is the one that will be returned.

Here is an example

>>> def foo():
 try:
  return 'trying...'
 finally:
  return 'finally'

 
>>> foo()
'finally'

That is all for today, I will continue with iterables next



Monday, June 26, 2017

Summer of code 2017: Python, Day 9 Collections: list, dictionary and set


As explained in my Summer of code 2017: Python post I decided to pick up Python

This is officially day 9, today, I looked at Collections in Python, here are my notes

Lists


Lists are mutable sequences, typically used to store collections of homogeneous items

Lists may be constructed in several ways:

  • Using a pair of square brackets to denote the empty list: [] 
  • Using square brackets, separating items with commas: [a], [a, b, c] 
  • Using a list comprehension: [x for x in iterable] 
  • Using the type constructor: list() or list(iterable) 

Lists implement all of the common and mutable sequence operations. Lists also provide the following additional method: sort

Here is what a list looks like in Python before and after the sort method is called

>>> a = [ [2],[4], [1]]
>>> a
[[2], [4], [1]]
>>> a.sort()
>>> a
[[1], [2], [4]]
>>> 

I also played around with making copies of lists

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
>>> a = [[1], [2], [4]]
>>> a
[[1], [2], [4]]

>>> b =a[0:1]
>>> b
[[1]]

>>> b =a[0:2]
>>> b
[[1], [2]]

>>> b=a[:]
>>> b
[[1], [2], [4]]

>>> a is b
False

>>> a == b
True

>>> a[0]
[1]

>>> b[0]
[1]

>>> a[0] is b[0]
True

>>> a[2].append(9)
>>> a
[[1], [2], [4, 9]]

>>> b
[[1], [2], [4, 9]]
>>> 


Here is what is going on (I added line numbers so it is easier to look at the code)
On line 1 made a new list a
On line 5 I made a list b with 1 element of list a
On line 9 I made list b have the first 2 elements of list a
On line 13 I made list be be a copy of list a by supplying just the colon
On line 17 you will see that list a and b are not the same, on line 29 you will see that the first element of both lists are the same
If I now append 9 to the 3 rd element of the list, you will see that both list a and b now have the same 3rd element. The reason for this is that the copies are shallow


I decided to try some more things, this time I created a list of words

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
>>> l ="SpaceX successfully launches and recovers second Falcon 9 in 48 hours".split()
>>> l
['SpaceX', 'successfully', 'launches', 'and', 'recovers', 'second', 'Falcon', '9',
 'in', '48', 'hours']

>>> i=l.index('Falcon')
>>> i
6

>>> 'launches' in l
True

>>> l.reverse()
>>> l
['hours', '48', 'in', '9', 'Falcon', 'second', 'recovers', 'and', 'launches',
 'successfully', 'SpaceX']

>>> l.sort()
>>> l
['48', '9', 'Falcon', 'SpaceX', 'and', 'hours', 'in', 'launches', 'recovers', 
'second', 'successfully']

>>> l.sort(reverse=True)
>>> l
['successfully', 'second', 'recovers', 'launches', 'in', 'hours', 'and', 'SpaceX', 
'Falcon', '9', '48']
>>> 

On line 1 I created a list by using the split method on a function
On line 6 I am grabbing the index of the item with the value Falcon
On line 10 I am checking if the value launches is part of this list
On line 13 I am reversing the list
On Line 18 I am sorting the list
On Line 23 I am sorting the list in reversed order

Make sure to type True and not true, otherwise you will get the error NameError: name 'true' is not defined
Here is what the error looks like in the console

>>> l.sort(reverse=true)
Traceback (most recent call last):
  File "", line 1, in 
    l.sort(reverse=true)
NameError: name 'true' is not defined





I then decided to play around with deleting items from the list

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
>>> l ="SpaceX successfully launches and recovers second Falcon 9 in 48 hours".split()
>>> l
['SpaceX', 'successfully', 'launches', 'and', 'recovers', 'second', 'Falcon', '9',
 'in', '48', 'hours']
>>> del l[1]
>>> l
['SpaceX', 'launches', 'and', 'recovers', 'second', 'Falcon', '9', 'in', '48', 
'hours']
>>> l.remove('recovers')
>>> l
['SpaceX', 'launches', 'and', 'second', 'Falcon', '9', 'in', '48', 'hours']

On line 5 I deleted an item by supplying the index
On line 9 I removed an item by supplying the value

What would happen if the same value is more than once in the list and I removed it by supplying the value?
Let's take a look

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
>>> x = ['and', 'and', 'and','boo']
>>> x
['and', 'and', 'and', 'boo']
>>> x.count('and')
3
>>> x.remove('and')
>>> x
['and', 'and', 'boo']
>>> x.count('and')
2
>>> 

On line 1 I created a list with a value of and 3 times
On line 4 we count this value and it returns 3
On line 6 we call the remove method
On line 9 we call count again and the count is now 2, only 1 item got removed.

Here is what the docs have to say

s.remove(x)
remove the first item from s where s[i] == x

As you can see, only the first item will be removed, not all of them with that value


You can grab the last element of a list by using -1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
>>> l ="SpaceX successfully launches and recovers second Falcon 9 in 48 
hours".split()
>>> l
['SpaceX', 'successfully', 'launches', 'and', 'recovers', 'second', 'Falcon', '9', 
'in', '48', 'hours']
>>> l[0]
'SpaceX'
>>> l[-1]
'hours'
>>> 

As you can see, on line 8 we use [-1] and the last element from the list is returned

To insert an item into a list at a specific place, you can use insert

1
2
3
4
5
6
7
8
9
>>> l ="SpaceX successfully and recovers second Falcon 9 in 48 hours".split()
>>> l
['SpaceX', 'successfully', 'and', 'recovers', 'second', 'Falcon', '9', 'in', 
'48', 'hours']
>>> l.insert(2, 'launches')
>>> l
['SpaceX', 'successfully', 'launches', 'and', 'recovers', 'second', 'Falcon', '9', 
'in', '48', 'hours']
>>> 

As you can see from line 5, we are inserting the value launches as the 3rd item (0 based, so index =2). now the whole list makes sense again


Dict


I also took a look at dictionaries/dicts, a dict in other languages is an associative array. Most likely you will think of dicts as key value pairs

Let's take a look what it looks like, I created a dictionary of stocks with tickers and prices

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
>>> stocks ={
'TGT' :  '51.33', 
'AAPL': '147.48',
'MSFT':  '71.49',
'F'   :  '11.12', 
'INTC':  '34.37'}

>>> stocks
{'TGT': '51.33', 'AAPL': '147.48', 'MSFT': '71.49', 'F': '11.12', 'INTC': '34.37'}

>>> stocks['AAPL']
'147.48'

>>> stocks.update({'AAPL': '147.25', 'MSFT': '71.45'})

>>> stocks['AAPL']
'147.25'

>>> stocks['MSFT']
'71.45'
>>> 


On lines 1 till 6 I created a dictionary with 5 items
On line 8 I wanted to see what would be returned if I asked back for it
On line 11 I just asked for the value of key AAPL
On line 15 I updated the value of key AAPL and MSFT
On lines 16 and 19 I asked for the values that I just updated and you can see the values have changed

I also decided to print the dictionary, here is what that looks like

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
>>> stocks ={
'TGT' :  '51.33', 
'AAPL': '147.48',
'MSFT':  '71.49',
'F'   :  '11.12', 
'INTC':  '34.37'}

>>> for key in stocks:
    print("{key} = {value}".format(key=key, value=stocks[key]))

    
TGT = 51.33
AAPL = 147.48
MSFT = 71.49
F = 11.12
INTC = 34.37
>>> 


Python also includes a module for pretty printing pprint, I played around with pprint and wrote a separate post about it here: Summer of code 2017: Python, Pretty printing with pprint in Python

Sets


A set is an unordered collection of unique, immutable objects
A set is delimited by { and }

>>> s ={1, 2, 3, 4, 5}
>>> print(s)
{1, 2, 3, 4, 5}

If you just do something like {} it will actually be a dict

>>> y ={}
>>> type(y)
<class 'dict'>
>>> 

To create an empty set use the set() constructor.

>>> x =set()
>>> x
set()
>>> print(x)
set()
>>> type(x)
<class 'set'>

>>> x.add(1)
>>> x
{1}


Printing an empty set just prints set()
As you can see from the output you can use add to add an item to a set


Sets are unique, to quickly remove duplicates, you can convert it to a set

Here is an example

>>> t =[1, 2, 2, 2, 2, 3, 4, 5]
>>> set(t)
{1, 2, 3, 4, 5}

Adding the same value to a set will just ignore it

>>> x={1}
>>> x
{1}
>>> x.add(2)
>>> x
{1, 2}
>>> x.add(2)
>>> x
{1, 2}


To remove items from a set you can user remove or discard, remove will throw an error if the item doesn't exist, discard will not throw an error

Here is an example

>>> x={1}
>>> x
{1}
>>> x.discard(3)
>>> x.remove(3)
Traceback (most recent call last):
  File "", line 1, in 
    x.remove(3)
KeyError: 3
>>> x.discard(3)
>>> 

Set Algebra

If you come from the database/SQL world, the following will look very familiar to you

Here is what sets support

union
Return a new set with elements from the set and all others

intersection
Return a new set with elements common to the set and all others

difference
Return a new set with elements in the set that are not in the others

symmetric_difference
Return a new set with elements in either the set or other but not both

issubset
Test whether every element in the set is in other

issuperset
Test whether every element in other is in the set

isdisjoint

Return True if the set has no elements in common with other. Sets are disjoint if and only if their intersection is the empty set

Here is some code that shows some of these

>>> s1 = {1,2,3,4,5}
>>> s2 = {3,4,5}
>>> s1.union(s2)
{1, 2, 3, 4, 5}
>>> s1.intersection(s2)
{3, 4, 5}
>>> s1.difference(s2)
{1, 2}
>>> s1.symmetric_difference(s2)
{1, 2}
>>> s1.issubset(s2)
False
>>> s1.issuperset(s2)
True
>>> s1.isdisjoint(s2)
False

We need to create some more examples for disjoint and symmetric_difference with different values so you can see what is returned

>>> s1 = {1,2,3,4,5}
>>> s2 ={0,8}
>>> s1.isdisjoint(s2)
True

>>> s1 = {1,2,3,4,5}
>>> s2 ={4,5,6,7}
>>> s1.symmetric_difference(s2)
{1, 2, 3, 6, 7}


That is all for this post... next time I will take a look at are exceptions


Summer of code 2017: Python, Pretty printing with pprint in Python


As explained in my Summer of code 2017: Python post I decided to pick up Python

This post is about using pprint to pretty print.


The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. The formatted representation keeps objects on a single line if it can, and breaks them onto multiple lines if they don’t fit within the allowed width. Construct PrettyPrinter objects explicitly if you need to adjust the width constraint.

Let's take a look at a dictionary

First we do a regular print

>>> stocks ={
'TGT' :  '51.33', 
'AAPL': '147.48',
'MSFT':  '71.49',
'F'   :  '11.12', 
'INTC':  '34.37'}
>>> print(stocks)
{'TGT': '51.33', 'AAPL': '147.48', 'MSFT': '71.49', 'F': '11.12', 'INTC': '34.37'}


As you can see everything is printed on one line


You can print on multiple lines by using a loop and format

stocks ={
'TGT' :  '51.33', 
'AAPL': '147.48',
'MSFT':  '71.49',
'F'   :  '11.12', 
'INTC':  '34.37'}
>>> for key in stocks:
    print("{key} = {value}".format(key=key, value=stocks[key]))

    
TGT = 51.33
AAPL = 147.48
MSFT = 71.49
F = 11.12
INTC = 34.37


Finally let's take a look at what pprint does

>>> stocks ={
'TGT' :  '51.33', 
'AAPL': '147.48',
'MSFT':  '71.49',
'F'   :  '11.12', 
'INTC':  '34.37'}
>>> from pprint import pprint as pp
>>> pp(stocks)
{'AAPL': '147.48',
 'F': '11.12',
 'INTC': '34.37',
 'MSFT': '71.49',
 'TGT': '51.33'}


As you can see, each key value pair is on one line


What about lists?

A list of integers looks pretty much the same

>>> a =  [ [2],[4], [1]]
>>> print(a)
[[2], [4], [1]]
>>> pp(a)
[[2], [4], [1]]


A list of strings is a different story

>>> l ="SpaceX successfully launches and recovers second Falcon 9 in 48 hours".split()
>>> print(l)
['SpaceX', 'successfully', 'launches', 'and', 'recovers', 'second', 'Falcon', '9',
 'in', '48', 'hours']
>>> pp(l)
['SpaceX',
 'successfully',
 'launches',
 'and',
 'recovers',
 'second',
 'Falcon',
 '9',
 'in',
 '48',
 'hours']
>>> 

As you can see that prints the elements on a separate line



The pprint module also supports some keyword parameters like indent, width, depth, let's take a look at indent



>>> import pprint
>>> l ="SpaceX successfully launches and recovers second Falcon 9 in 48 hours".split()
>>> pp = pprint.PrettyPrinter(indent=6)
>>> 
>>> pp.pprint(l)
[     'SpaceX',
      'successfully',
      'launches',
      'and',
      'recovers',
      'second',
      'Falcon',
      '9',
      'in',
      '48',
      'hours']
>>> 

As you can see the values are moved to the right by 6 spaces


Make sure to look at the examples in the docs online to see what other things pprint supports


 

Saturday, June 24, 2017

Summer of code 2017: Python, Day 7 Collections: tuple, str and range


As explained in my Summer of code 2017: Python post I decided to pick up Python

This is officially day 7, today, I looked at Collections in Python, here are my notes

Tuples


 A tuple is a heterogeneous immutable sequence


  • A tuple is delimited by parentheses
  • Items in a tuple are separated by commas
  • Element access in a tuple is done with square brackets and zero-based index t[index]
  • To get the number of elements in a tuple, use len(t)
  • Iterate over a tuple by using a for loop


Here is an example of what I described above

>>> t = ("Denis", "looks", "at", "tuples", 1, 66.100,5)
>>> len(t)
7
>>> t[2]
'at'
>>> t[6]
5
>>> for item in t:
 print(item)

 
Denis
looks
at
tuples
1
66.1
5
>>> 


  • Concatenation of tuples with + operator
  • Repetition of tuples with * operator


Here is an example of the  + operator as well as the * operator


>>> t = ("Denis", 1, 66.100)
>>> t + (3,2)
('Denis', 1, 66.1, 3, 2)
>>> t * 5
('Denis', 1, 66.1, 'Denis', 1, 66.1, 'Denis',
 1, 66.1, 'Denis', 1, 66.1, 'Denis', 1, 66.1)
>>> 

Tuples can contain any type of object
You can nest tuples
You access inner elements by using chain square-brackets indexing

>>> t = (("Denis", "looks") , (1,2), (5.1, 6.1))
>>> t[2][1]
6.1
>>> t[0][1]
'looks'
>>> t[0][0]
'Denis'
>>> 


Min and max functions can be used

>>> z = (1,2,4,7,9)
>>> min(z)
1
>>> max(z)
9

You don't need parentheses when creating tuples, you can ommit them

>>> x =1,4,7,9
>>> min(x)
1
>>> max(x)
9
>>> x
(1, 4, 7, 9)

However when printing the tuple to the console, the parentheses are displayed


Swapping
a, b = b, a is the idiomatic Python swap
Here is what it looks like

>>> x = 'ice'
>>> y = 'cream'
>>> x, y = y, x
>>> x
'cream'
>>> y
'ice'
>>> 

Tuple(iterable) constructor to create tuples from iterable series of objects

>>> tuple("abcdefg")
('a', 'b', 'c', 'd', 'e', 'f', 'g')
>>> 

This can be handy to find the min and max character from a string

>>> x = tuple("abcdefgdddzyyyaaa")
>>> min(x)
'a'
>>> max(x)
'z'
>>> 

To test for membership, you can use in and not in

>>> x = tuple("abcdefgdddzyyyaaa")
>>> "z" in(x)
True
>>> "m" in(x)
False
>>> "m" not in(x)
True
>>> "z" not in(x)
False
>>> 

And that's it for tuples

str

I will use str and string interchangeably in this post
a str is an immutable sequence of Unicode characters

len
Len gives you the number of characters in a str

>>> s = "abc"
>>> len(s)
3
>>> s = "  b  "
>>> len(s)
5
>>> 

As you can see, spaces are counted unlike the LEN function is SQL Server where spaces are trimmed from the end and start of a string

You can concatenate strings by using the + operator but just like in other language this is not efficient because it will create a new object

>>> s ="Den"
>>> s += "is"
>>> s
'Denis'
>>> 

What you should do is use join

Here is an example where we concatenate is to the string y

>>> y ="Den"
>>> ''.join([y,'is'])
'Denis'

As you can see Denis is printed to the console

Partition
The partition() method divides a string into three pieces around a separator: prefix, separator, suffix
In the code below, you can see that firstname and lastname have the values we want after we used partition with the separator

firstname, separator, lastname= "Michael:Jackson".partition(':')
>>> firstname
'Michael'
>>> lastname
'Jackson'

Use an underscore as a dummy name for the separator, this is also used by the many Python tools

>>> firstname, _, lastname= "Michael:Jordan".partition(':')
>>> firstname
'Michael'
>>> lastname
'Jordan'
>>> 

Format
Use format() to insert values into strings, replacement fields are delimited by { and }
Here is an example

>>> 
"My name is {0}, {1} {0}".format("Bond", "James")
'My name is Bond, James Bond'
>>> 


Range


A range is an arithmetic progression of integers, you can specify where to start, where to end and what the step is. A range is half-open, start is included but stop is not

ConstructorArguments Result
range(6) stop 0, 1, 2, 3, 4, 5
range(6, 10) start, stop 6, 7, 8, 9
range(10, 20, 2) start, stop, step10, 12, 14, 16, 18

Here is what it looks like in the console, range(5) will hold numbers between 0 and 4

>>> x = range(5)
>>> x
range(0, 5)
>>> for item in x:
 print(item)

 
0
1
2
3
4
>>> 



Here is what it looks like in the console, range(0, 10) will hold numbers between 0 and 9

>>> x = range(0,10)
>>> for item in x:
 print(item)

 
0
1
2
3
4
5
6
7
8
9



Here is what it looks like in the console, range(0, 10, 2) will hold numbers between 0 and 9, but because step 2 was supplied, it will be on the numbers 0,2,4,6 and 8

>>> x = range(0,10,2)
>>> x
range(0, 10, 2)
>>> for item in x:
 print(item)

 
0
2
4
6
8
>>> 




That it is for today..I will continue with collections and I will look at list, dict and set

Thursday, June 22, 2017

Summer of code 2017: Python, Day 5 Objects


As explained in my Summer of code 2017: Python post I decided to pick up Python

Today is officially day 5, today, I looked at objects in Python, here are my notes

if you do something like this

x = 1000

Python creates an int object with a value of 1000, x now points to this object with a value of 1000
if you now do this
x = 500

Python will not change the value of the object from 1000 to 500 but will instead create another object with a value of 500 and z will now point to that object. The garbage collector will destroy the object with a value of 1000. This all happens because in Python objects are immutable, they cannot change


If you assign  1000 to x, then assign 500 to y, then assign x to y, both are pointing to the same object. You can prove that by using the id() function

In this example below, you will see that after we assign x to y, bot x and y point to the same object with id 50127440


>>> x = 1000
>>> y = 500
>>> y =x
>>> y
1000
>>> x
1000
>>> id(x)
50127440
>>> id(y)
50127440
>>> 


What if you have two variables and assign the same value?

>>> a = 1
>>> b =1
>>> id(a)
499688544
>>> id(b)
499688544
>>> 


As you can see, those also point to the same object

You can also us is to see if 2 objects are the same, so if you do x is y, you will get back True or False


>>> x = 5
>>> y = 5
>>> x is y
True
>>> 



If you do math against a variable, the id will also change

>>> z = 5
>>> id(z)
499688672
>>> z +=2
>>> id(z)
499688736
>>> 



With list it works like this

if you have a list d [1, 2, 3], now you add list e with the values of list d, now both d and e are [1, 2, 3]. If you now modify the list for e to be [1, 1000, 3],  both  e and d are now [1, 1000, 3]. This is because you modified the list object from [1, 2, 3] to [1, 1000, 3] and both d and e point to this list object

Here is what it looks like in IDLE

>>> d = [1, 2, 3]
>>> d
[1, 2, 3]
>>> e = d
>>> e
[1, 2, 3]
>>> e[1] = 1000
>>> e
[1, 1000, 3]
>>> d
[1, 1000, 3]
>>> e is d
True
>>> 


What will happen if we create 2 lists with the same values by assign the values to each list. Now the values are the same

>>> f = [1, 2]
>>> g = [1, 2]
>>> f == g
True
>>> f is g
False
>>> f == f
True


With integers, both is and  == return true

>>> x = 1
>>> y = 1
>>> x == y
True
>>> x  is y
True
>>> 


The is operator determines equality of identity, == determines equivalence


So even though people call these object variables in Python, these are really named references to objects


I also learned about the dir built-in function

Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.

The default dir() mechanism behaves differently with different types of objects, as it attempts to produce the most relevant, rather than complete, information:


  • If the object is a module object, the list contains the names of the module’s attributes.
  • If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.
  • Otherwise, the list contains the object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes.


So for example if we call dir on the math object, it will return all the functions available

>>> import math
>>> type(math)

>>> dir(math)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh',
 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 
'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 
'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 
'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 
'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
>>> math.pi
3.141592653589793
>>> math.pow(2,4)
16.0
>>> 

So this is a quick way to get all the available methods/functions from a class