Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
N-PCs authored Nov 30, 2024
1 parent 4abdb43 commit 080bc1b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Tuple/Iterating_a_Tuple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#iterating a tuple
n=(1,2,3,4,5,6,7,8,9)
for i in n:
print(i,end=" ")
print('\n')

#iterating a tuple with a tuple as element
m=((1,2,3),(4,5,6),(7,8,9))
for i in m:
print(i,end=" ")
print('\n')

# iterating a tuple with a list as element
k=([1,2,3],[4,5,6],[7,8,9])
for i in k:
print(i,end=" ")
print('\n')

#iterating a tuple with both tuple and list as element
j=([1,2,3],(4,5,6),[7,8,9],(10,11,12))
for i in j:
print(i,end=" ")
print('\n')

#iterating a tuple with a string as element
p=("Neel","V","Pandey","a.k.a","N-PCs","is","the","G.O.A.T")
for i in p:
print(i,end=" ")
print('\n')

#example:
def full_emails(people):
result = []
for email, name in people:
result.append("{} <{}>".format(name, email))
return result
print(full_emails([("[email protected]", "Alex Diego"), ("[email protected]", "Shay Brandt")]))
53 changes: 53 additions & 0 deletions Tuple/What_is_a_Tuple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Tuples => Sequence of elements enclosed within open bracket/ parentheses () .
# Similar to lists , but have one major difference ; Tuples are immutable while lists are mutable.

''' Tuples are used in cases where we want to make sure an element in a
certain position or index refers to one specific thing and won't change.'''

'''Tuples are used for lots of different things in Python.
One common example is the return value of functions.
When a function returns more than one value, it's actually returning a tuple'''

fullname=("Neel","V","Pandey")
print(fullname)
print(type(fullname))
print("\n")


# tuple() operator
# Convert a list to a tuple
my_list = [1, 2, 3, 4]
print(my_list)
print(type(my_list))
my_tuple = tuple(my_list)
print(my_tuple)
print(type(my_tuple))
print('\n')

# Remember that although parentheses are often used to define a tuple,
# they're not always necessary. The following syntax is also valid:

my_tuple = 1, 2, 3, 4
print(my_tuple) # Outputs: (1, 2, 3, 4)


#seconds into hours , minutes , seconds
def convert_seconds():
seconds=int(input("Enter the number of seconds:"))
hours=seconds//3600
minutes=(seconds-(hours*3600))//60
remaining_seconds=seconds-(hours*3600)-(minutes*60)
return hours,minutes,remaining_seconds
result=convert_seconds()
print(result) #returns output as tuple
print(type(result))


# Unpacking a tuple
hours,minutes,remaining_seconds=result
print(hours,"hours",minutes,"min",remaining_seconds,"s")


#Another way to unpack
hours,minutes,remaining_seconds=convert_seconds()
print(hours,"hours",minutes,"min",remaining_seconds,"s")

0 comments on commit 080bc1b

Please sign in to comment.