Which of the following statements will create a tuple: Tup = (1, 'python', (2, 3), ('a', 'b')) Multiple Choice Questions Question 1 Yes, it is possible to have an integer, a string, a tuple of integers and a tuple of strings in a tuple because tuples can store elements of all data types. Question 13Ĭan you have an integer, a string, a tuple of integers and a tuple of strings in a tuple? are present only in string and not available in tuple object. Certain functions like split( ), capitalize( ), title( ), strip( ), etc. in operator works differently in both of them. No, a string and a tuple of characters are not the same even though they share similar properties like immutability, concatenation, replication, etc.Ī few differences between them are as follows:ġ. Is a string the same as a tuple of characters? TypeError: can only concatenate tuple (not "int") to tuple Question 12 If a is tuple and b is 5 then a + b will raise a TypeError because it is not possible to concatenate a tuple with an integer. What does a + b amount to if a is a tuple and b is 5? TypeError: can't multiply sequence by non-int of type 'tuple' Question 11 If a and b are tuples then a * b will throw an error since a tuple can not be multiplied to another tuple. What does a * b amount to if a and b are tuples? Here, c is a tuple with elements (1, 2, 3, 4, 5, 6) Question 10 Given a and b are tuples, so in this case the + operator will work as concatenation operator and join both the tuples. What does a + b amount to if a and b are tuples? TypeError: 'tuple' object does not support item assignment Tuple - We cannot change the elements of a tuple as tuple is an immutable sequence.Here, we are changing the value of first key-value pair of dictionary d. Dictionary - We can change the elements of a dictionary as dictionary is a mutable sequence.Yes, we can change any element of a sequence in python only if the type of the sequence is mutable. 1.Ĭan you change an element of a sequence? What if a sequence is a dictionary? What if a sequence is a tuple? Now the expression a] has become a where a represents fifth index of a i.e. Now the expression a]] has become a] where a represents second index of tuple a i.e., 4. Now the expression a+1]]] has become a]] where a represents fifth index of tuple a i.e., 1. a represents third index of tuple a which is 3.Now the expression a] has become a which represents first element of a i.e. a represents last index of tuple a which is 0.Now the expression a] has become a which implies 0. a represents first index of tuple a which is 5.If a = (5, 4, 3, 2, 1, 0) evaluate the following expressions: The individual elements of a tuple are accessed through their indexes given in square brackets as shown in the example below: How are individual elements of tuples accessed? A tuple can contain elements of all data types. No, all the elements of the tuple need not be of the same type. What values can we have in a tuple? Do they all have to be the same type*? Where sequence can be any type of sequence object like strings, tuples, lists, etc. By using the built-in tuple type object (tuple( )) to create tuples from sequences as per the syntax given below:.By placing a sequence of values separated by comma without parentheses (parentheses are optional).By placing a sequence of values separated by comma within parentheses.Tuples can be created by the following ways: What are different ways of creating a tuple? Tuples are called immutable types because we cannot change elements of a tuple in place.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |