You've successfully subscribed to Smartcodehub ™ Blog
Great! Next, complete checkout for full access to Smartcodehub ™ Blog
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Machine Learning Prerequisite | Python Basics: Variables and Datatypes

Machine Learning Prerequisite | Python Basics: Variables and Datatypes

Neeraj Dana
Neeraj Dana

Prerequisit Google Colab Setup

The Colab Notebook for this article can be found here Variables and datatypes

Someof this Might seems very basic to you but it may help someone so please free to skip when required

In the last article we had setup our colab notebook  . This is the very first article on programming basics

Variables

So what is a variable

think of it as a box which has some value in it and is stored somewhere in the memory when we say stored somewhere in tthe memory it means it has some memory location.

X = 1
print(X)
// in this example what we are doing is we are creating a variable X and storing 1 as a value in that variable and this variable will be stored somewhere in the memory 
and when ever yu want the value of the variable you can directly use the variable



so lets say ram is having a teastore selling tea for 10rs and milk for 20rs .in a day ram sold 20 teas and 40 milks what is the amount he got 

so we will use variables 
we will create 3 variables 

TeaPrice = 10
milkPrice = 20


Totalamount =0 

TotalAmount = (20*TeaPrice) + (40* milkPrice)
print(TotalAmount)

we are calculating the amount ram earned and storing it into another variable TotalAmount

I hope it is making sense



Memory Location of variable

as i have told you every variable is tored somewhere in the memory of the computer to get the memory address we use id  function (dont wory we will see it in great detail) and to get hex value of the memory address we use hex function

# Normal Id function to get memory address

print(id(TotalAmount))

# get memeory address in hex value

print(hex(id(TotalAmount)))

DataTypes

Every Variable in any programming language has a type for example in the above example

TotalAmount variable was having value 10 which is of type int .which means datatype of TotalAmount variable is int

To Get the ttype of any variable we use type function 

print(type(TotalAmount))
//output <class 'int'>

Thier are many DataType

Number

 
if the variable has number as value
int      ----------------->  10
float    ----------------->  10.5
complex  ----------------->  1j


Exmple
x=10
print(type(x))

x=10.50
print(type(x))

x=1j
print(type(x))

# Output will be 

<class 'int'>
<class 'float'>
<class 'complex'>

Boolean

 
if the variable has condition as a value
which is like true or false as value




Exmple
x=True
print(type(x))

x=False
print(type(x))



// Output will be 

<class 'bool'>
<class 'bool'>


String

string can be anything which is sourrunded by double quotes

name = "Neeraj Dana"
gender = "M"
ACCNO = "12345XXX789"

<class 'str'>

Collection Datatypes

There are four collection data types in the Python programming language:

You can loop through a collection by using a for loop. again we will se loops in great detail in upcomming articles for now loops means going to each element and doing something

  • List is a collection which is ordered and changeable. Allows duplicate members.
  • Dictionary is a collection which is unordered, changeable and indexed. No duplicate members.
  • Set is a collection which is unordered and unindexed. No duplicate members.

List

A list is a collection which is ordered and changeable. In Python lists are written with square brackets.

The index(position at which items are stored) starts from 0 (the index of first element will be 0 )

Lets Say in our tea stall we have serveral products 

products = ["Tea","Coffee","Milk"]

# now products is the list and we can access the item in the list by its # index(the position where element is stored)
# Tea is stored at 0 index 
tea = products[0]

# you can iteratte through the items in list as 
for item in products:
	print(item)
    
print(type(tea))
print(type(products))

# output 
Tea
Coffee
Milk
<class 'str'>
<class 'list'>



Dictionary

A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values.



lets upgrade our product as one object one product which have several properties like name and price 

tea = {"Name":"Tea","Price":10}
coffee = {"Name":"Cofee","Price":15}
milk = {"Name":"Milk","Price":20}


print(tea)
print(type(tea))
print("-------------------------------------")

# you can iterate through the items in Dict as 
for key in tea:
    print(key)
    itemProp = tea[key]
    print(itemProp)
    print(type(itemProp))
    print("-------------------------------------")
    


# output 

{'Name': 'Tea', 'Price': 10}
<class 'dict'>
-------------------------------------
Name
Tea
<class 'str'>
-------------------------------------
Price
10
<class 'int'>
-------------------------------------

Set

A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.

In simple words a list which only have unique elements (just for making things simple thier are maany differences between list and sets)




print("----------------Product list with duplicate elements---------------------")
productsList = ["Tea","Coffee","Milk","Tea"]
print(productsList)
print(type(productsList))

print("-------------------------------------")


print("----------------Product Set with duplicate elements---------------------")
productsSet = {"Tea","Coffee","Milk","Tea"}
# note even though we are passing duplicate elements it will only have # unique elements in result
print(productsSet)
print(type(productsSet))

print("-------------------------------------")



# output

----------------Product list with duplicate elements---------------------
['Tea', 'Coffee', 'Milk', 'Tea']
<class 'list'>
-------------------------------------
----------------Product Set with duplicate elements---------------------
{'Coffee', 'Tea', 'Milk'}
<class 'set'>
-------------------------------------