You are on page 1of 17

3/7/2021 DS_Program_File_4 - Jupyter Notebook

In [74]: # DS Exercise 1.18 more about indenting.....

if 5 > 2:
print("Five is greater than two!")

Five is greater than two!

In [75]: if 5 > 2:
print("Five is greater than two!")

File "<ipython-input-75-a314491c53bb>", line 2


print("Five is greater than two!")
^
IndentationError: expected an indented block

In [76]: if 5 > 2:
print ("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")

Five is greater than two!


Five is greater than two!

In [77]: if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")

File "<ipython-input-77-a4dba24e2a47>", line 3


print("Five is greater than two!")
^
IndentationError: unexpected indent

In [7]: if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")

Five is greater than two!


Five is greater than two!

In [12]: # DS Exercise 1.19, operator with strings

x = "Python is "
y = "awesome"
z = x + y
print(z)

x = "awesome"
print("Python is " + x)

Python is awesome
Python is awesome

In [22]: # DS Exercise 1.20, Local and Global variables, function

x = "awesome"

def myfunc():
x = "fantastic"
print("Python is " + x)

myfunc()
print("Python is " + x)

Python is fantastic
Python is awesome

localhost:8888/notebooks/DS_Program_File_4.ipynb# 1/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook

In [17]: # another way to code above program


x = "awesome"

def myfunc(x):

print("Python is " + x)

myfunc("fantastic")

print("Python is " + x)

Python is fantastic
Python is awesome

In [78]: # DS Exercise 1.21, global variable using gloabl keyword

def myfunc():
global x
x = "fantastic"

myfunc()

print("Python is " + x)

Python is fantastic

In [79]: # changing global variable inside local scope

x = "awesome"

def myfunc():
global x
x = "fantastic"

myfunc()

print("Python is " + x)

Python is fantastic

In [80]: # DS Exercise 1.22, function with arguments/parameters

def myfunc(a=8,b=10):
sum=a+b
print("sum is ",sum)
return sum
myfunc()

sum is 18

Out[80]: 18

In [81]: def myfunc(a,b):


sum=a+b
print("sum is ",sum)
return sum
myfunc(4,5)

sum is 9

Out[81]: 9

In [83]: def myfunc(a=8,b=10):


sum=a+b
print("sum is ",sum)
return sum
myfunc(2,5)

sum is 7

Out[83]: 7

localhost:8888/notebooks/DS_Program_File_4.ipynb# 2/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook

In [84]: #DS Exercise 1.23, arrays

cars = ["Ford", "Volvo", "BMW"]


print(cars)

x = cars[0]
print(x)

l= len(cars)
print(l)

for x in cars:
print(x)

cars.append("Honda")
print(cars)

cars.pop(1)
print(cars)

cars.remove("BMW")
print(cars)

['Ford', 'Volvo', 'BMW']


Ford
3
Ford
Volvo
BMW
['Ford', 'Volvo', 'BMW', 'Honda']
['Ford', 'BMW', 'Honda']
['Ford', 'Honda']

In [85]: #DS Exercise 1.24, matrix

a=[[1 ,2, 3],[4, 5, 6],[7, 8 ,9]]

print(a)

print("\n")

for row in a:
print(row)

print("\n")

print(a[1][2])

print("\n")

print(a[1])

print("\n")

col = [column[2] for column in a]


print(col)

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

[4, 5, 6]

[3, 6, 9]

localhost:8888/notebooks/DS_Program_File_4.ipynb# 3/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook

In [86]: # DS Exercise 1.25, Data Types

# 1-datatype string
x = "Hello World"
y= str("Hello World")
print(x)
print(y)

Hello World
Hello World

In [87]: # 2-datatype int


x=10
y=int(10)
print(x)
print(y)

10
10

In [88]: # 3-datatype float


x=23.34
y=float(23.34)
print(x)
print(y)

23.34
23.34

In [89]: #4-datatype complex


x1=1j
x2=2+4j
x3=4j+2
print(x1, x2, x3)
x1=complex(0,1)
x2=complex(2,4)
x3=complex('2+4j')
print(x1, x2, x3)
print(x3.real)
print(x3.imag)

1j (2+4j) (2+4j)
1j (2+4j) (2+4j)
2.0
4.0

In [90]: # 5-datatype list


# List items are ordered, changeable, and allow duplicate values.
# List items are indexed, the first item has index [0], the second item has index [1] etc.
x1= ["apple", "banana", "cherry"]
x2=[12,23.45,"hello"]
print(x1)
print(x2)
x1 = list(("apple", "banana", "cherry")) # list constructor, double round brackets
print(x)

['apple', 'banana', 'cherry']


[12, 23.45, 'hello']
23.34

In [91]: thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]


print(thislist[2])
print(thislist[2:5])
print(thislist[:4])
print(thislist[2:])

if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")

thislist[1] = "blackcurrant"
print(thislist)

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]

cherry
['cherry', 'orange', 'kiwi']
['apple', 'banana', 'cherry', 'orange']
['cherry', 'orange', 'kiwi', 'melon', 'mango']
Yes, 'apple' is in the fruits list
['apple', 'blackcurrant', 'cherry', 'orange', 'kiwi', 'melon', 'mango']

localhost:8888/notebooks/DS_Program_File_4.ipynb# 4/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook

In [92]: thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]


thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)

['apple', 'blackcurrant', 'watermelon', 'orange', 'kiwi', 'mango']

In [93]: thislist = ["apple", "banana", "cherry"]


thislist[1:2] = ["blackcurrant", "watermelon"]
print(thislist)

['apple', 'blackcurrant', 'watermelon', 'cherry']

In [94]: thislist = ["apple", "banana", "cherry"]


thislist[1:3] = ["watermelon"]
print(thislist)

['apple', 'watermelon']

In [95]: thislist = ["apple", "banana", "cherry"]


thislist.insert(2, "watermelon")
print(thislist)

['apple', 'banana', 'watermelon', 'cherry']

In [96]: thislist = ["apple", "banana", "cherry"]


thislist.append("orange")
print(thislist)

['apple', 'banana', 'cherry', 'orange']

In [97]: thislist = ["apple", "banana", "cherry"]


tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)
newlist=[thislist,tropical]
print(newlist)

['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']


[['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya'], ['mango', 'pineapple', 'papaya']]

In [98]: thislist = ["apple", "banana", "cherry"]


thistuple = ("kiwi", "orange")
thislist.extend(thistuple)
print(thislist)

['apple', 'banana', 'cherry', 'kiwi', 'orange']

In [99]: thislist = ["apple", "banana", "cherry"]


thislist.remove("banana")
print(thislist)

['apple', 'cherry']

In [100]: thislist = ["apple", "banana", "cherry"]


thislist.pop(1)
print(thislist)

['apple', 'cherry']

In [101]: thislist = ["apple", "banana", "cherry"]


thislist.pop()
print(thislist)

['apple', 'banana']

In [102]: thislist = ["apple", "banana", "cherry"]


del thislist
print(thislist)

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-102-4b9c960a30eb> in <module>
1 thislist = ["apple", "banana", "cherry"]
2 del thislist
----> 3 print(thislist)

NameError: name 'thislist' is not defined

localhost:8888/notebooks/DS_Program_File_4.ipynb# 5/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook

In [103]: thislist = ["apple", "banana", "cherry"]


thislist.clear()
print(thislist)

[]

In [104]: thislist = ["apple", "banana", "cherry"]


for x in thislist:
print(x)

apple
banana
cherry

In [105]: thislist = ["apple", "banana", "cherry"]


for i in range(3):
print(thislist[i])

apple
banana
cherry

In [106]: thislist = ["apple", "banana", "cherry"]


for i in range(0,3):
print(thislist[i])

apple
banana
cherry

In [107]: thislist = ["apple", "banana", "cherry"]


for i in range(len(thislist)):
print(thislist[i])

apple
banana
cherry

In [108]: thislist = ["apple", "banana", "cherry"]


for i in range(1,2):
print(thislist[i])

banana

In [109]: thislist = ["apple", "banana", "cherry"]


i = 0
while i < len(thislist):
print(thislist[i])
i = i + 1

apple
banana
cherry

In [110]: thislist = ["apple", "banana", "cherry"]


[print(x) for x in thislist] #short hand/shortest syntax for looping
[x for x in thislist]

apple
banana
cherry

Out[110]: ['apple', 'banana', 'cherry']

In [111]: fruits = ["apple", "banana", "cherry", "kiwi", "mango"]


newlist = []

for x in fruits:
if "a" in x:
newlist.append(x)

print(newlist)

['apple', 'banana', 'mango']

localhost:8888/notebooks/DS_Program_File_4.ipynb# 6/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook

In [112]: fruits = ["apple", "banana", "cherry", "kiwi", "mango"]

newlist = [x for x in fruits if "a" in x]

print(newlist)

['apple', 'banana', 'mango']

In [113]: fruits = ["apple", "banana", "cherry", "kiwi", "mango"]


newlist = [x for x in fruits if x != "apple"]
print(newlist)

['banana', 'cherry', 'kiwi', 'mango']

In [114]: newlist = [x for x in range(10)]


print(newlist)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [115]: newlist = [x for x in range(10) if x < 5]


print(newlist)

[0, 1, 2, 3, 4]

In [116]: fruits = ["apple", "banana", "cherry", "kiwi", "mango"]


newlist = [x.upper() for x in fruits]
print(newlist)

['APPLE', 'BANANA', 'CHERRY', 'KIWI', 'MANGO']

In [117]: fruits = ["apple", "banana", "cherry", "kiwi", "mango"]


newlist = [x if x != "banana" else "orange" for x in fruits]
print(newlist)

['apple', 'orange', 'cherry', 'kiwi', 'mango']

In [118]: fruits = ["apple", "banana", "cherry", "kiwi", "mango"]


newlist = ['hello' for x in fruits]
print(newlist)

['hello', 'hello', 'hello', 'hello', 'hello']

In [119]: thislist = [100, 50, 65, 82, 23]


thislist.sort()
print(thislist)

[23, 50, 65, 82, 100]

In [120]: thislist = [100, 50, 65, 82, 23]


thislist.sort(reverse = True)
print(thislist)

[100, 82, 65, 50, 23]

In [121]: thislist = [100, 50, 65, 82, 23]


thislist.reverse()
print(thislist)

[23, 82, 65, 50, 100]

In [122]: thislist = ["apple", "banana", "cherry"]


mylist = thislist.copy()
print(mylist)

['apple', 'banana', 'cherry']

In [123]: thislist = ["apple", "banana", "cherry"]


mylist = list(thislist)
print(mylist)

['apple', 'banana', 'cherry']

In [124]: list1 = ["a", "b", "c"]


list2 = [1, 2, 3]

list3 = list1 + list2


print(list3)

['a', 'b', 'c', 1, 2, 3]

localhost:8888/notebooks/DS_Program_File_4.ipynb# 7/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook

In [125]: list1 = ["a", "b" , "c"]


list2 = [1, 2, 3]

for x in list2:
list1.append(x)

print(list1)

['a', 'b', 'c', 1, 2, 3]

In [126]: list1 = ["a", "b" , "c"]


list2 = [1, 2, 3]

list1.extend(list2)
print(list1)

['a', 'b', 'c', 1, 2, 3]

In [127]: # 6. datatype-tuple
# A tuple is a collection which is ordered and unchangeable.
# When we say that tuples are ordered, it means that the items have a defined order, and that order will not cha
# Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created.
# Since tuple are indexed, tuples can have items with the same value

thistuple = ("apple", "banana", "cherry")


print(x)

thistuple = tuple(("apple", "banana", "cherry")) # constructor


print(x)
print(len(thistuple))

3
3
3

In [128]: thistuple = ("apple",) #One item tuple, remember the commma:


print(type(thistuple))

#NOT a tuple
thistuple = ("apple")
print(type(thistuple))

<class 'tuple'>
<class 'str'>

In [129]: # tuple can be of any data vtype


tuple1 = ("apple", "banana", "cherry")
tuple2 = (1, 5.2, 7, 9, 3)
tuple3 = (True, False, False)
tuple4 = ("abc", 34, True, 40, "male")

In [130]: # access tuple items


thistuple = ("apple", "banana", "cherry")
print(thistuple[1])

banana

In [131]: # negative indexing


thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])

cherry

In [132]: thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")


print(thistuple[-4:-1])

('orange', 'kiwi', 'melon')

In [133]: # Convert the tuple into a list to be able to change it:


x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)

print(x)

('apple', 'kiwi', 'cherry')

localhost:8888/notebooks/DS_Program_File_4.ipynb# 8/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook

In [134]: # possible error

thistuple = ("apple", "banana", "cherry")


thistuple.append("orange") # This will raise an error
print(thistuple)

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-134-bcf7c0b049a8> in <module>
2
3 thistuple = ("apple", "banana", "cherry")
----> 4 thistuple.append("orange") # This will raise an error
5 print(thistuple)

AttributeError: 'tuple' object has no attribute 'append'

In [135]: #this will raise an error because the tuple no longer exists
thistuple = ("apple", "banana", "cherry")
del thistuple
print(thistuple)

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-135-8b32ef6036a6> in <module>
2 thistuple = ("apple", "banana", "cherry")
3 del thistuple
----> 4 print(thistuple)

NameError: name 'thistuple' is not defined

In [136]: fruits = ("apple", "banana", "cherry")

green, yellow, red= fruits

print(green)
print(yellow)
print(red)

apple
banana
cherry

In [137]: fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")

(green, yellow, *red) = fruits

print(green)
print(yellow)
print(red)

apple
banana
['cherry', 'strawberry', 'raspberry']

In [138]: fruits = ("apple", "mango", "papaya", "pineapple", "cherry")

(green, *tropic, red) = fruits

print(green)
print(tropic)
print(red)

apple
['mango', 'papaya', 'pineapple']
cherry

localhost:8888/notebooks/DS_Program_File_4.ipynb# 9/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook

In [139]: # looping through a tuple


thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)

print("\n")

thistuple = ("apple", "banana", "cherry")


for i in range(len(thistuple)):
print(thistuple[i])

apple
banana
cherry

apple
banana
cherry

In [140]: # join 2 tuples


tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2


print(tuple3)

('a', 'b', 'c', 1, 2, 3)

In [141]: fruits = ("apple", "banana", "cherry")


mytuple = fruits * 2

print(mytuple)

('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')

In [142]: # 7-datatype, range

x = range(6)
print(x)
x = range(6)
print(x)

range(0, 6)
range(0, 6)

In [143]: # 8-datatype, dictionary

#As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.
# Dictionaries are changeable, meaning that we can change, add or remove items after the dictionary has been cre

x = {"name" : "John", "age" : 36}


print(x)

x = dict(name="John", age=36) #constructor


print(x)

{'name': 'John', 'age': 36}


{'name': 'John', 'age': 36}

In [144]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
print(thisdict["brand"])
print(len(thisdict))

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}


Ford
3

localhost:8888/notebooks/DS_Program_File_4.ipynb# 10/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook

In [145]: # Dictionaries cannot have two items with the same key:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(thisdict)

{'brand': 'Ford', 'model': 'Mustang', 'year': 2020}

In [146]: #The values in dictionary items can be of any data type:


thisdict = {
"brand": "Ford",
"electric": False,
"year": 1964,
"colors": ["red", "white", "blue"]
}
print(thisdict)

{'brand': 'Ford', 'electric': False, 'year': 1964, 'colors': ['red', 'white', 'blue']}

In [147]: # accessing items


thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["model"])
x = thisdict["model"]
print(x)
x = thisdict.get("model")
print(x)

Mustang
Mustang
Mustang

In [148]: # keys and values


thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict.keys()
print(x)
x = thisdict.values()
print(x)

dict_keys(['brand', 'model', 'year'])


dict_values(['Ford', 'Mustang', 1964])

In [149]: #adding new item

car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

print(car)

car["color"] = "white"
print(car)

car.update({"number": 1234})
print(car)

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}


{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'white'}
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'white', 'number': 1234}

localhost:8888/notebooks/DS_Program_File_4.ipynb# 11/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook

In [150]: # changes to dictionary


car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

x = car.values()

print(x) #before the change

car["brand"] = "Benz"
print(x) #after the change
car.update({"year": 2020})
print(x)

dict_values(['Ford', 'Mustang', 1964])


dict_values(['Benz', 'Mustang', 1964])
dict_values(['Benz', 'Mustang', 2020])

In [151]: # getting dictionary items as tuples


car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

x = car.items()

print(x)

dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])

In [152]: # Check if "model" is present in the dictionary:


thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")

Yes, 'model' is one of the keys in the thisdict dictionary

In [153]: # removing items


thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)

thisdict.pop("model")
print(thisdict)

thisdict.popitem()
print(thisdict)

del thisdict["brand"]
print(thisdict)

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}


{'brand': 'Ford', 'year': 1964}
{'brand': 'Ford'}
{}

localhost:8888/notebooks/DS_Program_File_4.ipynb# 12/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook

In [154]: # possible error


thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict
print(thisdict)

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-154-233f4b53a38c> in <module>
6 }
7 del thisdict
----> 8 print(thisdict)

NameError: name 'thisdict' is not defined

In [155]: # clear
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.clear()
print(thisdict)

{}

localhost:8888/notebooks/DS_Program_File_4.ipynb# 13/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook

In [156]: # looping the dict

thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

for x in thisdict:
print(x)

print("\n")

for x in thisdict.keys():
print(x)

print("\n")

for x in thisdict:
print(thisdict[x])

print("\n")

for x in thisdict.values():
print(x)

print("\n")

for x, y in thisdict.items():
print(x, y)

brand
model
year

brand
model
year

Ford
Mustang
1964

Ford
Mustang
1964

brand Ford
model Mustang
year 1964

In [157]: # copying

thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = thisdict.copy()
print(mydict)
print("\n")
mydict = dict(thisdict)
print(mydict)

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}

localhost:8888/notebooks/DS_Program_File_4.ipynb# 14/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook

In [158]: # first method: nested dictionries

myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}

print(myfamily)

print("\n")

# second method: nested dictionaries

child1 = {
"name" : "Emil",
"year" : 2004
}
child2 = {
"name" : "Tobias",
"year" : 2007
}
child3 = {
"name" : "Linus",
"year" : 2011
}

myfamily = {
"child1" : child1,
"child2" : child2,
"child3" : child3
}
print(myfamily)

{'child1': {'name': 'Emil', 'year': 2004}, 'child2': {'name': 'Tobias', 'year': 2007}, 'child3': {'name': 'Linus
011}}

{'child1': {'name': 'Emil', 'year': 2004}, 'child2': {'name': 'Tobias', 'year': 2007}, 'child3': {'name': 'Linus
011}}

In [159]: # 9-datatype, set


# A set is a collection which is both unordered and unindexed.
# Set items are unordered, unchangeable, and do not allow duplicate values.
# Unordered means that the items in a set do not have a defined order.
# Set items can appear in a different order every time you use them, and cannot be referred to by index or key.
# Once a set is created, you cannot change its items, but you can add new items.
# Sets cannot have two items with the same value.
#Set items can be of any data type:

x = {"apple", "banana", "cherry"}


print(x)
x = set(("apple", "banana", "cherry"))
print(x)
x = {"apple", "banana", "cherry", "apple"}
print(x)
x= {1, 5, 7, 9, 3}
print(x)
x= {True, False, False}
print(x)
x= {"abc", 34, True, 40, "male"}
print(x)

{'banana', 'cherry', 'apple'}


{'banana', 'cherry', 'apple'}
{'banana', 'cherry', 'apple'}
{1, 3, 5, 7, 9}
{False, True}
{True, 34, 40, 'male', 'abc'}

localhost:8888/notebooks/DS_Program_File_4.ipynb# 15/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook

In [160]: # looping in set

thisset = {"apple", "banana", "cherry"}

for x in thisset:
print(x)

print("\n")

print("banana" in thisset)

banana
cherry
apple

True

In [161]: # adding items to list

thisset = {"apple", "banana", "cherry"}


thisset.add("orange")
print(thisset)

thisset = {"apple", "banana", "cherry"}


tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)

thisset = {"apple", "banana", "cherry"}


mylist = ["kiwi", "orange"]
thisset.update(mylist)
print(thisset)

{'banana', 'cherry', 'orange', 'apple'}


{'pineapple', 'mango', 'banana', 'cherry', 'papaya', 'apple'}
{'banana', 'apple', 'orange', 'kiwi', 'cherry'}

In [162]: # removing item from list

thisset = {"apple", "banana", "cherry"}


thisset.remove("banana")
print(thisset)

thisset.discard("cherry")
print(thisset)

x = thisset.pop()
print(thisset)

{'cherry', 'apple'}
{'apple'}
set()

In [163]: # clear and delete with set


thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)

set()

In [164]: # possible error, after deleting you cant print


thisset = {"apple", "banana", "cherry"}
del thisset
print(thisset)

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-164-3f1d76fd246d> in <module>
2 thisset = {"apple", "banana", "cherry"}
3 del thisset
----> 4 print(thisset)

NameError: name 'thisset' is not defined

localhost:8888/notebooks/DS_Program_File_4.ipynb# 16/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook

localhost:8888/notebooks/DS_Program_File_4.ipynb# 17/17

You might also like