Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Python_1

This document is an introductory lecture on Python, covering its characteristics as an open-source, interpreted programming language used for scientific and engineering computations. It discusses installation options, basic arithmetic operations, variable usage, and the importance of Python modules such as NumPy and SciPy for mathematical functions. Additionally, it provides examples of Python code and different ways to import modules.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python_1

This document is an introductory lecture on Python, covering its characteristics as an open-source, interpreted programming language used for scientific and engineering computations. It discusses installation options, basic arithmetic operations, variable usage, and the importance of Python modules such as NumPy and SciPy for mathematical functions. Additionally, it provides examples of Python code and different ways to import modules.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Lecture 1: Introduction to Python

M. Asif Farooq

1 / 18
What is Python language?

Python is open source software, which means it is available for


free.
Python is an interpreted language, meaning that it can run code
without the need for a compiler.
Python is a programming language that is used for scientific and
engineering computations all around the world.
Python is used when it comes to data analysis, machine learning,
and AI.

2 / 18
Installing Python

Since Python is free so there are many softwares available that


runs Python.
Some of these softwares are Spyder, Pyscripter etc
You can also type command on Jupyter notebooks available
online.

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 [24]: (2+3j)*(-4 + 9j)


Out[24]: (-35+6j)

In [25]: (2+3j)/(-4+9j)
Out[25]: (0.1958762886597938-0.3092783505154639j)

In [26]: 2.5 - 3j**2


Out[26]: (11.5+0j)

In [27]: (2.5 - 3j)**2


Out[27]: (-2.75-15j)

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

Run above code by typing %run mytrip.py on console


Then type time, gallons, cost
You can change the number of digits by %precision 2
Type %precision returns IPython to its default state
%precision %e displays numbers in exponential format

11 / 18
Note About Printing

To return value of the variable on the screen use print


print(time)
print(gallons)
print(cost)

12 / 18
Python Module

Python consist of supplementary modules


NumPy It is the standard Python package for scientific computing
with Python
SciPy provides a wide spectrum of mathematical functions and
numerical routines for Python.
matplotlib It is the stadard Python package for making two and
three dimensional lots.
Pandas It is a Python package providing a powerful set of data
analysis tools.

13 / 18
Python Modules and Functions
In [36]: import numpy

In [37]: numpy.sin(0.5)
Out[37]: 0.479425538604203

In [38]: import math

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

code: Distance between in two points


i m p o r t numpy as np
x1 , y1 , z1 = 2 3 . 7 , −9.2 , −7.8
x2 , y2 , z2 = −3.5 , 4 . 8 , 8.1
d r = np . s q r t ( ( x2− x1 ) * * 2 + ( y2 − y1 ) * * 2 + ( z2− z1 ) * * 2 )

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

You might also like