The Statistics and Calculus with Python Workshop
eBook - ePub

The Statistics and Calculus with Python Workshop

  1. 740 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

About this book

With examples and activities that help you achieve real results, applying calculus and statistical methods relevant to advanced data science has never been so easyKey Features• Discover how most programmers use the main Python libraries when performing statistics with Python• Use descriptive statistics and visualizations to answer business and scientific questions• Solve complicated calculus problems, such as arc length and solids of revolution using derivatives and integralsBook DescriptionAre you looking to start developing artificial intelligence applications? Do you need a refresher on key mathematical concepts? Full of engaging practical exercises, The Statistics and Calculus with Python Workshop will show you how to apply your understanding of advanced mathematics in the context of Python.The book begins by giving you a high-level overview of the libraries you'll use while performing statistics with Python. As you progress, you'll perform various mathematical tasks using the Python programming language, such as solving algebraic functions with Python starting with basic functions, and then working through transformations and solving equations. Later chapters in the book will cover statistics and calculus concepts and how to use them to solve problems and gain useful insights. Finally, you'll study differential equations with an emphasis on numerical methods and learn about algorithms that directly calculate values of functions.By the end of this book, you'll have learned how to apply essential statistics and calculus concepts to develop robust Python applications that solve business challenges.What you will learn• Get to grips with the fundamental mathematical functions in Python• Perform calculations on tabular datasets using pandas• Understand the differences between polynomials, rational functions, exponential functions, and trigonometric functions• Use algebra techniques for solving systems of equations• Solve real-world problems with probability• Solve optimization problems with derivatives and integralsWho this book is forIf you are a Python programmer who wants to develop intelligent solutions that solve challenging business problems, then this book is for you. To better grasp the concepts explained in this book, you must have a thorough understanding of advanced mathematical concepts, such as Markov chains, Euler's formula, and Runge-Kutta methods as the book only explains how these techniques and concepts can be implemented in Python.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access The Statistics and Calculus with Python Workshop by Peter Farrell,Alvaro Fuentes,Ajinkya Sudhir Kolhe,Quan Nguyen,Alexander Joseph Sarver,Marios Tsatsos in PDF and/or ePUB format, as well as other popular books in Computer Science & Computer Science General. We have over one million books available in our catalogue for you to explore.

1. Fundamentals of Python

Overview
This chapter reviews the basic Python data structures and tools that will be used in future discussions. These concepts will allow us to refresh our memory regarding Python's most fundamental and important features, while simultaneously preparing us for advanced topics in later chapters.
By the end of this chapter, you will be able to use control flow methods to design your Python programs and initialize common Python data structures, as well as manipulate their content. You will solidify your understanding of functions and recursion in Python algorithm design. You will also be able to facilitate debugging, testing, and version control for Python programs. Finally, in the activity at the end of this chapter, you will create a Sudoku solver.

Introduction

Python has enjoyed an unprecedented increase in popularity and usage in recent years, especially in mathematics, which is the main topic of this chapter. However, before we delve into the advanced topics in mathematics, we will need to solidify our understanding of the fundamentals of the language.
This chapter will offer a refresher on the general concepts of Python; the topics covered will allow you to be in the best position for later discussions in this book. Specifically, we will be reviewing elementary concepts in general programming such as conditionals and loops, as well as Python-specific data structures such as lists and dictionaries. We will also discuss functions and the algorithm design process, which is an important part in any medium or large Python project that includes mathematics-related programs. All of this will be done through hands-on exercises and activities.
By the end of this chapter, you will be well positioned to tackle more complex, interesting problems in later chapters of this book.

Control Flow Methods

Control flow is a general term that denotes any programming syntax that can redirect the execution of a program. Control flow methods in general are what allow programs to be dynamic in their execution and computation: depending on the current state of a program or its input, the execution of that program and thus its output will dynamically change.

if Statements

The most common form of control flow in any programming language is conditionals, or if statements. if statements are used to check for a specific condition about the current state of the program and, depending on the result (whether the condition is true or false), the program will execute different sets of instructions.
In Python, the syntax of an if statement is as follows:
if [condition to check]:
[instruction set to execute if condition is true]
Given the readability of Python, you can probably already guess how conditionals work: when the execution of a given program reaches a conditional and checks the condition in the if statement, if the condition is true, the indented set of instructions inside the if statement will be executed; otherwise, the program will simply skip those instructions and move on.
Within an if statement, it is possible for us to check for a composite condition, which is a combination of multiple individual conditions. For example, using the and keyword, the following if block is executed when both of its conditions are satisfied:
if [condition 1] and [condition 2]:
[instruction set]
Instead of doing this, we can use the or keyword in a composite condition, which will display positive (true) if either the condition to the left or to the right of the keyword is true. It is also possible to keep extending a composite condition with more than one and/or keyword to implement conditionals that are nested on multiple levels.
When a condition is not satisfied, we might want our program to execute a different set of instructions. To implement this logic, we can use elif and else statements, which should immediately follow an if statement. If the condition in the if statement is not met, our program will move on and evaluate the subsequent conditions in the elif statements; if none of the conditions are met, any code inside an else block will be executed. An if...elif...else block in Python is in the following form:
if [condition 1]:
[instruction set 1]
elif [condition 2]:
[instruction set 2]
...
elif [condition n]:
[instruction set n]
else:
[instruction set n + 1]
This control flow method is very valuable when there is a set of possibilities that our program needs to check for. Depending on which possibility is true at a given moment, the program should execute the corresponding instructions.

Exercise 1.01: Divisibility with Conditionals

In mathematics, the analysis of variables and their content is very common, and one of the most common analyses is the divisibility of an integer. In this exercise, we will use if statements to consider the divisibility of a given number by 5, 6, or 7.
Perform the following steps in order to achieve this:
  1. Create a new Jupyter notebook and declare a variable named x whose value is any integer, as shown in the following code:
    x = 130
  2. After that declaration, write an if statement to check whether x is divisible by 5 or not. The corresponding code block should print out a statement indicating whether the condition has been met:
    if x % 5 == 0:
    print('x is divisible by 5')
    Here, % is the modulo operator in Python; the var % n expression returns the remainder when we divide the var variable by the number, n.
  3. In the same code cell, write two elif statements to check whether x is divisible by 6 and 7, respectively. Appropriate print statements should be placed under their corresponding conditionals:
    elif x % 6 == 0:
    print('x is divisible by 6')
    elif x % 7 == 0:
    print('x is divisible by 7')
  4. Write the final else statement to print out a message stating that x is not divisible by either 5, 6, or 7 (in the same code cell):
    else:
    print('x is not divisible by 5, 6, or 7')
  5. Run the program with a different value assigned to x each time to test the conditional logic we have. The following output is an example of this with x assigned with the value 104832:
    x is divisible by 6
  6. Now, instead of printing out a message about the divisibility of x, we would like to write that message to a text file. Specifically, we want to create a file named output.txt that will contain the same message that we printed out previously.
    To do this, we can use the with keyword together with the open() function to interact with the text file. Note that the open() function takes in two arguments: the name of the file to write to, which is output.txt in our case, and w (for write), which specifies that we would like to write to file, as opposed to reading the content from a file:
    if x % 5 == 0:
    with open('output.txt', 'w') as f:
    f.write('x is divisible by 5')
    elif x % 6 == 0:
    with open('output.txt', 'w') as f:
    f.write('x is divisible by 6')
    elif x % 7 == 0:
    with open('output.txt', 'w') as f:
    f.write('x is divisible by 7')
    else:
    with open('output.txt', 'w') as f:
    f.write('x is not divisible by 5, 6, or 7')
  7. Check the message in the output text file for its correctness. If the x variable still holds the value of 104832, your text file should have the following contents:
    x is divisible by 6
In this exercise, we applied the usage ...

Table of contents

  1. The Statistics and Calculus with Python Workshop
  2. Preface
  3. 1. Fundamentals of Python
  4. 2. Python's Main Tools for Statistics
  5. 3. Python's Statistical Toolbox
  6. 4. Functions and Algebra with Python
  7. 5. More Mathematics with Python
  8. 6. Matrices and Markov Chains with Python
  9. 7. Doing Basic Statistics with Python
  10. 8. Foundational Probability Concepts and Their Applications
  11. 9. Intermediate Statistics with Python
  12. 10. Foundational Calculus with Python
  13. 11. More Calculus with Python
  14. 12. Intermediate Calculus with Python
  15. Appendix