Python - Why should you learn?

Thamme Gowda

Feb 9th, 2018. SJCIT


@thammegowda
https://isi.edu/~tg

Hello World

World's simplest hello world program


In [11]:
print("Hello World")


Hello World

Two major version branches

  • Python 2.x
    • Latest: 2.7.x
  • Python 3.x
    • Latest: 3.6.x

Python 3.x is not backward compatible with python 2.x.

In Python 2.x

print "Hello World" # print is a keyword

In Python 3.x

print("Hello World") # print is a built in function

I strongly recommend you to use 3.x.

When a programmer submitted HW essay

How python programmer learned to fly

Huge Collection of Existing Libraries

  • get any library from repositories

High level ?

  • More english like (as high as we could get!)
  • Not a joke! Python code is 'executable pseudo code'

Interpreted

  • You DONT NEED TO compile code (to the machine language)
    • but if you want to (for performance reasons), you CAN!
  • python command runs an interpreter on your python code
    • converted to machine instructions at run time (of course!)
  • So, no more compile time errors! Is it good or bad?
    • Bad ? Write test cases

Dynamic Typed

  • What is a type?
  • What is a cast or (type) casting? - We dont have worry in python!

In [44]:
# sample function
def add(op1, op2):
    return op1 + op2

# Integers
var1 = 10
var2 = 20
var3 = add(var1, var2)
print(var3)

# Floats
var1, var2 = 1.5, 2.6 # multiple assignment
print(add(var1, var2))

# Strings
var1 = "ABCD"
var2 = "EFGH"
var3 = add(var1, var2)
print(var3)
print(">> With great power comes great responsibility!")


30
4.1
ABCDEFGH
>> With great power comes great responsibility!

Mis-Conceptions

  1. python does not have types?
    • No! Python does have types, but they are dynamic

In [12]:
x = "Hello"
print(type(x))
x = 10
print(type(x))
x = 1e10
print(type(x))
x = True
print(type(x))
x = None
print(type(x))


<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
<class 'NoneType'>

Automatic Memory Management

Look for this:

  • No new keyword (as in C++ or Java), no malloc calloc (as in C)
  • No need to call free (as in C) or delete (as in C++)
    • Just like Java, when you lose a reference to memory, garbage collector will mark it for cleaning

In [40]:
class User(object):

    def __init__(self, name, email):
        self.name = name
        self.email = email


u1 =  User(name="TG", email="tg@isi.edu")
print(u1.name, u1.email)
u1 = None # in python equivalent of NULL is None
print(u1)
# GC will free the above object when it needs memory


TG tg@isi.edu
None

General Purpose

Here are some libraries I have used

  • Web applications and API Services : Django, Flask,
  • Windowing Applications: PyQt, TkInter, PyGTK, PyKDE, Py
  • Operating system tools / Commandline : sys, os, argparse, subprocess modules
  • Machine Learning / Deep learning: scikit-learn, tensorflow, keras, pytorch, libSVM,
  • Scientific Computing: scipy
  • Text processing / Natural Language Processing: nltk, spacy
  • Linear Algebra: numpy
  • Data processing/ ETL: pandas,
  • Visualizations: Matplotlib
  • Distributed and parallel computing: py-spark
  • GPUs: py-cuda
  • ...

Obtaining and using libraries:

  • use conda or pip to search and install libraries (also called modules)
    • Look for miniconda and install it to your system
    • then you can use conda install [lib_name]
    • example: conda install numpy
  • use import keyword to enable a module in your program

In [41]:
import json

u1 =  User(name="TG", email="tg@isi.edu")
print(json.dumps(u1.__dict__))

# or, another way
®®from json import dumps
print(dumps(u1.__dict__))

# aliasing, yet another way 
from json import dumps as to_json
print(to_json(u1.__dict__))


{"name": "TG", "email": "tg@isi.edu"}
{"name": "TG", "email": "tg@isi.edu"}
{"name": "TG", "email": "tg@isi.edu"}
gc: collecting generation 0...
gc: objects in each generation: 934 1281 64370
gc: done, 588 unreachable, 0 uncollectable, 0.0004s elapsed

In [39]:
# Another example
import numpy as np

A = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]])
print(A)
AT_A = np.matmul(A.transpose(), A)
print(AT_A)


[[1 2 3]
 [4 5 6]
 [7 8 9]]
[[ 66  78  90]
 [ 78  93 108]
 [ 90 108 126]]

About me

  • BE in CSE at VTU/SJCIT : Class of 2012
  • MS in CSE at University of Southern California (USC) : Class of 2017

  • 2012 - 2014: Software Engineer @ Simplyphi Software Solutions, Bengaluru
  • 2013 - 2015: Co-Founder @ Datoin.com, Bengaluru
  • 2016 - 2017: Data Scientist @ NASA Jet Propulsion Laboratory, California, USA
  • 2017 - Present: Research Programmer @ USC Information Sciences Institute, California, USA

  • Committer at Apache Software Foundation
  • Google Summer of Code 2017 Mentor