Data
Science
Section1
Variables
Variables
• Variables are essentially a place to store different data. Almost always, its
easier to work with different data types in python by using variables.
• In python we don't have to explicitly declare a variable to create them, in
fact, the variable is created in the same statement where we assign a data
to it.
• Assignment of variables in Python happens using assignment operator (=).
Variables Naming Rules
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (name, Name and NAME are three
different variables)
• A variable name cannot be any of the Python keywords.
Variables Naming Rules (cont.)
Python Object Types
Python Object Types
• All data in Python is object.
• we can use type() method to returns the type of an object.
Numeric Types
and
Type Conversion
Numeric Types
➢There are 3 main types of Numeric Data Types:
▪ Integer
▪ Represent all integer values in Python such as 100, -5, 9000.
▪ Float
▪ Represent numerals that contain decimal points such as 5.5,
24.893, -3.69.
▪ Complex
▪ Used to represent complex numbers in python such as 3 + 7j
where the real component is 3 and the imaginary component
is 7.
Type Conversion
➢Type Conversion refers to the conversion of an object
from one data type to another.
▪ Example: from string to integer.
➢ Convert to Integer:
➢We can use int() function to convert something to an int, which
returns decimal.
Type Conversion (cont.)
➢ Convert to Float:
▪ We can use float() function to convert to
float.
➢ Convert an integer to a string.
▪ We can use str() function to convert integers
to string.
▪ We can use hex() function to convert to
hexadecimal string, and oct() function to
convert to octal string.
Type Conversion (cont.)
➢Convert two numbers to a complex data
type:
▪ We can use complex() function to convert
numbers(integers or float) to complex
number.
Python Strings
Strings
➢ String is an object type in python that is actually a subtype
of a border classification of object type called sequences.
Sequences are objects that contain components placed one
after the other. Where each object is given a numeric index
which identifies that component and its position within the
whole sequence. As python is a zero-index based language,
the first object of any sequence is always indexed 0.
➢ Defining Strings:
▪ Strings are defined using opening and
closing delimiters. These delimiters are
pairs of single or double quotes.
Strings (cont.)
➢To print single or double quote as a string
➢Any component within a sequence
type can be referenced by entering
an index within square brackets:
String operations and functions
➢We can retrieve individual elements of
a string using indexes.
➢We can check which position a
particular character is using index()
method.
➢We can slice a string and retrieve a
part of it, such as
var_name[start_index : end_index]
▪ Note that end_index is not included.
String operations and functions (cont.)
➢Retrieve a part of string from start to
index 7.
➢Retrieve a part of string from index 8
to the end.
➢Retrieve every character from 0 index
to 7 index with step 2.
➢We can use negative indexes to
retrieve characters from the end of a
string.
String operations and functions (cont.)
➢We can use upper() and lower()
functions to get an upper- and lower-
case version of the string.
➢We can split up a string into smaller
strings and place then into list using
split() function. The split process
based on the delimiter written in the
brackets.
▪ Ex1 : split using space “default”.
▪ Ex2 : split using “&”.
➢We can concatenate two strings
using the + symbol.
Escape Sequences
➢Escape sequences are special commands that tell python to
either:
▪ Suppress special meaning of some character or symbol in a string.
▪ Give an otherwise ordinary character, a special meaning.
➢First case Example:
▪We can solve this error by
using the backslash \
➢Second Case Example:
▪We can give the character t a
special meaning which used to
introduce tabbed space, and n
used to introduce a line break.
Python Lists and Tuples
Lists
➢List is a collection of objects, somewhat akin to array in many
other programming languages but more flexible.
➢Python lists are a type of sequence. Each object in a list is
assigned an index as is typical for all sequences. These objects
can be manipulated using its respective index.
➢ Defining Lists
➢Features of python lists:
✓ Lists elements can be manipulated using its index.
✓ Lists are mutable.
✓ Lists are dynamic.
✓ Lists can contain any objects.
✓ Lists can be nested.
List Operations
➢List elements can be accessed by index.
List Operations (cont.)
➢Concatenation and Replication
List Operations (cont.)
➢Modifying list values
List Operations (cont.)
➢Methods associated to lists
Tuples
➢Tuples are another collection of objects like lists, except for
two properties where they differ:
1. Tuples are immutable.
2. Tuples are defined using parenthesis instead of square brackets.
➢Defining Tuples
➢Slice process is performed like lists and strings.
Tuples (cont.)
➢Why use Tuples over List:
➢Program execution is faster when manipulating
Tuples.
➢Tuples are more efficient if you want to create an
ordered list of objects that doesn't need
modifications.
Python Dictionaries
Dictionaries
➢Dictionaries are another type of composite data types and are also
collection of objects much like tuples and lists. Like lists they are
also mutable, dynamic and can be nested.
➢Items in Dictionaries are paired using keys unlike in lists and
tuples where each item is assigned an index.
➢Defining a Dictionary:
➢A dictionary is defined within curly braces. Each key-value pair is separated
using comma. The key and values are themselves separated using a colon.
Dictionaries Operations and functions
➢Using Keys
▪ for accessing the value paired with that key.
▪ For changing the value associated to a particular key.
▪ Adding new key-value entry.
▪ Deleting the key and its associated value.
Dictionaries Operations and functions (cont.)
➢Using Functions
▪ get() retrieves the value associated to the key in parenthesis.
▪ Keys() returns the keys in the dictionary as objects in a list.
▪ values() return the values in the dictionary as objects in a list.
▪ popitem() method removes the last inserted item
Dictionaries Operations and functions (cont.)
➢Using Functions
▪ pop() method removes the item with the specified key name.
▪ clear() method empties the dictionary or list.
▪ update() method will update or adding the dictionary with the items from
the given argument
Operators
Arithmetic Operations
➢Note: the floor division // rounds the result down to the nearest whole
number
Logical Operations
➢Logical operators are used to combine conditional statements:
•
Comparison Operations
➢Comparison operators are used to compare two values:
Assignment Operations
• Assignment operators are used to assign values to variables:
•
Conditional Statements
Conditional Statements
➢We use if statements in python to write a condition.
➢Basic if statement :
➢The basic structure of an if statement is as shown:
Conditional Statements
➢Multiple conditions:
➢We use elif and else statements to specify multiple different conditions in
our coditional statements.
➢If only two conditions are needed, we don’t need to use an elif statement,
we can simply use an if statement followed by an else.
➢We can use multiple elif statement but must use only one else statement.