Page 190 - Ai_V3.0_c11_flipbook
P. 190
The following line would generate an error
city[1] = "New York"
A tuple cannot be edited, But, a new tuple can be created by using an existing tuple (city) and a new value (New York).
city2 = city + ("New York",) #comma indicates that it is a tuple
print(city2) # Output: city = ("Delhi", "Madrid", "Chennai", "Rome", "New York")
Operations on Dictionary
A dictionary is a collection of key-value pairs. Dictionaries are defined by enclosing elements in curly braces { }.
Creating a Dictionary
A dictionary can be created by enclosing key-value pairs in curly braces {}, with each pair separated by commas. Within
each pair, the key and value are divided by a colon “:”
# Creating a dictionary
car = {
"make": "Toyota",
"model": "Camry",
"year": 2021,
"colour": "Blue"
}
You can access the values in a dictionary by using the keys as shown:
print(car) # Output: {'make': 'Toyota', 'model': 'Camry', 'year': 2021, 'colour':
'Blue'}
print(car["make"]) # Output: Toyota
print(car["year"]) # Output: 2021
Mutability
In dictionaries, keys are immutable whereas values are mutable. You can change the values using the respective keys:
car["colour"]="Grey"
print(car) #Ouput: {'make': 'Toyota', 'model': 'Camry', 'year': 2021, 'colour': 'Grey'}
You can also add key-value pairs in an existing dictionary:
car["mileage"] = 15000
print (car) #Ouput: {'make': 'Toyota', 'model': 'Camry', 'year': 2021, 'colour':
'Grey', 'mileage': 15000}
Control Statements
Control statements in programming languages are essential tools that allow developers to dictate the flow of execution
within their code. Depending on how the statements in a program will be executed, the flow of control in a program can
be broadly classified into three types:
• • Sequence Statements
• • Conditional Statements
• • Iterative Statements
Sequence Statement
In Python, a sequence statement refers to a set of instructions that are executed in the order they appear. This is the most
straightforward form of control flow.
188 Touchpad Artificial Intelligence (Ver. 3.0)-XI

