Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
49 views

Data Science With Python - Lesson 04 - Python Environment Setup and Essentials

The document discusses Python data science fundamentals including Anaconda installation, Jupyter notebooks, data types, variables, data structures like lists and tuples, and control flow statements. It provides an overview of important concepts for getting started with Python for data analysis and modeling.

Uploaded by

Elie Mrad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Data Science With Python - Lesson 04 - Python Environment Setup and Essentials

The document discusses Python data science fundamentals including Anaconda installation, Jupyter notebooks, data types, variables, data structures like lists and tuples, and control flow statements. It provides an overview of important concepts for getting started with Python for data analysis and modeling.

Uploaded by

Elie Mrad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Data Science with Python

Python: Environment Setup and Essentials


Learning Objectives

By the end of this lesson, you will be able to:

Explain Anaconda and Jupyter notebook installation

List the important data types supported by Python

Discuss data structures, such as lists, tuples, sets, and dicts

Explain slicing and accessing the four data structures

Discuss basic operators and functions

Outline the important control flow statements


Anaconda: The World's Most Popular Data Science Platform
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
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:

Open source Python


distribution

400+ popular Python packages

Enterprise-ready Data
Analytics platform

Modern Data Science analytics


architecture

Multi-workload Data Analytics

Interactive visualizations,
governance, security, and
operational support

Big Data environments support


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.
Installation of Anaconda Python Distribution

You can install and run the Anaconda Python distribution on different platforms.
PYTHON 3.7

Windows Mac OS Linux

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

Windows Mac OS Linux

Website URL:
https://www.continuum.io/downloads

Graphical Installer
• Download the graphical installer.
• Double-click the downloaded .pkg file and follow the instructions.

Command Line Installer


• Download the command line installer.
• In your terminal window, type the command listed below and
follow the given instructions:
Python 3.7:
bash Anaconda2-4.0.0-MacOSX-x86_64.sh
Installation of Anaconda Python Distribution

PYTHON 3.7

Windows Mac OS Linux

Website URL:
https://www.continuum.io/downloads

Command Line Installer


• Download the installer.
• In your terminal window, type the command line
shown below and follow the instructions:

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:

Python language Content sharing and


support contribution

Big Data platform Built-in interactive


integration widgets
Jupyter Notebook: Installation

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:

The variable refers to the memory location


of the assigned value.

The variable appears on the left, while the


value appears on the right.

The data type of the assigned value and


the variable is the same.
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

Variable data value

Data type of the object


Multiple Assignments

You can access a variable only if it is defined. You can define multiple variables simultaneously.

Access variable
without assignment

Access variable after


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.

Ref: <address 1> Ref: <address 12>


Garbage collected

7 7
8

Memory location Memory location


Data Types and Structures
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
Basic Data Types: String

Python has extremely powerful and flexible built-in string processing capabilities.

With single quote

With double quote


String Three double quotes

Print string values


Basic Data Types: None and Boolean

Python also supports the None and Boolean data types.

Null value type

Boolean type

Boolean type
Type Casting

You can change the data type of a number using type casting.

Float number

Type cast to integer

Type cast to string value


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

Access the data at


index value 1

Try to modify
the tuple

Error: A tuple is immutable


and can’t be modified
Data Structure: Accessing Tuples

You can access a tuple using indices.

Tuple

Access with positive index

Access with negative index


Data Structure: Slicing Tuples

You can also slice a range of elements by specifying the start and end indices of the desired range.

Tuple

Count starts with the first index,


but stops before the second index

Even for negative indices, the count


stops before the second index
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

Modify a list: Add new items

Modify a list: Remove items

Access and remove list data using


element indices

Modify a list: Insert a new item at a


certain index
Data Structure: Accessing Lists

Just like tuples, you can access elements in a list through indices.

New modified list

Access with positive index

Access with negative index


Data Structure: Slicing Lists

Similar to tuples, you can also slice lists through indices.

New modified list

Count starts with the first index,


but stops before the second index

Even for negative indices, the count


stops before the second index
Data Structure: Dictionary (dict)

Dictionaries store a mapping between a set of keys and a set of values.

Key Value
Any
Any data
Dictionary immutable
type
type

Define Modify Lookup Delete


View
Data Structure: View Dictionaries

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

You can also access and modify individual elements in a dict.

Access with key

Modify dictionary:
update

Modify dictionary:
delete
Data Structure: Set

A set is an unordered collection of unique elements.

Create a set

View the set

Create a set

View the
object type

View the set


Data Structure: Set Operations

Let us look at some basic set operations.

Create sets

OR – Union
set operation

View the output of the OR


operation

AND – Intersection set operation

View the output of the


AND operation
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

Test presence of string


with in operator

Create a string

Test presence of substrings


with in operator
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
Basic Operator: *

The multiplication operator produces a new tuple, list, or string that repeats the original content.

* operator with tuple

* operator with list

* operator with string

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

def <name>(arg1, arg2, ..., argN): • Outcome of the function is communicated by


<statements> return statement
return <value> • Arguments in parenthesis are basically
assignments

Use def to create a function and assign it a name.


Functions: Considerations

Some important points to consider while defining functions:

• A function should always have a return value.


• If return is not defined, then it returns None.
• Function overloading is not permitted.
Functions: Returning Values

You can use a function to return a single value or multiple values.

Create function

Call function

Create function

Multiple return

Call function
Built-in Sequence Functions

The built-in sequence functions of Python are:

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

Use reversed function


to reverse the order

Define list of subjects


and count

Zip function to pair


the data elements of
lists
Returns list of tuples

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

Nested if, elif, and else


Control Flow Statements : for Loops

A for loop is used to iterate over a collection (like a list or tuple) or an iterator.

For loop iterator

The continue statement

The break statement


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
Control Flow Statements : Exception Handling

Handling Python errors or exceptions gracefully is an important part of building robust programs and algorithms.

Create function

Pass wrong argument type

Error

Exception handling with try–except block


Knowledge Check
Knowledge
Check
What is the data type of the object x = 3 * 7.5?
1

a. Int

b. Float

c. String

d. None of the above


Knowledge
Check
What is the data type of the object x = 3 * 7.5?
1

a. Int

b. Float

c. String

d. None of the above

The correct answer is b

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

The correct answer is b, c, d

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']

The correct answer is b

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

The correct answer is c

A set is used when a unique collection of values is desired.


Key Takeaways

You are now able to:

Explain Anaconda and Jupyter notebook installation

List the important data types supported by Python

Discuss data structures, such as lists, tuples, sets, and dicts

Explain slicing and accessing the four data structures

Discuss basic operators and functions

Outline the important control flow statements


Thank You

You might also like