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

No comments: