Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language.
Make use of the PPT to have a better understanding of Python.
1. Basic Math using Python
Dr. Shivakumar B. N.
Assistant Professor
Department of Mathematics
CMR Institute of Technology
Bengaluru
2.
3. History of Python
▪ It is a general-purpose interpreted, interactive,
object-oriented, and high-level programming
language.
▪ It was created by Guido van Rossum (Dutch
Programmer) during the period 1985- 1990.
▪ Python 3.9.2 is current version
Guido van Rossum
Inventor of Python
4. Interesting facts about Python Programming
1. Python was a hobby project
2. Why it was called Python [The language’s name isn’t about snakes, but
about the popular British comedy troupe Monty Python]
3. Flavors of Python
Python ships in various flavors:
• CPython- Written in C, most common implementation of Python
• Jython- Written in Java, compiles to bytecode
• IronPython- Implemented in C#, an extensibility layer to frameworks
written in .NET
• Brython- Browser Python, runs in the browser
• RubyPython- Bridge between Python and Ruby interpreters
• PyPy- Implemented in Python
• MicroPython- Runs on a microcontroller
5.
6. Scope of learning Python in the field of Mathematics:
▪ Data Analytics
▪ Big Data
▪ Data Mining
7. ▪ https://solarianprogrammer.com/2017/02/25/install-numpy-scipy-matplotlib-python-3 ( To install packages)
▪ windows/https://www.w3schools.com/python/default.asp
▪ https://www.tutorialspoint.com/python/python_environment.htm
▪ https://www.udemy.com/course/math-with-python/ (Cover picture)
▪ https://data-flair.training/blogs/python-career-opportunities/ (companies using python picture)
▪ https://geek-university.com/python/add-python-to-the-windows-path/ (To set path)
▪ https://www.programiz.com/python-programming/if-elif-else
12. Basic Python Learning
Exercise 1: To print a message using Python IDE
Syntax: print ( “ Your Message”)
Example code:
print (“Hello, I like Python coding”)# Displays the message
in next line
# Comments
13. Variables
13
▪ Variables are nothing but reserved memory locations to store values.
▪ We are declaring a variable means some memory space is being reserved.
▪ In Python there is no need of declaring variables explicitly, if we assign a value it , automatically gets
declared.
Example:
counter = 100 # An integer assignment
miles =1000.0 # A floating point
name =(“John”) # A string
print counter
print miles
print name
14. Rules to Name Variables
▪ Variable name must always start with a letter or underscore symbol i.e, _
▪ It may consist only letters, numbers or underscore but never special symbols like @, $,%,^,* etc..
▪ Each variable name is case sensitive
Good Practice : file file123 file_name _file
Bad Practice : file.name 12file #filename
16. Operators Precedence Rule
Operator
Symbol
Operator Name
( ) Parenthesis
** Exponentiation (raise to the power)
* / % Multiplication , Division and
Modulus(Remainder)
+ - Addition and Subtraction
<< >> Left to Right
17. Let a = 10 and b = 20
Python Arithmetic Operators
21. CONDITIONAL STATEMENTS
One-Way Decisions
Syntax:
if expression:
statement(s)
Program:
a = 33
b = 35
if b > a:
print("b is greater than a")
Indentation
Python relies on indentation (whitespace at
the beginning of a line) to define scope in the
code. Other programming languages often use
curly-brackets for this purpose.
23. Multi-way if
Syntax:
if expression1:
statement(s)
if expression2:
statement(s)
elif expression3:
statement(s)
elif expression4:
statement(s)
else:
statement(s)
else:
statement(s)
Program:
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
24. LOOPS AND ITERATION
Repeated Steps
Syntax:
While expression:
Body of while
Program:
count = 0
while (count < 3):
count = count + 1
print("Hello all")
25. For Loop
Syntax:
for val in sequence:
Body of for
Program:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Looking at in:
• The iteration variable "iterates through the sequence (ordered
set)
• The block (body) of code is executed once for each value in the
sequence
• The iteration variable moves through all the values in the sequence
26. The range() function
The range() function returns a sequence of numbers, starting from 0 by
default, and increments by 1 (by default), and ends at a specied number.
Program 1:
x = range(6)
for n in x:
print(n)
Program 2:
x = range(2,6)
for n in x:
print(n)
Program 3:
x = range(2,20,4)
for n in x:
print(n)
27. Quick Overview of Plotting in Python
https://matplotlib.org/
Use the command prompt to install the following:
✓ py -m pip install
✓ py -m pip install matplotlib
pip is a package management system used to install and manage software
packages written in Python.
28. import matplotlib.pyplot as plt
# line 1 points
x1 = [1,2,3]
y1 = [2,4,1]
# plotting the line 1 points
plt.plot(x1, y1, label = "line 1")
# line 2 points
x2 = [1,2,3]
y2 = [4,1,3]
# plotting the line 2 points
plt.plot(x2, y2, label = "line 2")
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
# giving a title to my graph
plt.title('Two lines on same graph!')
# show a legend on the plot
plt.legend()
# function to show the plot
plt.show()
Plotting two or more lines on same plot
29. import matplotlib.pyplot as plt
# x-coordinates of left sides of bars
left = [1, 2, 3, 4, 5]
# heights of bars
height = [10, 24, 36, 40, 5]
# labels for bars
tick_label = ['one', 'two', 'three', 'four', 'five']
# plotting a bar chart
plt.bar(left, height, tick_label = tick_label,
width = 0.8, color = ['red', 'green'])
# naming the x-axis
plt.xlabel('x - axis')
# naming the y-axis
plt.ylabel('y - axis')
# plot title
plt.title('My bar chart!')
# function to show the plot
plt.show()
Bar Chart
30. import matplotlib.pyplot as plt
# defining labels
activities = ['eat', 'sleep', 'work', 'play']
# portion covered by each label
slices = [3, 7, 8, 6]
# color for each label
colors = ['r', 'y', 'g', 'b']
# plotting the pie chart
plt.pie(slices, labels = activities, colors=colors,
startangle=90, shadow = True, explode = (0, 0, 0.1, 0),
radius = 1.2, autopct = '%1.1f%%')
# plotting legend
plt.legend()
# showing the plot
plt.show()
Pie-chart
31. # importing the required modules
import matplotlib.pyplot as plt
import numpy as np
# setting the x - coordinates
x = np.arange(0, 2*(np.pi), 0.1)
# setting the corresponding y - coordinates
y = np.sin(x)
# potting the points
plt.plot(x, y)
# function to show the plot
plt.show()
Plotting curves of given equation: 𝑺𝒊𝒏 𝒙