Tuple_Dictionary_Set (1)
Tuple_Dictionary_Set (1)
Tuple is an ordered sequence of items same as a list. The only difference is that tuples are immutable.
Tuples once created cannot be modified.
Tuples are used to write-protect data and are usually faster than lists as they cannot change
dynamically.
t = (5,'program', 1+3j)
# t[1] = 'program'
print("t[1] = ", t[1])
# Generates error
# Tuples are immutable
t[0] = 10
Python Set
Set is an unordered collection of unique items. Set is defined by values separated by comma inside
braces {}. Items in a set are not ordered.
a = {5,2,3,1,4}
We can perform set operations like union, intersection on two sets. Sets have unique values. They
eliminate duplicates.
a = {1,2,2,3,3,3}
print(a)
Since, set are unordered collection, indexing has no meaning. Hence, the slicing operator [ ] does not
work.
Python Dictionary
Dictionary is an unordered collection of key-value pairs.
It is generally used when we have a huge amount of data. Dictionaries are optimized for retrieving data.
We must know the key to retrieve the value.
In Python, dictionaries are defined within braces {} with each item being a pair in the form key-value.
Key and value can be of any type.
d = {1:'value','key':2}
print(type(d))
# Generates error
print("d[2] = ", d[2])
Decision making is required when we want to execute a code only if a certain condition is satisfied.
num = 3
if num > 0:
num = -1
if num > 0:
print(num, "is a positive number.")
Example of if...else
num = 3
# num = -5
# num = 0
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")
Example of if...elif...else
num = 3.4
# num = 0
# num = -4.5
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Loop body
Here, val is the variable that takes the value of the item inside the sequence on each iteration.
Loop continues until we reach the last item in the sequence. The body of for loop is separated from the
rest of the code using indentation.
Example:
# List of numbers
sum = 0
sum = sum+val
To force this function to output all the items, we can use the function list().
print(range(10))
print(list(range(10)))
print(list(range(2, 8)))
We can use the range() function in for loops to iterate through a sequence of numbers. It can be
combined with the len() function to iterate through a sequence using indexing. Here is an example.
for i in range(len(genre)):
digits = [0, 1, 5]
for i in digits:
print(i)
else:
This for…else statement can be used with the break keyword to run the else block only when
the break keyword was not executed.
student_name = 'Yuvraj'
if student == student_name:
print(marks[student])
break
else:
Example:
# Program to add natural
# numbers up to
# sum = 1+2+3+...+n
# n = int(input("Enter n: "))
n = 10
sum = 0
i=1
while i <= n:
sum = sum + i
Same as with for loops, while loops can also have an optional else block.
The else part is executed if the condition in the while loop evaluates to false.
The while loop can be terminated with a break statement. In such cases, the else part is ignored. Hence,
a while loop's else part runs if no break occurs and the condition is false.
'''Example to illustrate
print("Inside loop")
counter = counter + 1
else:
print("Inside else")
if val == "i":
break
print(val)
print("The end")
The continue statement is used to skip the rest of the code inside a loop for the current iteration only.
Loop does not terminate but continues on with the next iteration.
if val == "i":
continue
print(val)
print("The end")