TUPLE AND SETS IN PYTHON
ALL ABOUT TUPLE AND SETS IN PYTHON
THIS IS CODE OF SETS AND TUPLE IN PYTHON YOU CAN COPY PASTE THIS CODE AND RUN AND SEE THE OUTPUT IN THIS CODE I HAVE COVER ALMOST WORK RELATED TO SETS AND TUPLE
'''A tuple is a collection which is ordered and unchangeable. ''' mytuple = ("apple", "banana", "cherry","apple","cherry") | |
print(mytuple) | |
# Tuple items are ordered, unchangeable, and allow duplicate values. | |
# tuple length | |
thistuple = ("apple", "banana", "cherry") | |
print(len(thistuple)) | |
# create tuple with one item | |
thistuple1= ("apple",) | |
print(type(thistuple1)) | |
#NOT a tuple | |
thistuple2 = ("apple") | |
print(type(thistuple2)) | |
#tuple itmes in data types | |
tuple3=("apple","bannana","cherry") | |
tuple4=(1,5,7,9,3) | |
tuple5=(True,False,False) | |
print(tuple3) | |
print(tuple4) | |
print(tuple5) | |
print(type(tuple3)) | |
print(type(tuple4)) | |
print(type(tuple5)) | |
# access the tuple data | |
thistuple6=("apple","bannana","cheery") | |
print(thistuple6[0]) | |
# negative indexing | |
thistuple7=("apple","bannana","cherry") | |
print(thistuple7[-1]) | |
# range of indexing | |
thistuple8 = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango") | |
print(thistuple8[2:5]) | |
# check items exits | |
thistuple9=("apple","bannana","cherry") | |
if "apple" in thistuple9: | |
print("yes,'apple' is in the fruits tuple") | |
# change tuple value | |
""" Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called. | |
But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.""" | |
x = ("apple", "banana", "cherry") | |
y = list(x) | |
y[1] = "kiwi" | |
x = tuple(y) | |
print(x) | |
# add items | |
# 1.Convert into a list | |
thistuple10 = ("apple", "banana", "cherry") | |
y = list(thistuple10) | |
y.append("orange") | |
thistuple = tuple(y) | |
# 2. Add tuple to a tuple | |
thistuple11 = ("apple", "banana", "cherry") | |
y = ("orange",) | |
thistuple11 += y | |
# print(thistuple11) | |
# upacking a tuple, means tuples is outside barcket.... | |
fruits = ("apple", "banana", "cherry") | |
(green, yellow, red) = fruits | |
print(green) | |
print(yellow) | |
print(red) | |
# Using Asterisk* | |
fruits1 = ("apple", "banana", "cherry", "strawberry", "raspberry") | |
(green, yellow, *red) = fruits1 | |
print(green) | |
print(yellow) | |
print(red) |
SETS
'''
| ||
# Sets are written with curly brackets. | ||
thisset = {"apple", "banana", "cherry"} | ||
print(thisset) | ||
# # Duplicates Not Allowed | ||
thisset = {"apple", "banana", "cherry", "apple"} | ||
print(thisset) | ||
# # Get the Length of a Set | ||
thisset = {"apple", "banana", "cherry"} | ||
print(len(thisset)) | ||
# # Set Items - Data Types | ||
set1 = {"apple", "banana", "cherry"} | ||
set2 = {1, 5, 7, 9, 3} | ||
set3 = {True, False, False} | ||
print(set1) | ||
print(set2) | ||
print(set3) | ||
# # type of data types | ||
myset = {"apple", "banana", "cherry"} | ||
print(type(myset)) | ||
# # access items | ||
thisset = {"apple", "banana", "cherry"} | ||
print("apple" in thisset) | ||
# # add items | ||
thisset = {"apple", "banana", "cherry"} | ||
thisset.add("string") | ||
print(thisset) | ||
# # Add Sets | ||
thisset = {"apple", "banana", "cherry"} | ||
tropical = {"pineapple", "mango", "papaya"} | ||
thisset.update(tropical) | ||
print(thisset) | ||
''' | ||
iterables :- Iterable is an object, which one can iterate over | ||
The object in the update() method does not have to be a set, it can be any | ||
iterable object (tuples, lists, dictionaries etc.). ''' | ||
thisset = {"apple", "banana", "cherry"} | ||
mylist = ["kiwi", "orange"] | ||
print(type(thisset)) | ||
print(type(mylist)) | ||
thisset.update(mylist) | ||
print(thisset) | ||
# # remove items from list | ||
thisset = {"apple", "banana", "cherry"} | ||
thisset.remove("banana") | ||
print(thisset) | ||
# # Join Two Sets | ||
set1 = {"a", "b" , "c"} | ||
set2 = {1, 2, 3} | ||
set3 = set1.union(set2) | ||
print(set3) | ||
# # The update() method inserts the items in set2 into set1: | ||
set1 = {"a", "b" , "c"} | ||
set2 = {1, 2, 3} | ||
set1.update(set2) | ||
print(set1) | ||
# # loops items | ||
# # you can loops in the set items of the sets | ||
thisset = {"apple", "banana", "cherry"} | ||
for x in thisset: | ||
print(x) | ||
''' | ||
METHOD DESCRIPTION | ||
add() Adds an element to the set | ||
clear() Removes all the elements from the set | ||
copy() Returns a copy of the set | ||
difference() Returns a set containing the difference | ||
between two or more sets | ||
difference_update() Removes the items in this set that are | ||
also included in another, specified set | ||
discard() Remove the specified item | ||
intersection() Returns a set, that is the intersection of | ||
two other sets | ||
intersection_update() Removes the items in this set that are not present in other, | ||
specified set(s) | ||
isdisjoint() Returns whether two sets have a intersection or not | ||
issubset() Returns whether another set contains this set or not | ||
issuperset() Returns whether this set contains another set or not | ||
pop() Removes an element from the set | ||
remove() Removes the specified element | ||
symmetric_difference() Returns a set with the symmetric differences | ||
of two sets | ||
symmetric_difference_update() inserts the symmetric differences | ||
from this set and another | ||
union() Return a set containing the union of sets | ||
update() Update the set with the union of this set | ||
and others | ||
''' |
VIDEO RELATED TO SETS AND TUPLES
For notes you can check my website
I hope that it will be very helpful for
I hope that it will be very helpful for
You. For more about python and these type of cheatsheet.
join me🤗🙂🙂
Comment me. what you have like or dislike in this post.
Comments
Post a Comment