ADVANCED PROGRAMMING CONCEPTS - Python
Subject : ADVANCED PROGRAMMING CONCEPTS - Python
To solve assignments Please join Google Classroom.
Invitation of Google Classroom has been sent to your official email id.
Please accept the google classroom invitation through official email id.
Or directly by entering Class code of Google Classroom also you can join Google Class room to solve assignments.
Class code of Google Classroom : 3ojdsjm
With kind regards,
SD Bhosale
To know Attendance: Click here
Reference : https://www.w3schools.com/python/
Today's Learning:
Revision of Python List -Literal
Google Meet Session Video:
Program No. 1:
# Tuple
# A tuple is a collection which is ordered and unchangeable.
# In Python tuples are written with round brackets.
# Example
# Create a Tuple:
thistuple = ("apple", "banana", "cherry")
print(thistuple)
print(type(thistuple))
Output:
('apple', 'banana', 'cherry')
<class 'tuple'>
Program No. 2:
# Access Tuple Items
# You can access tuple items by referring to the index number,
# inside square brackets:
# Example
# Print the second item in the tuple:
thistuple = ("apple", "banana", "cherry","chiku")
print(thistuple[0:2])
Output:
('apple', 'banana')
Program No. 3:
# Negative Indexing
# Negative indexing means beginning from the end,
# -1 refers to the last item, -2 refers to the second last item etc.
# Example
# Print the last item of the tuple:
thistuple = ("chiku","apple", "banana", "cherry")
print(thistuple[-4])
Output:
chiku
Program No. 4:
# Range of Indexes or Slicing
# You can specify a range of indexes by specifying where
# to start and where to end the range.
# When specifying a range, the return value will be
# a new tuple with the specified items.
# Example
# Return the third, fourth, and fifth item:
# Note: The search will start at index
# 2 (included) and end at index 5 (not included).
# Remember that the first item has index 0.
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[0:3])
Output:
('apple', 'banana', 'cherry')
Program No. 5:
# Range of Negative Indexes
# Specify negative indexes if
# you want to start the search from the end of the tuple:
# Example
# This example returns the items
# from index -4 (included) to index -1 (excluded)
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[-10:-1])
Output:
('apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon')
Program No. 6:
# Change Tuple Values
# Once a tuple is created, you cannot change its values.
# Tuples are unchangeable, or immutable.
# But there is a workaround. You can convert the tuple into a list,
# change the list, and convert the list back into a tuple.
# Example
# Convert the tuple into a list to be able to change it:
x = ("apple", "banana", "cherry")
print(x)
print(type(x))
y = list(x)
print(type(y))
print(y)
y[1] = "kiwi"
x = tuple(y)
print(x)
Output:
('apple','kiwi','cherry')
With kind regards,
Google Meet Session Video:
No comments:
Post a Comment