Welcome to Python
Tuple, List, Dictionary
Gimme a definition! → Tuple
• Tuple is one of python data types that categorize
as sequence and it consists of comma-
separated objects like string, number, list and
even another tuple!
• Tuple is immutable, and that’s differ it from list
• Immutable: An object with a fixed value.
Immutable objects include numbers, strings and
tuples. Such an object cannot be altered. A new
object has to be created if a different value has to
be stored. They play an important role in places
where a constant hash value is needed, for
example as a key in a dictionary.(from
docs.python.org)
Examples for Tuple
Try it out !
1 >>> x = ("1", 2, [3])
>>> print(x)
????
>>> type(x)
????
2 >>> a = "1"
>>> b = 2
>>> c = [3]
>>> y = a, b, c
>>> print(y)
????
>>> type(y)
????
3 >>> a = "1"
>>> y = (a)
>>> type(y)
????
>>> a = "1"
>>> y = (a,)
>>> type(y)
????
>>> print(y)
????
>>> print(y[1])
????
>>> len(y)
????
Tuple: Advantages???
• tuple can assigned faster than list. Can you prove it by yourself? (Try to compare with list creation code!)
• Write protected!
• Use less memory than list. Because, tuple is fixed-size and list is variable-sized.
• Can assign as key at dictionary
Tuple: Operations
1. Accessing Values :
• x = (1, 2, 3, 4, 5)
• x[0]
• x[len(x)-1] # or ?
2. Concatenation :
• a = (1, 2, 3)
• b = (4, 5, 6)
• c = a + b
3. Multiply :
• x = ("A",)
• x = x * 2 #??
4. Delete Tuple :
• del x[0] #??
• del x # ??
5. Comparison :
• print((1, 2) == (1, 2))
• print((1, 2) is (1, 2))
 The == operator compares the values of both
the operands and checks for value equality.
Whereas is operator checks whether both the
operands refer to the same object or not.
• x = (1, 2)
• y = x
• print(x is y and x == y) #??
6. The ‘in’ operator:
• x = (1, 2)
• print(1 in x)
• print((1 and 2) in x) # print((1 or 4) in x)
7. ‘sorted’ function with ‘reverse’ parameter
One question?
>>> x = ("1", 2, [3])
>>> print(x)
>>> y = x[2]
>>> y.append(4)
>>> y.append(5)
>>> y.append(6)
>>> print(x)
List = Tuple + something
Additional to tuples:
• sort:
• x=[5,6,2,3,5]
• x.sort()
• reserve:
• x.reverse()
• copy:
• x = [1,3,6]
• y=x
• z=x.copy()
• y[1] = 888
• print(x[1]) #???
• print(z[1]) #???
• List as stack:
• append() and pop()
• del stands for delete
 What is lambda?
 What is list comprehension?!
Dictionary, Are you looking up for something?
• Dictionary consists of two main part:
• Key: What is you will save and retrieve
value with
• Value: What you want to hide with Key
• Why we used hide in above sentences?
• For creating dictionary just use this pattern:
• ‘[key]’:[value] like bellow:
• d = {‘name’:’some thing’,’age’:150}
• How to get ‘some thing’ back??
• d[0] or d[‘name’] or …
• There is no order in saving values for keys
(unlike list)
• Can we say list is a dictionary with ordered
integer as key?
• There is another way to build dictionary
with dict() constructor:
• dict([('sape', 4139), ('guido', 4127), ('jack',
4098)])
Dictionary
• Looping through dictionary elements:
• knights = {'gallahad': 'the pure', 'robin':
'the brave'}
• for k, v in knights.items():#(key,value)
• for v in knights.values():
• for k in knights.keys():
• zip:
• For making dictionary out of 2 list with
same size:
• questions = ['name', 'quest', 'favorite
color']
• answers = ['lancelot', 'the holy grail',
'blue']
• for a,b in zip(questions,answers):
• print("%s = %s" % (a,b))
• enumerate: (a way to make list!)
• for i, v in enumerate([‘ali’,’jack’]:
Link to source
https://docs.python.org/3/tutorial/datastructures.html
https://www.w3schools.com/python/

An Introduction to Tuple List Dictionary in Python

  • 1.
    Welcome to Python Tuple,List, Dictionary
  • 2.
    Gimme a definition!→ Tuple • Tuple is one of python data types that categorize as sequence and it consists of comma- separated objects like string, number, list and even another tuple! • Tuple is immutable, and that’s differ it from list • Immutable: An object with a fixed value. Immutable objects include numbers, strings and tuples. Such an object cannot be altered. A new object has to be created if a different value has to be stored. They play an important role in places where a constant hash value is needed, for example as a key in a dictionary.(from docs.python.org)
  • 3.
    Examples for Tuple Tryit out ! 1 >>> x = ("1", 2, [3]) >>> print(x) ???? >>> type(x) ???? 2 >>> a = "1" >>> b = 2 >>> c = [3] >>> y = a, b, c >>> print(y) ???? >>> type(y) ???? 3 >>> a = "1" >>> y = (a) >>> type(y) ???? >>> a = "1" >>> y = (a,) >>> type(y) ???? >>> print(y) ???? >>> print(y[1]) ???? >>> len(y) ????
  • 4.
    Tuple: Advantages??? • tuplecan assigned faster than list. Can you prove it by yourself? (Try to compare with list creation code!) • Write protected! • Use less memory than list. Because, tuple is fixed-size and list is variable-sized. • Can assign as key at dictionary
  • 5.
    Tuple: Operations 1. AccessingValues : • x = (1, 2, 3, 4, 5) • x[0] • x[len(x)-1] # or ? 2. Concatenation : • a = (1, 2, 3) • b = (4, 5, 6) • c = a + b 3. Multiply : • x = ("A",) • x = x * 2 #?? 4. Delete Tuple : • del x[0] #?? • del x # ?? 5. Comparison : • print((1, 2) == (1, 2)) • print((1, 2) is (1, 2))  The == operator compares the values of both the operands and checks for value equality. Whereas is operator checks whether both the operands refer to the same object or not. • x = (1, 2) • y = x • print(x is y and x == y) #?? 6. The ‘in’ operator: • x = (1, 2) • print(1 in x) • print((1 and 2) in x) # print((1 or 4) in x) 7. ‘sorted’ function with ‘reverse’ parameter
  • 6.
    One question? >>> x= ("1", 2, [3]) >>> print(x) >>> y = x[2] >>> y.append(4) >>> y.append(5) >>> y.append(6) >>> print(x)
  • 7.
    List = Tuple+ something Additional to tuples: • sort: • x=[5,6,2,3,5] • x.sort() • reserve: • x.reverse() • copy: • x = [1,3,6] • y=x • z=x.copy() • y[1] = 888 • print(x[1]) #??? • print(z[1]) #??? • List as stack: • append() and pop() • del stands for delete  What is lambda?  What is list comprehension?!
  • 8.
    Dictionary, Are youlooking up for something? • Dictionary consists of two main part: • Key: What is you will save and retrieve value with • Value: What you want to hide with Key • Why we used hide in above sentences? • For creating dictionary just use this pattern: • ‘[key]’:[value] like bellow: • d = {‘name’:’some thing’,’age’:150} • How to get ‘some thing’ back?? • d[0] or d[‘name’] or … • There is no order in saving values for keys (unlike list) • Can we say list is a dictionary with ordered integer as key? • There is another way to build dictionary with dict() constructor: • dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
  • 9.
    Dictionary • Looping throughdictionary elements: • knights = {'gallahad': 'the pure', 'robin': 'the brave'} • for k, v in knights.items():#(key,value) • for v in knights.values(): • for k in knights.keys(): • zip: • For making dictionary out of 2 list with same size: • questions = ['name', 'quest', 'favorite color'] • answers = ['lancelot', 'the holy grail', 'blue'] • for a,b in zip(questions,answers): • print("%s = %s" % (a,b)) • enumerate: (a way to make list!) • for i, v in enumerate([‘ali’,’jack’]:
  • 10.

Editor's Notes

  • #11 In Slide Show mode, select the arrows to visit links.