Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

3/24/23, 4:17 PM Machine_Learning_using_Python.

ipynb - Colaboratory

Declaring Variables

var1=2
var2=5.0
var3=True
var4="Machine Learning"

print("value of var1 :", var1)


print("value of var2 :", var2)
print("value of var3 :", var3)
print("value of var4 :", var4)

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

# declaring the var


Number = 100
# display
print( Number)

100

# declaring the var


Number = 100

# display
print("Before declare: ", Number)

# re-declare the var


Number = 120.3

print("After re-declare:", Number)

Before declare: 100


After re-declare: 120.3
Code Text
a = b = c = 10

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

a, b, c = 5, 27.2, "Machine Learning"

print(a)
print(b)
print(c)

5
27.2
Machine Learning

a = 70
a = "Machine Learning Using Python Class"

print(a)

Machine Learning Using Python Class

a = 10
b = 20
print(a+b)

a = "ML"
b = "Python"
print(a+b)

30
MLPython

Global and Local Variables in Python

Local variables are the ones that are defined and declared inside a function. We can not call this variable outside the function.

# This function uses global variables


def f():
s = "Machine Learning"
print(s)

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.

# This function has a variable with


# name same as s.
def f():
print(s)

# Global scope
s = "Machine Learning"
f()

Machine Learning

# Python program to modify a global


# value inside a function

x = 70
def change():

# using a global keyword


global x

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)

Value of x inside a function : 75


Value of x outside a function : 75

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 Set with


# the use of a String
set1 = set("Machine Learning")
print("\nSet with the use of String: ")
print(set1)

# Creating a Dictionary
# with Integer Keys
Dict = {1: 'Machine', 2: 'Learning', 3: 'Python'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)

Numeric data : 123


String with the use of Single Quotes:
Practical Class of Machine Learning using Python
<class 'bool'>
<class 'bool'>

Set with the use of String:


{'L', 'i', 'g', 'h', 'M', 'n', 'r', 'e', 'c', 'a', ' '}

Dictionary with the use of Integer Keys:


{1: 'Machine', 2: 'Learning', 3: 'Python'}

Data Types in Python

# 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))

Type of a: <class 'int'>

Type of b: <class 'float'>

Type of c: <class 'complex'>

Sequence Type

1. List

List = []
print("Initial blank List: ")
print(List)

# Creating a List with

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)

# Creating a List with


# the use of multiple values
List = ["Machine Learning", "Using", "Python"]
print("\nList containing multiple values: ")
print(List[0])
print(List[2])

# Creating a Multi-Dimensional List


# (By Nesting a list inside a List)
List = [['Machine', 'Learning'], ['Python']]
print("\nMulti-Dimensional List: ")
print(List)

Initial blank List:


[]

List with the use of String:


['Machine Learning']

List containing multiple values:


Machine Learning
Python

Multi-Dimensional List:
[['Machine', 'Learning'], ['Python']]

List = ["Machine", "Learning", "Python"]


print("Accessing element from the list")
print(List[0])
print(List[2])

print("Accessing element using negative indexing")

print(List[-1])

print(List[-3])

Accessing element from the list


Machine
Python
Accessing element using negative indexing
Python
Machine

2. String

String1 = 'Machine Learning Using Python'


print("String with the use of Single Quotes: ")
print(String1)

# 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))

# Creating String with triple


# Quotes allows multiple lines
String1 = '''Time
is
Money'''
print("\nCreating a multiline String: ")
print(String1)

String with the use of Single Quotes:


Machine Learning Using Python

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'>

String with the use of Triple Quotes:


Machine Learnign using python
<class 'str'>

Creating a multiline String:


Time
is
Money

# Python Program to Access


# characters of String

String1 = "MachineLearning"
print("Initial String: ")
print(String1)

# Printing First character


print("\nFirst character of String is: ")
print(String1[0])

# Printing Last character


print("\nLast character of String is: ")
print(String1[-1])

Initial String:
MachineLearning

First character of String is:


M

Last character of String is:


g

string0 = 'python'
string1 = 'machine learning'

string2 = """"This is a multiline string"""

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)

Tuple1 = ('Machine', 'Learning')


print("\nTuple with the use of String: ")
print(Tuple1)

list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")
print(tuple(list1))

# Creating a Tuple with the


# use of built-in function
Tuple1 = tuple('Machinelearning')
print("\nTuple with the use of function: ")
print(Tuple1)

# Creating a Tuple
# with nested tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('Machine', 'Learning')
Tuple3 = (Tuple1, Tuple2)
print("\nTuple with nested tuples: ")
print(Tuple3)

Initial empty Tuple:


()

Tuple with the use of String:


('Machine', 'Learning')

Tuple using List:


(1, 2, 4, 5, 6)

Tuple with the use of function:


('M', 'a', 'c', 'h', 'i', 'n', 'e', 'l', 'e', 'a', 'r', 'n', 'i', 'n', 'g')

Tuple with nested tuples:


((0, 1, 2, 3), ('Machine', 'Learning'))

tuple1 = tuple([1, 2, 3, 4, 5])

print("First element of tuple")


print(tuple1[0])

print("\nLast element of tuple")


print(tuple1[-1])

print("\nThird last element of tuple")


print(tuple1[-3])

First element of tuple


1

Last element of tuple


5

Third last element of tuple


3

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)

set1 = set("Machine Learning Using Python")


print("\nSet with the use of String: ")
print(set1)

set1 = set(["Machine", "Learning", "Python"])


print("\nSet with the use of List: ")
print(set1)

set1 = set([1, 2, 'Machine', 4, 'Learning', 6, 'Python'])


print("\nSet with the use of Mixed Values")
print(set1)

Initial blank Set:


set()

Set with the use of String:


{'s', 't', 'L', 'y', 'i', 'P', 'g', 'h', 'M', 'n', 'r', 'o', 'e', 'c', 'a', 'U', ' '}

Set with the use of List:


{'Machine', 'Python', 'Learning'}

Set with the use of Mixed Values


{1, 2, 4, 6, 'Machine', 'Python', 'Learning'}

set1 = set(["Machine", "Learning", "Python"])


print("\nInitial set")
print(set1)

print("\nElements of set: ")


for i in set1:
print(i, end =" ")

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)

{'Dhoni', 'Kohli', 'Raina'}

wc2015.difference(wc2011)

{'Dhawan', 'Jadeja', 'Rahane', 'Rayudu', 'Rohit'}

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 an empty Dictionary


Dict = {}
print("Empty Dictionary: ")
print(Dict)

# Creating a Dictionary
# with Integer Keys
Dict = {1: 'Machine', 2: 'Learning', 3: 'Python'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)

Dict = {'Name': 'Python', 1: [1, 2, 3, 4]}


print("\nDictionary with the use of Mixed Keys: ")
print(Dict)

Dict = dict({1: 'Machine', 2: 'Learning', 3:'Python'})


print("\nDictionary with the use of dict(): ")
print(Dict)

Dict = dict([(1, 'Machine'), (2, 'Learning')])


print("\nDictionary with each item as a pair: ")
print(Dict)

Empty Dictionary:
{}

Dictionary with the use of Integer Keys:


{1: 'Machine', 2: 'Learning', 3: 'Python'}

Dictionary with the use of Mixed Keys:


{'Name': 'Python', 1: [1, 2, 3, 4]}

Dictionary with the use of dict():


{1: 'Machine', 2: 'Learning', 3: 'Python'}

Dictionary with each item as a pair:


{1: 'Machine', 2: 'Learning'}

Dict = {1: 'Machine', 'name': 'Learning', 3: 'Python'}

print("Accessing a element using key:")


print(Dict['name'])

# accessing a element using get()


# method
print("Accessing a element using get:")
print(Dict.get(3))

Accessing a element using key:


Learning
Accessing a element using get:
Python

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())

{'Australia', 'India', 'Pakistan', 'SriLanka', 'west Indies'}

wcWinners[2015] ='Australia'

wcWinners

{1975: 'west Indies',


1979: 'west Indies',
1983: 'India',

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

Generating Sequence Numbers

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)

NameError: name 'addElements' is not defined

SEARCH STACK OVERFLOW

result

add = addElements(2.3,4.5)

add

addwords= addElements("MachineLearning","WithPython")

addwords

def addElements(a, b=4):


return a+b

addElements(2)

addElements(2,5)

Working with Collections

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 = batsmen + bowlers

all_players

'Bumrah' in bowlers

'Rayudu' in bowlers

'Dhoni' in batsmen

all_players.index('Dhoni')

all_players.reverse()

check 0s completed at 4:16 PM

https://colab.research.google.com/drive/1O7cZqcSTqMrbDDDLF5ebcqx8tu9K1JgN#scrollTo=daN9C5qdeug9&printMode=true 11/11

You might also like