You are on page 1of 9

2/2/2021 Python for O&G Lecture 41, 42: Tuples - Colaboratory

Python for Oil and Gas

Website - https://petroleumfromscratchin.wordpress.com/

LinkedIn - https://www.linkedin.com/company/petroleum-from-scratch

YouTube - https://www.youtube.com/channel/UC_lT10npISN5V32HDLAklsw

Lecture 41: Tuples - Part 1


A kind of data structure
Can store any type of data
Tuples are immutables unlike lists.
Means, we can not add or remove any thing from tuples

# syntax - ()

nums = (1, 2, 3)
/
2/2/2021 Python for O&G Lecture 41, 42: Tuples - Colaboratory

type(nums)

tuple

# in tuple we can store variety of data type

abc = (1, 2, 'Divyansh', [1,2], 0.56, 5+8j)

type(abc)

tuple

# tuple with a single element

num1 = (1, )
type(num1)

tuple

num2 = ('Divyansh', )
type(num2)

tuple

# Now if we are't able to add or remove anything from tuples because they are immutable, why do we use them?

# Because they are FAST

/
2/2/2021 Python for O&G Lecture 41, 42: Tuples - Colaboratory

Methods we can use in tuples

# count method --> To count the no. of times a element is present in a tuple

# syntax - tuple_name.index(element you want to count)

phi = (0.1, 0.25, 0.31, 0.1, 0.35, 0.25 ,0.1)


type(phi)

tuple

phi.count(0.25)

# index method --> to know the index of any particular item in our tuple

# syntax - tuple_name.index(element you want to check the index of)

phi.index(0.1)

# if there are repitions of any element

# syntax - tuple_name.index(element, start arg, stop argument) # stop argument is not included
phi = (0.1, 0.25, 0.31, 0.1, 0.35, 0.25 ,0.1)

phi.index(0.1, 4)

6 /
2/2/2021 Python for O&G Lecture 41, 42: Tuples - Colaboratory

phi[6]

0.1

length function

# len(tuple_name)

num3 = (5, )

len(num3)

slicing and indexing

# slicing and indexing

# same as of lists
phi = (0.1, 0.25, 0.31, 0.1, 0.35, 0.25 ,0.1)

phi[::-1]

(0.1, 0.25, 0.35, 0.1, 0.31, 0.25, 0.1)

# let's try to update our tuple

phi[0] = 0.5

/
2/2/2021 Python for O&G Lecture 41, 42: Tuples - Colaboratory

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-d86134acf5ba> in <module>()
1 # let's try to update our tuple
2
----> 3 phi[0] = 0.5

Looping in tuples'tuple' object does not support item assignment


TypeError:

SEARCH STACK OVERFLOW


# for loop

for i in phi:
print(i)

0.1
0.25
0.31
0.1
0.35
0.25
0.1

# while loop

i = 0
while i < len(phi):
print(phi[i])
i = i+1

0.1
0.25
0.31
0.1
0.35
0.25
0.1

Lecture 42: Tuples - Part 2

# tuples can also be written in this way without using paranthesis


/
2/2/2021 Python for O&G Lecture 41, 42: Tuples - Colaboratory

tuple1 = (1, 2, 'Python', 0.46, 'Divyansh')

# type(tuple1)

tuple2 = 1, 2, 'Python', 0.46, 'Divyansh'

type(tuple2)

tuple

Tuple unpacking

# Tuple unpacking

tuple2 = 1, 2, 'Python', 0.46, 'Divyansh'

el1, el2, el3, el4, el5 = tuple2

print(el3)

Python

/
2/2/2021 Python for O&G Lecture 41, 42: Tuples - Colaboratory
# This kind of unpacking can also be done in lists

abc = ['hey', 'python', 2, 5]

a, b, c, d = abc

print(a)
print(d)

hey
5

Lists inside tuple

# Now we can not add or remove anything in tuple but we can surely do it with lists

random = 2, 56, 0.48, [47, 10, 'Divyansh'], 5+7j

len(random)

random[3]

[47, 10, 'Divyansh']

random[3].pop() /
2/2/2021 Python for O&G Lecture 41, 42: Tuples - Colaboratory

'Divyansh'

print(random)

(2, 56, 0.48, [47, 10], (5+7j))

random[3].append('hey')

print(random)

(2, 56, 0.48, [47, 10, 'hey'], (5+7j))

min, max, sum functions

res_a = 0.2, 0.25, 0.55 # reservoir a's water sat, gas satur, hydrocarbon saturation

min(res_a)

0.2

max(res_a)

0.55

sum(res_a)

1.0

/
2/2/2021 Python for O&G Lecture 41, 42: Tuples - Colaboratory

You might also like