Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Tuple Concept in Python:-

A tuple is a collection of finite elements which can not be modified dynamically means tuple provide an immutable object, It is basically used to create a constant LIST.


Tuple will be declared by ()  small bracket but accessed by []


x = ("C","C++",1200,1200)

means we can not change the value of x  anyways.

x[0]= 12  #error we can not change tuple elements

x.append() #tuple can not be append


Where we use tuple in the project?

In the project, if we want to declare a set of constant then we can use a tuple object because the tuple object can not be modified hence it is also called an immutable object in python.



Tuple Example:-

s = ("C","CPP",1200,3500,12.34)
#s[0]=12
#del s[0]
#s.append(12)
for i in range(0,len(s)):
    print(s[i])



Tuple using for each Loop:-

x= (12,23,34,56,67)
for x1 in x:
    print(x1)

Tuple basic operation:-

We can add (merge tuple objects), repeat, and delete tuple objects.


a=(1,2,3)
b=(4,5,6)
c=()
c=a+b
c

........................................................................................................................................................

a= (1,2,3)*4
print(a)

......................................................................................................................................................


Program of Tuple:-

WAP to find the max element in Tuple?

WAP to divide even elements and odd elements separately in a tuple?

WAP to count total even number and Odd number in Tuple?

WAP to count total integer, float, and string in a tuple?

The solution to this program:-

t = (12,23,34,45.67,"hello", "welcome", True)
cint=0
cfloat=0
cstring=0
cbool=0

for i in range(0,len(t)):
    if type(t[i]).__name__=="int":
        cint=cint+1
    elif type(t[i]).__name__=="float":
        cfloat=cfloat+1
    elif type(t[i]).__name__=="str":
        cstring=cstring+1
    elif type(t[i]).__name__=="bool":
        cbool=cbool+1

print("Total integer is ",cint, "Total float is ", cfloat," Total String is",cstring," Total boolean is ",cbool),






14 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. #WAP to find the max element in Tuple?
    t=(45,56,78,90)
    max=0
    for i in range(0,len(t)):

    if t[i]>max:
    max=t[i]
    print("the largest number is",max)

    ReplyDelete
  2. #WAP to divide even elements and odd elements separately in a tuple?
    x=(2,3,4,5,6)
    for i in range(0,len(x)):
    if x[i]%2==0:
    print("even number=",x[i])
    else:
    print("odd number=",x[i])

    ReplyDelete
  3. #WAP to divide even elements and odd elements separately in a tuple?
    x=(2,3,4,5,6)
    c1=0
    c2=0
    for i in range(0,len(x)):
    if x[i]%2==0:
    c1=c1+1
    else:
    c2=c2+1
    if c1>0:
    print("total even",c1)
    if c2>0:

    print("total odd=",c2)

    ReplyDelete
  4. x=(2,3,4,5,6)
    c1=0
    c2=0
    for i in range(0,len(x)):
    if x[i]%2==0:
    c1=c1+1
    else:
    c2=c2+1
    if c1>0:
    print("total even",c1)
    if c2>0:

    print("total odd=",c2)

    ReplyDelete
  5. Q:- WAP to find the max element in Tuple ??
    t=(15,65,98,78,25,32)
    max=0
    for i in range(0,len(t)):

    if t[i]>max:
    max=t[i]
    print("The Largest Number is :- ",max)

    ReplyDelete
  6. Q:- WAP to divide even elements and odd elements separately in a tuple ??
    x=(2,3,4,5,6)
    for i in range(0,len(x)):
    if x[i]%2==0:
    print("Even Number :=",x[i])
    else:
    print("Odd Number :=",x[i])

    ReplyDelete
  7. Program to find maximum value in tuple-->>
    s=(897,1201,3456,34.36,123,345)
    t=s[0]
    for i in range(0,len(s)):
    if s[i]>s[0]:
    t=s[i]
    print("This value is maximum-",t)

    output-This value is maximum- 3456

    ReplyDelete
  8. x=(100,103,215,67,90)
    for i in range(0,len(x)):
    if x[i]%2==0:
    print("Even Number-",x[i])
    else:
    print("Odd Number-",x[i])

    ReplyDelete
  9. DOUBT-->>
    Sir divide even elements and odd elements separately in a tuple,iss program mein user input se kaise kare?????

    Isme append karna padh raha hai,but phir bhi execute nahi ho raha hai-->

    a=[]
    n=int(input("Enter number of elements-"))
    even=[]
    odd=[]
    for i in range(0,n):
    b=int(input("Enter element-"))
    a.append(b)
    for j in a:
    if(j%2==0):
    print("even numbers list-",even)
    else:
    print("odd numbers list-",odd)

    ReplyDelete
  10. Program to count even and odd numbers in tuple-->>

    x=(23,34,56,78,90,91,65,73)
    y=0
    z=0
    for i in range(0,len(x)):
    if x[i]%2==0:
    y=y+1
    else:
    z=z+1
    print("Total even numbers=",y)
    print("Total odd numbers=",z)

    ReplyDelete
  11. Program to count total integer,float,string,boolean in tuple-->>

    x=(121,234,341,98.49,"Python","Java", True,"I bought 3 burgers")
    cint=0
    cfloat=0
    cstring=0
    cbool=0
    for i in range(0,len(x)):
    if type(x[i]).__name__=="int":
    cint=cint+1
    elif type(x[i]).__name__=="float":
    cfloat=cfloat+1
    elif type(x[i]).__name__=="string":
    cstring=cstring+1
    elif type(x[i]).__name__=="bool":
    cbool=cbool+1
    print("total integers=",cint,"total floats=",cfloat,"total strings=",cstring,"total booleans=",cbool)

    ReplyDelete
  12. deependra singh jadaunNovember 26, 2020 at 3:35 PM

    wap to find max digit from given tuple or list:

    x=(1,2,3,4,5,4,3,7,50,-1)
    y=0
    for i in x:
    if i>y:
    y=i
    print(y)

    ReplyDelete
  13. deependra singh jadaunNovember 26, 2020 at 3:50 PM

    wap to divide eeven and odd nos. from a single given tuple:

    x=(1,2,3,4,5,6,8,7,9)
    x1=[]
    x2=[]
    for i in x:
    if i%2 ==0:
    x1.append(i)

    else:
    x2.append(i)
    print(x1)
    print(x2)

    ReplyDelete
  14. deependra singh jadaunNovember 26, 2020 at 9:56 PM

    wap to find first five max. nos. from given list:

    x=(1,2,3,4,5,6,7,8,20,18,0,13)
    y=0
    z=0
    t=0
    p=0
    m=0
    for i in x:
    if i>y:
    m=p
    p=t
    t=z
    z=y
    y=i

    elif i>z:
    m=p
    p=t
    t=z
    z=i
    elif i>t:
    m=p
    p=t
    t=i
    elif i>p:
    m=p
    p=i
    elif i>m:
    m=i
    print(y)
    print(z)
    print(t)
    print(p)
    print(m)

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post