Professional Documents
Culture Documents
if 5 > 2:
print("Five is greater than two!")
In [75]: if 5 > 2:
print("Five is greater than two!")
In [76]: if 5 > 2:
print ("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
In [77]: if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
In [7]: if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
x = "Python is "
y = "awesome"
z = x + y
print(z)
x = "awesome"
print("Python is " + x)
Python is awesome
Python is awesome
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
def myfunc(x):
print("Python is " + x)
myfunc("fantastic")
print("Python is " + x)
Python is fantastic
Python is awesome
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Python is fantastic
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Python is fantastic
def myfunc(a=8,b=10):
sum=a+b
print("sum is ",sum)
return sum
myfunc()
sum is 18
Out[80]: 18
sum is 9
Out[81]: 9
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
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)
print(a)
print("\n")
for row in a:
print(row)
print("\n")
print(a[1][2])
print("\n")
print(a[1])
print("\n")
[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
# 1-datatype string
x = "Hello World"
y= str("Hello World")
print(x)
print(y)
Hello World
Hello World
10
10
23.34
23.34
1j (2+4j) (2+4j)
1j (2+4j) (2+4j)
2.0
4.0
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
thislist[1] = "blackcurrant"
print(thislist)
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
['apple', 'watermelon']
['apple', 'cherry']
['apple', 'cherry']
['apple', 'banana']
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-102-4b9c960a30eb> in <module>
1 thislist = ["apple", "banana", "cherry"]
2 del thislist
----> 3 print(thislist)
localhost:8888/notebooks/DS_Program_File_4.ipynb# 5/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook
[]
apple
banana
cherry
apple
banana
cherry
apple
banana
cherry
apple
banana
cherry
banana
apple
banana
cherry
apple
banana
cherry
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)
localhost:8888/notebooks/DS_Program_File_4.ipynb# 6/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook
print(newlist)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4]
localhost:8888/notebooks/DS_Program_File_4.ipynb# 7/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook
for x in list2:
list1.append(x)
print(list1)
list1.extend(list2)
print(list1)
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
3
3
3
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
<class 'tuple'>
<class 'str'>
banana
cherry
print(x)
localhost:8888/notebooks/DS_Program_File_4.ipynb# 8/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook
---------------------------------------------------------------------------
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)
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)
print(green)
print(yellow)
print(red)
apple
banana
cherry
print(green)
print(yellow)
print(red)
apple
banana
['cherry', 'strawberry', 'raspberry']
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
print("\n")
apple
banana
cherry
apple
banana
cherry
print(mytuple)
x = range(6)
print(x)
x = range(6)
print(x)
range(0, 6)
range(0, 6)
#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
In [144]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
print(thisdict["brand"])
print(len(thisdict))
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', 'electric': False, 'year': 1964, 'colors': ['red', 'white', 'blue']}
Mustang
Mustang
Mustang
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(car)
car["color"] = "white"
print(car)
car.update({"number": 1234})
print(car)
localhost:8888/notebooks/DS_Program_File_4.ipynb# 11/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook
x = car.values()
car["brand"] = "Benz"
print(x) #after the change
car.update({"year": 2020})
print(x)
x = car.items()
print(x)
thisdict.pop("model")
print(thisdict)
thisdict.popitem()
print(thisdict)
del thisdict["brand"]
print(thisdict)
localhost:8888/notebooks/DS_Program_File_4.ipynb# 12/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-154-233f4b53a38c> in <module>
6 }
7 del thisdict
----> 8 print(thisdict)
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
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)
localhost:8888/notebooks/DS_Program_File_4.ipynb# 14/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook
myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}
print(myfamily)
print("\n")
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}}
localhost:8888/notebooks/DS_Program_File_4.ipynb# 15/17
3/7/2021 DS_Program_File_4 - Jupyter Notebook
for x in thisset:
print(x)
print("\n")
print("banana" in thisset)
banana
cherry
apple
True
thisset.discard("cherry")
print(thisset)
x = thisset.pop()
print(thisset)
{'cherry', 'apple'}
{'apple'}
set()
set()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-164-3f1d76fd246d> in <module>
2 thisset = {"apple", "banana", "cherry"}
3 del thisset
----> 4 print(thisset)
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