Skip to main content

GRADE XI - PYTHON - TUPLE

 

GRADE XI 

 PYTHON - TUPLE


A tuple is a sequence data type of Python that are used to store objects of different data types in a given order.

#Creating a tuple through input() and tuple( ) method

t1 = tuple(input("value -"))

print(t1)

>>>value -1234


#Creating tuple by accepting data from user using Looping statements

When data are accepted from user into a tuple using while loop, the entered data is concatenated in the tuple using + (concatenation) operator.

Example: Accepting n elements from user into a tuple using while loop

T1 = () #Empty tuple

n = int(input("Enter number of elements"))

i= 0

while i< n:

    num=eval(input("Enter the elements -"))

    T1+= (num,) # T1+= num, 

    i+=1

print(T1)


Accepting n elements from user into a tuple using for loop

T1 = () #Empty Tuple

n = int(input("Enter number of elements"))

for i in range(n):

    T1+= eval(input("elements")),

print(T1)


Tuple object with single element

To create a tuple with one element, a comma (,) is required to be given after the element.

Example:T = (2,) T=2,

In a single value Tuple, the comma after the value is necessary. Otherwise, Python will not consider the object as a tuple object, rather it will be treated as an object of the same type as the element given.

Example:>>> t = (3.14)

>>> type(t)

<class 'float'>


Q1. Write a program to input integer elements in a tuple and print all no.s with it’s position.

Sol:

tuple1, sum, i= (),0,0

n = int(input("Enter no of elements"))

while i<n:

    tuple1 += (int(input("Enter the element")),)

    i+=1

i=0

while i<n:

    print(tuple1[i],"Position -",i+1)

    i+= 1


Q2.#Write a program to input a tuple and display each element using for loop:

tuple=()

s = int(input("Enter no of elements -"))

for i in range(s):

    tuple+=(eval(input("Enter the element -")),)

for i in range(s):

    print(tuple[i])

Q3.#Write a program to input n number of integers in a tuple. Display sum and average

t1,sum =( ) , 0

n = int(input("Enter the number of elements"))

for i in range(n):

    t1+=(int(input("Enter the element")),)

for num in t1:

    sum +=num

print("sum", sum,"Average–",sum/n)


Q4.Write a program to create a tuple containing squares of integers from 1 till n. Input n from the user.

Sol:

tup= ()

n = int(input("Enter the number of elements in tuple"))

for i in range(1,n+1):

    tup+=(i**2,)

print(tup)

Q5.Write a program to create a tuple containing first n no. of even no.s in it. Input n from the user.

Sol:

tup=()

n = int(input("Enter the number of elements in tuple"))

for i in range(2,2*n+1,2):

    tup+=(i,)

print(tup)

Q6.Write a program to input integer elements in a tuple and print sum of all numbers divisible by 3.

Sol:

tuple1, sum, i= (),0,0

n = int(input("Enter no of elements"))

while i<n:

    tuple1+=(int(input("Enter the element")),)

    i+=1

i=0

while i<n:

    if tuple1[i] % 3 ==0: 

        sum+=tuple1[i]

    i+=1

print("Sum -",sum)

Q7.Write a program to input a tuple of s no. of elements and count number of non-numeric data in a tuple

Sol:

tuple=()

s = int(input("Enter no of elements -"))

for i in range(s):

    tuple += (eval(input("Enter the element -")),)

ctr=0

for i in range(s):

    if type(tuple[i])==str or type(tuple[i])==bool:

        ctr+=1

print("no. of non-numeric data -",ctr)

Q8.Write a program to input a tuple of n no. of elements and count all prime numbers in a tuple.


Q9. WAP to search a user input element from a tuple. Display the position of the element,

if found, otherwise display the message ”Not found”.

Sol:

tup=()

n=int(input("Enter number of elements"))

num=int(input("Enter the number to be searched:"))

for i in range(n):

    ele=int(input("Enter the element of tuple"))

    tup+=(ele,)

for i in range(n):

    if num==tup[i]:

        print(num,"found at:",i+1,"position")

        break

else:

    print("Element not found")

Q10. WAP to enter elements in the two tuples and store the sum in the third tuple.

Sol:

t1,t2,t3 = (),(),()

n = int(input("Enter no of elements "))

for i in range(n):

    t1 += (int(input("Enter element of tuple t1:")),)

    t2 += (int(input("Enter element of tuple t2:")),)

    t3 += ((t1[i] + t2[i]),)

print("First tuple",t1)

print("Second tuple",t2)

print("Third tuple:",t3)


Q11.WAP to create a tuple t1 by input elements from the user.

Also input the position from the user from where the element is to be deleted. Display the resultant tuple.

Sol:

t1=()

t2=()

n=int(input("Enter no. of elements for tuple:"))

i=1

while i<=n:

    x=int(input("Enter element no. :"))

    t1+=x,

    i+=1

print(t1)


s=int(input("Enter index no. of the tuple to be deleted:"))

i=0

for j in t1:

    if t1[i]!=t1[s-1]:

         t2+=(j,)

    i+=1

t1=t2

print("Updated Tuple",t1)

Q12.WAP to create a tuple t1 by taking elements of mixed datatype from the user. Transfer the contents in another tuple t2 as follows:

If the element is an integer, t2 should have element to the power of index.

If the element is float, t2 should have only integer part of it.

If the element is string, t2 should have length of string.

If the element is Boolean, t2 should have 1 if True otherwise 0 

Output:

Enter number of elements5

Enter the element:"kirti"

Enter the element:56.8

Enter the element:True

Enter the element:False

Enter the element:4

Original tuple: ('kirti', 56.8, True, False, 4)

New tuple: (5, 56, 1, 0, 256)

Sol:

t1,t2=(),()

n=int(input("Enter number of elements"))

for i in range (n):

    t1+=(eval(input("Enter the element")),)

for i in range (n):

    if type(t1[i])==int:

        t2+=(t1[i] ** i,)

    elif type(t1[i]) == float:

        t2+=(int(t1[i]),)

    elif type(t1[i])== str:

        t2+=(len(t1[i]),)

    elif type(t1[i])==bool:

        if t1[i]==True:

            t2+=(1,)

        else:

            t2+=(0,)

print("Original tuple:" ,t1)

print("New tuple:",t2)

Q13. 

Comments

Popular posts from this blog

CS - SORTING/SEARCHING ALGORITHMS

  SORTING ALGORITHMS                       SORTING ALGORITHM PDF LINK #Bubble Sort          ·        The outer loop iterates through the entire array. ·        The inner loop compares adjacent elements and swaps them if they are out of order. ·        The outer loop runs n times, and each pass moves the largest element to its correct position. arr=[3,8,5,2,1] n = len(arr) print(n) for i in range(n):  #traverse through all the elements         # Last i elements are already sorted, no need to check them         for j in range(0, n-i-1):              # Swap if the element found is greater than the next element              if arr[j] > arr[j+1]:               ...

GRADE XI - NESTED FOR LOOP

                                                         NESTED FOR LOOP 1. for var1 in range(3):      print(var1,"OUTER LOOP")          # 0 outer loop       1 outer loop      2 outer loop          for var2 in range(2):                  print(var2+1,"INNER LOOP")    #1  2 inner loop     1  2  inner loop   1 2 inner loop  2. Print the following pattern using for loop: 1  1 2  1 2 3  1 2 3 4  Sol: for r in range(1,5):   #ROWS     for c in range(1,r+1):   #COLUMNS         print(c,end=" ")     print() 3. Print the following pattern using for loop: @  @ @  @ @ @...