0% found this document useful (0 votes)
4 views25 pages

Python (2)

Uploaded by

satyamsharmabiz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views25 pages

Python (2)

Uploaded by

satyamsharmabiz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 25

Kirori Mal Collage

University Of Delhi

Mathematical Physics
Assignment

Submitted by-. Submitted to-


Vipin Kumar Maurya Dr. Kirtee poonia
Bsc Hons physics Department of Physics
2230165

1
2

What is python?
Python is an easy to learn, powerful programming
language. It has efficient high level data structures
and a simple but effective approach to object
oriented programming. It was created by Guido
van Rossum and released in 1991.

It is used for many purposes like:


Web development
Software development
Mathematics
System scripting

Advantages of using python

● Python works on different platforms like


windows,Mac,Linux, Raspberry Pi etc.
● It supports functional and structural
programing method as well as OOP.
3

● It can be used as scripting language or can


be compiled to byte code for building large
application.
● It can be easily integrated with c,c++,Java
and COM etc .

Python data type

Numeric type

There are three numeric types in python


1.Int
2.Float
3.Complex
Int
Int or integer, is a whole number, positive,
negative, without decimal of unlimited number.
Float
Float or floating point number is a number positive,
negative containing one or more decimals.

Complex
Complex number are written with 'j' as imaginary part.
4

Some basic numbers program:

Lists
# Creating a list
list1 = [1, 2, 3, 4, 5]

# Appending to a list
list1.append(6)
print(list1)

# Removing from a list


list1.remove(4)
print(list1)

# Sorting a list
list1.sort()
print(list1)

# Reversing a list
list1.reverse()
print(list1)

Tuples
5

# Creating a tuple
tuple1 = (1, 2, 3, 4, 5)

# Indexing a tuple
print(tuple1[0])

# Slicing a tuple
print(tuple1[1:4])

# Tuple Concatenation
tuple2 = (6, 7, 8)
print(tuple1 + tuple2)

Dictionaries
# Creating a dictionary
dict1 = {'name': 'John', 'age': 30, 'country':
'USA'}

# Accessing a value in a dictionary


print(dict1['name'])

# Adding a key-value pair to a dictionary


dict1['city'] = 'New York'
print(dict1)
6

# Removing a key-value pair from a


dictionary
del dict1['city']
print(dict1)

# Updating a value in a dictionary


dict1['age'] = 35
print(dict1)

# Looping through a dictionary


for key, value in dict1.items():
print(key, ":", value)

# calculate area and perimeter of circle:


7

String
String are character or word in python
surrounded by " ",' ' quotation marks.
8

Control flow statement:


If statement
The if statement is used to test a specific condition
If condition is true, a block of code (If block) will be
executed.
If-else
This statement is used to execute both true and
false part of given condition. If the condition id true
then if block is executed and if condition is false,
the else block is executed.

# program to find even or odd number


using if-else
9

While loop statement :


The while loop iteration of
code block is executed as
long as the given condition
is true,i.e.,conditional
expression is true.

Program for while loop:


10

Program to find volume and area of


regular shape with random dimension:
11

Program to determine range, maximum


height and time of flight of projectile
motion:
12

Quadratic equation:

Numpy :
Numpy is python library used for working with
array. It also has function for work in domain
of linear algebra, Fourier transform, and
matrices.
Numpy was created in 2005 by Travis
Oliphant. It is an open-source projact and you
can see it freely .

Use of Numpy :
In python we have lists that serve the purpose
of array, but they are slow to process. Numpy
13

aims to provide an array object that is up to


50x faster than traditional python lists.

The array object in Numpy is called ndarray, it


provide a lot of supporting functions that make
work with ndarray very easy. Arrays is very
frequently used in data science, where speed
and resources are very important .

Advantages of Numpy over lists:


Numpy arrays are stored at one continuous
place in memory unlike lists, so process can
access and manipulated them very efficiently.
This behaviour is called locality of reference in
computer science. This many reason why
Numpy is faster than lists .

Import Numpy
Once Numpy is installed, import it in your
application by adding the "import" keyword
e.g. "import Numpy".
Now Numpy is imported and ready to use.
14

Dimensions in arrays:
1. 1-D array :

2. 2-D array:

3. 3-D array:
15

Access array elements:


Array indexing is same as accessing elements
of an array. You can access an array element
by referring to its index number.
16

NumPy Getting Started:

import numpy as np

a = np.array([1, 2, 3, 4])
print(a)
Output:

[1 2 3 4]

NumPy Creating Arrays:

import numpy as np

a = np.array([1, 2, 3, 4])
b = np.array([[1, 2, 3], [4, 5, 6]])

print(a)
17

print(b)
Output:

[1 2 3 4]
[[1 2 3]
[4 5 6]]
NumPy Array Indexing:

import numpy as np

a = np.array([1, 2, 3, 4, 5, 6])

print(a[2])
print(a[-1])
Output:

3
6
18

NumPy Array Slicing:

import numpy as np

a = np.array([1, 2, 3, 4, 5, 6])

print(a[2:4])
print(a[:4])
print(a[2:])
Output:

[3 4]
[1 2 3 4]
[3 4 5 6]
NumPy Data Types:

import numpy as np

a = np.array([1, 2, 3, 4],
dtype='float32')
19

print(a.dtype)
Output:

float32
NumPy Copy vs View:

import numpy as np

a = np.array([1, 2, 3, 4, 5, 6])
b = a[2:4]

b[0] = 10

print(a)
Output:

[ 1 2 10 4 5 6]
NumPy Array Shape:
20

import numpy as np

a = np.array([1, 2, 3, 4, 5, 6])

print(a.shape)
Output:

(6,)
NumPy Array Reshape:

import numpy as np

a = np.array([1, 2, 3, 4, 5, 6])
b = a.reshape((2, 3))

print(b)
Output:

[[1 2 3]
[4 5 6]]
NumPy Array Iterating:
21

import numpy as np

a = np.array([1, 2, 3, 4, 5, 6])

for x in a:
print(x)
Output:

1
2
3
4
5
6

NumPy Array Split:

import numpy as np

a = np.array([1, 2, 3, 4, 5, 6])
22

# Split array into 3 parts


b = np.array_split(a, 3)
print("Array split into 3 parts: \n", b)
Output:

Array split into 3 parts:


[array([1, 2]), array([3, 4]), array([5,
6])]
NumPy Array Search:

import numpy as np

a = np.array([1, 2, 3, 4, 5, 6])

# Search for value 4 in the array


b = np.where(a==4)
print("Index of value 4 in the array:
\n", b)
Output:
23

Index of value 4 in the array:


(array([3]),)
NumPy Array Sort:

import numpy as np

a = np.array([3, 4, 1, 6, 2, 5])

# Sort the array in ascending order


b = np.sort(a)
print("Sorted array in ascending
order: \n", b)
Output:

Sorted array in ascending order:


[1 2 3 4 5 6]

Matplotlib:
Matplotlib is low level graph plotting library in
python that serves as a visualisation utility.
24

Matplotlib was created by John D. Hunter.

Most of matplotlib utilities lies under the pyplot


submodule and are usually imported under the plt
alias.
Import matplotlib.pyplot as plt

Approximate derivative of function:

Displacement and velocity- time graph of


simple harmonic oscillator:
25

Displacement-time & velocity -time


graph of undamped oscillator:

You might also like