Lesson 02 Python Environment Setup and Essentials

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 77

Data Analytics with Python

Python Environment Setup and Essentials


Learning Objectives

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

Install Anaconda and Jupyter notebook

List some of the important data types in Python

Use basic operators and functions

Apply data structures such as lists, tuples, sets, and dicts in a


Python program
Python Environment Setup
What Is Anaconda?

Anaconda is a free and open-source distribution of the Python and R programming languages for
scientific computing (data science, machine learning applications, large-scale data processing,
predictive analytics, etc.), that aims to simplify package management and deployment.

Wikipedia
Why Anaconda?

To use Python, it is recommended 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 and more 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: Python 2.7 and Python 3.7. However,
Python 3.7 is the most recent and the preferable version.
Installation of Anaconda Python 3.7 Distribution

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

Windows Mac OS Linux

Website URL:
https://www.anaconda.com/distribution

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 3.7 Distribution

Windows Mac OS Linux

Website URL:
https://www.anaconda.com/distribution/

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 3.7 Distribution

Windows Mac OS Linux

Website URL:
https://www.anaconda.com/distribution/

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 its advantages are:

Python language Content sharing and


support contribution

Big Data platform Built-in interactive


integration widgets
Getting Started with Jupyter Notebook

Import sys module

Print sys version


‘3.7.11’

Import platform library


View python version
‘3.7.11’

Comment line
( (

Test string

Test number operation


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 are the same.
Example: Variables and Assignment

Look at an example of how you can assign a value to a variable and print the variable 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 is not
equal to the value.

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

Garbage collected

7 7
8

Memory location Memory location


First Python Program

Objective: Write a simple python program to print a string of characters, an integer value, and a float value.

Access: To execute the practice, follow these steps:


• Go to the PRACTICE LABS tab on your LMS
• Click the START LAB button
• Click the LAUNCH LAB button to start the lab
Data Types in Python
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

With three double


( ) quotes
( ) Print string values
( )
Basic Data Types: Null and Boolean

Python also supports the Null 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 Types in Python

Objective: Write a python program to input basic data types and print their values and types.

Access: To execute the practice, follow these steps:


• Go to the PRACTICE LABS tab on your LMS
• Click the START LAB button
• Click the LAUNCH LAB button to start the lab
Basic Operators and Functions
Arithmetic Operators

These operations (operators) can be applied to all numeric types:

Operator Description Example

+, - Addition, Subtraction 10 + 3 = 13
40 - 14 = 26
*,% Multiplication, Modulo (gives remainder) 2*3=6
27 % 5 = 2
/ Division 10 / 3 = 3.3333333 (Python 3)
10 / 3 = 3 (Python 2)
// Truncation Division (also known as floor division) 10 // 3 = 3
The result of this division is the integral part of the result, 10.0 // 3 = 3.0
i.e., the fractional part is truncated, if there is any.
It works both for integers and floating-point numbers, but
there is a difference in the type of the results. If both the
dividend and the divisor are integers, the result will also be
an integer. If either the dividend or the divisor is a float, the
result will be the truncated result as a float.

** Power of number 2 ** 3 = 8(2 to power 3)


Assignment Operator

• ” = “ is used to assign a value to a variable.


Example
x = 20

• Python also allows assignment of multiple variables in a single line as follows:


a,b = 10, 20
• The expressions on the right-hand side are evaluated before any of the assignments take place.
• The right-hand side expressions are evaluated from left to right.

Example
a,b = 1,2
a,b = b, a+b
>>>a
2
>>>b
3
Comparison Operator

Comparison operators include: <, <=, >, >=, !=, ==

Example

>>> a = 20
>>> b = 30
>>>print(a>b)
False
Logical Operator

• and, or, and not (&&, ||, !) are logical operators.

Example

>>> a = 25
>>> print( a % 3 == 0 and a % 5 == 0)
False

• Consider A = a%3==0 and B = a%5==0. When logical operators are applied, they are evaluated as follows,
based on evaluation of expressions:

A B A and B A or B
T T T T
T F F T
F T F T
F F F F
Bitwise Operator

|, &, ^, and ~ (Bitwise Or, Bitwise And, Bitwise XOR, Bitwise Negation) are bitwise operators.

Example

a = 2 (010)
b = 3 (011)
a&b=2
a|b=3
a^b=1

A B A&B A|B A^B


1 1 1 1 0
1 0 0 1 1
0 1 0 1 1
0 0 0 0 0
Operators in Python

Objective: Write a program to insert three sides of a triangle and check whether it is an isosceles
triangle or not. Also calculate the area of the triangle.

Access: To execute the practice follow these steps:


• Go to the PRACTICE LABS tab on your LMS
• Click the START LAB button
• Click the LAUNCH LAB button to start the lab
Expressions and Variables
Variables

Variables are used to store data in a computer’s memory.

Example

>>> price= 20
>>> price= 30
>>>Print(price)
Output: 30

The type of a variable is the type of the value it refers to:

Example
>>> type (message)
Output: <type ‘str’>
>>> type(n)
Output: <type ‘int’>
Variable Names

Characteristics of a good variable name are as follows:

• A variable name can be arbitrarily long


• A variable name should be meaningful
• A variable name can contain both letters and numbers, but, it should not begin with a number
• A variable name can have underscore character

An illegal name to a variable will result in a syntax error:

Examples
>>> 76trombones = 'big parade'
SyntaxError: invalid syntax
>>> more@ = 1000000
SyntaxError: invalid syntax
>>> class = 'Advanced Theoretical Zymurgy'
SyntaxError: invalid syntax
Python Keywords

Python reserves 31 keywords for its use:

and del from not while

as elif global or with

assert else if pass yield

break except import print -

class exec in raise -

continue finally is return -

def for lambda try -


Expressions

An expression is a combination of values, variables, and operators. However, values and


variables can be considered as expressions, individually.

Following are legal expressions:

Examples
>>> 17
>>> x
>>> x + 17
String Operations
String in Python

A string is a sequence of characters. All strings in Python 3 are sequences of "pure" Unicode
characters; there is no specific encoding like UTF-8.

There are different ways to define strings in Python:

Examples
astring = "Hello world!“
astring2 = 'Hello world!’
astring3 = “’ 'A string in triple quotes can extend
over multiple lines like this one, and can contain
'single' and "double" quotes.'''
Access characters in Strings

A string in Python consists of a series or sequence of characters: letters, numbers, and special characters.
Strings can be subscripted or indexed. Like C, the first character of a string in Python has the index 0

Examples
astring = "Hello world“
print(astring[0])
#The last character of a string can be accessed like this:
print(astring[len(astring)-1]
Print(astring[-2])

Output:
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
H
H E L L O W O R L D
D
L 0 1 2 3 4 5 6 7 8 9 10
String functions: Concatenation, Repetition, and Indexing

Concatenation
Strings can be glued together (concatenated) with the + operator:
Example
Print(“Hello” + “World”)
Output: HelloWorld

Repetition
String can be repeated or repeatedly concatenated with the asterisk operator "*":
Example
Print("*-*" * 3)
Output: *-**-**-*

Indexing
String can be indexed using index() method.
Example
astring = "Hello world!“
print(astring.index("o"))
Output: 4
String functions: Slicing

Substrings can be created with the slice or slicing notation, that is, two indices in square brackets
separated by a colon:
Example
Print(“Python”[2:4])
Output: th

Size of a string can be calculated using len():


len("Python") will result in 6

Extended slice syntax can be used to create a substrings with skipping indices in between the string:

Example
astring = "Hello world!“
print(astring[3:7:2])
Output: l
String functions: Uppercase and Lowercase

String can be converted to uppercase and lowercase, respectively:

Example
astring = "Hello world!“
print(astring.upper())
print(astring.lower())
Output: HELLO WORLD!
hello world!
String functions: Startswith and Split

To determine whether the string starts with something or ends with something:

Example
astring = "Hello world!“
print(astring.startswith("Hello"))
print(astring.endswith("asdfasdfasdf"))
Output: True
False

To split the string into a bunch of strings grouped together in a list:


Example
astring = "Hello world!“
afewwords = astring.split(" ")
print(afewwords)
Output: True
False
Immutable Strings

Python strings cannot be changed. Trying to change an indexed position will raise an error:

Example
astring = "Some things are immutable!“
astring[-1] = “.”

Output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
Escape Sequences

The backslash (\) character is used to escape characters, that is, to "escape" the special
meaning which this character would otherwise have.

Escape Sequence Meaning Notes

\newline Ignored
\\ Backslash (\)
\’ Single quote (')
\" Double quote (")
\a ASCII Bell (BEL)
\b ASCII Backspace (BS)
\f ASCII Formfeed (FF)
\n ASCII Linefeed (LF)
\N{name} Character named name in the Unicode database (Unicode only)
\r ASCII Carriage Return (CR)
\t ASCII Horizontal Tab (TAB)
\uxxxx Character with 16-bit hex value xxxx (Unicode only)
\Uxxxxxxxx Character with 32-bit hex value xxxxxxxx (Unicode only)
\v ASCII Vertical Tab (VT)
\ooo Character with octal value ooo
\xhh Character with hex value hh
String Operations

Objective: Write a program to do the following:


• Create a string
• Access the second last character in the string
• Print third to seventh character in the string
• Print the characters between the third and second last character
• Update a character of the string
• Update the entire string
• Escape sequencing of the string
• Format the string: default order, positional formatting, keyword formatting, string alignment,
rounding off integers, formatting of integers, formatting of floats

Access: To execute the practice follow these steps:


• Go to the PRACTICE LABS tab on your LMS
• Click the START LAB button
• Click the LAUNCH LAB button to start the lab
Data Structures
Data Structure: Tuple

A tuple is a one-dimensional, immutably 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

The count starts with the first index


but stops before the second index

The count stops before the second


index for negative indices too
Create Different Types of Tuples

Objective: Write a program to do the following:


• Create an empty tuple
• Create a tuple without using round brackets
• Create a tuple of mixed numbers
• Create a tuple of mixed data types
• Create a tuple of tuples
• Create a tuple of size one
Access: To execute the practice follow these steps:
• Go to the PRACTICE LABS tab on your LMS
• Click the START LAB button
• Click the LAUNCH LAB button to start the lab
Operations on Tuples

Objective: Write a program to do the following:


• Access a tuple element via indexing, slicing, and reverse indexing
• Modify a tuple
• Delete a tuple
• Access elements from nested tuple
• Check if a specific element is present in a tuple
• Determine the length of a tuple
• Concatenate two tuples
• Compare elements of two tuples
Access: To execute the practice follow these steps:
• Go to the PRACTICE LABS tab on your LMS
• Click the START LAB button
• Click the LAUNCH LAB button to start the lab
Data Structure: List

A list is a one-dimensional, mutably 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 the elements in a list using indices.

New modified list

Access with positive index

Access with negative index


Data Structure: Slicing Lists

Just like tuples, you can slice the elements in a list using indices.

New modified list

The count starts with the first index


but stops before the second index

The count stops before the second


index for negative indices too
List and Its Operations

Objective: Write a program to do the following:


• Create various type of lists
• Add elements in a list
• Add tuples to the list
• Add list to a list
• Add element at a specific position
• Add multiple elements to the list at the end
• Access elements from the list
• Removing elements from the list
• Print elements of a range using slice operation
Access: To execute the practice follow these steps:
• Go to the PRACTICE LABS tab on your LMS
• Click the START LAB button
• Click the LAUNCH LAB button to start the lab
List and Its Operations

Objective: Write a program to do the following:


• Create a multidimensional list
• Access elements using negative indexing
• Access the third last element of a list
• Remove element at a specific location using the pop() method
• Print elements of a list from beginning to a predefined point
• Print elements in reverse using slice operation
Access: To execute the practice follow these steps:
• Go to the PRACTICE LABS tab on your LMS
• Click the START LAB button
• Click the LAUNCH LAB button to start the lab
Unassisted Practice: List and Its Operations

Accessing key-value pair using get() method

Accessing element using negative indexing

Accessing third last element

Removing the specific element from a list

Print elements of a list from beginning to a predefined point

Printing elements in reverse using slice operation


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
Dictionary and Its Operations

Objective: Write a program to do the following:


• Create a dictionary with mixed keys
• Add elements to the dictionary
• Add a set of values to a single key
• Access an element using a key
• Remove elements from a dictionary
• Delete a key from nested dictionary
Access: To execute the practice follow these steps:
• Go to the PRACTICE LABS tab on your LMS
• Click the START LAB button
• Click the LAUNCH LAB button to start the lab
Dictionary and Its Operations

Objective: Write a program to do the following:


• Create a nested dictionary
• Create three dictionaries, then create one dictionary that will contain the other three dictionary
• Access key-value pair using get() method
• Update existing key’s value
• Check if key exists
• Delete entire dictionary
• Make a copy of a dictionary using the copy() method
Access: To execute the practice follow these steps:
• Go to the PRACTICE LABS tab on your LMS
• Click the START LAB button
• Click the LAUNCH LAB button to start the lab
Unassisted Practice: Dictionary and Its Operations

A dictionary containing
three other dictionaries

Output
Unassisted Practice: Dictionary and Its Operations

Three separate dictionaries

One dictionary containing


the other three dictionaries

Output
Unassisted Practice: Dictionary and Its Operations

Accessing key-value pair using get() method

Access with key

Updating an existing key-value pair

Modify dictionary:
update
Checking if a key exists

Modify dictionary:
Making a copy of an existing dictionary delete

Deleting an existing dictionary


Key Takeaways

You are now able to:

Install Anaconda and Jupyter notebook

List some of the important data types in Python

Use basic operators and functions

Apply data structures such as lists, tuples, sets, and dicts in a


Python program
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.


Generate Report Cards for Students

Problem Statement:
You have been asked to generate report cards for the students of a class.
Write a program to insert the names and marks of five subjects of the
students and generate a report card with the following information:
name, marks, total, and percentage.
Instructions to Perform the Assignment:
Use dictionaries to insert names and marks of five subjects.
Thank You

You might also like