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.
Size: 1.26 MB
Language: en
Added: Apr 21, 2022
Slides: 33 pages
Slide Content
Basic Math using Python
Dr. Shivakumar B. N.
Assistant Professor
Department of Mathematics
CMR Institute of Technology
Bengaluru
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
Scope of learning Python in the field of Mathematics:
▪Data Analytics
▪Big Data
▪Data Mining
▪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
Steps to install Python:
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
Variables
13
▪Variablesarenothingbutreservedmemorylocationstostorevalues.
▪Wearedeclaringavariablemeanssomememoryspaceisbeingreserved.
▪InPythonthereisnoneedofdeclaringvariablesexplicitly,ifweassignavalueit,automaticallygets
declared.
Example:
counter =100# An integer assignment
miles =1000.0# A floating point
name =(“John”)# A string
printcounter
printmiles
printname
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
Operators Precedence Rule
Operator
Symbol
Operator Name
( ) Parenthesis
** Exponentiation (raise to the power)
* / % Multiplication , Division and
Modulus(Remainder)
+ - Addition and Subtraction
<< >> Left to Right
Let a = 10 and b = 20
Python Arithmetic Operators
Python Comparison Operators
Python Assignment Operators
Operators Precedence Example
CONDITIONAL STATEMENTS
One-Way Decisions
Syntax:
if expression:
statement(s)
Program:
a = 33
b = 35
ifb > a:
print("b is greater than a")
Indentation
Pythonreliesonindentation(whitespaceat
thebeginningofaline)todefinescopeinthe
code.Otherprogramminglanguagesoftenuse
curly-bracketsforthispurpose.
Two-way Decisions
Syntax:
if expression:
statement(s)
Else or elif:
statement(s)
Program:
a =33
b =33
ifb > a:
print("b is greater than a")
elifa == b:
print("a and b are equal")
Multi-way if
Syntax:
if expression1:
statement(s)
if expression2:
statement(s)
elifexpression3:
statement(s)
elifexpression4:
statement(s)
else:
statement(s)
else:
statement(s)
Program:
a =200
b =33
ifb > a:
print("b is greater than a")
elifa == b:
print("a and b are equal")
else:
print("a is greater than b")
LOOPS AND ITERATION
Repeated Steps
Syntax:
While expression:
Body of while
Program:
count = 0
while(count < 3):
count = count + 1
print("Hello all")
For Loop
Syntax:
for valin sequence:
Body of for
Program:
fruits = ["apple", "banana", "cherry"]
forx infruits:
print(x)
Lookingatin:
•Theiterationvariable"iteratesthroughthesequence (ordered
set)
•Theblock(body)ofcodeisexecutedonceforeachvalueinthe
sequence
•Theiterationvariablemovesthroughallthevaluesinthesequence
The range() function
Therange()functionreturnsasequenceofnumbers,startingfrom0by
default,andincrementsby1(bydefault),andendsataspeciednumber.
Program 1:
x = range(6)
forn inx:
print(n)
Program 2:
x = range(2,6)
forn inx:
print(n)
Program 3:
x = range(2,20,4)
forn inx:
print(n)
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.
importmatplotlib.pyplotas 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
import matplotlib.pyplotas 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
importmatplotlib.pyplotas 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
# importing the required modules
import matplotlib.pyplotas plt
import numpyas 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: ????????????????????????
PYTHON FOR EVERYBODY
(https://www.python.org/)
For More Details:
•http://www.pythonlearn.com/
•https://www.py4e.com/lessons