Skip to content

Commit c17c666

Browse files
Add files via upload
1 parent 6e64d40 commit c17c666

File tree

4 files changed

+550
-0
lines changed

4 files changed

+550
-0
lines changed

2.3.a. Python Tuples.py

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#Creating a Tuple
2+
'''# empty tuple
3+
# Output: ()
4+
my_tuple = ()
5+
print(my_tuple)
6+
7+
# tuple having integers
8+
# Output: (1, 2, 3)
9+
my_tuple = (1, 2, 3)
10+
print(my_tuple)
11+
12+
# tuple with mixed datatypes
13+
# Output: (1, "Hello", 3.4)
14+
my_tuple = (1, "Hello", 3.4)
15+
print(my_tuple)
16+
17+
# nested tuple
18+
# Output: ("mouse", [8, 4, 6], (1, 2, 3))
19+
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
20+
print(my_tuple)
21+
22+
# tuple can be created without parentheses
23+
# also called tuple packing
24+
# Output: 3, 4.6, "dog"
25+
26+
my_tuple = 3, 4.6, "dog"
27+
print(my_tuple)
28+
29+
# tuple unpacking is also possible
30+
# Output:
31+
# 3
32+
# 4.6
33+
# dog
34+
a, b, c = my_tuple
35+
print(a)
36+
print(b)
37+
print(c)
38+
'''
39+
40+
41+
#2)Creating a tuple with one element is a bit tricky.
42+
'''#Having one element within parentheses is not enough. We will need a trailing comma to indicate that it is in fact a tuple.
43+
# only parentheses is not enough
44+
# Output: <class 'str'>
45+
my_tuple = ("hello")
46+
print(type(my_tuple))
47+
48+
# need a comma at the end
49+
# Output: <class 'tuple'>
50+
my_tuple = ("hello",)
51+
print(type(my_tuple))
52+
53+
# parentheses is optional
54+
# Output: <class 'tuple'>
55+
my_tuple = "hello",
56+
print(type(my_tuple))''''
57+
58+
#3) Accessing Elements in a Tuple
59+
#3.1. Indexing
60+
"""my_tuple = ('p','e','r','m','i','t')
61+
62+
# Output: 'p'
63+
print(my_tuple[0])
64+
65+
# Output: 't'
66+
print(my_tuple[5])
67+
68+
# index must be in range
69+
# If you uncomment line 14,
70+
# you will get an error.
71+
# IndexError: list index out of range
72+
73+
#print(my_tuple[6])
74+
75+
# index must be an integer
76+
# If you uncomment line 21,
77+
# you will get an error.
78+
79+
# TypeError: list indices must be integers, not float
80+
81+
#my_tuple
82+
83+
# 3.1.2.nested index
84+
# Output: 's'
85+
print(n_tuple[0][3])
86+
87+
# nested index
88+
Output: 4
89+
print(n_tuple[1][1])"""
90+
91+
#3.2. Negative Indexing
92+
'''my_tuple = ('p','e','r','m','i','t')
93+
94+
# Output: 't'
95+
print(my_tuple[-1])
96+
97+
# Output: 'p'
98+
print(my_tuple[-6])'''
99+
100+
#3.3 Slicing
101+
#We can access a range of items in a tuple by using the slicing operator - colon ":".
102+
'''my_tuple = ('i','n','b','a','n','y','o','g','i')
103+
104+
# elements 2nd to 4th
105+
# Output: ('r', 'o', 'g')
106+
print(my_tuple[1:4])
107+
108+
# elements beginning to 2nd
109+
# Output: ('p', 'r')
110+
print(my_tuple[:-7])
111+
112+
# elements 8th to end
113+
# Output: ('i', 'z')
114+
print(my_tuple[7:])
115+
116+
# elements beginning to end
117+
# Output: ('i','n','b','a','n','y','o','g','i')
118+
print(my_tuple[:])'''
119+
120+
#3.4 Changing a Tuple Unlike lists, tuples are immutable
121+
122+
'''my_tuple = (4, 2, 3, [6, 5])
123+
124+
# we cannot change an element
125+
# If you uncomment line 8
126+
# you will get an error:
127+
# TypeError: 'tuple' object does not support item assignment
128+
129+
#my_tuple[1] = 9
130+
131+
# 3.4.1.but item of mutable element can be changed
132+
# Output: (4, 2, 3, [9, 5])
133+
my_tuple[3][0] = 9
134+
print(my_tuple)
135+
136+
# tuples can be reassigned
137+
# Output: ('i','n','b','a','n','y','o','g','i')
138+
my_tuple = ('i','n','b','a','n','y','o','g','i')
139+
print(my_tuple)
140+
141+
#3.4.2.Both + and * operations result into a new tuple.
142+
# Concatenation
143+
# Output: (1, 2, 3, 4, 5, 6)
144+
print((1, 2, 3) + (4, 5, 6))
145+
146+
# 3.4.3Repeat
147+
# Output: ('Repeat', 'Repeat', 'Repeat')
148+
print(("Repeat",) * 3)''''
149+
150+
#3.5 Python Tuple Methods
151+
#Method Description
152+
#count(x) Return the number of items that is equal to x
153+
#index(x) Return index of first item that is equal to x
154+
'''my_tuple = ('a','p','p','l','e',)
155+
156+
# Count
157+
# Output: 2
158+
print(my_tuple.count('p'))
159+
160+
# Index
161+
# Output: 3
162+
print(my_tuple.index('l'))'''
163+
164+
#3.6 Other Tuple Operations
165+
#3.6.1. Tuple Membership Test
166+
#We can test if an item exists in a tuple or not, using the keyword in.
167+
168+
'''my_tuple = ('a','p','p','l','e',)
169+
# In operation
170+
# Output: True
171+
print('a' in my_tuple)
172+
# Output: False
173+
print('b' in my_tuple)
174+
# Not in operation
175+
# Output: True
176+
print('g' not in my_tuple)'''
177+
178+
#3.6.2. Iterating Through a Tuple
179+
"""#Using a for loop we can iterate though each item in a tuple.
180+
181+
# Output:
182+
# Hello John
183+
# Hello Kate
184+
for name in ('John','Kate'):
185+
print("Hello",name)"""
186+
187+
188+
189+
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Tuples
2+
#1) A tuple is a group of one or more values that are treated as a whole.
3+
4+
#1.1 Using Tuples
5+
6+
t1 = ("apple", "orange")
7+
print(type(t1))
8+
t2 = "banana", "cherry"
9+
print(type(t2))
10+
11+
#1.2 You can mix data types within tuples. You can even put tuples in tuples.
12+
13+
t1 = ("apple", 3, 1.4)
14+
t2 = ("apple", 3, 1.4, ("banana", 5))
15+
16+
#1.3 To find out how many elements a tuple contains, you can use the len() function.
17+
t1 = ("apple", "orange")
18+
t2 = ("apple", 3, 1.4)
19+
t3 = ("apple", 3, 1.4, ("banana", 5))
20+
print(len(t1))
21+
print(len(t2))
22+
print(len(t3))
23+
24+
#1.4 You can use a for loop to access individualelements of a tuple in sequence.
25+
t1 = ("apple", 3, 1.4, ("banana", 5))
26+
for element in t1:
27+
print (element)
28+
29+
#1.5 You can also use the max() and min()& sum() functions
30+
t1 = (234, 456,78, 409)
31+
print(max(t1))
32+
print(min(t1))
33+
print(sum(t1))
34+
35+
#1.6 You can test whether an element is part of a tuple by using the in operator.
36+
t1 = ("apple", "orange", "cherry")
37+
print("banana" in t1)
38+
print("orange" in t1)
39+
40+
#2)TUPLE ASSIGMENTS
41+
t1 = ("apple")
42+
print(type(t1))
43+
44+
#len
45+
t1 = ("apple")
46+
print(type(t1))
47+
print(len(t1))
48+
49+
#left, right
50+
t1, t2 = "apple", "banana"
51+
print(t1)
52+
print(t2)
53+
54+
t1, t2 = ("apple", "banana"), "cherry"
55+
print(t1)
56+
print(t2)
57+
58+
#3) Tuple indices
59+
60+
# 3.1 individual elements
61+
t1 = ("apple", "banana", "cherry", "durian")
62+
print(t1[2])
63+
64+
#3.2 slices
65+
t1 = ("apple", "banana", "cherry", "durian" , "orange")
66+
print(t1[1.4])
67+
68+
#3.3 while loop use
69+
t1 = ("apple", "banana", "cherry", "durian" , "orange")
70+
i = 0
71+
while i < len(t1)
72+
print(t1[i])
73+
i+= 1
74+
75+
#4 TUPLE COMPARISONS:
76+
77+
t1 = ("apple", "banana")
78+
t2 = ("apple", "banana")
79+
t3 = ("apple", "cherry")
80+
t4 = ("apple", "banana", "cherry")
81+
print (t1==t2)
82+
print(t1<t3)
83+
print(t1>t4)
84+
print(t3>t4)
85+
86+
#5 TULPLE are IMMUTABLE:
87+
t1 = ("apple", "banana", "cherry")
88+
t1[0] = "orange"
89+
90+
#6 APPLICATIONS OF TUPLES
91+
92+
from math import sqrt
93+
94+
#Distance between two point in N-dimensional space.
95+
#The points should have the same dimension. i.e. they are tuples
96+
#of numeric values, and they should have the same length.
97+
98+
def distance (p1, p2):
99+
total = 0
100+
for i in range (len(p1)):
101+
total += (p1[i] - p2[i]**2
102+
return sqrt(total)
103+
104+
# 1- dimensional space
105+
point1 = (1,)
106+
point2 = (5,)
107+
print("1D: Distance between", point1, "and", point2, "is", distance(point1, point2))
108+
109+
#2- dimensional space
110+
point1 = (1,2)
111+
point2 = (5,5)
112+
print("1D: Distance between", point1, "and", point2, "is", distance(point1, point2))
113+
114+
#3 - dimensional space
115+
point1 = (1,2,4)
116+
point2 = (5,5,8)
117+
print("1D: Distance between", point1, "and", point2, "is", distance(point1, point2))
118+
119+
120+
121+
122+
123+
124+
125+
126+
127+
128+
129+
130+
131+
132+
133+
134+
135+
136+
137+
138+
139+
140+
141+
142+
143+
144+
145+
146+
147+
148+
149+
150+
151+

2.3.c. Application of tuple.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#6 APPLICATIONS OF TUPLES
2+
3+
from math import sqrt
4+
5+
#Distance between two point in N-dimensional space.
6+
#The points should have the same dimension. i.e. they are tuples
7+
#of numeric values, and they should have the same length.
8+
9+
def distance (p1, p2):
10+
total = 0
11+
for i in range (len(p1)):
12+
#total += ((p1[i] - p2[i]**2)
13+
# return sqrt(total)
14+
15+
"""# 1- dimensional space:
16+
point1 = (1,)
17+
point2 = (5,)
18+
print("1D: Distance between", point1, "and", point2, "is", distance(point1, point2))
19+
20+
#2- dimensional space
21+
point1 = (1,2)
22+
point2 = (5,5)
23+
print("1D: Distance between", point1, "and", point2, "is", distance(point1, point2))
24+
25+
#3 - dimensional space
26+
point1 = (1,2,4)
27+
point2 = (5,5,8)
28+
print("1D: Distance between", point1, "and", point2, "is", distance(point1, point2))"""

0 commit comments

Comments
 (0)