Robotics & Artificial Intelligence
Explain the following data types:
(a) String
(b) List
(c) Tuple
(d) Dictionary
Python Funda
3 Likes
Answer
(a) String: In Python, a string is defined as a collection of one or more characters enclosed within single, double or triple quotes. When a string is assigned to a variable, each character sets itself to an index and can be accessed individually with reference to memory locations. The index starts from 0 in the forward direction and from -1 in the backward direction.
For example: wd = "Artificial Intelligence"
(b) List: A list is defined as an ordered sequence of one or more data items of the same or different data types. The data items in a list are separated by commas and enclosed within square brackets [ ]. One of the most important features of a list is that it is mutable, i.e., an element can be altered in its place. The values can be accessed with their indices, starting from the 0th index till the (length – 1)th index.
For example: list1 = ['Python', 2026, 'Edition', 'ICSE']
(c) Tuple: Like a list, a tuple is also an ordered set of elements containing one or more data items of the same or different types. The data items in a tuple are separated by commas but enclosed within parentheses ( ). The main difference between lists and tuples is that a list is mutable (can be modified) whereas a tuple is immutable (the elements cannot be updated).
For example: tuple1 = ('Robotics', 'Artificial', 'Intelligence', 'with', 'Python')
(d) Dictionary: A dictionary is an unordered set of elements where data is stored as keys along with their corresponding values. Every key has its corresponding value, forming a pair in the form of Key:Value. The collection of such pairs is enclosed within curly brackets { }. Generally, dictionary keys are strings, whereas values can be any arbitrary Python object.
For example: dict = {"Robotics":1, "Artificial":2, "Intelligence":3, "with":4, "Python":5}
Answered By
2 Likes