Python Environment Setup and Essentials
Python Environment Setup and Essentials
Neuron
Python: Environment Setup and Essentials
Neuron
Learning Objectives
Neuron
Anaconda: The World's Most Popular Data Science Platform
Neuron
Quick Recap: Python for Data Science
You know the importance of Python and its libraries in various aspects of Data Science.
Acquire
Wrangle
Explore
Model
Data Science
Visualize
Bokeh
Neuron
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
Neuron
Installation of Anaconda Python Distribution
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.
Neuron
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.
Neuron
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.
Neuron
Installation of Anaconda Python Distribution
PYTHON 3.7
Website URL:
https://www.continuum.io/downloads
Python 3.7:
bash Anaconda2-4.0.0-Linux-x86_64.sh
Neuron
Jupyter Notebook
Jupyter is an open source and interactive web-based Python interface for Data Science and scientific computing.
Some of the advantages are:
Neuron
Getting Started
Neuron
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:
Neuron
Variables and Assignment: Example
Let us look at an example of how you can assign a value to a variable, and print it and its data type.
Assignment
Neuron
Multiple Assignments
You can access a variable only if it is defined. You can define multiple variables simultaneously.
Access variable
without assignment
Multiple assignments
Neuron
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
Neuron
Data Types and Structures
Neuron
Basic Data Types: Integer and Float
Python supports various data types. There are two main numeric data types:
Numeric
Integer value
Integer Float
Float value
32-bit 64-bit
Neuron
Basic Data Types: String
Python has extremely powerful and flexible built-in string processing capabilities.
Neuron
Basic Data Types: None and Boolean
Boolean type
Boolean type
Neuron
Type Casting
You can change the data type of a number using type casting.
Float number
Neuron
Data Structure: Tuple
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
Neuron
Data Structure: Accessing Tuples
Tuple
Neuron
Data Structure: Slicing Tuples
You can also slice a range of elements by specifying the start and end indices of the desired range.
Tuple
Neuron
Data Structure: List
A list is a one-dimensional, mutable ordered sequence of items which can be of mixed data types.
Create a list
View a list
Neuron
Data Structure: Accessing Lists
Just like tuples, you can access elements in a list through indices.
Neuron
Data Structure: Slicing Lists
Neuron
Data Structure: Dictionary (dict)
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
Neuron
Data Structure: Access and Modify dict Elements
Modify dictionary:
update
Modify dictionary:
delete
Neuron
Data Structure: Set
Create a set
Create a set
View the
object type
Neuron
Data Structure: Set Operations
Create sets
OR – Union
set operation
Neuron
Basic Operator: in
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
Neuron
Basic Operator: +
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
Neuron
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.
Neuron
Functions
Neuron
Functions
Functions are the primary and most important method of code organization and reuse in Python.
Syntax Properties
Neuron
Functions: Considerations
Neuron
Functions: Returning Values
Create function
Call function
Create function
Multiple return
Call function
Neuron
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
Neuron
Built-in Sequence Functions: enumerate
Neuron
Built-in Sequence Functions: sorted
Sort numbers
Sort a string
value
Neuron
Built-in Sequence Functions: reversed and zip
Create a list of
numbers for range 15
View type
Neuron
Control Flow Statements: if, elif, else
The if, elif, and else statements are the most commonly used control flow statements.
If condition
Else block
Neuron
Control Flow Statements : for Loops
A for loop is used to iterate over a collection (like a list or tuple) or an iterator.
The continue
statement
Neuron
Control Flow Statements : while Loops
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
Neuron
Control Flow Statements : Exception Handling
Handling Python errors or exceptions gracefully is an important part of building robust programs and algorithms.
Create function
Error
Neuron
Knowledge Check
Neuron
1. What is the data type of the object x = 3 * 7.5?
1
a. Int
b. Float
c. String
Neuron
2. Which of the data structures can be modified? Select all that apply.
2
a. tuple
b. list
c. dict
d. set
Neuron
3. What will be the output of the following code?
a. [‘NYC', 'Madrid']
b. [‘London', 'Madrid']
c. [‘Miami', 'Madrid']
d. [‘Miami', ‘Paris']
Neuron
4. Which of the following data structures is preferred to contain a unique collection of
values?
4
a. dict
b. list
c. set
d. tuple
Neuron
Key Takeaways
Neuron
Thank You
Neuron