/* */

Thursday 24 March 2011

Tuples - Intro

Tuples are immutable sequences and are created simply by separating value with commas;
1,2,3, you have a tuple!
Usually you'd enclose the tuple in parenthesis:
x = 1,2,3
x
(1,2,3)
() → creates an empty tuple
42, → creates a tuple with 1 value (42,). The added comma specifies its a tuple
note the difference a comma can make:
3x(40+2)
126
3x(40+2,)
(42,42,42)

String formatting with Dictionaries

phonebook = {'Beth' : '9102' , 'Alice' : '2341' , 'Cecil' : '3228' }
"Cecil's phone number is %(Cecil)s." %phonebook
"Cecil's phone number is 3258."

The (Cecil) points to that particular key, 'phonebook' directs towards the dictionary.

Advantages of Dictionaries over Lists

x = []
x = [42] = 'Foobar'
-error
x = {}
x[42] = 'Foobar'
x
{42 : 'Foobar'}
When we try to assign a value to position 42 in a list it returns an error because items 0-41 dont exist. Because a dictionary relies on mapping rather than sequencing, the key can be created.    

dictionary functions

len(d) - returns number of items (key-value pairs) in d
d[k] - returns the value associated with the key 'k'
d[k]= v - assigns the value 'v' to key 'k'
k in d - checks if the key 'k' is in dictionary 'd'

-dict

The dict function creates a dictionary from other mappings (other dictionaries) or from sequences of pairs (key,value):
items =[('name', 'Jim'), ('age', 42)]
d= dict(items)
d
{'age' : 42, 'name' : 'Jim'}
d['name']
'Jim'

Also can be used with keyword argument:
d= dict(name= 'Jim', age= 42)
d
{'age' : 42,  'name' : 'Jim'}

Wednesday 23 March 2011

-in

in; the membership operator....
name= raw_input('whats yer name? ')
if 's' in name:
    print 'Your name contains the letter "s"'
else:
    print 'Your name doesn't have the letter "s"'

Comparisons

Comparisons are boolean operations and utilise operators to return a true or false answer (o or 1).

x=y= [1,2,3]
z= [1,2,3]
x==y
True
x==z
True
x is y
True
x is z
Flase

The == simply means 'same value' - Slight differeation from a single =, we use this to assign a value to a variable, we use the double == to test whether two values are equal.
The is operator tests if they are the same object - in the above example x is not the same object as z (although they share the same value). Lists can be equal but not identicle.

-lower

returns the lower case version of the string:
'Trotsky Hammer Prowl'.lower()
'trotsky hammer prowl'

-Join

Joins the elements of a sequence, does the opposite of split:
seq = [1,2,3,4,5]
sep = '+'
sep join(seq)

This would give an error because we are trying to join a list of numbers with an opeator. If the sequence consisted of:
['1','2','3','4','5']
the reult would be different
'1+2+3+4+5'

Conversion specifiers

%s-
Mark the place where the string/value is to be inserted. %s means to convert to a string.

Strings - Intro

Strings are immutable and support standard sequence operations:


indexing, slicing, multiplication, membership, length, minimum, maximum.

tuple

The tuple function takes a sequence and converts it to a tuple:

tuple([1,2,3])
(1,2,3)
tuple(abc)
('a','b','c')
tuple((1,2,3)) → takes a tuple within a tuple and converts it to one tuple
(1,2,3)

Creating Dictioanries

phonebook = {'Alice' : '2341', 'Beth' : '9102', 'Cecil' : '3258'}

Phone books consist of pairs - their 'key' and irs corresponding item.
e.g. the key 'Alice' has a value of '2341'.
This script has expressed the values as 'strings' and not integeres in order to avoid them being interpreted as octagonal numbers.

Creating an empty dictionary is written:

phonebook = {}

Dictionary - Intro

Dictionaries:
  • refer to value by mapping 'mapping'
  • values don't have an order, but stored in a 'key'
  • 'key's' can be numbers, strings, tuples

Access tuple elements

x = 1,2,3
x[1]
2 → indexing is the same procedure as lists
x[0:2]
(1,2) → again same as lists, index items from 0 up to 2 (excluding 2)

  • Tuples can be used as keys when mapping.

Tuesday 22 March 2011

Using 'key' in sort

x = ['add', 'acme', 'aerate', 'aardvark', 'abalone']
x.sort(key = len)
x
['add', 'acme', 'aerate', 'abalone', 'aardvark']

This example uses 'len' as the key function - it arranges the items in order of their length.

Sorting tutorial online:

http://wiki.python.org/moin/HowTo/Sorting 

Sorted

x = [4,6,2,1,7,9]
y= sorted(x)
x
[4,6,2,1,7,9]
y
[1,2,4,6,7,9]

Sorted function very similar to 'sort'. Can also be used on a sequence:

sorted('Python')
['P,' 'h', 'o', 'n', 't', 'y']

Arranges the string into an alphabetical sequence.

Sort

Sort a list into numerical order - this changes the original rather than returning a copy of the list;

x = [4,6,2,1,7,9]
x.sort()
x
[1,2,4,6,7,9]

Reverse

reverse elements in a list;

x = [1,2,3]
x.reverse()
x
[3,2,1]

This reverses the list but doesn't return anything.

Remove

removes 1st occurance of a value in a list;

x = ['to', 'be', 'or', 'not', 'to', 'be']
x.remove('be')
x
['to', 'or', 'not','to', 'be' ]

Pop

x= [1,2,3,]
x.pop()
3

Deletes the last entry of the list

x
[1,2]
x.pop(0) - specifies which item to delete
1 - returns the value of the item it just deleted
x
[2]

Insert

insert an object into specified position in a list:

numbers [1,2,3,4,,5,6]
numbers.insert(3,'four') - insert 'four' at position 3
number
[1,2,3,'four',5,6,7]

Index

Knights = ['Richard', 'of ', 'York', 'Gave', 'Battle', 'in', 'vein']
Knights.index('York')
2
Knights.index('pudding')
-error

Finds the word in the list and returns its position.

Count

['to','be','or','not','to','be'].count ('to')
2

This counts the amount of times a given object appears in the list (in this case the word 'to')

Extend

append several items at once:

a = [1,2,3]
b = [4,5,6]
a.extend(b)
a
[1,2,3,4,5,6]

This is similar to concatenation except the variable 'a' is modified. In concatenation, the result would create a new variable.

Append

lst = [1,2,3,]
lst.append(4)
lst
[1,2,3,4]

Simply adds the value in the brackets to the variable (note the variable was declared 'lst' and not 'list'. 'list' is a function and would have given an error).

Remove elements

numbers = [1,2,3,4,5,]
numbers [1,4] = []
numbers = [1,5]

This has similar effect to the delete statement and is related to replace. In this case you are simply replacing all the elements from position 1 up to (but not including) position 4 and replacing it with [] - an empty list. The items that survive in the list are those in position [0,4]. 

Insert elements

numbers = [1,5]
numbers [1:1] = [2,3,4]
numbers
[1,2,3,4,5,]

The inserted elements are placed in the list at position 1

List statement, assign to slices

name = list('Pearl')
name
['P','e','a,'r','l'']
name[2:] = list('ar')
name
['P','e','a','r']

List statement turned the string ('Pearl') into a list.
The list('ar') replaces anything from position 2 onwards in the name list, hence it's new value being ['P','e','a','r']

Deletion

names = ['Alice', 'Beth', 'Julie', 'Cecil', 'Gary']
del names [2]
names
['Alice', 'Beth', 'Cecil', 'Gary']

Use the del function to delete item from list.

Changing Lists

- assignment:
x = [1,1,1] - Declare list and its values
x[1] = 2 - Assign value of 2 to position 1
x
[1,2,1,] - New value of x

To change list items, index the item you want to change (x[1]) and assign a new value to it (x[1] =2).

Len Min/Max

example:

numbers = [100, 34, 678]
len(numbers)
3

max(numbers)
678

min(numbers)
34

max(2,3)
3

min(2,3)

Len function returns either the highest or lowest values in a sequence (the first two examples) or directly as a sequence argument (the last two examples).

Summary

Sequences - data structure with numbered elements
Membership - whether a value can be found in a sequence
Methods - like functions but are tied to a specific value
Vectors   
                        =                    ||V|| =√(12²+(-5²))
Vectors are split into two values:
  1. magnitude - the length of the vector 
  2. direction - the direction of the vector 
magnitude is expressed as ||V||  - V stands for vector, so the magnitude of vector [12 -5] would be written ||12 -5||
magnitude =           ||V|| =√(a²+b²)
= V=√(12²+(-5²))