About Me

My photo
Mumbai, Maharastra, India
He has more than 7.6 years of experience in the software development. He has spent most of the times in web/desktop application development. He has sound knowledge in various database concepts. You can reach him at viki.keshari@gmail.com https://www.linkedin.com/in/vikrammahapatra/ https://twitter.com/VikramMahapatra http://www.facebook.com/viki.keshari

Search This Blog

Monday, September 2, 2019

List is mutable in Python

Lets check this out by writing a small script, which will create two list and copy both the list to form another list.

a = ['neha','sharma']
b=[
10,12]
print('a location:', id(a), ' b location:', id(b))
c=[a
,b]
print('c list [a,b]:', c, ' c location :', id(c))

Output:
a location: 14501328  b location: 14502488
c list [a,b]: [['neha', 'sharma'], [10, 12]]  c location : 51493064

Now lets insert a value at the end of second list that is b and check if it is affecting list c

b.insert(2,13)
print('b after inserting 13', b)
print('b locatiton after inserting new value :', id(b))
print(id(c))

Output
b after inserting 13 [10, 12, 13]
b locatiton after inserting new value : 14502488
c after changing b :   [['neha', 'sharma'], [10, 12, 13]]  c location after change : 51493064


Here we can see, changing the list of b, changed the list of c, without changing the memory location. So here we can see list is mutable.

Complete Code:

a = ['neha','sharma']
b=[
10,12]
print('a location:', id(a), ' b location:', id(b))
c=[a
,b]
print('c list [a,b]:', c, ' c location :', id(c))
b.insert(
2,13)
print('b after inserting 13', b)
print('b locatiton after inserting new value :', id(b))
print('c after changing b :  ',c, ' c location after change :', id(c))


But what if we don’t want c to get changed with the change in b…. let’s check this in next post..


Data Science with…Python J
Post Reference: Vikram Aristocratic Elfin Share

Sunday, September 1, 2019

Reversing a string in Python

It is quite simple in Python, it can be done easily using step parameter of string extract

>>> str = 'reverse'
>>> str[::-1]
'esrever'

With the help of step = -1, we can easily reverse any string in python.

Lets create a function, which will reverse the given string and then check if reverse = actual then return true else false.

def reverseStr(str):
   
if str[::-1] == str:
       
print('The result is True')
   
else:
       
print('The result is False')

str =
'liril'
reverseStr(str)

Output:
True

So here you can assign str with value ‘liril’ which in turn is passed to the function and the result was equal to the reverse of the string passed that’s why the result was true.


Data Science with…Python J
Post Reference: Vikram Aristocratic Elfin Share

How immutable objects are treated in python function

ImMutable: An immutable object can’t not be changed after it is created, example are: any build-in datatype-int, float, str, bool, tuple)

What exactly it means that, when you declare and initialize any immutable variable, it preserve a space in the memory, and the moment you change the value of the variable, modified value get a new  space in memory.

>>> a=3
>>> a
3
>>> id(a)
1812388048
>>> a=a+2
>>> a
5
>>> id(a)
1812388080
>>> type(a)
<class 'int'>

Here in above example if you see, Variable “a” is initialized with value 3 and it occupies memory address of 1811288048, and the moment we tried to update the value of Variable ‘a’ to 5, the modified value i.e. 5 gets a  new memory space which is 1812388080, that is because of immutable characteristics of int datatype.

Now lets create a function, here the function takes a parameter of int type

def updateNumber(n):
   
print(id(n))
    n+=
10
   
print(n)

lets try to call the function with a int variable

def updateNumber(n):
   
print('Inside function, before modifying value of a, Mem address of n = ',id(n))
    n+=
10
   
print('Value of a inside function after modification =', n)

a=
5
print('Before calling function Mem address of a = ', id(a))
updateNumber(a)
print('Value of a after calling function = ', a)

Output:

Before calling function Mem address of a =  1812388080
Inside function, before modifying value of a, Mem address of n =  1812388080
Value of a inside function after modification = 15
Value of a after calling function =  5

Here if you see, the location of n inside the function is same as location of a, before calling function i.e. 1812388080, then function modify the value of n from 5 to 15 so inside function when we tried to print the value of n, it gave 15. But once we came out of function and tried to print the value of a, it still gave 5 unchanged.

That means modification of value inside function was not retained outside function. Lets find out why .. by printing a location of n after modifying the value of n in function.

def updateNumber(n):
   
print('Inside function, before modifying value of a, Mem address of a = ',id(n))
    n+=
10
   
print('Inside function after modification of value of n, location of n is = ', id(n))
   
print('Value of a inside function after modification =', n)

a=
5
print('Before calling function Mem address of a = ', id(a))
updateNumber(a)
print('Value of a after calling function = ', a)
print('After calling function Mem address of a = ', id(a))

Output:

Before calling function Mem address of a =  1812388080
Inside function, before modifying value of a, Mem address of a =  1812388080
Inside function after modification of value of n, location of n is =  1812388240
Value of a inside function after modification = 15
Value of a after calling function =  5
After calling function Mem address of a =  1812388080

Here we can see, the location of n inside function was same as a outside function but the moment function modifies the value of n, it took a separate location and that is the reason why a value even after modifying it inside function didn’t change.

The reason behind this kind of behavior is datatype int is immutable.


Data Science with…Python J
Post Reference: Vikram Aristocratic Elfin Share

Sunday, August 25, 2019

Creating first database in Azure SQL Database

Login to your Azure Account, here below you can see there is no resource… Click on SQL Database service on left vertical panel



Here you can see there is no SQL Database listed, click on Add on right top area



Few item which need to be understood before creating any database

Subscription: All resources in azure subscription are billed together. You need to select your subscription here.

Resource group: A container that holds related resources for an Azure solution. The resource group includes those resources that you want to manage as a group. You decide how to allocate resources to resource groups based on what makes the most sense for your organization.

Resource: A manageable item that is available through Azure. Virtual machines, storage accounts, web apps, databases, and virtual networks are examples of resources.

Elastic Pool: SQL Database Elastic Pool is a shared resource model that enables higher resource utilisation efficiency, with all the databases within an elastic pool sharing predefined resources within the same pool.

Any database in Azure need to be created under a logical server, to create a logical server, click create new under server link and enter the server name and login detail.



Now configure storage and DTU (database throughput unit), here I am keeping it default Basic



The final version of configuration



Now click on Resource Group and select the recently created resource to see all resource which we created just above .. i.e. server and database



Now we are ready with our first ever database in Azure, lets connect it with SSMS client, but before that, we need to set the firewall to allow our client SSMS IP address registered in Azure logical SQL Server

For setting the firewall in newly created logical SQL Server, go to resource group à go to sql database created à select set server firewall



Click Add Clien IP à Give the IP address à Press Save



Once that is done, go to the resource group à. Select the newly created resource à select the newly created server à go to the server properties à copy the server name



Now lets open SSMS at client machine, enter the server detail which was copied from above, put the login name and password which was set in above steps



Now we are able to connect to our first Azure database




Enjy coding…SQL Azure J
Post Reference: Vikram Aristocratic Elfin Share

Different Flavor of Azure SQL Database


We have three different Kind of Databaase deployment in Azure SQL database:
  •          Singleton:
  •          Elastic Pool:
  •          Managed Instance




Singleton : With a single database, each database is isolated from each other and portable, each with its own service tier within the DTU-based purchasing model. The single database deployment option creates a database in Azure SQL Database with its own set of resources and is managed via a SQL Database server.

Elastic Pool: SQL Database elastic pools are a simple, cost-effective solution for managing and scaling multiple databases that have varying and unpredictable usage demands. The databases in an elastic pool are on a single Azure SQL Database server and share a set number of resources at a set price.

Managed Instance: A managed instance in Azure SQL Database is a fully managed SQL Server Database Engine Instance hosted in Azure cloud. This is the best PaaS option for migrating your SQL Server database to the cloud.


Note: For Azure SQL Database single databases and elastic pools, only master Database and tempdb Database apply. For Azure SQL Database Managed Instance, all system databases apply. For more information on Managed Instances in Azure SQL Database

Post Reference: Vikram Aristocratic Elfin Share