हिंदी

TypeError occurs while statement 2 is running. Give reason. How can it be corrected? >>> tuple1 = (5) #statement 1 >>> len(tuple1) #statement 2 - Computer Science (Python)

Advertisements
Advertisements

प्रश्न

TypeError occurs while statement 2 is running. Give reason. How can it be corrected?

>>> tuple1 = (5)         #statement 1
 >>> len(tuple1)         #statement 2
टिप्पणी लिखिए
Advertisements

उत्तर

The ‘statement 1’ is creating a variable, tuple1 which is of ‘int’ data type. The ‘statement 2’ is checking for the length of the variable, but the argument passed is an ‘int’ data type. The 'len()' function can return the length only when the object is a sequence or a collection. This is the reason for the type error. 

The error can be corrected by adding one comma after ‘5’ in statement 1, as this will create a tuple and as a tuple is a collection, the 'len()' function will not return an error. The correct statement will be:

>>> tuple1 = (5,)
>>> len(tuple1)
shaalaa.com
Concepts of Tuples in Python
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 10: Tuples and Dictionaries - Exercise [पृष्ठ २२४]

APPEARS IN

एनसीईआरटी Computer Science [English] Class 11
अध्याय 10 Tuples and Dictionaries
Exercise | Q 8. | पृष्ठ २२४

संबंधित प्रश्न

What is the use of the type() function in python?


Write the syntax of creating a Tuple with n number of elements.


What are the advantages of Tuples over a list?


“Lists and Tuples are ordered”. Explain.


What advantages do tuples have over lists?


When to use a tuple or dictionary in Python. Give some examples of programming situations mentioning their usefulness.


Prove with the help of an example that the variable is rebuilt in case of immutable data types.


Write a program to read the email IDs of n number of students and store them in a tuple. Create two new tuples, one to store only the usernames from the email IDs and the second to store domain names from the email IDs. Print all three tuples at the end of the program. [Hint: You may use the function split()]


Write a program to input the names of n students and store them in a tuple. Also, input a name from the user and find if this student is present in the tuple or not. We can accomplish these by:

writing a user-defined function


Our heritage monuments are our assets. They are a reflection of our rich and glorious past and an inspiration for our future. UNESCO has identified some Indian heritage sites as World heritage sites. Collect the following information about these sites:

  • What is the name of the site?
  • Where is it located?
    District
    State
  • When was it built?
  • Who built it?
  • Why was it built?
  • The website link (if any).

Write a Python program to

  • create an administrative user ID and password to add, modify or delete an entered heritage site in the list of sites.
  • display the list of world heritage sites in India.
  • search and display information about a world heritage site entered by the user.
  • display the name(s) of world heritage site(s) on the basis of the state input by the user.

Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×