10/26/17
Python - Basics
• Python
• Created by Guido van Rossum
Data Science • First released in 1991
Lecture # 4 • Widely used high-level programming language for general purpose
programming
• Python is powerful… and fast; plays well with others; runs everywhere; is
friendly & easy to learn; is open
• Will use NumPy and Pandas libraries in future, valeable for doing data
science in Python
Note: A ll Images are taken from edx.org
Python - Basics Variables in Python
• Python
• Lines in a typical Python program equals 5 times lines in other languages
like C++ or Java
• Python is surprisingly fast for interpretive language • By the end of this lecture, you will should be able to
• Jupyter • Write a simple program in python
• We will eventually be using Jupyter
• Use dynamic typing to assign values to variables
• Originally the combination of three languages, Julia (JU), Python (Pyt)
and R (R)
• These days Jupyter supports more than 40 languages
3 4
Note: A ll Images are taken from edx.org Note: A ll Images are taken from edx.org
Variables in Python Variables in Python
• Python is dynamic
typed language there
are no explicit types for
x and y
• C, C++ are strongly
typed languages
• There are still types
behind the scene with
Python
5 6
Note: A ll Images are taken from edx.org
1
10/26/17
Common Types in Python Variables in Python
• Numeric: Integers, float, complex
• Sequence: List, tuple, range • There will be an error
• Binary: Byte, bytearray with C code
• True/Falase: Bool • There will be no error
with Python
• Text: string • Why? Dynamic typing
7 8
Note: A ll Images are taken from edx.org
Python -Objects Part 1 Python -Objects Part 1
• Objects
• By the end of this lecture, you will should be able to • Can hold data
• Can have actions associated with them
• Describe an object from a programming perspective
• Recognize that everything in python is an object • In previous slide we’ve seen that x can hold integer as well as float values
because everything in Python is an object
9 10
Note: A ll Images are taken from edx.org Note: A ll Images are taken from edx.org
Python -Objects Part 1 What Happens When x = 4.5?
• X = 3 will create
PyIntObject with value of • First a PyFloatObject is
three created
• X is created in stack • Then x point to it
• PyIntObject in heap • Garbage collector in
• Stack holds local variable Python automatically
managed by program free the space for
PyIntObject
• Heap holds dynamic data
managed by OS 11 12
2
10/26/17
Another Example Python -Objects Part 2
• x is y…? • By the end of this lecture, you will should be able to
• The answer is False
• == test for equality • Create objects and call methods on objects
• x == y…?
• The answer is True
13 14
Note: A ll Images are taken from edx.org
An Example An Example
• Create a string object
• There are built-in
methods with string
15 16
Note: A ll Images are taken from edx.org
Loops in Python Loops in Python
17 18
Note: A ll Images are taken from edx.org Note: A ll Images are taken from edx.org
3
10/26/17
Loops in Python Loops in Python
19 20
Note: A ll Images are taken from edx.org Note: A ll Images are taken from edx.org
Python -Conditions Python -Conditions
21 22
Note: A ll Images are taken from edx.org Note: A ll Images are taken from edx.org
Python -Conditions Python - Functions
• Neither return type nor the parameter type need to define
• Advantage is no need to write separate function for int, float, double etc
• Problem is when you will call this function with string parameter
23 24
Note: A ll Images are taken from edx.org Note: A ll Images are taken from edx.org
4
10/26/17
Python - Functions Python - Functions
25 26
Note: A ll Images are taken from edx.org Note: A ll Images are taken from edx.org
Python - Functions Python - Functions
• Output will be
• 2.7
• None
• Because function
is not returning
anything
27 28
Note: A ll Images are taken from edx.org Note: A ll Images are taken from edx.org
Python - Scope Python – Global Variable
29 30
Note: A ll Images are taken from edx.org Note: A ll Images are taken from edx.org
5
10/26/17
Python – Global Variable
31
Note: A ll Images are taken from edx.org