SlideShare a Scribd company logo
1
Python Math Concepts
By: Rohan Karunaratne
Email: rohanhawaii@gmail.com
Find me on Linked In @ Rohan Karunaratne
 2016 Rohan Karunaratne All Rights Reserved
2
Math concepts in python can be confusing at first, but with a little help
from this book you can master the basic concepts needed to perform
mathematical functions. Start by opening python (the 2.7 and 3.5
versions work). You will first see the shell. Press File, then New File.
Program in this window. To do mathematical functions in python, you
will need a library called numpy. To import numpy, you could go online,
or just go to the terminal and write import numpy as np. At the
beginning of each program, write import numpy as np. You will not
need to do this for the first few programs, but as it gets more complex,
you will need to start activating and using this library. If you have not
programmed at all, you should start doing basic Programs located in this
book. The introductory paragraph is located below. For those of you
who are experienced in python, skip the paragraph and go right to
example 1. Here is the formatting of this book.
First sentence: Introduction to lesson
Italicized paragraph: code
Paragraph after code: code breakdown / explanation
Introductory paragraph:
Lets first begin by understanding what a print is. A print command is
issued when the programmer want to convey or print some message to
the user. This can be done by using the print command. The print
command is stated below:
print ("1")
The command above tells the computer to print the number "1".
Next let’s talk about conditionals. Conditionals give us the ability to
check conditions and change the behavior of the program accordingly.
This statement gives the computer the power to make decisions based on
what we want it to do. Some examples of conditionals are:
IF statements
3
IF ELSE statements
IF ELIF statements
IF ELIF ELSE statements
FOR statements
and so on......
Most of these statements are covered in this book, but some you will
have to search on the internet.
Let’s conclude with a briefing on loops. Loops let us run code for long
amounts of time without us having to constantly run the code over and
over again. Some examples are:
FOR I IN RANGE statements
IF TRUE statements
WHILE statements
WHILE NOT statements
WHILE TRUE statements
Let’s start with a basic example on how to add two numbers.
Ex 1: Input and adding 2 numbers
number1 = input ("Give me a number to add:")
number2 = input ("Give me a second number to add:")
total = number1 + number2
print(total)
So in the first line, you assign the variable "number1" a number. This
number comes from the input which we ask "Give me a number to add:"
4
Let’s say the user puts in 2 for the first number. Next the variable
"number2" is assigned to the value from the question "Give me a second
number to add:". Let’s say the user puts in 3 for the second question.
The third line defined a new variable called "total". The total is the sum
of number1 plus number2. So the computer would do the calculation 2
plus 3 which is 5. Finally, we ask the computer to print the "total". If
you want the input to be only numbers, include int before the variable
input. Notice how we don’t use quotation marks to print variables. This
is because we don’t want to ask or print any variable, we simply want to
print the variable.
You can repeat these steps for subtraction, multiplication, and division
just by replacing the "+" in example one with the desired operator.
The next concept we will work is comparing numbers. You can compare
numbers by using the "<" and ">" operators. This example will also
show how to use if statements.
Ex 2: Comparing 2 numbers
number1 = input("Give me a number:")
number2 = input("Give me another number:")
if number1 > number2:
print(number1, " is greater than “, number2)
if number1 < number2:
print(number2, " is greater than ", number2)
if number1 == number2:
print(number1 " is equal to " number2)
5
In the first two lines of code, we again identify what number1 and
number2 are. Then, in the first "if" statement, we say that if the first
inputted number is greater than the second inputted number, we
basically print say that in words. In the second "if" statement, we say
that if the first inputted number is less than the second inputted number,
print it we print it in words. Then in the last "if" statement, we say that if
number1 is equal to number2, print it in words. Remember to put double
== signs. This means "equal to". To create a "not equal to" statement,
we use !=.
You can make multiple "if" statements if you want to sort a group of
numbers. Also note that there is a semicolon after the "if" statements.
NEVER forget it in python or you will get many syntax error and they
will drive you nuts. Usually in python, during a syntax error there will
be a red highlighted area. This is usually where the syntax error is
located.
Next I will show you how to compute the average of 5 numbers.
Ex 3: Computing average of 5 numbers
number1 = input ("Give me a number:")
number2 = ("Give me a second number:")
number 3 = ("Give me a third number:")
number4 = ("Give me a fourth number:")
number 5 = ("Give me a fifth number:")
total1 = number1 + number2 + number3 + number4 + number5
total2 = total1 / 5
print("The average of the 5 numbers you inputted is ", total2)
The first five lines of code are asking for the 5 input numbers. You
should be pretty familiar with these commands. For more clarification,
look back at examples 1 and 2. For the variable total1, you add the 5
6
input numbers. Then total2 is total1 divided by 5. If you know in math,
when averaging, you add the numbers before dividing them. This is why
we did the adding command before the dividing command. Then we
printed total2 or the average.
Now, I will be showing you how to count numbers in python.
Ex 4: How to count by 1's, 5's and 10's in python and introduction to the
for i in range loop
print("Counting by 1's:")
for i in range(0,11,1):
print(i, end=" ")
print("Counting by 5's:")
for i in range(0, 100, 5):
print(i, end=" ")
print("Counting by 10's:")
for i in range(0, 100, 10):
print(i, end=" ")
So, if I explain the first loop you will pretty much get the rest. So we say
that we are counting by 1's in the print command. Now it gets a little
tricky. For i in range means for every integer in the range...... There are
three numbers after that. The first number is where the range starts, the
second is where the range ends and the last is what increments are you
counting at. So in loop two, you start at zero, end at 100, and count by
5's. The third line is where we print the counting and at the end, we add
a space. So just by fiddling around with three numbers, you can create
different counting sequences. I would like to point out something in the
first loop that also applies to the other loops. If you try to run the first
loop, the notice how it stops at 10 but the loop says 11. This is because
the for i in range loop is not a greater than or equal to loop. I mean that
the loop is looking through the numbers like 9 is in the loop 10 is in the
7
loop, but it doesn’t count 11 because it is not less than 11. Even in the
second loop, It counts to 95, but not 100, and even in the third loop, it
counts till 90, then stops. So if you want to count in increments of 3 and
end at 99, you need to go to 102 to get the number 99.
I will now explain how to add values to a variable.
Ex 5: How to add values
Count = 1
Count +=1
So in line 1, we define what value count has (1). In python, we don't say
Count = +1, we say Count +=1. If you wanted to add 5 to Count, you
would write Count+=5.
Ex 6: How to subtract values
Count = 5
Count -= 3
So same with example five, you write count -=3, not count = -3
Now let’s get into some trigonometry with Sin., Cos., and Tan. The
formulas for Sin, Cos, and Tan are below.
Sin = opposite/hypotenuse
Cos = adjacent/hypotenuse
Tan = opposite/adjacent
y = math.sin(theta)
y = math.cos(theta)
y = math.tan(theta)
8
Theta is another word for angle and by manipulating theta, you can
create different types of waves and designs. These designs are called
Lissajou patterns. Let’s go to this next example.
Ex 7: Manipulating theta
for i in range(-400 , 400, 1):
theta = (i * 0.01)
y = math.sin(theta * 15.7)
# theta * 3
#theta * 2
#theta + 3.14
#theta + 1.57
#theta * 6
#theta * 12
#theta (3.14 * 5)
#((theta * 9) + 0.785)
#theta * 2, theta * 3
#(theta * 3) + (3.14/4)
#theta * 15.7
#theta * 5
Again we use a for i in range loop and we start at -400 and count by
increments of 1 to get to 400. Then we assign theta to and integer * 0.01.
Then you have the math.sin(x) function with theta in replace for "x".
After that, you see 12 different manipulation of theta that begin with #.
Open python and use the code above. First try sin(theta), then try
cos(theta). Later, use the different examples and change theta and you
will see the different Lissajou patterns. Try creating your own theta
manipulations.
9
Now I will explain about radians in python. A radian is a different way
of measuring degrees. There are 2 radians in a circle. The next code I
will show you can help you use radians to create sin and cos waves, just
by manipulating radians.
Ex 8: Radians
for angle in range(360):
y = math.sin(math.radians(angle))
turtle.goto(angle, y * 80)
turtle.goto(0,0)
So we begin with a for angle in range(360) loop where we basically find
the integer in 360 degrees. Then we set the math.sin function to a
math.radians function. We then tell the turtle to go to the angle of y
times 80 and once that is done, go back to 0,0.
10
Thank You for reading this book and I hope that now you understand
math concepts in Python. Be sure to check out my website
(programinator.weebly.com) for more coding activities. I would like to
thank my friend Martin Liu for helping edit this book. And Keep Calm
and Code on!
 2016 Rohan Karunaratne All Rights Reserved

More Related Content

What's hot

Lab7: More Arrays, Strings, Vectors, and Pointers
Lab7: More Arrays, Strings, Vectors, and PointersLab7: More Arrays, Strings, Vectors, and Pointers
Lab7: More Arrays, Strings, Vectors, and Pointersenidcruz
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and commentsNilimesh Halder
 
Coding in Disaster Relief - Worksheet (Advanced)
Coding in Disaster Relief - Worksheet (Advanced)Coding in Disaster Relief - Worksheet (Advanced)
Coding in Disaster Relief - Worksheet (Advanced)Charling Li
 
Python for Machine Learning
Python for Machine LearningPython for Machine Learning
Python for Machine LearningStudent
 

What's hot (15)

L06
L06L06
L06
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
 
Maxbox starter
Maxbox starterMaxbox starter
Maxbox starter
 
Introduction to Python
Introduction to Python  Introduction to Python
Introduction to Python
 
Python for Beginners(v2)
Python for Beginners(v2)Python for Beginners(v2)
Python for Beginners(v2)
 
Lab7: More Arrays, Strings, Vectors, and Pointers
Lab7: More Arrays, Strings, Vectors, and PointersLab7: More Arrays, Strings, Vectors, and Pointers
Lab7: More Arrays, Strings, Vectors, and Pointers
 
Ma3696 lecture 4
Ma3696 lecture 4Ma3696 lecture 4
Ma3696 lecture 4
 
Control structures pyhton
Control structures  pyhtonControl structures  pyhton
Control structures pyhton
 
C# Strings
C# StringsC# Strings
C# Strings
 
Python ppt
Python pptPython ppt
Python ppt
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and comments
 
Coding in Disaster Relief - Worksheet (Advanced)
Coding in Disaster Relief - Worksheet (Advanced)Coding in Disaster Relief - Worksheet (Advanced)
Coding in Disaster Relief - Worksheet (Advanced)
 
General Talk on Pointers
General Talk on PointersGeneral Talk on Pointers
General Talk on Pointers
 
Python for Machine Learning
Python for Machine LearningPython for Machine Learning
Python for Machine Learning
 

Similar to Python Math Concepts Book

ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxpriestmanmable
 
Python if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_StatementPython if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_StatementAbhishekGupta692777
 
Course project solutions 2018
Course project solutions 2018Course project solutions 2018
Course project solutions 2018Robert Geofroy
 
This is all about control flow in python intruducing the Break and Continue.pptx
This is all about control flow in python intruducing the Break and Continue.pptxThis is all about control flow in python intruducing the Break and Continue.pptx
This is all about control flow in python intruducing the Break and Continue.pptxelezearrepil1
 
Tenth class state syllabus-text book-em-ap-ts-mathematics
Tenth class state syllabus-text book-em-ap-ts-mathematicsTenth class state syllabus-text book-em-ap-ts-mathematics
Tenth class state syllabus-text book-em-ap-ts-mathematicsNaukriTuts
 
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...Dadangsachir WANDA ir.mba
 
Notes2
Notes2Notes2
Notes2hccit
 
Programming basics
Programming basicsProgramming basics
Programming basicsillidari
 
Python-review1.pdf
Python-review1.pdfPython-review1.pdf
Python-review1.pdfpaijitk
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Techglyphs
 
1. control structures in the python.pptx
1. control structures in the python.pptx1. control structures in the python.pptx
1. control structures in the python.pptxDURAIMURUGANM2
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxmonicafrancis71118
 
Learn python
Learn pythonLearn python
Learn pythonmocninja
 
Spreadsheets for developers
Spreadsheets for developersSpreadsheets for developers
Spreadsheets for developersFelienne Hermans
 
23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)ssuser7f90ae
 

Similar to Python Math Concepts Book (20)

ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
 
Python_Module_2.pdf
Python_Module_2.pdfPython_Module_2.pdf
Python_Module_2.pdf
 
Python if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_StatementPython if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_Statement
 
Course project solutions 2018
Course project solutions 2018Course project solutions 2018
Course project solutions 2018
 
This is all about control flow in python intruducing the Break and Continue.pptx
This is all about control flow in python intruducing the Break and Continue.pptxThis is all about control flow in python intruducing the Break and Continue.pptx
This is all about control flow in python intruducing the Break and Continue.pptx
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Tenth class state syllabus-text book-em-ap-ts-mathematics
Tenth class state syllabus-text book-em-ap-ts-mathematicsTenth class state syllabus-text book-em-ap-ts-mathematics
Tenth class state syllabus-text book-em-ap-ts-mathematics
 
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
 
Chapter08.pptx
Chapter08.pptxChapter08.pptx
Chapter08.pptx
 
Notes2
Notes2Notes2
Notes2
 
Programming basics
Programming basicsProgramming basics
Programming basics
 
Python-review1.ppt
Python-review1.pptPython-review1.ppt
Python-review1.ppt
 
Python-review1.pdf
Python-review1.pdfPython-review1.pdf
Python-review1.pdf
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1
 
1. control structures in the python.pptx
1. control structures in the python.pptx1. control structures in the python.pptx
1. control structures in the python.pptx
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
 
Es272 ch2
Es272 ch2Es272 ch2
Es272 ch2
 
Learn python
Learn pythonLearn python
Learn python
 
Spreadsheets for developers
Spreadsheets for developersSpreadsheets for developers
Spreadsheets for developers
 
23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)
 

Recently uploaded

Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfmbmh111980
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?XfilesPro
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfOrtus Solutions, Corp
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILNatan Silnitsky
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...informapgpstrackings
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAlluxio, Inc.
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessWSO2
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfMeon Technology
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns
 
iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockSkilrock Technologies
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesKrzysztofKkol1
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareinfo611746
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandIES VE
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTier1 app
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisNeo4j
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...Alluxio, Inc.
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowPeter Caitens
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAlluxio, Inc.
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion Clinic
 

Recently uploaded (20)

Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by Skilrock
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysis
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
 

Python Math Concepts Book

  • 1. 1 Python Math Concepts By: Rohan Karunaratne Email: rohanhawaii@gmail.com Find me on Linked In @ Rohan Karunaratne  2016 Rohan Karunaratne All Rights Reserved
  • 2. 2 Math concepts in python can be confusing at first, but with a little help from this book you can master the basic concepts needed to perform mathematical functions. Start by opening python (the 2.7 and 3.5 versions work). You will first see the shell. Press File, then New File. Program in this window. To do mathematical functions in python, you will need a library called numpy. To import numpy, you could go online, or just go to the terminal and write import numpy as np. At the beginning of each program, write import numpy as np. You will not need to do this for the first few programs, but as it gets more complex, you will need to start activating and using this library. If you have not programmed at all, you should start doing basic Programs located in this book. The introductory paragraph is located below. For those of you who are experienced in python, skip the paragraph and go right to example 1. Here is the formatting of this book. First sentence: Introduction to lesson Italicized paragraph: code Paragraph after code: code breakdown / explanation Introductory paragraph: Lets first begin by understanding what a print is. A print command is issued when the programmer want to convey or print some message to the user. This can be done by using the print command. The print command is stated below: print ("1") The command above tells the computer to print the number "1". Next let’s talk about conditionals. Conditionals give us the ability to check conditions and change the behavior of the program accordingly. This statement gives the computer the power to make decisions based on what we want it to do. Some examples of conditionals are: IF statements
  • 3. 3 IF ELSE statements IF ELIF statements IF ELIF ELSE statements FOR statements and so on...... Most of these statements are covered in this book, but some you will have to search on the internet. Let’s conclude with a briefing on loops. Loops let us run code for long amounts of time without us having to constantly run the code over and over again. Some examples are: FOR I IN RANGE statements IF TRUE statements WHILE statements WHILE NOT statements WHILE TRUE statements Let’s start with a basic example on how to add two numbers. Ex 1: Input and adding 2 numbers number1 = input ("Give me a number to add:") number2 = input ("Give me a second number to add:") total = number1 + number2 print(total) So in the first line, you assign the variable "number1" a number. This number comes from the input which we ask "Give me a number to add:"
  • 4. 4 Let’s say the user puts in 2 for the first number. Next the variable "number2" is assigned to the value from the question "Give me a second number to add:". Let’s say the user puts in 3 for the second question. The third line defined a new variable called "total". The total is the sum of number1 plus number2. So the computer would do the calculation 2 plus 3 which is 5. Finally, we ask the computer to print the "total". If you want the input to be only numbers, include int before the variable input. Notice how we don’t use quotation marks to print variables. This is because we don’t want to ask or print any variable, we simply want to print the variable. You can repeat these steps for subtraction, multiplication, and division just by replacing the "+" in example one with the desired operator. The next concept we will work is comparing numbers. You can compare numbers by using the "<" and ">" operators. This example will also show how to use if statements. Ex 2: Comparing 2 numbers number1 = input("Give me a number:") number2 = input("Give me another number:") if number1 > number2: print(number1, " is greater than “, number2) if number1 < number2: print(number2, " is greater than ", number2) if number1 == number2: print(number1 " is equal to " number2)
  • 5. 5 In the first two lines of code, we again identify what number1 and number2 are. Then, in the first "if" statement, we say that if the first inputted number is greater than the second inputted number, we basically print say that in words. In the second "if" statement, we say that if the first inputted number is less than the second inputted number, print it we print it in words. Then in the last "if" statement, we say that if number1 is equal to number2, print it in words. Remember to put double == signs. This means "equal to". To create a "not equal to" statement, we use !=. You can make multiple "if" statements if you want to sort a group of numbers. Also note that there is a semicolon after the "if" statements. NEVER forget it in python or you will get many syntax error and they will drive you nuts. Usually in python, during a syntax error there will be a red highlighted area. This is usually where the syntax error is located. Next I will show you how to compute the average of 5 numbers. Ex 3: Computing average of 5 numbers number1 = input ("Give me a number:") number2 = ("Give me a second number:") number 3 = ("Give me a third number:") number4 = ("Give me a fourth number:") number 5 = ("Give me a fifth number:") total1 = number1 + number2 + number3 + number4 + number5 total2 = total1 / 5 print("The average of the 5 numbers you inputted is ", total2) The first five lines of code are asking for the 5 input numbers. You should be pretty familiar with these commands. For more clarification, look back at examples 1 and 2. For the variable total1, you add the 5
  • 6. 6 input numbers. Then total2 is total1 divided by 5. If you know in math, when averaging, you add the numbers before dividing them. This is why we did the adding command before the dividing command. Then we printed total2 or the average. Now, I will be showing you how to count numbers in python. Ex 4: How to count by 1's, 5's and 10's in python and introduction to the for i in range loop print("Counting by 1's:") for i in range(0,11,1): print(i, end=" ") print("Counting by 5's:") for i in range(0, 100, 5): print(i, end=" ") print("Counting by 10's:") for i in range(0, 100, 10): print(i, end=" ") So, if I explain the first loop you will pretty much get the rest. So we say that we are counting by 1's in the print command. Now it gets a little tricky. For i in range means for every integer in the range...... There are three numbers after that. The first number is where the range starts, the second is where the range ends and the last is what increments are you counting at. So in loop two, you start at zero, end at 100, and count by 5's. The third line is where we print the counting and at the end, we add a space. So just by fiddling around with three numbers, you can create different counting sequences. I would like to point out something in the first loop that also applies to the other loops. If you try to run the first loop, the notice how it stops at 10 but the loop says 11. This is because the for i in range loop is not a greater than or equal to loop. I mean that the loop is looking through the numbers like 9 is in the loop 10 is in the
  • 7. 7 loop, but it doesn’t count 11 because it is not less than 11. Even in the second loop, It counts to 95, but not 100, and even in the third loop, it counts till 90, then stops. So if you want to count in increments of 3 and end at 99, you need to go to 102 to get the number 99. I will now explain how to add values to a variable. Ex 5: How to add values Count = 1 Count +=1 So in line 1, we define what value count has (1). In python, we don't say Count = +1, we say Count +=1. If you wanted to add 5 to Count, you would write Count+=5. Ex 6: How to subtract values Count = 5 Count -= 3 So same with example five, you write count -=3, not count = -3 Now let’s get into some trigonometry with Sin., Cos., and Tan. The formulas for Sin, Cos, and Tan are below. Sin = opposite/hypotenuse Cos = adjacent/hypotenuse Tan = opposite/adjacent y = math.sin(theta) y = math.cos(theta) y = math.tan(theta)
  • 8. 8 Theta is another word for angle and by manipulating theta, you can create different types of waves and designs. These designs are called Lissajou patterns. Let’s go to this next example. Ex 7: Manipulating theta for i in range(-400 , 400, 1): theta = (i * 0.01) y = math.sin(theta * 15.7) # theta * 3 #theta * 2 #theta + 3.14 #theta + 1.57 #theta * 6 #theta * 12 #theta (3.14 * 5) #((theta * 9) + 0.785) #theta * 2, theta * 3 #(theta * 3) + (3.14/4) #theta * 15.7 #theta * 5 Again we use a for i in range loop and we start at -400 and count by increments of 1 to get to 400. Then we assign theta to and integer * 0.01. Then you have the math.sin(x) function with theta in replace for "x". After that, you see 12 different manipulation of theta that begin with #. Open python and use the code above. First try sin(theta), then try cos(theta). Later, use the different examples and change theta and you will see the different Lissajou patterns. Try creating your own theta manipulations.
  • 9. 9 Now I will explain about radians in python. A radian is a different way of measuring degrees. There are 2 radians in a circle. The next code I will show you can help you use radians to create sin and cos waves, just by manipulating radians. Ex 8: Radians for angle in range(360): y = math.sin(math.radians(angle)) turtle.goto(angle, y * 80) turtle.goto(0,0) So we begin with a for angle in range(360) loop where we basically find the integer in 360 degrees. Then we set the math.sin function to a math.radians function. We then tell the turtle to go to the angle of y times 80 and once that is done, go back to 0,0.
  • 10. 10 Thank You for reading this book and I hope that now you understand math concepts in Python. Be sure to check out my website (programinator.weebly.com) for more coding activities. I would like to thank my friend Martin Liu for helping edit this book. And Keep Calm and Code on!  2016 Rohan Karunaratne All Rights Reserved