Skip to main content

Python Concatenating two Tuples and using sum function- max() and min() functions

 #Concatenating two Tuples:

tup1 = (1, 2, 7,3, 4, 5)

tup2 = (3, 4, 5, 6, 7)

# concatenate tuples using + operator

tup3 = tup1 + tup2

print(tup3)

output:

(1, 2, 7, 3, 4, 5, 3, 4, 5, 6, 7)


sum() function

We can also use the Python built-in function sum to concatenate two tuples.

tup1 = (1, 2, 7,3, 4, 5)

tup2 = (3, 4, 5, 6, 7)

tup3=sum((tup1,tup2),())

print(tup3)

#(1, 2, 7, 3, 4, 5, 3, 4, 5, 6, 7)

#nested uples

nestedtuple = ((20, 40, 60), (10, 30, 50), "Python")

print(nestedtuple[2][0])  

for i in nestedtuple:

    print("tuple", i, "elements")

    for j in i:

        print(j, end=", ")

    print("\n")

output:

P

tuple (20, 40, 60) elements

20, 40, 60, 

tuple (10, 30, 50) elements

10, 30, 50, 

tuple Python elements

P, y, t, h, o, n, 

#built-in functions with tuple min() and max():

tup1 = ('xyz', 'zara', 'abc')

print(max(tup1))  

print(min(tup1))


tup2 = (11, 22, 10, 4)

print(max(tup2))

print(min(tup2))

output:

zara

abc

22

4






#python datatypes, #python tuple, #python tuple functions, #python training in guntur, #python job support in guntur, #python Freshers jobs








Comments

Popular posts from this blog

Python tuple packing and unpacking - Python coding

Python tuple packing and unpacking python tuple can also be created without using a tuple() class or enclosing the items inside the parentheses. it is called the variable packing #python tuple packing into tuple tuple=23,44,66,'django' print(tuple) print(type(tuple)) #unpacking tuple into variable a,b,c,d=tuple print("a is =",a) print("b is =",b) print("c is =",c) print("d is =",d) print(type(a)) print(type(b)) print(type(c)) print(type(d)) output :  (23, 44, 66, 'django') <class 'tuple'> a is = 23 b is = 44 c is = 66 d is = django <class 'int'> <class 'int'> <class 'int'> <class 'str'>

Python tuples - tuple datatype in python

Python tuples - tuple datatype in python   python tuples are ordered it can store variables of all types that are unchangeable creating tuple in two ways using parenthesis() A tuple is created by enclosing comma separated elements inside rounded brackets. python tuple characteristics:     Ordered : tuples are part of sequence data types     Unchangeable : tuple are unchangeble, we cannot add or delete element     Heterogeneous : means different datatypes Create a tuple using the two ways: #Using parenthesis () tuple=(56,77,88,78,23) print(tuple) print(type(tuple)) output : (56,77,88,78,23) #Using a tuple() constructor x=tuple((56,88,99,34.77,83)) print(x) print(type(x)) output: ((56,88,99,34.77,83)) #Heterogeneous  type tuple elements and nested tuple sampletuple = ('ram',44,45.65,[23,25, 78]) print(sampletuple) output:  ('ram',44,45.65,[23,25, 78])

remove specific element in python list - remove index based element

  #remove specific element in python list list=list([34,55,34,66,77,45.6,23,34,]) print(list) list.remove(66) print(list) output: [34, 55, 34, 66, 77, 45.6, 23, 34] [34, 55, 34, 77, 45.6, 23, 34] #Remove all occurrence of a specific python repeated element mylist = list([26, 4, 26, 26, 8, 12]) for item in mylist:     mylist.remove(26) print(mylist) output :  [4,8,12] #remove item present at given index mylist = list([2, 4, 6, 8, 10, 12]) mylist.pop(2)  #remove 2 index based element print(mylist) output:  [2, 4, 8, 10, 12] # remove item without passing index number(last value remove) using pop() mylist = list([2, 4, 6, 8, 10, 12]) mylist.pop()   print(mylist) output : [2, 4, 6, 8, 10] [2, 4, 8, 10] #remove the range of elements python list with slice: mylist = list([2, 4, 6, 8, 10, 12]) #remove item from index 2 to 5 del mylist[2:5] print(mylist) output: [2, 4, 12] #remove all items starting from index 2 mylist=list([23,44,55,34,53,78,55]) del myl...