To execute code in a Jupyter Notebook, press shift and enter at the same time
Define a variable and equal it to a number, string, list, or dictionary
Number: x = 5
String: name = “Alice”
List: numbers = [1, 2, 3, 4, 5]
Dictionary: person = {“name”: “Bob”, “age”: 30}
Regular variables in Python are used to store single data points. A variable can hold data of any type, like integers, floating-point numbers, strings, or even complex objects.
List can store more than one element.
A dictionary in Python is an unordered collection of data in a key:value pair form. Dictionary stores more than one element and is able to make a link between elements.
data = input(“Enter something: “)
data = str(data) # Ensures that data is a string
print(data)
OR
print(type(“input”)) # Prints the type of the input
Put the data in quotation mark
Welcome = “Welcome to my exam”
Use the type function
print(type(“variable”)) # Prints the type of the variable
It is a fundamental control structure that enables a program to make decisions and execute different code branches based on whether a given condition is True or False.
Temperature = 25 # Assign a value to the variable
if Temperature > 24: # If statement to check if the number is greater than 24
print(“The weather is hot outside.”)
if is the first condition to check, and it triggers its block if the condition is True.
Use if, for one-condition problems.
Temperature = 25 # Assign a value to the variable
if Temperature > 24: # If statement to check if the number is greater than 24
print(“The weather is hot outside.”)
else does not have a condition; it catches anything that wasn’t caught by the preceding if and elif statements. It’s essentially the “default” action when all other conditions fail. Use else for two condition problems.
if Temperature > 24: # If statement to check if the number is greater than 24
print(“The weather is hot outside.”)
else:
print(“The weather is not hot outside”)
elif follows an if or another elif and provides additional conditions to check. It is useful when multiple, mutually exclusive conditions need to be checked sequentially. Use elif for more than two condition problems.
if Temperature > 24: # If statement to check if the number is greater than 24
print(“The weather is hot outside.”)
elif Temperature > 20:
print(“The weather is okay outside”)
elif Temperature < 15:
print(“The weather is not good outside”)
else:
print(“The weather is not hot outside”)
List: A list is an ordered sequence of elements. Each element in a list can be accessed by its position, or index, within the list. Indexes in lists are integers starting from 0 for the first element.
List: numbers = [1, 2, 3, 4, 5]
Dictionary: A dictionary is an unordered collection of data in a key-value pair format. Each element in a dictionary is stored as a key paired with a value. The keys must be unique within a single dictionary, and they are typically used to describe or identify the associated value. Elements in a dictionary would be link together.
Dictionary: person = {“name”: “Bob”, “age”: 30}
List: Ideal for ordered tasks where the arrangement of elements is significant, such as storing a list of numbers, processing items in a specific sequence, or iterating through elements in the order they were added.
Dictionary: Used when you need a logical association between key:value pairs of items. Dictionaries are faster at finding values than lists since they use a hashing mechanism to store and retrieve data. They are ideal for representing real-world data that involves mapping between unique identifiers and data (like a database).
Functionality: The and operator checks that all conditions specified are True. If all conditions are True, then the and expression itself returns True. If any one of the conditions is False, the and expression returns False.
age = 17 # Assign values to variables
is_citizen = True
if age >= 17 and is_citizen: # If statement using the ‘and’ operator to check both conditions
print(“You are eligible to vote.”)
else:
print(“You are not eligible to vote.”)
Functionality: The or operator checks if at least one of the conditions is True. The or expression returns True as soon as one of its conditions is True. It only returns False if all conditions are False.
if age >= 17 or is_citizen: # If statement using the ‘and’ operator to check both conditions
print(“You are eligible to vote.”)
else:
print(“You are not eligible to vote.”)
Seaborn and Matplotlib are two of the most popular libraries in Python for data visualization
Import seaborn: import seaborn as sns
Import Matplotlib: import matplotlib.pyplot as plt
To visualize our data and find a trend. They offer a wide array of plots and customization options, making it highly flexible for creating sophisticated and professional figures.
The For Loop function
Define a list of numbers
numbers = [1, 2, 3, 4, 5]
Loop through each number in the list
for number in numbers:
print(number)
Repetition: For loops are used to repeat a block of code multiple times. For example, if you want to perform the same operation on every element of a list, a for loop can automate and streamline this process.
Control Structure: For loops offer a way to include decision-making processes within the loop, using conditional statements like IF, to perform different actions depending on the item.
For loop can run a specific code many times without the need to run it for individuals.
PuLP is a popular open-source library in Python used for linear programming. It enables users to formulate mathematical optimization problems using Python expressions and solve them using various supported solvers.
What type of problems would be solved by PuLP?
PuLP is primarily used for linear optimization problems but can also handle integer and mixed-integer programming problems.
Import the PuLP Library>
specify the name of the problem and whether it is a maximization or minimization problem>
Define Decision Variables>
Define the Objective Function>
Add Constraints>
Solve the Problem>
Retrieve and Display the Results>
Name, lower bound, upper bound, type(integer, float)
x = LpVariable(“x”, lowBound=0, upBound=1000, cat=’continuous’)
y = LpVariable(“y”, lowBound=0, upBound=1000, cat=’integer’)
z = LpVariable(“z”, lowBound=0, upBound=1000, cat=’binary’)
Set a name for the model and define its type(max or min)
from pulp import LpMaximize, LpProblem, LpVariable
problem = LpProblem(“Maximize_Profit”, LpMaximize)
A = LpVariable(‘A’, lowBound=0, upBound=100, cat=’Continuous’)
B = LpVariable(‘B’, lowBound=0, upBound=100, cat=’Continuous’)
problem += 5 * A + 3 * B, “Total_Profit”
Optimal value of variables and objective functions.
Is it possible that an optimal value of a variable be zero?how?
Yes, it depends on the opjective function and coefficient of variables.
Feasibility refers to whether a given solution satisfies all the constraints of the optimization problem.
Optimality, on the other hand, refers to a solution that not only satisfies all the constraints of the optimization problem (i.e., it is feasible) but also maximizes or minimizes the objective function. An optimal solution is the best possible solution among all feasible solutions according to the criterion defined by the objective function
A DataFrame is a two-dimensional data structure with labeled axis (rows and columns). It is one of the most commonly used data structures in data science and analytics. DataFrame is abale to make a data set just like a real table with headers.
Pandas is a powerful library for data manipulation and analysis in Python. It provides data structures and functions designed to make working with structured data fast, easy, and expressive.
NumPy is a fundamental package for scientific computing with Python. It provides support for multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently.
Pandas relies heavily on NumPy arrays for its internal data representation and operations. When you create a Pandas DataFrame, the data is stored internally as one or more NumPy arrays