0% found this document useful (0 votes)
195 views8 pages

Automata Lab Report 2

The document provides information on Python basics including syntax, loops, conditions, and input/output. It discusses using print(), input(), for loops, if conditions. It also covers the numpy and matplotlib.pyplot libraries for working with arrays, matrices, and data visualization. Regular expressions and the re module are also introduced. Code examples are provided to demonstrate key concepts like defining arrays, matrix multiplication, plotting graphs, and using regular expressions to search/extract patterns from text.

Uploaded by

Not
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)
195 views8 pages

Automata Lab Report 2

The document provides information on Python basics including syntax, loops, conditions, and input/output. It discusses using print(), input(), for loops, if conditions. It also covers the numpy and matplotlib.pyplot libraries for working with arrays, matrices, and data visualization. Regular expressions and the re module are also introduced. Code examples are provided to demonstrate key concepts like defining arrays, matrix multiplication, plotting graphs, and using regular expressions to search/extract patterns from text.

Uploaded by

Not
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/ 8

1|Page

TOPIC 1: Python Basic Programs


Title: Working with python basic syntax, loop, conditions and basic input output.
Objective: To gain basic knowledge about python.
Theory:

Python is an interpreted , high level, general purpose programming language, created by Guido
Van Rossum and first released in 1991, Python's design philosophy emphasizes code
readability with its notable use of significant whitespace. Its language constructs and object
oriented approach aim to help programmers write clear, logical code for small and large-scale
projects.
Python is dynamically typed and garbage collected. It supports multiple programming paradigms,
including procedural, object-oriented, and functional programming. Python is often described as a
"batteries included" language due to its comprehensive standard library.

Implementation:
I have implemented the algorithm according to above theory. The tools I used here:
 Python
 Environment : Anaconda

1.1 Source Code:


print("Welcome all to Python.automata")

Result: Output of the code


Welcome all to Python.automata

1.2 input():
Methodology:

In python input() function takes input from users and by evaluating expression it
automatically identifies whether the input is a string or a number.

Source Code:
1. in= input("Enter whatever you want: ")
2. print(in)
3. print(type(in))
4. n= int(in)
5. print(n)
6. print(type(n))
2|Page

Result: Output of the code


1. Enter whatever you want: 17
2. 17
3. <class 'str'>
4. 17
5. <class 'int'>

1.3 for loop:


Methodology:
Generally for loop is used to iterate over a sequence of data. In python the range() function
used for loop. This function defaults to zero as starting value, we can also specify the starting
value.

Source Code:
1. for i in range(0,10,1):
2. print(i)

Result: Output of the code


1
2
3
4
5
6
7
8
9

1.4 if condition:
Methodology:
In Python, it is mandatory to obey the indentation rules. For creating compound statements,
the indentation will be utmost necessary. We can understand the body of if condition in
python using indentation.

Source Code:
1. #for loop in python
2. for i in range(0,10,1):
3|Page

3. print(i)
4. if i%2==0:
5. print(i,"is even")

Result: Output of the code


1. 0
2. 0 is even
3. 1
4. 2
5. 2 is even
6. 3
7. 4
8. 4 is even
9. 5
10. 6
11. 6 is even
12. 7
13. 8
14. 8 is even
15. 9

TOPIC 2: numpy Module in Python


Title: A program to use and analyze numpy library in python.
Objective: To know about numpy library in Python.
Theory:
numpy is a Python package which stands for ‘Numerical Python’. It is the core library for
scientific computing, which contains a powerful n-dimensional array object, provide tools for
integrating C, C++ etc. It is also useful in linear algebra, random number capability etc.

Implementation:
I have implemented the algorithm according to above theory. The tools I used here:
 Python
 Environment : Anaconda

2.1 Array and dimension:


Methodology:
4|Page

As we open Jupyter notebook for writing code, we can see this interface and in
the In[]: section we have to insert our script. The NumPy arrays will be the keyway of
implementing the entire NumPy library.

Source Code:
1. import numpy as np
2. mat1 = np.array( [ (1,3,5,7),(2,4,6,8),(1,1,2,3) ])
3. print(mat1)
4. dimention = mat1.shape
5. print(dimention)
6. dimention_row = mat1.shape[0]
7. print(dimention_row)
8. dimention_row = mat1.shape[0]
9. print(dimention_row)

Result: Output of the code

[[1 3 5 7]
[2 4 6 8]
[1 1 2 3]]
(3, 4)
3

2.2 NumPy Multiplication Matrix:


Methodology:
As we open Jupyter notebook for writing code, we can see this interface and in
the In[]: section we have to insert our script. The NumPy arrays will be the keyway of
implementing the entire NumPy library.
Multiplication of two Matrices in Single line using Numpy in Python Matrix multiplication is
an operation that takes two matrices as input and produces single matrix by multiplying rows
of the first matrix to the column of the second matrix.In matrix multiplication make sure that
the number of rows of the first matrix should be equal to the number of columns of the
second matrix.

Source Code:
1. import numpy as np
2. mat1 = np.array( [ (1,3,5,7),(2,4,6,8),(1,1,2,3) ])
3. mat2 = np.array( [ (1,9,7),(4,5,6),(7,4,1),(2,5,8) ])
4. mat3 = np.dot(mat1,mat2)
5. print(mat3)
6. mat4 = np.zeros((4,3))
7. print(mat4)
5|Page

Result: Output of the code

[[ 62 79 86]
[ 76 102 108]
[ 25 37 39]]
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

TOPIC 3: matplotlib.pyplot Module in Python


Title: A program to use and analyze matplotlib.pyplot library in python.
Objective: To know about matplotlib.pyplot library in Python.
Theory:

Matplotlib is a Python 2D plotting library which produces publication quality figures in a


variety of hardcopy formats and interactive environments across platforms. Matplotlib tries
to make easy things easy and hard things possible. We can generate plots, histograms, power
spectra, bar charts, errorcharts, scatterplots, etc. with just a few lines of code.

Implementation:
I have implemented the algorithm according to above theory. The tools I used here:
 Python
 Environment : Anaconda

3.1 plot():
Source Code:
1. #matplot
2. %matplotlib inline
3. import numpy as np
4. import matplotlib.pyplot as plt
5. x = np.array([1,3,5,7])
6. y = np.array(x**2)
7. plt.plot(x,y)
8. plt.show()
6|Page

Result: Output of the code

3.2 plot():
Source Code:
1. #matplot
2. %matplotlib inline
3. import numpy as np
4. import matplotlib.pyplot as plt
5. '''''''
6. x = np.array([1,3,5,7])
7. #y = np.array(x**2)
8. y = np.array(np.exp(x))
9. '''
10. x = np.arange(0,10,0.4)
11. y = np.array(np.exp(x))
12. plt.plot(x,y, 'r--')
13. plt.show()
7|Page

Result: Output of the code

TOPIC 4: re Module in Python


Title: A program to use and re Module in python.
Objective: To know about re Module in Python.
Theory:
Python is a high level open source scripting language. Python’s built-in “re” module provides
excellent support for regular expressions, with a modern and complete regex flavor. The only
significant features missing from Python’s regex syntax are atomic grouping, possessive
quantifiers, and Unicode properties.
Implementation:

I have implemented the algorithm according to above theory. The tools I used here:
 Python
 Environment : Anaconda

4.1 re.findall():
Source Code:
1. import re
2. 2. data = '''''''
3. 3. Hello everyone, good morning, I am Faruk and I am from RUET.
4. 4. I meet Ronaldo last 17/Jan/2020 and got this phone number +8801725345621 , his man
age
5. r gaves me another phone number +8801955377859
6. '''
7. date_pattern = re.compile(r'\d+/[A-Za-z]+/\d{4}')
8. dates = date_pattern.findall(data)
9. print(dates)
10. phone_pattern = re.compile(r'\+[8][8][0][1][3|5|7|8|9]\d{8}')
11. phone_number = phone_pattern.findall(data)
12. print(phone_number)
8|Page

13. name_pattern = re.compile(r'[A-Z][a-z]+')


14. name = name_pattern.findall(data)
15. print(name)

Result: Output of the code


['17/Jan/2020']
['+8801725345621', '+8801955377859']
['Hello', 'Faruk', 'Ronaldo', 'Jan']

4.2 re.compile(pattern,repl,string):
Source Code:
1. import re
2. import numpy as np
3. numbers = np.array( [ '+8801838167090','+88018381dgg090','+876038167090','+8801988167
090'
4. ,'+9801838167090' ])
5. for i in numbers:
6. phone_pattern = re.compile(r'\+[8][8][0][1][3|5|7|8|9]\d{8}')
7. phone_number = phone_pattern.findall(i)
8. if phone_number == []:
9. print("invalid number")
10. else:
11. print(phone_number, "valid")

Result: Output of the code

['+8801838167090'] valid
invalid number
invalid number
['+8801988167090'] valid
invalid number

You might also like