Saturday, 1 September 2018

Introduction to Python

Python is a powerful yet easy to learn programming language. This post serves mostly as an introduction to Python as a programming language. In our next few posts, we will explore more about Data Science related aspects of Python. Let's get started from the basics.

I was pretty new to Python. I had never worked on Python before and was always wary of it. But it all changed after I got this book by Irv Kalib from the public library. It explained the basics of Python in a pretty simplistic manner. It gradually increased in complexity as you browsed through.  I will try to summarize the entire book as well as my additional sources of basic Python programming as much as possible.

Variables and assignment


In Python, you need not declare the data type of a variable while initializing it. It sort of 'knows' what you are trying to do.

Int


var_int = 123
print('The integer variable is:',(var_int))
print('Type of variable is : ', (type(var_int)))

Will get you an output of:


The integer variable is: 123
Type of variable is :  <class 'int'>                             
   
Other ways of doing it:







Float


var2 = 123.0
print('The new variable is:', var2)
print('Type of var2 variable is :',(type(var2)))

Output:


The new variable is: 123.0
Type of var2 variable is :  <class 'float'>                         

Notice how Python automatically identifies the new variable as float and not integer


String

Same goes for strings as well:

cityName = 'Chennai'
print(f'The cityName variable is: {cityName}')
print('Type of cityName is : ',(type(cityName)))

The cityName variable is: Chennai 
Type of cityName is :  <class 'str'>

Functions


As with most of the programming languages, Python has 2 types of functions known as built-in functions and user defined functions.
We already saw few built-in functions in action in our earlier section.

In Juypter notebook, you can get function details by pressing 'shift + tab' keys together. It will provide a brief overview about a function describing what arguments it takes, return types, if any, etc. This view can also be expanded to get details about the function from the Python documentation library.

Built-in functions












Some other examples of built-in functions include print(), conversion functions such as int(), float(), etc..

User defined functions


In Python, user-defined functions (UDF) operate a bit differently. 

The functions are defined using 'def' keyword. The tab / spaces denote the function block. There are no open or close brackets to define the boundaries. Also, there is no type declaration in the function inputs and return types as well. Based on the input it receives, it will return appropriate values.
For example:

def add2inputs(a,b):
    input1 = a;
    input2 = b;
    return input1 + input2

This does not have any input types. Based on the input given to the function, this will respond accordingly.

Function call

add2inputs(1,3)

Will give you an output of 4.

Whereas,

add2inputs('Python ','Code')

will give you 'Python Code' as output.

One thing to note about Python's functions is that you can also return multiple values


The For loop


Here is where things will get little interesting.


We are defining a function that squares the user input.

def square_fn(a):
    return a*a

Assigning a list of numbers to the variable list_a.

list_a = [1,2,3,4,5,10]

This following line

list_b = [square_fn(num) for num in list_a]

is equivalent to:

for num in list_a:
    sq_num = square_fn(num)
    list_b.append(sq_num)

Most places in Python, you will encounter for loop in just a single-line as mentioned above. It is convenient to write and once you get a hold of it, it will be easier to understand as well.

2 comments: