05 December, 2014

Python Lesson 2

### Tuples

- A sequence (like strings + lists)
- An immutable (r/o) list
      * Static program data
- Def 
      * An ordered set of data
      * a single row in DB table

atuple = ( 'perl', 'ruby', 'python', 3,14, 400 )
             s        s        s       f    

### Clear screen
>>> def wiper():
print("\n" * 100)
>>> wiper() 

>>> atuple = ("CBT", "Nuggets", "is", 1.0)
>>> atuple
('CBT', 'Nuggets', 'is', 1.0)

>>> newtup = (1, "Tbilisi", 3.14)
>>> type(newtup)
<class 'tuple'>

>>> len(newtup)
3

>>> type(newtup[2])
<class 'float'>
>>> type(newtup[1])
<class 'str'>
>>> type(newtup[0])
<class 'int'>

>>> ntup2 = newtup[0:2]
>>> ntup2
(1, 'Tbilisi')

>>> newtup
(1, 'Tbilisi', 3.14)
>>> newtup.index("Tbilisi")
1
>>> newtup.index("tbilisi")
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    newtup.index("tbilisi")
ValueError: tuple.index(x): x not in tuple

>>> newtup.count(3.14)
1                       !(we have ony one time this value)
>>> multitup = ("tim", 12, 14, "tim", 24.232, "tim")
>>> multitup.count("tim")   
3                                             !(3 times)


>>> foo = ["spam", 335, "eggs", 323.234]
>>> foo
['spam', 335, 'eggs', 323.234]
>>> type(foo)
<class 'list'>

>>> foo2 = foo[0:3]
>>> foo2
['spam', 335, 'eggs']

>>> foo2.remove(335)
>>> foo2
['spam', 'eggs']

>>> footup = tuple(foo2)
>>> footup
('spam', 'eggs')
>>> type(footup)
<class 'tuple'>

>>> foo2
['spam', 'eggs']
>>> foo2.append(3.14)
>>> foo2
['spam', 'eggs', 3.14]

>>> foo2.count("eggs")
1
>>> foo2.append("eggs")
>>> foo2.count("eggs")
2

>>> foo2.insert(1, "Tbilisi")
>>> foo2
['spam', 'Tbilisi', 'eggs', 3.14, 'eggs']

>>> foo2.insert(0, "Georgia")
>>> foo2
['Georgia', 'spam', 'Tbilisi', 'eggs', 3.14, 'eggs']

>>> foo2.reverse()
>>> foo2
['eggs', 3.14, 'eggs', 'Tbilisi', 'spam', 'Georgia']

### Dictionary
>>> months = {1: "January", 2: "February", 3: "March"}
>>> type(months)
<class 'dict'>

>>> months[2]
'February'
>>> months[4]
Traceback (most recent call last):
  File "<pyshell#111>", line 1, in <module>
    months[4]
KeyError: 4

>>> months[4] = "April"
>>> months
{1: 'January', 2: 'February', 3: 'March', 4: 'April'}

>>> months.keys()
dict_keys([1, 2, 3, 4])

>>> months.values()
dict_values(['January', 'February', 'March', 'April'])

>>> months[5] = "TEST"
>>> months[5]
'TEST'
>>> months[5] = "TEST-TEST"
>>> months[5]
'TEST-TEST'


>>> months = {1: "January", 2: "February", 3: "March"}
>>> months
{1: 'January', 2: 'February', 3: 'March'}

>>> list2 = list(months)
>>> list2
[1, 2, 3]

>>> list3 = list(months.values())
>>> list3
['January', 'February', 'March']

>>> tup = ("some", "data", "here")
>>> months[4] = tup
>>> months
{1: 'January', 2: 'February', 3: 'March', 4: ('some', 'data', 'here')}

>>> L1 = ["one", "two", "three", "four"]
>>> L2 = [1,2,3,4]
>>> L1
['one', 'two', 'three', 'four']
>>> L2
[1, 2, 3, 4]
>>> L1.append(L2)
>>> L1
['one', 'two', 'three', 'four', [1, 2, 3, 4]]

>>> adict = {"one":"uno", "two":"dos", "three":"tres"}
>>> adict
{'one': 'uno', 'two': 'dos', 'three': 'tres'}
>>> len(adict)
3
>>> L1 = ['one', 'two', 'three', 'four', [1, 2, 3, 4]]
>>> len(L1)
5
>>> string1 = "Nuggets"
>>> len(string1)
7
>>> adict
{'one': 'uno', 'two': 'dos', 'three': 'tres'}
>>> adict.pop("three")                             !(remove)
'tres'
>>> adict
{'one': 'uno', 'two': 'dos'}

! Create basic Script 

val1 = input("Enter str element 1/3: ")
val2 = int(input("Enter int element 2/3: "))
val3 = float(input("Enter float element 3/3: "))

lst = [val1, val2, val3]
tpl = (val1, val2, val3)
dict = {"First element: ":val1, "Second element":val2, "Third element":val3}

print("\n")
print("Here is your list: ", lst)
print("Here is your tuple: ", tpl)
print("Here is your dictionary: ", dict)

print("\n")
val4 = input("Add a new str list element: ")
lst.append(val4)
print("Here is your new list", lst)

! Run This Script

Enter str element 1/3: Tbilisi
Enter int element 2/3: 2014
Enter float element 3/3: 2.15


Here is your list:  ['Tbilisi', 2014, 2.15]

Here is your tuple:  ('Tbilisi', 2014, 2.15)
Here is your dictionary:  {'First element: ': 'Tbilisi', 'Second element': 2014, 'Third element': 2.15}


Add a new str list element: By CBT Nuggets
Here is your new list ['Tbilisi', 2014, 2.15, 'By CBT Nuggets']