Python_1
Python_1
M. Asif Farooq
1 / 18
What is Python language?
2 / 18
Installing Python
3 / 18
Checking Curren Version in Spyder
1 import platform
2 platform.python_version()
4 / 18
Interactive Python as a Calculator
In [1]: 2*3
Out[1]: 6
In [2]: 6 + 21/3
Out[2]: 13.0
In [3]: (6 + 21)/3
Out[3]: 9.0
5 / 18
Binary Arithmetic Operations in Python
operation Symbol
addition +
subtraction -
multiplication *
division /
floor division //
remainder %
exponentiation **
Table: Binary Operators.
6 / 18
Types of Numbers
Integers
Floating Point
Complex Numbers
In [4]: 12*3
Out[4]: 36
In [5]: 4 + 5*6-(21*8)
Out[5]: -134
In [6]: 11/5
Out[6]: 2.2
In [7]: 11//5
Out[7]: 2
In [8]: 9734828*79372
Out[8]: 772672768016
7 / 18
Types of Numbers (Cont.)
In [9]: 12.*3
Out[9]: 36.0
In [10]: 5**0.5
Out[10]: 2.23606797749979
In [11]: 11./5
Out[11]: 2.2
In [12]: 11.//5
Out[12]: 2.0
In [13]: 11.%5
Out[13]: 1.0
In [22]: 6.022e23*300
Out[22]: 1.8066e+26
8 / 18
Types of Numbers (Cont.)
In [25]: (2+3j)/(-4+9j)
Out[25]: (0.1958762886597938-0.3092783505154639j)
9 / 18
Variables
In [1]: a = 23
In [2]: p, q = 83.4, 2**0.5
In [3]: a = a + 1
In [4]: a
Out[4]: 24
In [5]: c, d = 4, 7.92
In [6]: c+= 2
In [7]: c
Out[7]: 6
In [8]: c*=3
In [9]: c
Out[9]: 18
In [10]: d/=-2
In [11]: d
Out[11]: -3.96
10 / 18
Script Files and Programs
Code: mytrip.py
d i s t a n c e = 400 #miles
mpg = 30 # c a r mileage
speed = 60 # average speed
c o s t p e r g a l l o n =2.85
t i m e = d i s t a n c e / speed
g a l l o n s = d i s t a n c e / mpg
cost = gallons * costpergallon
11 / 18
Note About Printing
12 / 18
Python Module
13 / 18
Python Modules and Functions
In [36]: import numpy
In [37]: numpy.sin(0.5)
Out[37]: 0.479425538604203
In [39]: math.sin(0.5)
Out[39]: 0.479425538604203
In [40]: numpy.sin(3+4j)
Out[40]: (3.853738037919377-27.016813258003932j)
In [41]: math.sin(3+4j)
Traceback (most recent call last):
File "C:1 5992870784069.py ”, line1, in < cellline : 1 > math.sin(3 + 4j)
TypeError: can’t convert complex to float
14 / 18
Python Modules and Functions
In [1]: import numpy as np
In [2]: np.sin(0.5)
Out[2]: 0.479425538604203
In [3]: np.sqrt(2)
Out[3]: 1.4142135623730951
In [4]: np.exp(2)
Out[4]: 7.38905609893065
In [5]: np.log(3)
Out[5]: 1.0986122886681098
In [6]: np.log10(2)
Out[6]: 0.3010299956639812
In [7]: np.degrees(1.47)
Out[7]: 84.22479588423101 15 / 18
Some NumPy Math Functions
sqrt(x) square root
exp(x) exponential of x
log(x) natural log of x
log10(x) base 10 log
degrees(x) converts x from radians to degrees
radians(x) converts x from degrees to radians
sin(x) sin of x (x in radians)
cos(x) cos of x (x in radians)
tan(x) tan of x (x in radians)
arcsin(x) inverse sin of x
arccos(x) arc cosine of x
arctang(x) arc tanent of x
fabs(x) absolute value of x
math.factorial(n) factorial of integer
round(x) rounds a float to nearest integer
floor(x) rounds a float down to nearest integer
ceil(x) rounds a float up to nearest integer 16 / 18
Scripting Example
17 / 18
Different Ways of Importing Modules
import math
import numpy as np
from numpy import log
from numpy import log, sin, cos
18 / 18