Python basics for data science

  While there are a host of great languages for data science like C/C++, Julia, R, Python, etc. I keep this for python only. Python is a beginner friendly language and is very close to the spoken English language. You too think so that is why you came here to learn python basics for data science. 

There are certain basics to getting started. I will go through the syntax followed by some examples. 

Variables in python

In python, there is no need to declare a variable. When you define a variable, python smartly catches the variable type. 

x="Subarna" 

is a string variable. 

y=45

is an integer variable. 

You could even change the variable type in this way

x=45

What are variables in python?

Like in any other language, in python, a variable is a container for storing data. This data may be an integer in which case we call it an integer variable. If this data is a string then we call it a string variable, if this data is a decimal then we call it a decimal variable, if this data is a Boolean then we call it a Boolean variable. 

In the above examples, '45' is an integer variable while 'Subarna' is a string variable. Note that if we write 

z='45' 

then z becomes a string variable because quotation marks (' ') have been used. 

Note that you could use single quotes (' ') or double quotes (" ") to define a string variable, it makes no difference. 

Variable name and variable value

Note the difference between variable name and variable value. In the above examples, x, y, and z are variable names. Whereas, 45, '45', and 'Subarna' are variable values. 

Variable name is the metaphorical container while variable value is the content of that container. This content may be of string, integer, floating point, Boolean, etc. types. 

Variable names are case sensitive in python

In python, x and X are different variable names. So, you could write

x=12
X=55

Note that we do not end a python statement with a semicolon (;) like in C or many other programming languages. 

Casting in Python

Though python is smart enough to catch the variable type, you could also declare it. This method by which you tell python about the type of variable you are using is called casting. 

a = int (3)
b = float (3)
c = string (3)

This means you are assigning these values to a, b, and c.
 
a is 3
b is 3.0
c is '3'

Type function in python

When you are writing lengthy codes then you may forget later what kind of values you have assigned to a variable. For this, there is a feature called the type() function. For example,

print(type(y))

It will give output to tell you that it is an integer type variable.

Types of variables in python

Int

These are the integers. Which means positive numbers, negative numbers and zero. Fractions and decimals are not integers. 
5, -9, 0, etc. are integers. However, -0.3, 7/9, 5.6, etc. are not integers. 

Float

When there are decimal points like -9.8, 8.7, 0.3, etc. then these are floating point variables. Notice the difference between p and q.
p = 3 
q=3.0
p is an integer variable while q is a floating point variable. 

Similarly, 0 is an integer whereas, 0.0 is a floating point variable. 

Some more examples of floating point variables are 15e+17, -2e+2, 8.9e-15, etc. 

What does "e" mean in python?

e= 10^
Hence, -2e+2 means -2*10².

Long

If you want to write a number like 4589627L then python considers it a long integer. -452308L is also a long integer. You could write "L" or "l" for a long integer. It makes no difference. But, people prefer "L" because "l" resembles "1"(the digit one) and creates confusion. 

Complex

Any number written in a+bj form is a complex number. For example, (7.8+5.3j), (4j), (4e+5j), etc.

(4e+5j) means 4x10^5j (four into ten to the power five j).

Also note that 'j' and 'J' make no difference. So, '4j' and '4J' mean the same things. 

Boolean

Boolean variables can take up only two values, "true" and "false".

What are true values in Boolean?

Only empty strings are false, all other strings are true.
Only 0 is false among numbers, all other numbers are true. 
Similarly, empty lists, tuples, and dictionaries (we will learn later) are false all others are true. 

print(bool('Subarna')) 

will return 'True'

bool(False)

will return 'False.'

However, 
bool('False')
will return 'True'.

It is because False  is a Boolean variable whereas 'False'  is a string variable. 

bool(" ")
will return 'False' because it is an empty string.

bool(None)
will also return "False".

bool(())
bool([])
bool({})

all will return 'False' because they are empty lists, empty tuples, and empty dictionaries (more on that later) respectively. 

x='Subarna'
print(bool(x))

will return 'True'.

print(bool(9))

will return 'True'.

y=9
print(bool(y))

will return 'True'.

Python gives Boolean output in some cases

When python has to compare between two values, for example, 
print(2>6)
gives output 'False'

When python uses if statement (later more on this) then when the condition then python gives 'True' and if not then it gives 'False'.

Number type conversion in python

We have learnt that 3 is an integer whereas 3.0 is a floating point variable. How can we convert an integer to a floating type or any number type to another number type for example. 

x=3.0
print(int(x))

gives 3

Comments

Popular posts from this blog

Diary -24th July

How to make clip art faces-Part 1: Pretty girl face with braided hair decorated with flowers

Object oriented programming in dart concepts