Sei sulla pagina 1di 14

LAB 1

Objective: Installation of python

Step 1: Download the Python 3 Installer


Open a browser window and navigate to the Download page for Windows at python.org.
Underneath the heading at the top that says Python Releases for Windows, click on the link
for the Latest Python 3 Release - Python 3.x.x. (As of this writing, the latest is Python 3.6.5.)
Scroll to the bottom and select either Windows x86-64 executable installer for 64-bit
or Windows x86 executable installer for 32-bit. (See below.)

Step 2: Run the Installer


Once you have chosen and downloaded an installer, simply run it by double-clicking on the
downloaded file. A dialog should appear that looks something like this:

Then just click Install Now. That should be all there is to it. A few minutes later a working
Python 3 will be installed on the system.
LAB 2
Objective: Control structures and Functions.
1. If else
# Program checks if the number is positive or negative and displays an appropriate
message

num = 3
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")

Output: Positive or Zero

2. While
# Program to add natural numbers upto sum = 1+2+3+...+n

n = int(input("Enter n: "))
n = 10
sum = 0
i=1
while i <= n:
sum = sum + i
i = i+1
print("The sum is", sum)

Output: Enter n: 10
The sum is 55

3. For loop
# Program to find the sum of all numbers stored in a list

numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]


sum = 0
for val in numbers:
sum = sum+val
print("The sum is", sum)

Output: The sum is 48

4. Function
# Program to implement binary search

def binarySearch(alist, item):


first = 0
last = len(alist)-1
found = False
while first<=last and not found:
midpoint = (first + last)//2
if alist[midpoint] == item:
found = True
else:
if item < alist[midpoint]:
last = midpoint-1
else:
first = midpoint+1
return found
testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42,]
print(binarySearch(testlist, 3))
print(binarySearch(testlist, 13))

Output: False
True
LAB 3
Objective: Quick sort using recursion
#Program to implement Quick Sort

def quickSort(alist):
quickSortHelper(alist,0,len(alist)-1)

def quickSortHelper(alist,first,last):
if first<last:
splitpoint = partition(alist,first,last)
quickSortHelper(alist,first,splitpoint-1)
quickSortHelper(alist,splitpoint+1,last)

def partition(alist,first,last):
pivotvalue = alist[first]
leftmark = first+1
rightmark = last
done = False

while not done:


while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
leftmark = leftmark + 1

while alist[rightmark] >= pivotvalue and rightmark >= leftmark:


rightmark = rightmark -1

if rightmark < leftmark:


done = True
else:
temp = alist[leftmark]
alist[leftmark] = alist[rightmark]
alist[rightmark] = temp

temp = alist[first]
alist[first] = alist[rightmark]
alist[rightmark] = temp
return rightmark

alist = [54,26,93,17,77,31,44,55,20]
quickSort(alist)
print(alist)

Output: [17, 20, 26, 31, 44, 54, 55, 77, 93]
LAB 4
Objective: Control structures and Functions.
#Program to print roman values of the given decimal number.

class py_solution:
def int_to_Roman(self, num):
val = [
1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4,
1
]
syb = [
"M", "CM", "D", "CD",
"C", "XC", "L", "XL",
"X", "IX", "V", "IV",
"I"
]
roman_num = ''
i=0

while num > 0:


for _ in range(num // val[i]):
roman_num += syb[i]
num -= val[i]
i += 1
return roman_num

print(py_solution().int_to_Roman(1))
print(py_solution().int_to_Roman(4000))

Output: I
MMMM
LAB 5
Objective: Object Orientation
class Person:
def say_hi(self):
print('Hello, how are you?')

p = Person()
p.say_hi()
# The previous 2 lines can also be written as
# Person().say_hi()

Output: $ python oop_method.py


Hello, how are you?
LAB 6
Objective: GUI implementation
#!/usr/bin/python

import tkinter
top = tkinter.Tk()
# Code to add widgets will go here...
top.mainloop()

Output:
LAB 6
Objective: Installing LaTex
Step 1 – Go to miktex.org
Step 2 – Open download section
Step 3 – Download MiKTeX

Step 4 – Run MiKTeX Installer


Step 5 – Choose to install missing packages automatically

Step 6 – Open TeXworks


The Installation is complete at this point. TeXworks is the name of your new LaTeX editor
for now.
Step 7 – Write code and hit compile

Step 8 – Enjoy your very first document


Now you're able to compile all the code shown on this website and on the blog. All packages
will be downloaded automatically. Instructions for Linux and Mac will follow soon.
LAB 8
Objective: Working with LaTex
After a successful installation, and likely also after a computer restart, it is a good idea to
check whether TeX is installed correctly. To this end, please start any text editor (e.g.,
Notepad), type the text shown in the following frame, and save the file under the
name test.tex.

\documentclass{article}
\begin{document}
My first \TeX~document.
\end{document}

Now please go to the command line and execute the following command.

latex test.tex

You should obtain the test.dvi file. This file can be seen in any DVI Viewer contained in the
TeX distribution, such as WinDVI, YAP and should look like what is shown below.
LAB 9 and 10
Objective: Concept of inserting table, arrays, contents, references in a
research paper using LaTex.

1. Table:

\documentclass{article}
\begin{document}
\begin{table}[h!]
\begin{center}
\caption{Your first table.}
\label{tab:table1}
\begin{tabular}{l|c|r} % <-- Alignments: 1st column left, 2nd middle and 3rd
right, with vertical lines in between
\textbf{Value 1} & \textbf{Value 2} & \textbf{Value 3}\\
$\alpha$ & $\beta$ & $\gamma$ \\
\hline
1 & 1110.1 & a\\
2 & 10.1 & b\\
3 & 23.113231 & c\\
\end{tabular}
\end{center}
\end{table}
\end{document}
Ouput:
2. Array:
\begin{array}{lcl}
z & = & a \\
& = & a \\
f(x,y,z) & = & x + y + z
\end{array}

Ouput:
z =a
=a
f(x,y,z) = x+y+z

3. References:

documentclass{article}
\begin{document}
Random citation \cite{DUMMY:1} embeddeed in text.
\newpage
\bibliography{lesson7a1}
\bibliographystyle{ieeetr}
\end{document}

Output:

4. Table of contents:
\begin{document}

\begin{figure}
\caption{Dummy figure}
\end{figure}

\begin{table}
\caption{Dummy table}
\end{table}

\begin{appendix}
\listoffigures
\listoftables
\end{appendix}

\end{document}
Output: Compile two times

Potrebbero piacerti anche