Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
62 views

Python Manual Student

The document provides an introduction and history of the Python programming language. It discusses how Python was created in the late 1980s and reached version 1.0 in 1994. Major releases include Python 2.0 in 2000 and Python 3.0 in 2008, which was a backwards incompatible release. The document outlines the origins and key features of Python, including that it is designed to be simple, easy to learn, and free/open source software.

Uploaded by

Sai Sriram
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
62 views

Python Manual Student

The document provides an introduction and history of the Python programming language. It discusses how Python was created in the late 1980s and reached version 1.0 in 1994. Major releases include Python 2.0 in 2000 and Python 3.0 in 2008, which was a backwards incompatible release. The document outlines the origins and key features of Python, including that it is designed to be simple, easy to learn, and free/open source software.

Uploaded by

Sai Sriram
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 66

INTRODUCTION

TO
PYTHON PROGRAMMING

1
Expt: 1
Date:

INTRODUCTION TO PYTHON

History of Python
The programming language Python was conceived in the late 1980s, and 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. 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 was named for the BBC TV show Monty Python's Flying Circus.

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 backwards- compatible
Python 2.6 and 2.7.

Early history

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".[1] Python's exception model also resembles Modula-3's, with the addition of
an else clause.[3] In 1994 comp.lang.python, the primary discussion forum for Python, was

formed, marking a milestone in the growth of Python's userbase.

Version

2
Python reached version 1.0 in January 1994. The major new features included in this release
were the functional programming tools lambda , map , filt and 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.

During Van Rossum's stay at CNRI, he launched the Computer Programming for Everybody (CP4E)
initiative, intending to make programming more accessible to more people, with a basic "literacy" in
programming languages, similar to the basic English literacy and mathematics skills required by most
employers. 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.

BeOpen

In 2000, the Python core development team moved to BeOpen.com to form the BeOpen
PythonLabs team. CNRI requested that a version 1.6 be released, summarizing Python's development up
to the point at which the development team left CNRI. Consequently, the release schedules for 1.6 and 2.0
had a significant amount of overlap. Python 2.0 was the only release from BeOpen.com. After Python 2.0
was released by BeOpen.com, Guido van Rossum and the other PythonLabs developers joined Digital
Creations.

The Python 1.6 release included a new CNRI license that was substantially longer than the CWI license
that had been used for earlier releases. The new license included a clause stating that the

3
license was governed by the laws of the State of Virginia. The Free Software Foundation argued that the
choice-of-law clause was incompatible with the GNU General Public License. BeOpen, CNRI and the
FSF negotiated a change to Python's free software license that would make it GPL-compatible. Python
1.6.1 is essentially the same as Python 1.6, with a few minor bug fixes, and with the new GPL-compatible
license

Version 2

Python 2.0 introduced list comprehensions, a feature borrowed from the functional programming
languages SETL and Haskell. Python's syntax for this construct is very similar to Haskell's, apart from
Haskell's preference for punctuation characters and Python's preference for alphabetic keywords. Python
2.0 also introduced a garbage collection system capable of collecting reference cycles.

Python 2.1 was close to Python 1.6.1, as well as Python 2.0. Its license was renamed Python Software
Foundation License. All code, documentation and specifications added, from the time of Python 2.1's
alpha release on, is owned by the Python Software Foundation (PSF), a non- profit organization formed in
2001, modeled after the Apache Software Foundation. The release included a change to the language
specification to support nested scopes, like other statically scoped languages. (The feature was turned off
by default, and not required, until Python 2.2.)

A major innovation in Python 2.2 was the unification of Python's types (types written in C) and classes
(types written in Python) into one hierarchy. This single unification made Python's object model purely
and consistently object oriented. Also added were generators which were inspired by Icon.

Python 2.6 was released to coincide with Python 3.0, and included some features from that release, as
well as a "warnings" mode that highlighted the use of features that were removed in Python 3.0.Similarly,
Python 2.7 coincided with and included features from Python 3.1,which was released on June 26, 2009.
Parallel 2.x and 3.x releases then ceased, and Python 2.7 was the last release in the 2.x series. In
November 2014, it was announced that Python 2.7 would be supported until 2020, but users were
encouraged to move to Python 3 as soon as possible.

Version 3

4
Python 3.0 (also called "Python 3000" or "Py3K") was released on December 3, 2008.It was designed to
rectify fundamental design flaws in the language—the changes required could not be implemented while
retaining full backwards compatibility with the 2.x series, which necessitated a new major version
number. The guiding principle of Python 3 was: "reduce feature duplication by removing old ways of
doing things".

Python 3.0 was developed with the same philosophy as in prior versions. However, as Python had
accumulated new and redundant ways to program the same task, Python 3.0 had an emphasis on removing
duplicative constructs and modules, in keeping with "There should be one— and preferably only one —
obvious way to do it".

Nonetheless, Python 3.0 remained a multi-paradigm language. Coders still had options among object-
orientation, structured programming, functional programming and other paradigms, but within such broad
choices, the details were intended to be more obvious in Python 3.0 than they were in Python 2.x.

Compatibility
Python 3.0 broke backward compatibility, and much Python 2 code does not run unmodified on Python 3.
Python's dynamic typing combined with the plans to change the semantics of certain methods of
dictionaries, for example, made perfect mechanical translation from Python 2.x to Python 3.0 very
difficult. A tool called "2to3" does the parts of translation that can be done automatically. At this, 2to3
appeared to be fairly successful, though an early review noted that there were aspects of translation that
such a tool would never be able to handle.Prior to the roll- out of Python 3, projects requiring
compatibility with both the 2.x and 3.x series were recommended to have one source (for the 2.x series),
and produce releases for the Python 3.x platform using 2to3. Edits to the Python 3.x code were
discouraged for so long as the code needed to run on Python 2.x.This is no longer recommended; as of
2012 the preferred alternative is to create a single code base that can run under both Python 2 and 3 using
compatibility modules.
Features of Python

a) Simple

5
Python is a simple and minimalistic language. Reading a good Python program feels almost like reading
English (but very strict English!). This pseudo-code nature of Python is one of its greatest strengths. It
allows you to concentrate on the solution to the problem rather than the syntax i.e. the language itself.

b) Easy to Learn

As you will see, Python is extremely easy to get started with. Python has an extraordinarily simple syntax
as already mentioned.

c) Free and Open Source

Python is an example of a FLOSS (Free/Libre and Open Source Software). In simple terms, you can
freely distribute copies of this software, read the software's source code, make changes to it, use pieces of
it in new free programs, and that you know you can do these things. FLOSS is based on the concept of a
community which shares knowledge. This is one of the reasons why Python is so good - it has been
created and improved by a community who just want to see a better Python.

d) High-level Language

When you write programs in Python, you never need to bother about low-level details such as managing
the memory used by your program.

e) Portable

Due to its open-source nature, Python has been ported (i.e. changed to make it work on) to many many
platforms. All your Python programs will work on any of these platforms without requiring any changes
at all. However, you must be careful enough to avoid any system- dependent features.

You can use Python on Linux, Windows, Macintosh, Solaris, OS/2, Amiga, AROS, AS/400, BeOS,
OS/390, z/OS, Palm OS, QNX, VMS, Psion, Acorn RISC OS, VxWorks, PlayStation, Sharp Zaurus,
Windows CE and PocketPC !

6
f) Interpreted

This requires a little explanation.

A program written in a compiled language like C or C++ is translated from the source language
i.e. C/C++ into a language spoken by your computer (binary code i.e. 0s and 1s) using a compiler with
various flags and options. When you run the program, the linker/loader software just stores the binary
code in the computer's memory and starts executing from the first instruction in the program.

When you use an interpreted language like Python, there is no separate compilation and execution steps.
You just run the program from the source code. Internally, Python converts the source code into an
intermediate form called bytecodes and then translates this into the native language of your specific
computer and then runs it. All this makes using Python so much easier. You just run your programs - you
never have to worry about linking and loading with libraries, etc. They are also more portable this way
because you can just copy your Python program into another system of any kind and it just works!

g) Object Oriented

Python supports procedure-oriented programming as well as object-oriented programming. In procedure-


oriented languages, the program is built around procedures or functions which are nothing but reusable
pieces of programs. In object-oriented languages, the program is built around objects which combine data
and functionality. Python has a very powerful but simple way of doing object-oriented programming,
especially, when compared to languages like C++ or Java.

h) Extensible

If you need a critical piece of code to run very fast, you can achieve this by writing that piece of code in
C, and then combine that with your Python program.

7
i) Embeddable

You can embed Python within your C/C++ program to give scripting capabilities for your program's
users.

j) Extensive Libraries

The Python Standard Library is huge indeed. It can help you do various things involving regular
expressions, documentation generation, unit testing, threading, databases, web browsers, CGI, ftp, email,
XML, XML-RPC, HTML, WAV files, cryptography, GUI(graphical user interfaces) using Tk, and also
other system-dependent stuff. Remember, all this is always available wherever Python is installed. This is
called the "batteries included" philosophy of Python.

Besides the standard library, there are various other high-quality libraries such as the Python Imaging
Library which is an amazingly simple image manipulation library.

Version release dates

Release dates for the major and minor versions:

 Python 1.0 - January 1994


 Python 1.5 - December 31, 1997
 Python 1.6 - September 5, 2000
 Python 2.0 - October 16, 2000
 Python 2.1 - April 17, 2001
 Python 2.2 - December 21, 2001
 Python 2.3 - July 29, 2003
 Python 2.4 - November 30, 2004
 Python 2.5 - September 19, 2006
 Python 2.6 - October 1, 2008
 Python 2.7 - July 3, 2010

8
 Python 3.0 - December 3, 2008
 Python 3.1 - June 27, 2009
 Python 3.2 - February 20, 2011
 Python 3.3 - September 29, 2012
 Python 3.4 - March 16, 2014
 Python 3.5 - September 13, 2015
 Python 3.6 - December 23, 2016
Python is a widely used high-level programming language for general-purpose programming, created
by Guido van Rossum and first released in 1991.

Python is a general purpose programming language created in the late 1980s, and named after Monty
Python, that's used by thousands of people to do things from testing microchips at Intel, to powering
Instagram, to building video games with the PyGame library.

Python is a programming language. It's used for many different applications. It's used in some high
schools and colleges as an introductory programming language because Python is easy to learn, but it's
also used by professional software developers at places such as Google, NASA, and Lucasfilm Ltd.

When he began implementing Python, Guido van Rossum was also reading the published scripts from
“Monty Python's Flying Circus”, a BBC comedy series from the 1970s. Van Rossum thought he needed
a name that was short, unique, and slightly mysterious, so he decided to call the language Python.

The Python programming language was conceived in the late 1980s, and 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 on a Macintosh running Mac OS X is in principle very similar to Python on any other Unix
platform, but there are a number of additional features such as the IDE and the Package Manager that are
worth pointing out. The Mac-specific modules are documented in Mac OS X specific services.
9
Python is a high-level programming language designed to be easy to read and simple to implement. It is
open source, which means it is free to use, even for commercial applications. Python is considered a
scripting language, like Ruby or Perl and is often used for creating Web applications and dynamic Web
content

Python is a widely used high-level programming language for general-purpose programming, created by
Guido van Rossum and first released in 1991. CPython, the reference implementation of Python, is open
source software and has a community-based development model, as do nearly all of its variant
implementations

Python is used in GIS programming. It is used as a scripting language for ArcGIS, and for Quantum GIS.
Python is true general purpose language. Your question is kind of like asking, what area should you focus
on if you want C++ job or Java job.

Learning Python is really easy, a breeze if you've learned other object oriented programming languages.
... It's recommended to be the first language people learn as it contains all the OO concepts but is easier
to read and less strongly typed as C++ or to a lesser extent Java.

Apart from Windows, Linux and MacOS, CPython implementation runs on 21 different platforms.
IronPython is a .NET framework based Python implementation and it is cabable of running in both
Windows, Linux and in other environments where .NET framework is available.

Python is, or can be used as the scripting language in these software products:
 Abaqus (Finite Element Software)
 Advantage Framework.
 Amarok.
 ArcGIS, a prominent GIS platform, allows extensive modeling using Python.
 Autodesk Maya, professional 3D modeler allows Python scripting as an alternative to MEL as of version
8.5.

10
 It is still common to start students with a procedural (subset of a) statically typed language such as Pascal,
C, or a subset of C++ or Java. Students may be better served by learning Python as their first language. ...
Many other aspects of Python make it a good first language.
Here is a look at 10 of the most famous software programs that are written in Python and what
they do.
 YouTube. If you love watching hours of homemade and professional quality video clips on YouTube, you
can thank Python for giving you that option. ...
 DropBox. ...
 Google. ...
 Quora. ...
 Instagram. ...
 BitTorrent. ...
 Spotify. ...
 Reddit.
 The interpreter operates somewhat like the Unix shell: when called with standard input connected to a
tty device, it reads and executes commands interactively; when called with a file name argument or with a
file as standard input, it reads and executes a script from that file
 A 'shell' is a user interface for something. A python shell is a way for a user to interact with the python
interpreter.
 Run a Python script under Windows with the Command Prompt. Note that you must use the full path of
the Python interpreter. If you want to simply typepython.exe C:\Users\Username\Desktop\
my_python_script.py you must addpython.exe to your PATH environmental variable
 In computer science, an interpreter is a computer program that directly executes, i.e. performs,
instructions written in a programming or scripting language, without previously compiling them into a
machine language program.
 IDLE (Integrated Development Environment or Integrated Development and Learning Environment) is
an integrated development environment for Python, which has been bundled with the default
implementation of the language since 1.5.2b1. Python shell with syntax highlighting.

11
 The Console is where you may enter, interact with and visualize data, inside a command interpreter. All
the commands entered in the console are executed in a separate process, thus allowing the user to
interrupt any process at any time. Many command windows may be created in the Console: Python
interpreter.
Mac[edit]
1. Create a folder on your computer to use for your Python programs. ...
2. Open the Applications folder, go into the Utilities folder, and open the Terminal program.
3. Type cd python practice to change directory to your python practice folder, and hit Enter.
4. Type python ./hello.py to run your program!
Writing Your First Python Program
1. Create a folder called Python Programs on your C:\ drive. ...
2. Go to Start and either type Run in the Start Search box at the bootom or click on Run.
3. Type in notepad in the field called Open.
4. In Notepad type in the following program exactly as written:

The compiler translates the entire program before it is run. The interpreters translate a line at a time
while the program is being run. Unlike compiled languages which are translated into machine language
ahead of time (right), interpreted languages are translated at runtime.
Integrated Development Environment
IDLE is the standard Python development environment. Its name is an acronym of "Integrated
Development Environment". It works well on both Unix and Windows platforms. It has a Python shell
window, which gives you access to the Python interactive mode.
Python IDE is the first thing you need to get started with python programming You can get started
with Python programming in a basic text editor like notepad or choose to go ahead with a complete and
feature rich Python IDE like PyCharm, Pydev, Emacs or Eric
Unix/Linux file systems do not rely on extensions the way windows does. You should not need the .py at
the end of a file to run it. Or by marking it executable and defining the interpreter on the first line (e.g.
#!/usr/bin/python ).PyCharm is an Integrated Development Environment (IDE) used in computer
programming.

12
PyCharm Community Edition is totally free and open-source, available under the Apache 2.0 license. ...
PyCharm 3.0 Professional Edition is a free update for everyone who purchased their license after Sep 24,
2012. As usual, a 30-day trial is available if you want to try PyCharm as your new Python/Django
IDE.specifically for the Python language. It is developed by the Czech company JetBrains.

13
SAMPLE
PROGRAMS

14
SAMPLE PYTHON PROGRAMS

SUM OF TWO NUMBERS

AIM:

To find the sum of two numbers using python.

ALGORITHM:

Step 1: Start the program.


Step 2: Read the value of a and b
Step 3: Calculate the sum c=a+b
Step 4: Print c
Step 5: Stop the program

PSEUDOCODE:

READ the number a and b


Calculate sum c=a+c
WRITE c
STOP

FLOWCHART:

Start

Read thevalue ofa,b

Calculate Sumc=a+b

Print the valueof C

15
Stop
PROGRAM:

a=int(input("Enter the number"))


b=int(input("Enter the number"))
c=a+b
print("The sum of two numbers is",c)

OUTPUT:

Enter a number: 20
Enter a number: 50
The sum of two numbers is 70

RESULT:

Thus the Sum of two numbers program has been written and executed successfully.
EVEN OR ODD NUMBER

AIM:

To find the given number is even or odd using python.

ALGORITHM:

Step 1: Start the program.


Step 2: Read the value of a
Step 3: if a%2 == 0
Step3.1: print(“Even Number”)
else
Step3.2: print(“Odd Number”)
Step 5: Stop the program.

PSEUDOCODE:

READ the number a


IF a%2 == 0 THEN
WRITE Even Number
ELSE
WRITE Odd Number
END IF
STOP
FLOWCHART:

Start

Read the value of a

NO YES
Ifa%2 == 0

Odd Number
Even Number

Stop

PROGRAM:

a=int(input("Enter the number")) if


a%2==0:
print("%d is even number" %(a))
else:
print("%d is odd number" %(a))
OUTPUT:

Enter the number 3 3

is odd number

RESULT:

Thus the program to find the given number even or odd has been written and executed
successfully.
LEAP YEAR OR NOT

AIM:

To find the given year is leap or not using python.

ALGORITHM:

Step 1: Start the program.


Step 2: Read the value of y
Step 3: if y%4 == 0
Step3.1: print(“Leap Year”)
else
Step3.2: print(“Not a Leap Year”)
Step 5: Stop the program.

PSEUDOCODE:

READ the number y


IF y%4 == 0 THEN
WRITE Leap Year
ELSE
WRITE Not a Leap Year
END IF
STOP
FLOWCHART:

Start

Read the value of y

NO YES
Ify%4 == 0

Not a Leap year YearYear


Leap Year

Stop

PROGRAM:

y=int(input("Enter the year")) if


y%4==0:
print("%d is Leap Year" %(y)) else:
print("%d is Not a Leap Year" %(y))
OUTPUT:

Enter the year 1988

1988 is Leap Year

RESULT:

Thus the program to find the given year leap year or not has been written and executed successfully.
GREATEST OF THREE NUMBERS

AIM:

To write a program to find the greatest of three number using python.

ALGORITHM:

Step 1: Start the program. Step


2: Read the value of a,b,c
Step 3: if (a>b) & (a>c)
Step 3.1: print(“a is big”)
Step 4: elif (b>a) & (b>c)
Step 4.1: print(“b is big”)
else
Step 4.2: print(“c is big”)
Step 5: Stop the program.

PSEUDOCODE:

READ the value of a,b,c


IF (a>b) & (a>c) THEN
WRITE a is big
ELIF (b>a) & (b>c) THEN
WRITE b is big
ELSE
WRITE c is big
END IF
STOP
FLOWCHART

Start

Read the value of a,b,c

YES
if (a>b) & (a>c) a is big

NO
YES
if (b>a) & (b>c)
b is big

NO

c is big

Stop
PROGRAM:

a=int(input("Enter the first number"))


b=int(input("Enter the second number"))
c=int(input("Enter the Third number"))
if a>b&a>c:
print("%d is Greater" %(a))
elif b>a&b>c:
print("%d is Greater" %(b))
else:
print("%d is Greater" %(c))

OUTPUT:

Enter the first number 5

Enter the second number 4

Enter the Third number 7 7

is Greater

RESULT:

Thus the program to find the greatest of three numbers has been written and executed
successfully.
FACTORIAL NUMBER

AIM:

To write a program to find the factorial of a given number using python.

ALGORITHM:

Step 1: Start the program.


Step 2: Set initial value to fact=1, i=1
Step 3: Read the value of n
Step 4: while (i<=n) then
Step 4.1: fact = fact * i
Step 4.2: i = i + 1
Step 5: Repeat the step 4 until the condition became false
Step 6: Print the value of fact
Step 7: Stop the program

PSEUDOCODE:

Set the value fact=1, i=1


READ the value of n
WHILE (i<=n) THEN
fact = fact * i i
= i +1
END WHILE
WRITE fact
STOP
FLOWCHART

Start

Read the value of n

Set fact=1, i=1

NO
while
(i<=n)

YES

fact= fact* i
i = i +1

Print the fact

Stop
PROGRAM:

n=int(input("Enter the number"))


fact=1
i=1
while i<=n:
fact=fact*i
i=i+1
print("The factorial of given number is %d"%(fact))

OUTPUT:

Enter the number 6

The factorial of given number is 720

RESULT:

Thus the program to find the factorial of a given number has been written and executed
successfully.
LAB
EXERCISES
Expt: 2
Date:
LCM OF TWO NUMBERS

AIM:

To write a program to find the LCM of two numbers using python.

ALGORITHM:

Step 1: Start
Step 2: Initialize A and B with positive integers
Step 3: Store maximum of A & B to max
Step 4: Check if max is divisible by A and B
Step 5: If divisible, Display max as LCM
Step 6: If not divisible then step increase max, goto step 4
Step 7: End

FLOWCHART:
PROGRAM:

OUTPUT:

RESULT:

Thus the LCM of two numbers program is written and executed successfully.
Expt: 3
Date:
SUM OF SQUARES OF FIRST N NATURAL NUMBERS

AIM:

To calculate the sum of squares of first N natural numbers using python.

ALGORITHM:

Step 1. Start
Step 2. Read number num
Step 3. [Initialize]
sum=0, i=1
Step 4. Repeat step 4 through 6 until i<=num
Step 5. sum=sum+(i*i)
Step 6. i=i+1
Step 7. print the sum of square
Step 8. stop
[end of loop step 4]
[end of sum of square procedure]

FLOWCHART:
PROGRAM:

OUTPUT:

RESULT:

Thus the calculation of square root of numbers program has been written and executed
successfully.
Expt: 4
Date:
FIBONACCI SERIES

AIM:

To write a program to find the Fibonacci series of a number using python.

ALGORITHM:

Step 1: Start
Step 2:Declare variables i, a,b , show
Step 3:Initialize the variables, a=0, b=1, and show =0
Step 4:Enter the number of terms of Fibonacci series to be printed
Step 5:Print First two terms of series
Step 6:Use loop for the following steps
->show=a+b
->a=b
->b=show
->increase value of i each time by 1
-> print the value of show
Step 7:End

FLOWCHART:
PROGRAM:

OUTPUT:

RESULT:

Thus the Fibonacci series program is written and executed successfully.


Expt: 5
Date:
ARMSTRONG NUMBER

AIM:

To write a program to find the whether a number is a Armstrong or not by using python.

ALGORITHM:

Step 1: Start
Step 2: Declare Variable sum, temp, num
Step 3: Read num from User
Step 4: Initialize Variable sum=0 and temp=num
Step 5: Repeat Until num>=0
5.1 sum=sum + cube of last digit i.e [(num%10)*(num%10)*(num%10)]
5.2 num=num/10
Step 6: IF sum==temp
Print "Armstrong Number"
ELSE
Print "Not Armstrong Number"
Step 7: Stop
FLOWCHART

PROGRAM:
OUTPUT:

RESULT:

Thus the program for to find the Armstrong number is written and executed successfully
Expt: 6
Date:
SUM OF DIGITS IN A NUMBER

AIM:

Write a program to find a sum of digits in a number using python.

ALGORITHM:

Step 1: Input N
Step 2: Sum = 0
Step 3: While (N != 0)
Rem = N % 10;
Sum = Sum +
Rem; N = N / 10;
Step 4: Print Sum

FLOWCHART
PROGRAM:

OUTPUT:

RESULT:

Thus the program for sum of digits in a number is written and executed successfully.
Expt: 7
Date:
FIRST N PRIME NUMBER

AIM:

To write a program to find the first N Prime number using python.

ALGORITHM:

Step 1: Start the program. Step 2:


Read the number in a
Step 3: Assign k
=0
Step 4: for(i in range(2,a//2)):
if(a%i==0): k = k + 1 if(k
<= 0):
Step 5: Print the number is prime else not Prime
Step 6: Stop
FLOWCHART:

PROGRAM:
OUTPUT:

RESULT:

Thus the Program to find first N prime number is written and executed successfully.
Expt: 8
Date:
FACTORIAL OF A NUMBER USING RECURSION

AIM:

To write a program to find the factorial of a number using recursion in python.

ALGORITHM:

Step 1: Start the program.


Step 2: Set initial value to fact=1, i=1
Step 3: Read the value of n
Step 4: while (i<=n) then
Step 5: fact = fact * i
Step 6: i = i + 1
Step 7: Repeat the step 4,5,6 until the condition became false
Step 8: Print the value of fact
Step 9: Stop the program

FLOWCHART:
PROGRAM:

OUTPUT:

RESULT:

Thus the Program to find factorial of a number using recursion has been written and executed

successfully.
Expt:9
Date: COUNT THE NUMBER OF VOWELS IN A STRING

AIM:
To write a program to count the number of vowels in a string using python.
ALGORITHM:

Step 1: Create a set of vowels using set() and initialize a count variable to 0.
Step 2: Traverse through the alphabets in the string and check if the letter in the string is present in set
vowel.
Step 3: If it is present, the vowel count is incremented.

FLOWCHART:
PROGRAM:

OUTPUT:

RESULT:

Thus the Program to count the number of vowels in a string has been written and executed successfully.
Expt:10
Date:
MATRIX MULTIPLICATION

AIM:
To write a program to perform matrix multiplication using python.
T
o
ALGORITHM:
Step 1: Start the program.
Step 2: Initialize the list for matrix 1 & matrix 2 & rmatrix
Step 3: Set loop FOR loop for i, j, & k in range in range(0,len(matrix1)
Step 4: Compute rmatrix[i][j] += matrix1[i][k] * matrix2[k][j]
Step 5: Print resultant in r matrix
Step 6: Stop the program

FLOWCHART:
PROGRAM:

OUTPUT:

RESULT:

Thus the Program to find Matrix multiplication has been written and executed successfully.
Expt: 11
Date:

SIMPLE CALCULATOR
AIM:

Write a program to create a simple calculator by using python program.

ALGORITHM:
Step 1: Start
Step 2: Read Two Numbers
Step 3: Input Choice (1-Addition, 2-Subtraction, 3-Multiplication, 4-Division)
Step 4: if Choice equals 1
Calculate res = a + b
else If Choice equals 2
Calculate res = a - b
else if Choice equals 3
Calculate res = a * b
else if Choice equals 4
Calculate res = a / b
Step 5: Output Result, res
Step 6: End

FLOWCHART
PROGRAM:

OUTPUT:

RESULT:

Thus the program for simple calculator has been executed successfully.
Expt: 12
Date:
LINEAR SEARCH

AIM:

Write a program to search an element in a list using linear search concept in python.

ALGORITHM:

Step 1: Start the program. Step 2:


Initialize the list
Step 3: Read the search number in search
Step 4: Compare search==list[i] using loop until upper limit for i in
range(0,len(list)):if search==list[i]
Step 5: Print the position of the search number
. Step 6: Else print search number not found
Step 7: Stop the program

FLOWCHART:
PROGRAM:

OUTPUT:

RESULT:

Thus the program for linear search has been written and executed successfully,
Selection sort
Selection sort is one of the simplest sorting algorithms. It is similar to the hand picking where we take the
smallest element and put it in the first position and the second smallest at the second position and so on. It
is also similar. We first check for smallest element in the list and swap it with the first element of the list.
Again, we check for the smallest number in a sub list, excluding the first element of the list as it is where
it should be (at the first position) and put it in the second position of the list. We continue repeating this
process until the list gets sorted.

If there are n elements to be sorted then, the process mentioned above should be repeated n-1 times to get
required result. But, for better performance, in second step, comparison starts from second element
because after first step, the required number is automatically placed at the first (i.e, In case of sorting in
ascending order, smallest element will be at first and in case of sorting in descending order, largest
element will be at first.). Similarly, in third step, comparison starts from third element and so on.

The time complexity of selection sort is (O(n2)).

We follow the following steps to perform selection sort:

1. Start from the first element in the list and search for the smallest element in the list.
2. Swap the first element with the smallest element of the list.
3. Take a sub list (excluding the first element of the list as it is at its place) and search for the
smallest number in the sub list (second smallest number of the entire list) and swap it with
the first element of the list (second element of the entire list).
4. Repeat the steps 2 and 3 with new sub lists until the list gets sorted.
Working of selection sort:

Initial list

16 19 11 15 10 12 14

First iteration

16 19 11 15 10 12 14
10 19 11 15 16 12 14

Second iteration

10 19 11 15 16 12 14
10 11 19 15 16 12 14

Third iteration

10 11 19 15 16 12 14
10 11 12 15 16 19 14

Fourth iteration

10 11 12 15 16 19 14
10 11 12 14 16 19 15

Fifth iteration

10 11 12 14 16 19 15
10 11 12 14 15 19 16

Sixth iteration

10 11 12 14 15 19 16
10 11 12 14 15 16 19

Final list

10 11 12 14 15 16 19
Expt: 13 SELECTION SORT

Date:

AIM:
To sort the given list by selection sort algorithm using python.

ALGORITHM:
Step1: Start the Program.
Step2:Intialize the list to be sorted.
Step3:Initialize i as 0.
Step4:Check i with length of the list.
Step4.1:If true continue the iteration as assigning minimum of the list to smallest
Step4.2:Assign the index value to index of smallest
Step4.3:Swap the smallest number to the first position with compared number position.
Step4.4:Increment i value by 1
Step4.5:Continue the iteration till i less than the length of the list
Step5:Print the sorted list.
Step6:Stop the program.
FLOWCHART:
PROGRAM:

OUTPUT:

RESULT:
Thus the program for selection sort has been written and executed successfully.
Insertion Sort
Insertion sort is a simple Sorting algorithm which sorts the array by shifting elements one by
one.Insertion sort is an in-place comparison-based sorting algorithm. The lower part of an array is
maintained to be sorted. An element which is to be 'insert'ed in this sorted sub-list, has to find its
appropriate place and then it has to be inserted there. Hence the name, insertion sort.

The array is searched sequentially and unsorted items are moved and inserted into the sorted sub- list (in
the same array). This algorithm is not suitable for large data sets as its average and worst case complexity
are of Ο(n2), where n is the number of items.

Characteristics of Insertion sort:

 It has one of the simplest implementation


 It is efficient for smaller data sets, but very inefficient for larger lists.
 Insertion Sort is adaptive, that means it reduces its total number of steps if given
a partially sorted list, hence it increases its efficiency.
 It is better than Selection Sort and Bubble Sort algorithms.
 Its space complexity is less. Like Bubble Sorting, insertion sort also requires a single
additional memory space.
 It is a Stable sorting, as it does not change the relative order of elements with equal keys

Working Of Insertion sort:

Insertion sort compares the first two elements.

It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.

Insertion sort moves ahead and compares 33 with 27.


And finds that 33 is not in the correct position.

It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here we see that the sorted
sub-list has only one element 14, and 27 is greater than 14. Hence, the sorted sub-list remains sorted
after swapping.

By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10.

These values are not in a sorted order.

So we swap them.

However, swapping makes 27 and 10 unsorted.

Hence, we swap them too.


Again we find 14 and 10 in an unsorted order.

We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items.

This process goes on until all the unsorted values are covered in a sorted sub-list.
Expt: 14 INSERTION SORT

Date:

AIM:
To sort the given list by insertion sort algorithm using python.

ALGORITHM
Step 1: Start the program
Step 2: Initialize the list of elements to be sorted
Step 3: Define the function for sort
Step 4: Check the condition, index in range of length of the list
Step 4.1: If it is true iteration continues as assigning the list of index to the current value
Step 4.2: Index assigned as position
Step 4.3: Check the condition position greater than 0 and list of position-1 greater than current
value
Step 4.4:Assign list of position-1 as list of position
Step 4.5:Assign position-1 as position
Step 4.6:Assign current value as list of position
Step 4.7:Repeat until list is sorted.
Step 5:Print the list
Step 6:Stop the program.
FLOWCHART:
PROGRAM:

OUTPUT:

RESULT:
Thus the program for insertion sort has been written and executed successfully.
Expt: 15
Date:
WORD COUNT

AIM:

To write a program to count the word in the text file using python

ALGORITHM:

Step 1: Start the program.


Step 2: Open the file in read mode
Step 3: Read each line to calculate the word count.
for line in f:
words = line.split() num_words += len(words)
Step 4: Print the number of words Step 5:
Stop the program.

FLOWCHART:
PROGRAM:

OUTPUT:

RESULT:

Thus the program for word count in file has been executed successfully.

You might also like