Machine - Learning - Using - Python - Ipynb - Colaboratory
Machine - Learning - Using - Python - Ipynb - Colaboratory
ipynb - Colaboratory
Declaring Variables
var1=2
var2=5.0
var3=True
var4="Machine Learning"
value of var1 : 2
value of var2 : 5.0
value of var3 : True
value of var4 : Machine Learning
type(var1)
int
type(var2)
float
type(var3)
bool
type(var4)
str
# An integer assignment
age = 45
# A floating point
salary = 111456.8
# A string
name = "John"
print(age)
print(salary)
print(name)
45
111456.8
John
100
# display
print("Before declare: ", Number)
print(a)
https://colab.research.google.com/drive/1O7cZqcSTqMrbDDDLF5ebcqx8tu9K1JgN#scrollTo=daN9C5qdeug9&printMode=true 1/11
3/24/23, 4:17 PM Machine_Learning_using_Python.ipynb - Colaboratory
print(b)
print(c)
10
10
10
print(a)
print(b)
print(c)
5
27.2
Machine Learning
a = 70
a = "Machine Learning Using Python Class"
print(a)
a = 10
b = 20
print(a+b)
a = "ML"
b = "Python"
print(a+b)
30
MLPython
Local variables are the ones that are defined and declared inside a function. We can not call this variable outside the function.
f()
Machine Learning
Global variables are the ones that are defined and declared outside a function, and we need to use them inside a function.
# Global scope
s = "Machine Learning"
f()
Machine Learning
x = 70
def change():
https://colab.research.google.com/drive/1O7cZqcSTqMrbDDDLF5ebcqx8tu9K1JgN#scrollTo=daN9C5qdeug9&printMode=true 2/11
3/24/23, 4:17 PM Machine_Learning_using_Python.ipynb - Colaboratory
# increment value of a by 5
x = x + 5
print("Value of x inside a function :", x)
change()
print("Value of x outside a function :", x)
var = 123
print("Numeric data : ", var)
# Sequence Type
String1 = 'Practical Class of Machine Learning using Python'
print("String with the use of Single Quotes: ")
print(String1)
# Boolean
print(type(True))
print(type(False))
# Creating a Dictionary
# with Integer Keys
Dict = {1: 'Machine', 2: 'Learning', 3: 'Python'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
# Python program to
# demonstrate numeric value
a = 5
print("Type of a: ", type(a))
b = 5.0
print("\nType of b: ", type(b))
c = 2 + 4j
print("\nType of c: ", type(c))
Sequence Type
1. List
List = []
print("Initial blank List: ")
print(List)
https://colab.research.google.com/drive/1O7cZqcSTqMrbDDDLF5ebcqx8tu9K1JgN#scrollTo=daN9C5qdeug9&printMode=true 3/11
3/24/23, 4:17 PM Machine_Learning_using_Python.ipynb - Colaboratory
# the use of a String
List = ['Machine Learning']
print("\nList with the use of String: ")
print(List)
Multi-Dimensional List:
[['Machine', 'Learning'], ['Python']]
print(List[-1])
print(List[-3])
2. String
# Creating a String
# with double Quotes
String1 = "Machine Learning"
print("\nString with the use of Double Quotes: ")
print(String1)
print(type(String1))
# Creating a String
# with triple Quotes
String1 = "Machine Learnign using python"
print("\nString with the use of Triple Quotes: ")
print(String1)
print(type(String1))
https://colab.research.google.com/drive/1O7cZqcSTqMrbDDDLF5ebcqx8tu9K1JgN#scrollTo=daN9C5qdeug9&printMode=true 4/11
3/24/23, 4:17 PM Machine_Learning_using_Python.ipynb - Colaboratory
String with the use of Double Quotes:
Machine Learning
<class 'str'>
String1 = "MachineLearning"
print("Initial String: ")
print(String1)
Initial String:
MachineLearning
string0 = 'python'
string1 = 'machine learning'
string0.upper()
'PYTHON'
string0.lower()
'python'
3. Tuple
https://colab.research.google.com/drive/1O7cZqcSTqMrbDDDLF5ebcqx8tu9K1JgN#scrollTo=daN9C5qdeug9&printMode=true 5/11
3/24/23, 4:17 PM Machine_Learning_using_Python.ipynb - Colaboratory
Tuple1 = ()
print("Initial empty Tuple: ")
print (Tuple1)
list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")
print(tuple(list1))
# Creating a Tuple
# with nested tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('Machine', 'Learning')
Tuple3 = (Tuple1, Tuple2)
print("\nTuple with nested tuples: ")
print(Tuple3)
Boolean
Data type with one of the two built-in values, True or False. Boolean objects that are equal to True are truthy (true), and those equal to False are
falsy (false). But non-Boolean objects can be evaluated in Boolean context as well and determined to be true or false. It is denoted by the class
bool.
print(type(True))
print(type(False))
<class 'bool'>
<class 'bool'>
Set
https://colab.research.google.com/drive/1O7cZqcSTqMrbDDDLF5ebcqx8tu9K1JgN#scrollTo=daN9C5qdeug9&printMode=true 6/11
3/24/23, 4:17 PM Machine_Learning_using_Python.ipynb - Colaboratory
set1 = set()
print("Initial blank Set: ")
print(set1)
print("Geeks" in set1)
Initial set
{'Machine', 'Python', 'Learning'}
Elements of set:
Machine Python Learning False
SetOfNumbers = {6,1,1,2,4,5}
SetOfNumbers
{1, 2, 4, 5, 6}
wc2011 = {"Dhoni","Sehwag","Tendulkar","Gambhir","Kohli","Raina","yovraj","yusuf"}
wc2015 = {"Dhoni","Dhawan","Rohit","Rahane","Kohli","Raina","Rayudu","Jadeja"}
wc2011.union(wc2015)
{'Dhawan',
'Dhoni',
'Gambhir',
'Jadeja',
'Kohli',
'Rahane',
'Raina',
'Rayudu',
'Rohit',
'Sehwag',
'Tendulkar',
'yovraj',
'yusuf'}
wc2011.intersection(wc2015)
wc2015.difference(wc2011)
https://colab.research.google.com/drive/1O7cZqcSTqMrbDDDLF5ebcqx8tu9K1JgN#scrollTo=daN9C5qdeug9&printMode=true 7/11
3/24/23, 4:17 PM Machine_Learning_using_Python.ipynb - Colaboratory
Dictionary
# Creating a Dictionary
# with Integer Keys
Dict = {1: 'Machine', 2: 'Learning', 3: 'Python'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
Empty Dictionary:
{}
wcWinners = {1975: "west Indies", 1979: "west Indies", 1983: "India", 1987: "Australia", 1991: "Pakistan", 1996: "SriLanka", 1999: "Austr
wcWinners[1983]
'India'
wcWinners.values()
dict_values(['west Indies', 'west Indies', 'India', 'Australia', 'Pakistan', 'SriLanka', 'Australia', 'Australia', 'Australia',
'India'])
set(wcWinners.values())
wcWinners[2015] ='Australia'
wcWinners
https://colab.research.google.com/drive/1O7cZqcSTqMrbDDDLF5ebcqx8tu9K1JgN#scrollTo=daN9C5qdeug9&printMode=true 8/11
3/24/23, 4:17 PM Machine_Learning_using_Python.ipynb - Colaboratory
1987: 'Australia',
1991: 'Pakistan',
1996: 'SriLanka',
1999: 'Australia',
2003: 'Australia',
2007: 'Australia',
2011: 'India',
2015: 'Australia'}
Conditional Statements
if var1>1:
print("Bigger than 1")
Bigger than 1
x=10
y=12
if x > y:
print("x>y")
elif x < y:
print("x<y")
else:
print("x=y")
x<y
x=5
isGreater = True if x > 10 else False
print(isGreater)
False
numbers = range(1,6)
numbers
range(1, 6)
for i in numbers:
print(i)
1
2
3
4
5
numbers = range(0,9)
numbers
for i in numbers:
print(i)
0
1
2
3
4
5
6
7
8
i=1
while i<5:
print(i)
i=i+1;
print("Done")
1
Done
2
Done
3
Done
4
Done
https://colab.research.google.com/drive/1O7cZqcSTqMrbDDDLF5ebcqx8tu9K1JgN#scrollTo=daN9C5qdeug9&printMode=true 9/11
3/24/23, 4:17 PM Machine_Learning_using_Python.ipynb - Colaboratory
i=1
while i<5:
print(i)
i=i+1;
print(i)
print("Done")
1
2
Done
2
3
Done
3
4
Done
4
5
Done
Functions
result = addElements(10,15)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-54-4d517483be12> in <module>
----> 1 result = addElements(10,15)
result
add = addElements(2.3,4.5)
add
addwords= addElements("MachineLearning","WithPython")
addwords
addElements(2)
addElements(2,5)
1. List
emptyList = []
batsmen = ['Rohit','Dhawan','Kolhi','Rahane','Rayudu','Dhoni']
batsmen[0]
batsmen[4]
batsmen[5]
batsmen [0:2]
batsmen[-1]
https://colab.research.google.com/drive/1O7cZqcSTqMrbDDDLF5ebcqx8tu9K1JgN#scrollTo=daN9C5qdeug9&printMode=true 10/11
3/24/23, 4:17 PM Machine_Learning_using_Python.ipynb - Colaboratory
len(batsmen)
bowlers = ['Bumrah','Shami','Bhuvi','Chachal','Kuldeep']
all_players
'Bumrah' in bowlers
'Rayudu' in bowlers
'Dhoni' in batsmen
all_players.index('Dhoni')
all_players.reverse()
https://colab.research.google.com/drive/1O7cZqcSTqMrbDDDLF5ebcqx8tu9K1JgN#scrollTo=daN9C5qdeug9&printMode=true 11/11