0% found this document useful (0 votes)
84 views86 pages

Python Basics for Data Science & AI

This document outlines a course on Python programming for data science, AI, and application development, covering foundational skills, data types, expressions, and variable naming conventions. It explains key Python concepts such as lists, tuples, dictionaries, and string operations, along with practical examples and exercises. The course aims to prepare learners for real-world projects in their chosen career paths in data-related fields.

Uploaded by

ennyzkid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views86 pages

Python Basics for Data Science & AI

This document outlines a course on Python programming for data science, AI, and application development, covering foundational skills, data types, expressions, and variable naming conventions. It explains key Python concepts such as lists, tuples, dictionaries, and string operations, along with practical examples and exercises. The course aims to prepare learners for real-world projects in their chosen career paths in data-related fields.

Uploaded by

ennyzkid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PYTHON FOR DATA SCIENCE, AI, AND DEVELOPMENT

Introduction

This course was designed to provide the building blocks for Python programming and data
collection for those choosing a career in Data Analysis, Data Science, Data Engineering, AI or
Application Development.

Initially conceived as a foundation course for Data Science and AI it has been refreshed several
times to keep pace with emerging career options. Additional content has been added which is
applicable to Data Science, Data Engineering, AI or Application Development.

After completing this course you will have learned foundational skills in Python programming
which you can then go on to apply in the Python Project course for your chosen career. The
Python Project courses involve real world scenarios where you are in charge of a final project as
a Data Scientist, a Data Engineer, or in AI and Application Development. By finishing this
course and your follow-on Python Project, you will gain the basic skills to continue the steps on
your chosen career path.

Data Types in Python

Python is a dynamically typed language, which means you don't need to explicitly declare the
data type of a variable. However, Python has built-in data types that represent the various types
of values you can work with. Here are some common data types in Python:

Numeric Types:

 int: Integer type, e.g., x = 5.


 float: Floating-point type, e.g., y = 3.14.
 complex: Complex number type, e.g., z = 2 + 3j.

1
Sequence Types:

 str: String type, e.g., text = "Hello, World!".


 list: List type, e.g., my_list = [1, 2, 3].
 tuple: Tuple type, e.g., my_tuple = (1, 2, 3).

Set Types:

 set: Unordered set type, e.g., my_set = {1, 2, 3}.

Mapping Type:

dict: Dictionary type, e.g., my_dict = {'key': 'value'}.

Boolean Type:

bool: Boolean type, either True or False.

None Type:

2
 NoneType: Represents the absence of a value, similar to null in other languages.

In Python, type casting or type conversion refers to the process of converting a variable from one
data type to another. Python provides several built-in functions for type casting. Here are some
commonly used type casting functions:

int(): Converts a variable to an integer.

float(): Converts a variable to a floating-point number.

str(): Converts a variable to a string.

list(): Converts a variable to a list.

tuple(): Converts a variable to a tuple.


3
set(): Converts a variable to a set.

bool(): Converts a variable to a boolean.

dict(): Converts a variable to a dictionary.

Expression and variable

In Python, expressions are combinations of values and operators that can be evaluated to produce
a result. Expressions can include literals, variables, operators, and function calls. Let's break
down some key elements:

Literals: Literal values are hard-coded constants in your code.

4
Variables: Variables are names given to memory locations where you store values. They are
used to reference and manipulate data in your program.

Rules for naming a variable

When naming variables in Python, it's important to follow certain rules to ensure clarity,
readability, and adherence to the language syntax. Here are the rules for naming variables in
Python:

Valid Characters:

Variable names can only contain letters (a-z, A-Z), digits (0-9), and underscores (_).

The first character of a variable name cannot be a digit.

Case Sensitivity:

Python is case-sensitive. This means that variables with different letter cases are treated as
distinct. For example, myVar and myvar are two different variables.

Reserved Words:

Avoid using Python keywords as variable names. Examples of keywords include if, else, for,
while, True, False, None, and others. Using these words as variable names may lead to confusion
and errors.

5
Convention for Style:

Follow the Python naming convention, known as PEP 8.

According to PEP 8:

Use lowercase letters for variable names.

Separate words in a variable name with underscores to improve readability (snake_case).

Use descriptive and meaningful names to convey the purpose of the variable.

Avoid Starting with Underscore:

While starting a variable name with an underscore is technically allowed, it is often reserved for
special cases in Python. Avoid using a single leading underscore in regular variable names.

Avoid Double Underscores at the Beginning and End:

Variable names with double underscores at the beginning and end (sometimes referred to as
"dunder" names) are typically reserved for special methods or attributes in Python classes.

Operators

Operators perform operations on variables and values. Python supports various operators,
including arithmetic, comparison, logical, and more.

6
Expressions: Expressions are combinations of literals, variables, and operators that can be
evaluated to produce a result.

In this example, x + y * 2 is an expression that involves addition and multiplication operators.

Arithmetic operation

Python provides several arithmetic operators for performing common mathematical operations.
Here are the main arithmetic operators in Python:

Addition (+):

Subtraction (-):

Multiplication (*):

7
Division (/):

In Python 3, the division operator (/) always returns a floating-point result, even if the division is
exact.

Floor Division (//):

The floor division operator returns the largest integer less than or equal to the division result.

Modulus or Remainder (%)

The modulus operator returns the remainder of the division.

Exponentiation (**):

The exponentiation operator raises the left operand to the power of the right operand.

8
These operators can be used in combination within expressions to perform more complex
calculations. For example:

It's important to be aware of the order of operations in expressions, and you can use parentheses
to specify the order if needed.

Function Calls: Function calls can also be part of expressions. Functions take input arguments,
perform some operation, and return a result.

Here, len() is a function that returns the length of a string.

String Operation

Strings in Python are sequences of characters, and Python provides several built-in functions and
operators for working with strings. A string can be enclose in single or double quote. It can
contain text, numbers or special characters. A string can be access using index. The first index of
the array is 0 and to access the array element you write the array name then followed by the
index number.

Note: Strings are immutable.

The index of a string can be numbered from right to left. Doing so will make the First element of
the string to the right to be -1 as shown below.

9
Here are some common string operations in Python:

Concatenation (+):

Repetition (*)

Repeating names with space between

Indexing and Slicing:

Strings can be indexed to access individual characters, and slicing can be used to extract
substrings.

10
String Stride

Length (len()):

String Methods:

Python provides a variety of string methods for manipulation, such as:

11
 lower(): Converts all characters to lowercase.
 upper(): Converts all characters to uppercase.
 strip(): Removes leading and trailing whitespaces.
 replace(old, new): Replaces occurrences of a substring with another substring.
 find(substring): Returns the index of the first occurrence of a substring (or -1 if not
found).
 count(substring): Returns the number of occurrences of a substring.
 Split(separator): Splits the string into a list of substrings based on the given separator.

Formatting Strings:

Python provides different ways to format strings, including the old % formatting, the [Link]()
method, and the more recent f-strings.

Example 1

name = "Alice"

age = 25

formatted_str = "My name is %s and I am %d years old." % (name, age)

Example 2

name = "Bob"

age = 30

formatted_str = "My name is {} and I am {} years old.".format(name, age)

Example 3

12
name = "Charlie"

age = 35

formatted_str = f"My name is {name} and I am {age} years old."

String escape sequence.

\ are meant to proceed escape sequences. Escape sequences are strings that are difficult to input
Example

\t represent a tab. To place a back slash in your string use a double backslash or r in the front of
the string.

String find method

13
List and tuples

lists and tuples are both data structures used to store ordered collections of items. They have
some similarities, but there are key differences between them. Here's an overview of lists and
tuples:

Lists:

 Mutable: Lists are mutable, meaning you can change their elements (add, remove, or
modify) after the list is created.

Syntax: Defined using square brackets [].

Example:

List Operations:

 Adding elements: my_list.append(4) or my_list.insert(2, "orange"). Note append only add


one element to the list no matter how many element is appended.
 Extend method is use to add more than one element to the list.
 Removing elements: my_list.remove("banana") or my_list.pop(1)
 Note we can also use the del method to remove element from the list example de;(A[0])
 Modifying elements: my_list[0] = "new_value"
 Also split method convert list within space to a new string example “Hard Work”.split().
[“hard”, “rock”]
 Split and delimitation. Example “A,B,C,D”.split(“,”) result [“A”, “B”, “C”, “D”]

Use Case: Use lists when you need a collection of items that may change during the lifetime of
the program, and you want to perform various operations on them.

Tuples:

Immutable: Tuples are immutable, meaning once they are created, you cannot change their
elements. However, you can create a new tuple with modified elements.

14
Syntax: Defined using parentheses ().

Example:

Operations:

 Accessing elements: value = my_tuple[2]


 Concatenating tuples: new_tuple = my_tuple + (4, 5, 6)

Use Case: Use tuples when you have a collection of items that should remain constant
throughout the program, and you don't need to modify them.

Common Operations (Applicable to Both Lists and Tuples):

Indexing and Slicing: Both lists and tuples support indexing and slicing to access elements.

Length (len()):

Iteration

Membership Test

15
Note: We can sort element of a tuple. Using the sort method.

Nesting tuples

Summary on list

About the Dataset

Imagine you received album recommendations from your friends and compiled all of the
recommandations into a table, with specific information about each album.

The table has one row for each movie and several columns:

Artist - Name of the artist

Album - Name of the album

Released_year - Year the album was released

Length_min_sec - Length of the album (hours,minutes,seconds)

16
Genre - Genre of the album

Music_recording_sales_millions - Music recording sales (millions in USD) on


SONG://DATABASE

Claimed_sales_millions - Album's claimed sales (millions in USD) on SONG://DATABASE

Released - Date on which the album was released

Soundtrack - Indicates if the album is the movie soundtrack (Y) or (N)

Rating_of_friends - Indicates the rating from your friends from 1 to 10

Lists

Indexing

We are going to take a look at lists in Python. A list is a sequenced collection of different objects
such as integers, strings, and even other lists as well. The address of each element within a list is
called an index. An index is used to access and refer to items within a list.

17
To create a list, type the list within square brackets [ ], with your content inside the parenthesis
and separated by commas.

18
List Content

Lists can contain strings, floats, and integers. We can nest other lists, and we can also nest tuples
and other data structures. The same indexing conventions apply for nesting:

List Operations

We can also perform slicing in lists. For example, if we want the last two elements, we use the
following command:

19
20
Copy and Clone List

When we set one variable B equal to A, both A and B are referencing the same list in memory:

Initially, the value of the first element in B is set as "hard rock". If we change the first element in
A to "banana", we get an unexpected side effect. As A and B are referencing the same list, if we
change list A, then list B also changes. If we check the first element of B we get "banana" instead
of "hard rock":

21
You can clone list A by using the following syntax:

22
Exercise

Create a list a_list, with the following elements 1, hello, [1,2,3] and True.

Find the value stored at index 1 of a_list.

Retrieve the elements stored at index 1, 2 and 3 of a_list.

Concatenate the following lists A = [1, 'a'] and B = [2, 1, 'd']:

Scenario : Shopping list

Task-1 Create an empty list

At first we need to create a empty list for storing the items to buy in Shopping list.

Task-2 Now store the number of items to the shopping_list

 Watch
 Laptop
 Shoes
 Pen
 Clothes

Task-3 Add a new item to the shopping_list

Seems like I missed one item "Football" to add in the shopping list.

Task-4 Print First item from the shopping_list

Let's check the first item that we need to buy.

Task-5 Print Last item from the shopping_list

Let's check the last time that we need to buy.

23
Task-6 Print the entire Shopping List

Task-7 Print the item that are important to buy from the Shopping List

Task-8 Change the item from the shopping_list

Instead of "Pen" I want to buy "Notebook" let's change the item stored in the list.

Task-9 Delete the item from the shopping_list that is not required

Let's delete items that are unimportant, such as; I don't want to buy Clothes, let's delete it.

Task-10 Print the shopping list

We are ready with our shopping list.

Task-10 Print the shopping list

Summary on tuples

Tuples

In Python, there are different data types: String, Integer, and Float. These data types can all be
contained in a tuple as follows:

Now, let us create your first tuple with string, integer and float.

24
Indexing

Each element of a tuple can be accessed via an index. The following table represents the
relationship between the index and the items in the tuple. Each element can be obtained by the
name of the tuple followed by a square bracket with the index number:

25
26
27
Evaluation

28
Access the element, with respect to index 3

Use slicing to obtain indexes 3, 4 and 5:

Find the first two elements of the tuple genres_tuple:

Generate a sorted List from the Tuple C_tuple=(-5, 1, -3):

Dictionary

A dictionary is a built-in data type that represents a collection of key-value pairs. Each key must
be unique within a dictionary, and it maps to a corresponding value. Dictionaries are also known
as associative arrays, hash maps, or hash tables in other programming languages.

Creating dictionary

# Creating an empty dictionary

my_dict = {}

# Dictionary with key-value pairs

person = {

"name": "John Doe",

"age": 30,

"city": "New York"

29
# Another way to create a dictionary using the dict() constructor

person = dict(name="John Doe", age=30, city="New York")

Accessing values

Modifying and adding element

Removing elements

Dictionary Methods:

keys(): Returns a list of all keys in the dictionary.

30
values(): Returns a list of all values in the dictionary.

items(): Returns a list of tuples, where each tuple contains a key-value pair.

Iterating Over a Dictionary:

Checking if a Key Exists:

31
Summary on Dictionary

What are Dictionaries?

A dictionary consists of keys and values. It is helpful to compare a dictionary to a list. Instead of
being indexed numerically like a list, dictionaries have keys. These keys are the keys that are
used to access values within a dictionary.

The best example of a dictionary can be accessing person's detais using the social security
number.

Here the social security number which is a unique number will be the key and the details of the
people will be the values associated with it.

32
Create a Dictionary and access the elements

An example of a Dictionary Dict: Here we are creating a dictionary named Dict with he
following details

Keys are key1, key2, key3, key4, key5.

Values are {1,2,[3,3,3],(4,4,4),5,(0,1):6} corresponding to the keys

33
In summary, like a list, a dictionary holds a sequence of elements. Each element is represented
by a key and its corresponding value. Dictionaries are created with two curly braces containing
keys and values separated by a colon. For every key, there can only be one single value,
however, multiple keys can hold the same value. Keys can only be strings, numbers, or tuples,
but values can be any data type.

It is helpful to visualize the dictionary as a table, as in the following image. The first column
represents the keys, the second column represents the values.

34
35
36
Evaluation

b) In the dictionary soundtrack_dic what are the values ?

You will need this dictionary for the following questions:

The Albums Back in Black, The Bodyguard and Thriller have the following music recording
sales in millions 50, 50 and 65 respectively:

a) Create a dictionary album_sales_dict where the keys are the album name and the sales in
millions are the values.

b) Use the dictionary to find the total sales of Thriller:

c) Find the names of the albums from the dictionary using the method keys():

d) Find the values of the recording sales from the dictionary using the method values:

Scenario:Inventory Store

The inventory store scenario project utilizes a dictionary-based approach to develop a robust
system for managing and tracking inventory in a retail store.

37
Note:- You will be working with two product details.

Task-1 Create an empty dictionary

First you need to create an empty dictionary, where you will be storing the product details.

Task-2 Store the first product details in variable

 Product Name= Mobile phone


 Product Quantity= 5
 Product price= 20000
 Product Release Year= 2020

Task-3 Add the details in inventory

Task-4 Store the second product details in a variable.

Product Name= "Laptop"

Product Quantity= 10

Product price = 50000

Product Release Year= 2023

Task-5 Add the item detail into the inventory.

Task-6 Display the Products present in the inventory

Use print statement for displaying the products

Task-7 Check if ProductNo1_releaseYear and ProductNo2_releaseYear is in the inventory

As in inventory Release year is not required, let's delete it.

Task-8 Delete release year of both the products from the inventory

Set

a set is a built-in data type that represents an unordered collection of unique elements. Sets are
similar to lists or tuples but differ in that they do not allow duplicate elements. Sets are

38
implemented using a hash table, which makes membership tests and adding/removing elements
very fast.

Creating set

Adding and removing element

# Adding elements

[Link]("grape")

[Link]({"pear", "kiwi"})

# fruits is now {"apple", "orange", "banana", "grape", "pear", "kiwi"}

# Removing elements

[Link]("banana")

# fruits is now {"apple", "orange", "grape", "pear", "kiwi"}

# Discarding an element (similar to remove, but does not raise an error if the element is not
present)

[Link]("banana") # No error is raised even if "banana" is not in the set

Set Operations:

Union (|): Combines two sets, returning a new set containing all unique elements from both sets.

39
Intersection (&): Returns a new set containing only the common elements between two sets.

Difference (-): Returns a new set containing elements present in the first set but not in the
second set.

Symmetric Difference (^): Returns a new set containing elements that are unique to each set.

Other Set Methods:

 clear(): Removes all elements from the set.


 copy(): Returns a shallow copy of the set.
 pop(): Removes and returns an arbitrary element from the set.

40
 len(): Returns the number of elements in the set.

Condition and branching

conditions and branching are used to control the flow of a program based on certain conditions.
The primary construct for conditional statements is the if statement, and you can extend it with
elif and else to handle multiple conditions.

if Statement:

The if statement is used to test a condition. If the condition is true, the indented block of code
following the if statement is executed. Otherwise, the block is skipped.

if-else Statement:

The if-else statement allows you to execute one block of code if the condition is true and another
block if the condition is false.

41
if-elif-else Statement:

The elif (short for "else if") allows you to check multiple conditions in sequence. The code
associated with the first true condition is executed, and subsequent conditions are ignored.

Logical Operators:

You can use logical operators (and, or, not) to combine multiple conditions.

42
Nested if Statements:

You can nest if statements inside each other to handle more complex conditions.

Ternary Conditional Expression:

In Python, you can use a ternary conditional expression for concise if-else statements.

Loops

loops are used to execute a block of code repeatedly. There are two main types of loops: for
loops and while loops.

for Loop:

43
A for loop is used to iterate over a sequence (such as a list, tuple, string, or range) and execute a
block of code for each element in the sequence.

Iterating over a list

Iterating over a range

Note: Range can take two argument example for I in range(10, 15). This will print out
result 10, 11, 12, 13, 14.

Using enumerate:

If you need both the index and the value, you can use the enumerate function.

while Loop:

A while loop is used to repeatedly execute a block of code as long as a specified condition is
true.

44
Copying list values to a new list

Squares=[“Orange”, “Orange”, “Purple”, “Orange”, “Red”]

NewList=[]

i=0

while squares[i] ==“Orange”

[Link](squares[i])

i=i+1

break and continue Statements:

The break statement is used to exit a loop prematurely.

Continue statement

The continue statement is used to skip the rest of the code inside a loop for the current iteration
and move to the next iteration.

45
else Clause in Loops:

Python allows an else clause to be associated with a for or while loop. The code in the else block
is executed when the loop condition becomes false.

Infinite Loops:

Be cautious with while loops to avoid infinite loops. Make sure the loop condition has a way to
become false.

Function

A function is a reusable block of code that performs a specific task. Functions are a fundamental
building block in programming, promoting code modularity, reusability, and maintainability. a
function is a reusable block of code that performs a specific task. Functions are a fundamental
building block in programming, promoting code modularity, reusability, and maintainability.

Function Definition:

46
In Python, you define a function using the def keyword, followed by the function name and a
pair of parentheses. The code block that belongs to the function is indented.

Function Invocation:

Once a function is defined, you can call or invoke it to execute the code inside the function.

Function Parameters:

Functions can accept parameters (inputs) to make them more versatile. Parameters are specified
within the parentheses during function definition.

Default Parameters:

You can provide default values for function parameters. If a value is not passed when calling the
function, the default value is used.

47
Return Statement:

Functions can return values using the return statement. The returned value can be used in the
calling code.

Multiple Return Values:

A function can return multiple values as a tuple.

48
Scope of Variables:

Variables defined inside a function have local scope and are not accessible outside the function.
Variables defined outside functions have global scope. Note local variable can have same name
as a global variable without conflict. Also a variable writing in the local scope can be converted
to a global variable by adding the keyword global before the variable name.

Lambda Functions:

Lambda functions, also known as anonymous functions, are concise functions defined using the
lambda keyword. They are often used for short-lived operations.

Docstrings:

It's good practice to include a docstring to describe the purpose and usage of a function. Note you
can use the help() function to display Docstring.

49
Default functions

 len()
 lum()
 sorted()
 reverse()

Function that dose nothing

def noWork():

pass

print(noWork())

Looping through a function

You can use loops within functions to perform repetitive tasks. Below are some examples of how
you can incorporate loops into functions in Python.

Example 1: Looping Through a Range in a Function

50
Example 2: Looping Through a List in a Function

Example 3: Returning a List of Squares

51
Example 4: Using a While Loop in a Function

Example 5: Nested Loop in a Function

Example 6: Factorial Calculation Using a Loop

52
Using * within a function

Exception handling

Exception handling in Python is a mechanism to deal with runtime errors, allowing you to handle
unexpected situations in a controlled manner. Python provides the try, except, else, and finally
blocks to implement exception handling.

Basic Exception Handling:

53
Handling Multiple Exceptions:

else Block:

The else block is executed if no exceptions occur in the try block.

finally Block:

The finally block is always executed, whether an exception occurs or not. It is often used for
cleanup operations.

54
Raising Exceptions:

You can use the raise statement to raise an exception explicitly.

Classes:

Class Definition: A class is a blueprint or template for creating objects. It defines a set of
attributes and methods that the objects created from the class will have. Classes are defined using
the class keyword.

55
Attributes:

Attributes are variables that store data. They represent the characteristics or properties of an
object.

In the example above, name and age are attributes of the Dog class.

Methods:

Methods are functions that operate on the attributes of an object. They represent the behavior of
the object. In the example above, bark is a method of the Dog class.

Objects:

Object Instantiation: An object is an instance of a class. You create objects based on the class
definition. Object instantiation is done by calling the class as if it were a function.

Accessing Attributes and Calling Methods:

You can access the attributes and call methods of an object using the dot notation
([Link] or [Link]()).

56
Constructor (__init__ Method):

The __init__ method is a special method called the constructor. It is automatically called when
an object is created from the class. It initializes the attributes of the object. The self parameter
refers to the newly created instance of the class.

Inheritance:

Inheritance is a mechanism where a new class inherits properties and behaviors from an existing
class. The new class is called a subclass or derived class, and the existing class is the superclass
or base class.

The Puppy class inherits from the Dog class and has an additional method play.

Encapsulation:

Encapsulation is the bundling of data and methods that operate on the data within a single unit
(class). It restricts direct access to some of an object's components, providing a way to control
the access to the data.

Example:

class Car:

def __init__(self, make, model, year):

[Link] = make

57
[Link] = model

[Link] = year

[Link] = 0

def accelerate(self):

[Link] += 5

print(f"The car is accelerating. Current speed: {[Link]} mph")

def brake(self):

if [Link] > 0:

[Link] -= 5

print(f"The car is braking. Current speed: {[Link]} mph")

else:

print("The car is already stopped.")

# Creating an object of the Car class

my_car = Car(make="Toyota", model="Camry", year=2022)

# Accessing attributes and calling methods

print(f"{my_car.year} {my_car.make} {my_car.model}")

my_car.accelerate()

my_car.brake()

This is a simple example of a Car class with attributes (make, model, year, speed) and methods
(accelerate, brake). The my_car object is created, and its attributes and methods are accessed.

Reading and Writing files with open

To create a folder using terminal

 Open your pycharm or any IDE you are using

58
 Locate and open your terminal
 Type mkdir newFiles on the terminal

To create a file in the newFiles folder using terminal

 If pycharm and terminal are open type


 Echo > [Link]
 Press control c to exit the shell

To open the [Link] on notepad

 Make sure that both pycharm and terminal are open.


 Type notepad [Link] on the terminal and press enter
 The file will open on notepad now type in what you want into the file

Reading and write file

You can read and write files using built-in functions provided by the python language. Here's a
basic overview of file operations:

Reading a File:

To read the contents of a file, you can use the open function to open the file and then use
methods like read, readline, or readlines to read its content.

Reading the Entire File:

Reading line by line

59
Writing to a File:

To write to a file, you can use the open function with the file mode set to 'w' (write). If the file
doesn't exist, it will be created. If it already exists, its content will be overwritten.

Writing Text to a File:

Writing lines to a file

Appending to a File:

To append content to an existing file, you can use the 'a' (append) mode.

60
Exception handling when opening and closing files.

Pandas

Pandas is a popular Python library for data manipulation and analysis. It provides data structures
for efficiently storing large datasets and tools for working with them. The two primary data
structures in pandas are:

 Series: A one-dimensional array that can hold any data type. It is similar to a column in a
spreadsheet or a column in a SQL table.
 DataFrame: A two-dimensional table with rows and columns. It is similar to a spreadsheet
or a SQL table.

Installing pandas:

You can install pandas using pip:

61
Importing pandas

Creating a series

Creating a DataFrame

Reading Data from a File:

62
pandas supports various file formats, such as CSV, Excel, SQL, and more. For example, reading
a CSV file:

What is a CSV file and how to create 1

A CSV (Comma-Separated Values) file is a plain text file format used to store tabular data, such
as a spreadsheet or a database. In a CSV file, each line of the file represents a row of data, and
the values in each row are separated by commas (or other delimiters, like semicolons or tabs).
Here's a simple example of what a CSV file might look like:

In this example, the first row typically contains headers, indicating the names of the columns. To
create a CSV file in Python, you can use the csv module, which provides functionality for both
reading from and writing to CSV files. eHere's an example of how you can create a CSV file and
write data to it using Python:

In this example: The [Link] object is used to write data to the CSV file. writerows is used to
write multiple rows at once. After running this code, you will find a file named
"[Link]" in your working directory containing the specified data.

63
Reading CSV file using pandas

Basic operation

Data Manipulation:

64
Handling missing data

Saving Data to a File

Note you can also us [Link][0,0] to access the first-row and first column in the dataframe df?

You can also determine unique values from a dataframe using the following
df[“column_name”].unique()

65
We can also decide to fetch records that are greater than a certain period base on a column
df1=df[df[“column_name]>=10]

The new created dataframe can be saved using the following df.to_csv(“new_song.csv)

Downloading and installing jupyter notebook

Pip install jupyter-server

Pip install jupyter

If you encounter error then install the dependence separately

pip install notebook

pip install jupyter-core

pip install jupyter-client

# Install ipywidgets

pip install ipywidgets

# Install qtconsole

pip install qtconsole

After this install jupyter again using pip install jupyter

Check jupyter version

Jupyter –version

Creating jupyter project

 Create a folder on your desktop or any where


 Click on the address bar and type cmd. Then enter this will open command prompt
directed to that folder.

How to lunch jupyter notebook

Type jupyter notebook on your command prompt

66
How to create a new jupyter notebook

 Click on new
 Select notebook
 Make sure python3 is selected then click select
 Now write your code and click on the run button
 To save click on file and select save notebook

Exit jupyter notebook

 Save all changes


 Click on Kernel and select stop all kernel
 Close all jupyter tab
 Check kernel shutdown by typing jupyter notebook list on command prompt
 Use shift enter after writing every block of code. This will debug and compile the code.
If there is no error, it will either output your result or move you to the next line.

Numpy

Numpy is a library for scientific computing. It has many useful functions. There are many other
advantages like speed and memory. Numpy is also the basis for pandas.

To use NumPy, you need to install NumPy before you can import and use it in your Python
script. NumPy is not included in the standard Python library, so you need to use a package
manager like pip to install it. You can install NumPy using the following command in your
terminal or command prompt:

One dimensional Numpy

A NumPy 1-dimensional array, often referred to as a "numpy array" or simply a "1D array," is a
fundamental data structure provided by the NumPy library in Python. It represents a one-
dimensional, homogeneous array of elements, all of the same data type. NumPy arrays are more

67
efficient for numerical operations compared to Python lists because they are implemented in C
and have a fixed size at creation.

How to create Numpy

Checking type of array

Basic array attribute (size, ndim, shape)

Note ndim and shape will be suitable in a high dimension not 1 dim

Indexing and slicing method. Using the 2 to change array element

68
Slicing

Basic operation concept using Euclidean vector

a. Vector addition

Adding constant to a numpy array if we add a scalar to the array, numpy will add it to every
element of the array this property is known as broadcasting

b. Vector subtraction

69
c. Array multiplication with scalar

d. Product of 2 numpy array

e. Dot product

70
Universal function (Sum, mean, max, min)

71
We can use numpy to create functions that map numpy to new numpy arrays

We can access the value of pie in numpy as follows. We can create the following numpy array in
radians. This array corresponds to the following vector. We can apply the function sin to the
array x and assign the values to the array y. This applies the sin function to each element in the
array, this corresponds to applying the sine function to each component of the vector. The result
is a new array y, where each value corresponds to a sine function being applied to each element
in the array x.

Linspace function

The linspace function is part of the NumPy library, and it is used to create evenly spaced values
over a specified range. The syntax for linspace is as follows:

[Link](start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

72
 start: The starting value of the sequence.
 stop: The end value of the sequence.
 num: The number of evenly spaced values to generate. (Default is 50)
 endpoint: If True, stop is the last value in the range. If False, the range doesn't include
stop. (Default is True)
 retstep: If True, return the step size between values. (Default is False)
 dtype: The data type of the output array. If not specified, the data type is inferred from the
input values.
 axis: The axis in the result along which the linspace samples are stored. The default is 0.

Example

Plotting mathematical function using linespace

To plot graph we need to first use pip and install a dependence called matplotlib as follow

We can use the function line space to generate 100 evenly spaced samples from the interval zero
to two pie. We can use the numpy function sin to map the array x to a new array y. We can
import the library pyplot as plt to help us plot the function. As we are using a Jupiter notebook,
we use the command matplotlib inline to display the plot. The following command plots a graph.
The first input corresponds to the values for the horizontal or x-axis. The second input
corresponds to the values for the vertical or y-axis.

73
Two dimensional numpy

We can create numpy arrays with more than one dimension. This section will focus only on 2D
arrays but you can use numpy to build arrays of much higher dimensions. Note he term "2D" in
the context of a 2-dimensional array does not imply a specific number of rows or columns
example 2 rows and 2 columns. Instead, it indicates that the data structure has two dimensions,
typically organized in rows and columns.

Creating a two dimensional array

74
Using various attribute on the array

Accessing element of 2 dim

Using slicing

75
Example 2

Addition

Follows the concept of matrix addition

Example

76
Multiplication by scalar

Example

77
Multiplication

Example

78
Multiplication

We can also perform matrix multiplication with Numpy arrays. Matrix multiplication is a little
more complex but let's provide a basic overview. Consider the matrix A where each row is a
different color. Also, consider the matrix B where each column is a different color. In linear
algebra, before we multiply matrix A by matrix B, we must make sure that the number of
columns in matrix A in this case three is equal to the number of rows in matrix B, in this case
three.

79
From matrix multiplication, to obtain the ith row and jth column of the new matrix, we take the
dot product of the ith row of a with the jth columns of B. For the first column, first row we take
the dot product of the first row of A with the first column of B as follows. The result is zero. For
the first row and the second column of the new matrix, we take the dot product of the first row of
the matrix A, but this time we use the second column of matrix B, the result is two. For the
second row and the first column of the new matrix, we take the dot product of the second row of
the matrix A. With the first column of matrix B, the result is zero. Finally, for the second row and
the second column of the new matrix, we take the dot product of the second row of the matrix A
with the second column of matrix B, the result is two. In numpy, we can define the numpy arrays
A and B.

Example

80
Simple API

In this video we will discuss Application Program Interfaces API for short. Specifically, we will
discuss: What is an API API Libraries REST API, including: Request and Response An Example
with PyCoinGecko An API lets two pieces of software talk to each other For example you have
your program, you have some data, you have other software components. You use the api to
communicate with the api via inputs and outputs. Just like a function, you don’t have to know
how the API works, but just its inputs and outputs. Pandas is actually a set of software
components, much of which are not even written in Python. You have some data. You have a set
of software components. We use the pandas api to process the data by communicating with the
other Software Components. Let’s clean up the diagram. When you create a dictionary, and then
create a pandas object with the Dataframe constructor, in API lingo, this is an “instance.” The
data in the dictionary is passed along to the pandas API. You then use the dataframe to
communicate with the API. When you call the method head, the dataframe communicates with
the API displaying the first few rows of the dataframe. When you call the method mean the API
will calculate the mean and return the values.

Play video starting at :1:32 and follow transcript1:32

REST APIs are another popular type of API; they allow you to communicate through the internet
allowing you to take advantage of resources like storage, access more data, artificial intelligent
algorithms, and much more. The RE stands for Representational, the S stands for State, the T

81
stand for Transfer. In rest API’s your program is called the client. The API communicates with a
web service you call through the internet. There is a set of rules regarding Communication, Input
or Request, and Output or Response. Here are some common terms. You or your code can be
thought of as a client. The web service is referred to as a resource. The client finds the service via
an endpoint. We will review this more in the next section. The client sends requests to the
resource and the the resource (web service) sends a response to the client. HTTP methods are a
way of transmitting data over the internet We tell the Rest API’s what to do by sending a request.
The request is usually communicated via an HTTP message. The HTTP message usually
contains a JSON file. This contains instructions for what operation we would like the service to
perform. This operation is transmitted to the webservice via the internet. The service performs
the operation. In the similar manner, the webservice returns a response via an HTTP message,
where the information is usually returned via a JSON file. This information is transmitted back to
the client. Crypto Currency data is excellent to be used in an API because it is being constantly
updated and it is vital to CryptoCurrency Trading We will use the Py-Coin-Gecko Python
Client/Wrapper for the Coin Gecko API, updated every minute by Coin-Gecko We use the
Wrapper/Client because it is easy to use so you can focus on the task of collecting data, we will
also introduce pandas time series functions for dealing with time series data Using Py-Coin-
Gecko to collect data is quite simple All we need is to install and import the library Create a
client object And finally use a function to request our data. In this function we are getting data on
bitcoin, in U.S. Dollars, for the past 30 days. In this case our response is a JSON expressed as a
python dictionary of nested lists including price, market cap, and total volumes which contain the
unix timestamp and the price at that time. We are only interested in price so that is what we will
select using the key price To make things simple, we can convert our nested list to a DataFrame,
with the columns time stamp and price its difficult to understand the column time stamp we Will
convert it to a more readable format using the pandas Function to_datetime

Play video starting at :4:28 and follow transcript4:28

Using the to datetime function, we create readable time data, the input is the time stamp column
unit of time is set to milliseconds We append the output to the new column date

Play video starting at :4:43 and follow transcript4:43

82
We. Would like to create a candle stick plot To get the data for the daily candlesticks we will
group by the date to find the minimum, maximum, first, and last price of each day Finally we
will use plotly to create the candlestick chart and plot it Now we can view the candlestick chart
by opening the html file and clicking trust HTML in the top left of the tab It should look
something like this

Example

To use pyCoinGecko you need to import it

Getting the data

Fetching historical market data for Bitcoin using the CoinGecko API and then create a
DataFrame with the timestamp and price information.

83
To make the time stamp readable we convert it to datetime

Candlestick plot

84
A candlestick plot, or candlestick chart, is a type of financial chart used to represent the price
movement of an asset, such as a stock, currency, or commodity, over a certain time period. It is
commonly used in technical analysis of financial markets. The chart consists of individual
"candlesticks" that visually represent price movements.

85
Anapralin

08140037618 Jabir Virus

86

You might also like