Practical File of Programming in Data Structures
Practical File of Programming in Data Structures
DATA STRUCTURES
Submitted in partial fulfilment of the requirements for the degree
B.Tech(Computer Science and Engineering)
Submitted to
MR. INDERPAL SINGH
Submitted by
HARSHPREET KAUR
Roll No:2016CSA1413
2
INTRODUCTION TO PYTHON
Python is a good choice for mathematical calculation ,since we can write code quickly, test it
easily, and its syntax is similar to the way mathematical ideas are expressed in the
mathematical literature .By learning python u will also be learning a major tool used by many
web developers
HISTORY
The history of the Python programming language dates back to the late 1980s.
Python was conceived in the late 1980sand its implementation was started in December 1989
by Guido van Rossum at CWI in the Netherlands as a successor to the ABC programming
language capable of exception handling and interfacing with the Amoeba operating system.
Python was named for the BBC TV show Monty Python's Flying Circus. Van Rossum is
Python's principal author, and his continuing central role in deciding the direction of Python
is reflected in the title given to him by the Python community, Benevolent Dictator for Life
(BDFL).
Python 2.0 was released on October 16, 2000, with many major new features, including a
cycle-detecting garbage collector (in addition to reference counting) for memory
management and support for Unicode. However, the most important change was to the
development process itself, with a shift to a more transparent and community-backed process.
Python 3.0, a major, backwards-incompatible release, was released on December 3, 2008
after a long period of testing. Many of its major features have also been backported to the
backwardscompatible Python 2.6 and 2.7.
First publication
In February 1991, Van Rossum published the code (labeled version 0.9.0) to alt sources.
Already present at this stage in development were classes with inheritance exception
handling, functions, and the core datatypes of list, dict, str and so on. Also in this initial
release was a module system borrowed from Modula-3.Van Rossum describes the module as
"one of Python's major programming units". Python's exception model also resembles
Modula-3's, with the addition of an else clause.In 1994 comp .lang. python the primary
discussion forum for Python, was formed, marking a milestone in the growth of Python's
userbase.
Python reached version 1.0 in January 1994. The major new features included in this release
were the functional programming tools lambda, mapfilter and reduce. Van Rossum stated that
"Python acquired lambda, reduce(), filter() and map(), courtesy of a Lisp hacker who missed
them and submitted working patches"
The last version released while Van Rossum was at CWI was Python 1.2. In 1995, Van
Rossum continued his work on Python at the Corporation for National Research Initiatives
(CNRI) in Reston, Virginia whence he released several versions.
By version 1.4, Python had acquired several new features. Notable among these are the
Modula-3 inspired keyword arguments(which are also similar to Common Lisp's keyword
arguments) and built-in support for complex numbers. Also included is a basic form of data
hiding by name mangling, though this is easily bypassed.
4
Python served a central role in this: because of its focus on clean syntax, it was already
suitable, and CP4E's goals bore similarities to its predecessor, ABC. The project was funded
by DARPA. As of 2007, the CP4E project is inactive, and while Python attempts to be easily
learnable and not too arcane in its syntax and semantics, reaching out to non-programmers is
not an active concern.
GETTING STARTED
1.Running python as a calculator The easiest way to get started is to run python as an
interpreter, which behaves similar to the way one would use a calculator .In the interpreter
,you type a command, and python produces the answer .then u type another command ,which
again produces an answer ,and so on .In OS X or Linux, to start the python interpreter is as
simple as typing the command python on the command line in a terminal shell .In windows
,assuming that python has already been installed, you need to find python on the appropriate
menu. Windows users can choose to run python in a command shell where it will behave
very similarly to Linux or OS X.
For all three operating systems there is also an integrated development environment for
python named IDLE. If interested,you may download and install this on your computer .Once
python start running in interpreter mode ,using IDLE or a command shell ,it produces a
prompt ,which waits for ur input .for example ,this is what i get when I start python in my
command shell or my Linux box.
2.QUITTING THE INTERPRETER
In a terminal you can quit python session by CTRL-D. In IDLE you can also quit from the
menu.
If the interpreter gets struck in an infinite loop, you can quit the current execution by CTRL-
C.
4. Defining functions:
It is possible and very useful to define our own functions in python. Generally speaking, if
you need to do a calculation only once ,then use the interpreter. But when u or others have
need to perform certain type of calculation many times then define a function. For a simple
example, the compound command >>>def f(x):
defines the squaring function f(x)=x^2,a popular example used in elementary math courses In
the definition ,the first line is the function header where the name, of the function is specified.
subsequent lines give the body of the function, where the output value is calculated. Note that
final step is to return the answer; without it we would never see any results .continuing the
example ,we can use the function to calculate the square of any given input :
>>>f(2)
>>> f(2.5)
6.25 5.
FILES:
Python allows us to store our code in files(also called modules).This is very useful for more
serious to change one mistake .In doing this ,we are essentially defining our own modules
,just like the modules defined already in the python library
PYTHON FEATURES
• Easy-to-learn: Python has few keywords, simple structure, and a clearly defined
syntax. This allows the student to pick up the language quickly.
• Easy-to-read: Python code is more clearly defined and visible to the eyes.
• Easy-to-maintain: Python's source code is fairly easy-to-maintain.
• A broad standard library: Python's bulk of the library is very portable and
crossplatform compatible on UNIX, Windows, and Macintosh.
• Interactive Mode:Python has support for an interactive mode which allows
interactive testing and debugging of snippets of code.
• Portable: Python can run on a wide variety of hardware platforms and has the same
interface on all platforms.
• Extendable: You can add low-level modules to the Python interpreter. These
modules enable programmers to add to or customize their tools to be more efficient.
• Databases: Python provides interfaces to all major commercial databases.
• GUI Programming: Python supports GUI applications that can be created and ported
to many system calls, libraries and windows systems, such as Windows MFC,
Macintosh, and the X Window system of Unix.
• Scalable: Python provides a better structure and support for large programs than shell
scripting.
Apart from the above-mentioned features, Python has a big list of good features, few are
listed below:
6
• It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
8
STEP 3:
Enter your product key. This is the 25 character key that came with your purchased copy of
Windows 8. It may be located on a sticker on your computer or underneath your laptop.
• You do not need to enter the dashes between the groups of characters.
• This is not an optional step. Previous versions of Windows allowed you to register
your product up to 60 days after installing. You must now enter the key before
installation begins.
10
STEP 4:
Accept the license agreement. Once you have read the agreement, check the box signifying
that you accept the agreement and click next.
11
STEP 5:
Click Custom Install. You will have two options to install windows. Picking Custom will
allow you to do a complete installation of Windows 8. Selecting upgrade can cause
performance problems in the long run. It is highly recommended that you do a clean Custom
Install.
12
STEP 6:
Delete the partition. A window will open asking where you would like to install Windows 8.
In order to perform a clean install, you need to delete the old partition and start with a clean
slate. Click “Drive options (advanced).” This will give you the ability to delete and create
partitions.
• Select the partition of your existing operating system and click the Delete button.
13
• If you are installing an operating system for the first time on this hard drive, then
there will be no partitions to delete.
14
• If your hard drive has multiple partitions, be sure to delete the correct one. Any data
on a deleted partition is lost for good.
• Confirm the deletion process.
15
16
STEP 7:
Select the Unallocated Space and click Next. There is no need to create a partition before
you install Windows 8, this is done automatically.
17
STEP 8:
Wait while Windows installs files. The percentage next to Expanding Windows files will
steadily increase. This part of the process can take up to 30 minutes.
18
19
STEP 9:
Wait while Windows gathers information. After the computer reboots, you will see the
Windows 8 logo. Beneath it will be the text “Getting devices ready” followed by a percentage.
Windows is collecting information on the hardware installed in your computer.
20
STEP 10:
Personalize your Windows 8.Once the computer finishes rebooting, you will be asked to pick
the color scheme for your installation of Windows 8.
• You can change the color at any time in the Windows 8 settings.
21
STEP 11:
Enter a computer name. This is the name that the computer displays on the network. Any
other device on the network will see your computer listed with this name.
STEP 12:
Choose your wireless network. If you have a wireless-enable computer or device, you will
see a menu asking you to choose a network. If you haven’t installed the driver for your
wireless card yet, this step will automatically be skipped.
22
Step 13
Choose your settings.The most common option is Express Settings, which will enable
automatic updates, Windows Defender, and error reporting to Microsoft, among other things.
• If you’d rather set these yourself, choose Customize instead of Express Settings.
23
24
STEP 14:
Create an account. To log in to Windows, you will need an account. Microsoft recommends
using a Microsoft account, so that you can make purchases in the Windows store. If you
don’t have a Microsoft account, entering a valid email address will create one for free.
• If you don’t have an email address, click the “Sign up for a new email address” link to
create one. This requires an internet connection.
25
• If you’d rather sign in the old way, without using a Microsoft account, click the
bottom link. This will create a login similar to previous versions of Windows.
26
27
Program:-1
Write A program to represent your bio data
a=input("enter your name")
print("name:", a)
print("roll no:", b)
print("stream:", c)
print("address:", d)
28
PROGRAM-2
if(d>90):
PROGRAM-3
29
print("no is even") else:
print("no is odd")
PROGRAM 4
30
a=int(input("enter a number"));
if(a%i==0):
f=1;
break;
if(f==1):
else: print("prime
number")
PROGRAM-5
for i in range(1,20):
print(n,"*",i,"=",(n*i))
31
Program 6
32
print("number is zero"); output:
Program 7
print(b);
33
Program 8
b=int(input("enter a number"));
c=int(input("eneter a number"));
s=(a+b+c)/2; area=(s*(s-a)*(s-
34
Program 9
b=int(input("enter a number"));
c=int(input("enter a number"));
if(a>b)and(a>c): print("a is
greater");
elif(b>a)and(b>c):
print("c is greater");
35
output:
Program 10
list=[1,2,3,4,5];
if(a in list):
not in list):
36
Program 11
if(a is b):
b=30;
if(a is b ):
37
PROGRAM 12:
b=0; for i in
range(1,a+1):
if(a%i==0):
b=b+1; if(b==2):
print(a) output:
38
PROGRAM 13:
if (n<=1):
return n else:
return n+recur_sum(n-1)
num=int(input("enter a number")) if
(num<0):
39
PROGRAM 14:
sum=0
while(a!=0): a=int(input("enter
40
PROGRAM 15:
total=arg1+arg2;
print(total)
sum(10,20) output:
41
PROGRAM 16:
while(a>0):
remainder=a%10;
reverse=(reverse*10)+remainder;
is",reverse) output:
42
PROGRAM 17:
factorial=1;O if (num<0):
elif(num==0):
print("factorial is 1");
else: for i in
range(1,num+1):
43
factorial=(factorial*i);
print(factorial) output:
PROGRAM 18:
print(bin(dec))
print(hex(dec))
print(oct(dec)) output:
44
PROGRAM 19:
p=0; c=1;
in range(1,num+1):
n=(p+c)
45
print(n)
p=c; c=n;
output:
PROGRAM 20:
if(n<=1):
return n else:
return(recur_fibo(n-1)+recur_fibo(n-2))
nterms=int(input("enter a number")) if
(nterms<=0):
46
print("fibonnici sequence:")
for i in range(nterms):
print(recur_fibo(i)) output:
PROGRAM 21:
my_list=[12,65,54,39,102,339,221]
result=list(filter(lambda x%2==0),my_list))
output:
47
PROGRAM 22:
print(square(5)) output:
48
PROGRAM 22:
return x + y
49
def subtract(x, y):
return x - y
return x * y
return x / y
print("Select operation.")
print("1.Add") print("2.Subtract")
print("3.Multiply")
print("4.Divide")
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
50
print(num1,"-",num2,"=", subtract(num1,num2))
print(num1,"*",num2,"=", multiply(num1,num2))
print(num1,"/",num2,"=", divide(num1,num2))
else:
PROGRAM 23:
= list(map(lambda x: n ** x, range(terms)))
range(terms):
51
52
PRACTICAL FILE OF PROGRAMMING IN
PYTHON
Submitted in partial fulfilment of the requirements for the degree
B.Tech(Computer Science and Engineering)
Submitted by
GIRJA
Roll No:2016CSA1411
Submitted to
Ms Babita
53
INTRODUCTION TO PYTHON
Python is a good choice for mathematical calculation ,since we can write code quickly, test it
easily, and its syntax is similar to the way mathematical ideas are expressed in the
mathematical literature .By learning python u will also be learning a major tool used by many
web developers
HISTORY
The history of the Python programming language dates back to the late 1980s.
Python was conceived in the late 1980sand its implementation was started in December 1989
by Guido van Rossum at CWI in the Netherlands as a successor to the ABC programming
language capable of exception handling and interfacing with the Amoeba operating system.
Python was named for the BBC TV show Monty Python's Flying Circus. Van Rossum is
Python's principal author, and his continuing central role in deciding the direction of Python
is reflected in the title given to him by the Python community, Benevolent Dictator for Life
(BDFL).
Python 2.0 was released on October 16, 2000, with many major new features, including a
cycle-detecting garbage collector (in addition to reference counting) for memory
management and support for Unicode. However, the most important change was to the
development process itself, with a shift to a more transparent and community-backed process.
Python 3.0, a major, backwards-incompatible release, was released on December 3, 2008
after a long period of testing. Many of its major features have also been backported to the
backwardscompatible Python 2.6 and 2.7.
First publication
In February 1991, Van Rossum published the code (labeled version 0.9.0) to alt sources.
Already present at this stage in development were classes with inheritance exception
54
handling, functions, and the core datatypes of list, dict, str and so on. Also in this initial
release was a module system borrowed from Modula-3.Van Rossum describes the module as
"one of Python's major programming units". Python's exception model also resembles
Modula-3's, with the addition of an else clause.In 1994 comp .lang. python the primary
discussion forum for Python, was formed, marking a milestone in the growth of Python's
userbase.
Python reached version 1.0 in January 1994. The major new features included in this release
were the functional programming tools lambda, mapfilter and reduce. Van Rossum stated that
"Python acquired lambda, reduce(), filter() and map(), courtesy of a Lisp hacker who missed
them and submitted working patches"
The last version released while Van Rossum was at CWI was Python 1.2. In 1995, Van
Rossum continued his work on Python at the Corporation for National Research Initiatives
(CNRI) in Reston, Virginia whence he released several versions.
By version 1.4, Python had acquired several new features. Notable among these are the
Modula-3 inspired keyword arguments(which are also similar to Common Lisp's keyword
arguments) and built-in support for complex numbers. Also included is a basic form of data
hiding by name mangling, though this is easily bypassed.
Python served a central role in this: because of its focus on clean syntax, it was already
suitable, and CP4E's goals bore similarities to its predecessor, ABC. The project was funded
55
by DARPA. As of 2007, the CP4E project is inactive, and while Python attempts to be easily
learnable and not too arcane in its syntax and semantics, reaching out to non-programmers is
not an active concern.
GETTING STARTED
1.Running python as a calculator The easiest way to get started is to run python as an
interpreter, which behaves similar to the way one would use a calculator .In the interpreter
,you type a command, and python produces the answer .then u type another command ,which
again produces an answer ,and so on .In OS X or Linux, to start the python interpreter is as
simple as typing the command python on the command line in a terminal shell .In windows
,assuming that python has already been installed, you need to find python on the appropriate
menu. Windows users can choose to run python in a command shell where it will behave
very similarly to Linux or OS X.
For all three operating systems there is also an integrated development environment for
python named IDLE. If interested,you may download and install this on your computer .Once
python start running in interpreter mode ,using IDLE or a command shell ,it produces a
prompt ,which waits for ur input .for example ,this is what i get when I start python in my
command shell or my Linux box.
2.QUITTING THE INTERPRETER
In a terminal you can quit python session by CTRL-D. In IDLE you can also quit from the
menu.
If the interpreter gets struck in an infinite loop, you can quit the current execution by CTRL-
C.
4. Defining functions:
It is possible and very useful to define our own functions in python. Generally speaking, if
you need to do a calculation only once ,then use the interpreter. But when u or others have
need to perform certain type of calculation many times then define a function. For a simple
example, the compound command >>>def f(x):
defines the squaring function f(x)=x^2,a popular example used in elementary math courses In
the definition ,the first line is the function header where the name, of the function is specified.
56
subsequent lines give the body of the function, where the output value is calculated. Note that
final step is to return the answer; without it we would never see any results .continuing the
example ,we can use the function to calculate the square of any given input :
>>>f(2)
>>> f(2.5)
6.25
5. FILES:
Python allows us to store our code in files(also called modules).This is very useful for more
serious to change one mistake .In doing this ,we are essentially defining our own modules
,just like the modules defined already in the python library
PYTHON FEATURES
• Easy-to-learn: Python has few keywords, simple structure, and a clearly defined
syntax. This allows the student to pick up the language quickly.
• Easy-to-read: Python code is more clearly defined and visible to the eyes.
• Easy-to-maintain: Python's source code is fairly easy-to-maintain.
• A broad standard library: Python's bulk of the library is very portable and
crossplatform compatible on UNIX, Windows, and Macintosh.
• Interactive Mode:Python has support for an interactive mode which allows
interactive testing and debugging of snippets of code.
• Portable: Python can run on a wide variety of hardware platforms and has the same
interface on all platforms.
• Extendable: You can add low-level modules to the Python interpreter. These
modules enable programmers to add to or customize their tools to be more efficient.
• Databases: Python provides interfaces to all major commercial databases.
• GUI Programming: Python supports GUI applications that can be created and ported
to many system calls, libraries and windows systems, such as Windows MFC,
Macintosh, and the X Window system of Unix.
• Scalable: Python provides a better structure and support for large programs than shell
scripting.
Apart from the above-mentioned features, Python has a big list of good features, few are
listed below:
57
STEPS FOR INSTALLATION OF
WINDOW 8
Select your language. Once the Windows 8 installation begins, you will be prompted to
select a language, time and currency format, and keyboard layout. Once you select these,
click Next.
58
STEP 2:
Click Install Now. This will begin the installation process. The other option is for repairing
an existing Windows installation.
59
STEP 3:
60
Enter your product key. This is the 25 character key that came with your purchased copy of
Windows 8. It may be located on a sticker on your computer or underneath your laptop.
• You do not need to enter the dashes between the groups of characters.
• This is not an optional step. Previous versions of Windows allowed you to register
your product up to 60 days after installing. You must now enter the key before
installation begins.
61
STEP 4:
Accept the license agreement. Once you have read the agreement, check the box signifying
that you accept the agreement and click next.
62
STEP 5:
Click Custom Install. You will have two options to install windows. Picking Custom will
allow you to do a complete installation of Windows 8. Selecting upgrade can cause
performance problems in the long run. It is highly recommended that you do a clean Custom
Install.
63
STEP 6:
Delete the partition. A window will open asking where you would like to install Windows 8.
In order to perform a clean install, you need to delete the old partition and start with a clean
slate. Click “Drive options (advanced).” This will give you the ability to delete and create
partitions.
• Select the partition of your existing operating system and click the Delete button.
64
• If you are installing an operating system for the first time on this hard drive, then
there will be no partitions to delete.
65
• If your hard drive has multiple partitions, be sure to delete the correct one. Any data
on a deleted partition is lost for good.
• Confirm the deletion process.
66
67
STEP 7:
Select the Unallocated Space and click Next. There is no need to create a partition before
you install Windows 8, this is done automatically.
68
STEP 8:
Wait while Windows installs files. The percentage next to Expanding Windows files will
steadily increase. This part of the process can take up to 30 minutes.
69
70
STEP 9:
Wait while Windows gathers information. After the computer reboots, you will see the
Windows 8 logo. Beneath it will be the text “Getting devices ready” followed by a percentage.
Windows is collecting information on the hardware installed in your computer.
71
STEP 10:
Personalize your Windows 8.Once the computer finishes rebooting, you will be asked to pick
the color scheme for your installation of Windows 8.
• You can change the color at any time in the Windows 8 settings.
72
STEP 11:
Enter a computer name. This is the name that the computer displays on the network. Any
other device on the network will see your computer listed with this name.
STEP 12:
Choose your wireless network. If you have a wireless-enable computer or device, you will
see a menu asking you to choose a network. If you haven’t installed the driver for your
wireless card yet, this step will automatically be skipped.
73
Step 13
Choose your settings.The most common option is Express Settings, which will enable
automatic updates, Windows Defender, and error reporting to Microsoft, among other things.
• If you’d rather set these yourself, choose Customize instead of Express Settings.
74
75
STEP 14:
Create an account. To log in to Windows, you will need an account. Microsoft recommends
using a Microsoft account, so that you can make purchases in the Windows store. If you don’t
have a Microsoft account, entering a valid email address will create one for free.
• If you don’t have an email address, click the “Sign up for a new email address” link
to create one. This requires an internet connection.
76
• If you’d rather sign in the old way, without using a Microsoft account, click the
bottom link. This will create a login similar to previous versions of Windows.
77
78
Program:-1
Write A program to represent your bio data
a=input("enter your name")
print("name:", a)
print("roll no:", b)
print("stream:", c)
print("address:", d)
79
PROGRAM-2
if(d>90):
PROGRAM-3
80
print("no is even") else:
print("no is odd")
PROGRAM 4
81
a=int(input("enter a number"));
if(a%i==0):
f=1;
break;
if(f==1):
else: print("prime
number")
PROGRAM-5
for i in range(1,20):
print(n,"*",i,"=",(n*i))
82
Program 6
83
print("number is zero"); output:
Program 7
print(b);
84
Program 8
b=int(input("enter a number"));
c=int(input("eneter a number"));
s=(a+b+c)/2; area=(s*(s-a)*(s-
85
Program 9
b=int(input("enter a number"));
c=int(input("enter a number"));
if(a>b)and(a>c): print("a is
greater");
elif(b>a)and(b>c):
print("c is greater");
86
output:
Program 10
list=[1,2,3,4,5];
if(a in list):
not in list):
87
Program 11
if(a is b):
b=30;
if(a is b ):
88
PROGRAM 12:
b=0; for i in
range(1,a+1):
if(a%i==0):
b=b+1; if(b==2):
print(a) output:
89
PROGRAM 13:
if (n<=1):
return n else:
return n+recur_sum(n-1)
num=int(input("enter a number")) if
(num<0):
90
PROGRAM 14:
sum=0
while(a!=0): a=int(input("enter
91
PROGRAM 15:
total=arg1+arg2;
print(total)
sum(10,20) output:
92
PROGRAM 16:
while(a>0):
remainder=a%10;
reverse=(reverse*10)+remainder;
is",reverse) output:
93
PROGRAM 17:
factorial=1;O if (num<0):
elif(num==0):
print("factorial is 1");
else: for i in
range(1,num+1):
94
factorial=(factorial*i);
print(factorial) output:
PROGRAM 18:
print(bin(dec))
print(hex(dec))
print(oct(dec)) output:
95
PROGRAM 19:
p=0; c=1;
in range(1,num+1):
n=(p+c)
96
print(n)
p=c; c=n;
output:
PROGRAM 20:
if(n<=1):
return n else:
return(recur_fibo(n-1)+recur_fibo(n-2))
nterms=int(input("enter a number")) if
(nterms<=0):
97
print("fibonnici sequence:")
for i in range(nterms):
print(recur_fibo(i)) output:
PROGRAM 21:
my_list=[12,65,54,39,102,339,221]
result=list(filter(lambda x%2==0),my_list))
output:
98
PROGRAM 22:
print(square(5)) output:
99
PROGRAM 23:
return x + y
100
def subtract(x, y):
return x - y
return x * y
return x / y
print("Select operation.")
print("1.Add") print("2.Subtract")
print("3.Multiply")
print("4.Divide")
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
101
print(num1,"-",num2,"=", subtract(num1,num2))
print(num1,"*",num2,"=", multiply(num1,num2))
print(num1,"/",num2,"=", divide(num1,num2))
else:
PROGRAM 24:
= list(map(lambda x: n ** x, range(terms)))
range(terms):
102
PROGRAM 25:
while(a>0):
remainder=a%10;
reverse=(reverse*10)+remainder;
while(reverse>a): if (reverse%a==0):
isPrime=False
break else:
103
a=a+1;
if(isPrime):
print(“not prime”)
104