888.40K
Categories: programmingprogramming databasedatabase

Unit 11.2А: Data structures

1.

NIS PhM Kostanay
Tuples
Unit Unit 11.2А: Data structures
www.bzfar.org

2.

Learning Objectives
• 11.2.4.1 create a tuple;
• 11.2.2.1 perform access to the elements
of strings, lists, tuples;
• 11.2.4.2 convert from one data structure
to another;
• 11.4.3.2 solve applied problems of
various subject areas.
www.bzfar.org

3.

What do we already know?
• Remember what we learned in the
last lesson.
www.bzfar.org

4.

Collections
• What types of data collections have you explored?
• How are collections declared in the program?
• What are the features of each collections?
www.bzfar.org

5.

Tuples
A tuple is an ordered collection of values.
Tuples are a lot like lists:
•Tuples are ordered – Tuples maintains a left-to-right positional ordering among the items they
contain.
•Accessed by index – Items in a tuple can be accessed using an index.
•Tuples can contain any sort of object – It can be numbers, strings, lists and even other tuples.
except:
•Tuples are immutable – you can’t add, delete, or change items after the tuple is defined.
empty = ()
card = ('7', 'peak')
t = (18,)
print(len(card))
www.bzfar.org

6.

Differences between collections
Mutable
Ordered
Indexing/Slicing
Duplicate Elements
Sets




Strings




Lists




Tuples




www.bzfar.org

7.

Tuples
empty = ()
card = ('7', ‘peak')
t = (18,)
print(len(card))
www.bzfar.org

8.

Tuples
empty = ()
card = ('7', 'пик')
t = (18,)
print(len(card)) # 2
print(card[0])
www.bzfar.org

9.

Tuples
empty = ()
card = ('7', 'пик')
t = (18,)
print(len(card)) # 2
print(card[0]) # 7
www.bzfar.org

10.

Tuples
empty = ()
card = ('7', 'пик')
t = (18,)
print(len(card)) # 2
print(card[0]) # 7
print(card + t)
www.bzfar.org

11.

Tuples
empty = ()
card = ('7', 'пик')
t = (18,)
print(len(card)) # 2
print(card[0]) # 7
print(card + t) # ('7', ‘peak', 18)
www.bzfar.org

12.

Tuples
empty = ()
card = ('7', 'пик')
t = (18,)
print(len(card)) # 2
print(card[0]) # 7
print(card + t) # ('7', ‘peak', 18)
card[1] = ‘clubs’ # TypeError: 'tuple' object does not support
item assignment
www.bzfar.org

13.

Tuple Comparison
(1, 2, 3) == (1, 3, 2)
www.bzfar.org

14.

Tuple Comparison
(1, 2, 3) == (1, 3, 2)
False
www.bzfar.org

15.

Tuple Comparison
(1, 2, 3) == (1, 3, 2)
(1, 2, 3) < (1, 2, 4)
False
www.bzfar.org

16.

Tuple Comparison
(1, 2, 3) == (1, 3, 2)
(1, 2, 3) < (1, 2, 4)
False
True
www.bzfar.org

17.

Tuple Comparison
(1, 2, 3) == (1, 3, 2)
(1, 2, 3) < (1, 2, 4)
(1, 2) <= (5,)
False
True
www.bzfar.org

18.

Tuple Comparison
(1, 2, 3) == (1, 3, 2)
(1, 2, 3) < (1, 2, 4)
(1, 2) <= (5,)
False
True
True
www.bzfar.org

19.

Tuple Comparison
(1, 2, 3) == (1, 3, 2)
(1, 2, 3) < (1, 2, 4)
(1, 2) <= (5,)
('7', 'clubs') > ('7', ‘worms')
False
True
True
www.bzfar.org

20.

Tuple Comparison
(1, 2, 3) == (1, 3, 2)
(1, 2, 3) < (1, 2, 4)
(1, 2) <= (5,)
('7', 'clubs’) > ('7', ‘worms')
False
True
True
False
www.bzfar.org

21.

Tuple Comparison
(1, 2, 3) == (1, 3, 2)
(1, 2, 3) < (1, 2, 4)
(1, 2) <= (5,)
('7', 'clubs’) > ('7', ‘worms')
(1, 2) != ('7', ‘peak')
False
True
True
False
www.bzfar.org

22.

Tuple Comparison
(1, 2, 3) == (1, 3, 2)
(1, 2, 3) < (1, 2, 4)
(1, 2) <= (5,)
('7', 'clubs’) > ('7', ‘worms')
(1, 2) != ('7', ‘peak')
False
True
True
False
True
www.bzfar.org

23.

Tuple Comparison
(1, 2, 3) == (1, 3, 2)
(1, 2, 3) < (1, 2, 4)
(1, 2) <= (5,)
('7', 'clubs’) > ('7', ‘worms')
(1, 2) != ('7', ‘peak’)
(3, 4) < ('5', 'tambourine')
False
True
True
False
True
www.bzfar.org

24.

Tuple Comparison
(1, 2, 3) == (1, 3, 2)
(1, 2, 3) < (1, 2, 4)
(1, 2) <= (5,)
('7', 'clubs’) > ('7', ‘worms')
(1, 2) != ('7', ‘peak’)
(3, 4) < ('5', 'tambourine')
False
True
True
False
True
'<' not supported between
instances of 'int' and 'str'
www.bzfar.org

25.

Assigning Tuples
(n, s) = (10, 'hello')
www.bzfar.org

26.

Assigning Tuples
(n, s) = (10, 'hello’)
#the same as
n, s = 10, 'hello'
www.bzfar.org

27.

Assigning Tuples
(n, s) = (10, 'hello') #the same as
n, s = 10, 'hello'
#the same as
n = 10
s = 'hello'
www.bzfar.org

28.

Assigning Tuples
cards = [('7', 'peak'), ('D', 'clubs'), ('Т', 'peak')]
value, suit = cards[0]
print('Values of the card:', value)
www.bzfar.org

29.

Assigning Tuples
cards = [('7', 'peak'), ('D', 'clubs'), ('Т', 'peak')]
value, suit = cards[0]
print('Value of the card:', value)
Value of the card: 7
www.bzfar.org

30.

Assigning Tuples
cards = [('7', 'peak'), ('D', 'clubs'), ('Т', 'peak')]
value, suit = cards[0]
print('Value of the card:', value)
print(‘Suit of the card:’, suit)
Value of the card : 7
www.bzfar.org

31.

Assigning Tuples
cards = [('7', ‘peak'), ('D', ‘clubs'), ('Т', ‘peak')]
value, suit = cards[0]
print(‘Value of the card:', value)
print(‘Suit of the card:’, suit)
Value of the card : 7
Suit of the card : peak
www.bzfar.org

32.

Value Exchange
a, b = 1, 2
a=b
b=a
print(a, b, sep=', ')
www.bzfar.org

33.

Value Exchange
a, b = 1, 2
a=b
b=a
print(a, b, sep=', ')
2, 2
www.bzfar.org

34.

Value Exchange
a, b = 1, 2
a=b
b=a
print(a, b, sep=', ')
2, 2
www.bzfar.org

35.

Value Exchange
a, b = 1, 2
t=a
a=b
b=t
print(a, b, sep=', ')
www.bzfar.org

36.

Value Exchange
a, b = 1, 2
t=a
a=b
b=t
t
a
b
1
2
1
2
1
print(a, b, sep=', ')
2, 1
www.bzfar.org

37.

Value Exchange
a, b = 1, 2
a, b = b, a
print(a, b, sep=', ')
www.bzfar.org

38.

Value Exchange
a, b = 1, 2
a, b = b, a
print(a, b, sep=', ')
#2, 1
2, 1
www.bzfar.org

39.

Value Exchange
a, b = 1, 2
a, b = b, a
print(a, b, sep=', ')
a, b, c = 3, 2, 1
b, a, c = c, a, b
print(b, c, a, sep=', ')
#2, 1
www.bzfar.org

40.

Value Exchange
a, b = 1, 2
a, b = b, a
print(a, b, sep=', ')
a, b, c = 3, 2, 1
b, a, c = c, a, b
print(b, c, a, sep=', ')
#2, 1
# 1, 2, 3
www.bzfar.org

41.

Fibonacci numbers
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
1+1=2
1+2=3
2+3=5
3+5=8
5+8=13

www.bzfar.org

42.

Fibonacci numbers
n = int(input())
f1, f2 = 0, 1
for i in range(n):
print(f2)
f1, f2 = f2, f1 + f2
www.bzfar.org

43.

Fibonacci numbers
n = int(input()) # n=7
f1, f2 = 0, 1
for i in range(n):
print(f2, “ “)
f1, f2 = f2, f1 + f2
1 1 2 3 5 8 13
www.bzfar.org

44.

Convert from one data structure to another
word = "caw"
word[1] = "o"
www.bzfar.org

45.

String to List to String
word = "caw"
word[1] = "o“ # TypeError: 'str' object does not support item
assignment
lst = list(word)
lst[1] = "o"
word = "".join(lst)
print(word)
www.bzfar.org

46.

String to List to String
word = “My favourite subject is programming"
lst = word.split()
print(lst)
word = " ".join(lst)
print(word)
www.bzfar.org

47.

Tuple to List to Tuple
tup = ("Amir", 5)
tup[1] = 4 # TypeError: 'tuple' object does not support item assignment
lst = list(tup)
lst[1] = 4
tup = tuple(lst)
print(tup)
www.bzfar.org

48.

List to Set to List
capitals = ["Nur-Sultan", "Moscow", "Baku", "Ankara", "Moscow"]
print(len(capitals))
unique_capitals = set(capitals)
capitals = list(unique_capitals)
print(capitals)
print(len(capitals))
www.bzfar.org

49.

Tuple to Set to Tuple
capitals = ("Nur-Sultan", "Moscow", "Baku", "Ankara", "Moscow")
print(len(capitals))
unique_capitals = set(capitals)
capitals = tuple(unique_capitals)
print(capitals)
print(len(capitals))
www.bzfar.org

50.

Set to List
capitals = {"Nur-Sultan", "Moscow", "Baku", "Ankara"}
lst = list(capitals)
print(lst)
www.bzfar.org

51.

• What new things did you learn today?
• What do you remember well?
• What questions do you have left?
www.bzfar.org

52.

Solving tasks
• Stepik.org
• 5.7
www.bzfar.org
English     Русский Rules