Data Science With Python - Lesson 04 - Python Environment Setup and Essentials
Data Science With Python - Lesson 04 - Python Environment Setup and Essentials
You know the importance of Python and its libraries in various aspects of Data Science.
Acquire
Wrangle
Explore
Model
Data Science
Visualize
Bokeh
Why Anaconda
To use Python, we recommend that you download Anaconda. Following are some of the reasons why
Anaconda is one of the best Data Science platforms:
Enterprise-ready Data
Analytics platform
Interactive visualizations,
governance, security, and
operational support
Currently, there are two versions of Python. You can download and use 3.7 version, as the course is designed
based on the latest version.
Installation of Anaconda Python Distribution
You can install and run the Anaconda Python distribution on different platforms.
PYTHON 3.7
Website URL:
https://www.continuum.io/downloads
Graphical Installer
• Download the graphical installer.
• Double-click the .exe file to install Anaconda and
follow the instructions on the screen.
Installation of Anaconda Python Distribution
PYTHON 3.7
Website URL:
https://www.continuum.io/downloads
Graphical Installer
• Download the graphical installer.
• Double-click the downloaded .pkg file and follow the instructions.
PYTHON 3.7
Website URL:
https://www.continuum.io/downloads
Python 3.7:
bash Anaconda2-4.0.0-Linux-x86_64.sh
Jupyter Notebook
Jupyter is an open source and interactive web-based Python interface for Data Science and scientific computing.
Some of the advantages are:
To install Jupyter notebook on your system, type the command shown here on Anaconda prompt and press
Enter to execute it.
Getting Started
Variables and Assignment
A variable can be assigned or bound to any value. Some of the characteristics of binding a variable in Python are
listed here:
Let us look at an example of how you can assign a value to a variable, and print it and its data type.
Assignment
You can access a variable only if it is defined. You can define multiple variables simultaneously.
Access variable
without assignment
Multiple assignments
Assignment and Reference
When a variable is assigned a value, it refers to the value’s memory location or address. It does not equal the
value itself.
7 7
8
Python supports various data types. There are two main numeric data types:
Numeric
Integer value
Integer Float
Float value
32-bit 64-bit
Basic Data Types: String
Python has extremely powerful and flexible built-in string processing capabilities.
Boolean type
Boolean type
Type Casting
You can change the data type of a number using type casting.
Float number
A tuple is a one-dimensional, immutable ordered sequence of items which can be of mixed data types.
Create a tuple
View tuple
Try to modify
the tuple
Tuple
You can also slice a range of elements by specifying the start and end indices of the desired range.
Tuple
A list is a one-dimensional, mutable ordered sequence of items which can be of mixed data types.
Create a list
View a list
Just like tuples, you can access elements in a list through indices.
Key Value
Any
Any data
Dictionary immutable
type
type
You can view the keys and values in a dict, either separately or together, using the syntax shown here.
Create a
dictionary
View entire
dictionary
View only
keys
View only
values
Data Structure: Access and Modify dict Elements
Modify dictionary:
update
Modify dictionary:
delete
Data Structure: Set
Create a set
Create a set
View the
object type
Create sets
OR – Union
set operation
The in operator is used to generate a Boolean value to indicate whether a given value is present
in the container or not.
Create a list
Create a string
The plus operator produces a new tuple, list, or string whose value is the concatenation of its arguments.
Create tuples
Add tuples
Create lists
Add lists
Create strings
Concatenate
strings
Basic Operator: *
The multiplication operator produces a new tuple, list, or string that repeats the original content.
The * operator does not actually multiply the values; it only repeats the values for the specified
number of times.
Functions
Functions
Functions are the primary and most important method of code organization and reuse in Python.
Syntax Properties
Create function
Call function
Create function
Multiple return
Call function
Built-in Sequence Functions
enumerate
Indexes data to keep track of indices and corresponding data mapping
sorted
Returns the new sorted list for the given sequence
reversed
Iterates the data in reverse order
Zip
Creates lists of tuples by pairing up elements of lists, tuples, or other sequence
Built-in Sequence Functions: enumerate
Built-in Sequence Functions: sorted
Sort numbers
Sort a string
value
Built-in Sequence Functions: reversed and zip
Create a list of
numbers for range 15
View type
Control Flow Statements: if, elif, else
The if, elif, and else statements are the most commonly used control flow statements.
If condition
Else block
A for loop is used to iterate over a collection (like a list or tuple) or an iterator.
A while loop specifies a condition and a block of code that is to be executed until the condition evaluates to False or
the loop is explicitly ended with break.
While condition
Control Flow Statements : Exception Handling
Handling Python errors or exceptions gracefully is an important part of building robust programs and algorithms.
Create function
Error
a. Int
b. Float
c. String
a. Int
b. Float
c. String
Since one of the operands is float, the x variable will also be of the float data type.
Knowledge
Check
Which of the data structures can be modified? Select all that apply.
2
a. tuple
b. list
c. dict
d. set
Knowledge
Check
Which of the data structures can be modified? Select all that apply.
2
a. tuple
b. list
c. dict
d. set
Only a tuple is immutable and cannot be modified. All the other data structures can be modified.
Knowledge What will be the output of the following code?
Check
a. [‘NYC', 'Madrid']
b. [‘London', 'Madrid']
c. [‘Miami', 'Madrid']
d. [‘Miami', ‘Paris']
Knowledge What will be the output of the following code?
Check
a. [‘NYC', 'Madrid']
b. [‘London', 'Madrid']
c. [‘Miami', 'Madrid']
d. [‘Miami', ‘Paris']
Slicing starts at the first index and stops before the second index. Here, the element at index 3 is London and the element
before index -1 is Madrid.
Knowledge
Check
Which of the following data structures is preferred to contain a unique collection of values?
4
a. dict
b. list
c. set
d. tuple
Knowledge
Check
Which of the following data structures is preferred to contain a unique collection of values?
4
a. dict
b. list
c. set
d. tuple