100% found this document useful (1 vote)
70 views35 pages

Case Study New 1

The document provides details about Python including its history, features, why it is useful, applications, variables and data types. Python is an interpreted, object-oriented scripting language created by Guido Van Rossum in 1989. It has an easy to read syntax and supports features like OOP, functional programming, dynamic typing and automatic memory management. The document discusses Python's portability, scalability, extensive standard library and ease of use which make it suitable for web development, scientific computing, data analysis and more. It also describes how variables work in Python and different data types that can be assigned to variables.

Uploaded by

Neha sherin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
70 views35 pages

Case Study New 1

The document provides details about Python including its history, features, why it is useful, applications, variables and data types. Python is an interpreted, object-oriented scripting language created by Guido Van Rossum in 1989. It has an easy to read syntax and supports features like OOP, functional programming, dynamic typing and automatic memory management. The document discusses Python's portability, scalability, extensive standard library and ease of use which make it suitable for web development, scientific computing, data analysis and more. It also describes how variables work in Python and different data types that can be assigned to variables.

Uploaded by

Neha sherin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CASE PYHTON

STUDY

ACKNOWLEDGMENT
I consider it is a great privilege to be express my heartfelt gratitude to many

respected personalities who guided, inspired and helped as in successful completion of this

case study. First and for most I would like to express my deepest gratitude to

[Link].K.V(Co-ordinator, Department of MSc Computer Science) for initiating me into

this project for her valuable guidance and suggestions.

I also thanks [Link] Muhammed Fazil, [Link].k.p and Mrs. Ashifa.p,assistant


Professors of Departement of MSC Computer Science,who was my case study [Link] his
constant encouragement in doing this case study for guiding in this endeavor and for their
valuable suggestion in the preparation of the case [Link] addition I would like to thank all
my friends of 2nd semester MSC Computer Science for their suggestions and constructive
criticism.

1
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

INDEX

[Link] CONTENTS PAGE NO.


1 INTRODUCTION 3

2 FEATURES OF PYTHON 4

3 WHY PYTHON 5

4 APPLICATIONS OF PYTHON 6

4 PYTHON-VARIABLES 7-8

5 DATA TYPES 9-11

6 BUILT-IN FUNCTIONS 12

7 SAMPLE PROGRAMS 13-23

2
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

INTRODUCTION
Python is a high-level, interpreted, interactive and object-oriented scripting language which has
easy syntax and dynamic semantics. Python is much easier than other programming
languages and helps you to create beautiful application with less effort and much more ease. Python
is created by Guido Rossum in 1989. Python is designed to be highly readable. It uses English
keywords frequently whereas the other languages use punctuations. It has fewer syntactical
constructions than other languages.

Python is Interpreted: Python is processed at runtime by the interpreter. You do notneed to


compile your program before executing it. This is similar to PERL and PHP.
Python is Interactive: You can actually sit at a Python prompt and interact with theinterpreter
directly to write your programs.
Python is Object-Oriented: Python supports Object-Oriented style or technique of
programming that encapsulates code within objects.
Python is a Beginner's Language: Python is a great language for the beginner levelprogrammers
and supports the development of a wide range of applications from simple text processing to
WWW browsers to games.
Python is widely used in Artificial Intelligence, Natural Language Creation, Neural
Networks and other advanced fields of Computer Science. Python had deep focus on code
readability.

3
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

FEATURES OF PYTHON
Python's features include-
Easy-to-learn: Python has few keywords, simple structure, and a clearly defined syntax.
This allows a 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 cross platform
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 sameinterface 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 ot 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 shellscripting.
Apart from the above-mentioned features, Python has a big list of good features. A few are listed
below-
It supports functional and structured programming methods as well as OOP.
It can be used as a scripting language or can be compiled to byte-code for building large
applications.
It provides very high-level dynamic data types and supports dynamic type checking.
It supports automatic garbage collection.
It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.

4
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

WHY PYTHON
You would wonder why use python always? Let me help you understand using the keywords
in the formal definition of Python.
High Level
Python derives components from the natural language that humans use to
communicate with each other. This makes it easier for anyone to try and relate what exactly could be
happening without the burden of going through tons of machine code.

Interpreted
Python codes are compiled line-by-line which makes debugging errors much easier and
efficient. But this comes at a cost as it is much slower than other programming languages.

Easy Syntax
Python makes use of indentations instead of braces to distinguish what blocks of code come
under which class or function. This makes the code look well distributed and makes it easy for
anyone to read it.

Dynamic Semantics
If you are on old school coder, you would know that before using anything, you would need to

initialize it. It does all of this dynamically.

5
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

APPLICATIONS OFPYTHON

1) WEB APPLICATIONS
We can use Python to develop web applications.
It provides libraries to handle internet protocols such as HTML and XML, JSON, Email
processing, request etc.
Some important developments are: PythonWikiEngines, Pocoo,
PythonBlogSoftware

2) SOFTWARE DEVELOPMENT

Python is helpful for software development process.


It works as a support language and can be used for build control and management, testing etc.

3) SCIENTIFIC AND NUMERIC

Python is popular and widely used in scientific and numeric computing.


Some useful library and package are SciPy, Pandas, IPython etc.
SciPy is group of packages of engineering, science and mathematics.

4) AUDIO OR VIDEO BASED APPLICATIONS


Python is awesome to perform multiple tasks and can be used to develop multimedia
applications.
Some of real applications are: TimPlayer, cplay etc.

5) 3D CAD APPLICATIONS
To create CAD application Fandango is a real application which provides full features
of CAD.

6
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

PYHTON-VARIABLES
Variables are nothing but reserved memory locations to store values. It means that when you
create a variable, you reserve some space in the memory.

Based on the data type of a variable, the interpreter allocates memory and decides what can be
stored in the reserved memory. Therefore, by assigning different data types to the variables, you can
store integers, decimals or characters in these variables.

Assigning Values to Variables

Python variables do not need explicit declaration to reserve memory space.


The declaration happens automatically when you assign a value to a variable. The
equal sign (=) is used to assign values to variables.

The operand to the left of the = operator is the name of the variable and the
operand to the right of the = operator is the value stored in the variable. For example-

counter = 100 # An integer assignment

name = "John" # A string

print (counter) print

(name)

Here, 100, 1000.0 and "John" are the values assigned to counter, miles, and
name variables, respectively. This produces the following result −

100

John

7
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

Multiple Assignment

Multiple Assignment Python allows you to assign a single value to several


variables simultaneously.

For example-

a=b=c=1

Here, an integer object is created with the value 1, and all the three variables
are assigned to the same memory location. You can also assign multiple objects to
multiple variables.

For example-

a, b, c = 1, 2, "john"

Here, two integer objects with values 1 and 2 are assigned to the variables a and b
respectively, and one string object with the value "john" is assigned to the variable c.

8
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

PYTHON DATA TYPES


Data types are the classification or categorization of data items .It represents the kind of value that
tells what operation can be performed on a particular data. Since everything is an object in Python
programming, data types are actually classes and variables are instance of these classes.
Following are the standard or built-in data type of Python:
1. Numeric
2. Sequence Type
3. Boolean
4. Set
5. Dictionary

1) NUMERIC
In Python ,numeric data type represent the data which has numeric value. Numeric
value can be integer, floating number or even complex numbers. These values are defined as int, float
and complex class in Python.
Integers : This value is represented by int class. It contains positive or negative whole numbers
without fraction or decimal. In Python there is no limit to how long an integer value can be.
Float : This value is represented by float class. It is a real number with floating point
representation. It is specified by a decimal point. Optionally the character e or E followed by
a positive or negative integer may be appended to specify scientific notation.
Complex number is represented by complex class. It is specified as (real part) +(imaginary
part).

2) SEQUENCE TYPE
Python, sequence is the ordered collection of similar or different data types. Sequence
allows to store multiple values in an organized and efficient fashion. There are several sequence
types in Python.
String

9
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

List
Tuple

STRING
In Python, Strings are arrays of bytes representing Unicode characters. A string is a collection
of one or more characters put in a single quote,double quote or triple quote. In Python there is no
character data type, a character is a string of length one. It is represented by str class. In Python,
updation or deletion of characters from a string is not allowed. This will cause an error because item
assignment or item deletion from a string is not supported. Only new strings can be reassigned to
the same name.

LIST
Lists are just like the arrays, declared in other languages. Lists need not be
homogeneous always which makes it the most powerful tool in Python. A single list may contain data
types like Integers, Strings as well as [Link] are mutable and hence they can be altered even after
their creation. List in Python are ordered and have a definite count. The elements in a list are indexed
according to a definite sequence and the indexing of a list is done with 0 being the first index. Each
element in the list has its definite place in the list, which allows duplicating of elements in the list, with
each element having its own distinct place and credibility. It is represented by list class. List in
Python can be created by just placing the sequence inside the square brackets ([ ]).

TUPLE
Tuple is an ordered collection of Python objects much like a list. The sequence of values
stored in a tuple can be of any type and they are indexed by integers. The important difference between a
list and a tuple is that tuples are immutable. Also, Tuples are hashable whereas lists are not. It is
represented by tuple class.
In Python, tuples are created by placing sequence of values separated by ‘comma’ with or
without the use of parentheses for grouping of data sequence. Tuples can contain any number of
elements and of any data types.

10
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

3) BOOLEAN
Data type with one of the two built-in values, True or False. Boolean objects that are equal to
True are truthy and those equal to false are falsy. But non-boolean objects can be evaluated in
Boolean context as well and determined to be true or false. It is denoted by class bool. True and
False with capital ‘T’ and ‘F’ are valid Booleans. Otherwise Python will throw an error.

4) SET
In Python, Set is an unordered collection of data type that is iterable, mutable and has no
duplicate elements. The order of elements in a set is undefined though it may consist of various elements.
The major advantage of using set,as opposed to a list,is that it has highly optimized method for checking
whether a specific element is contained in the set.
Set can be created by using the built-in set() function with an iterable object or a sequence
by placing the sequence inside curly braces separated by comma.

5) DICTIONARY
Dictionary in Python is an unordered collection of data values,used to store data values like
a map, which unlike other data types that hold only single value as an element. Dicionary holds
key:value pair.

11
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

PYTHON BUILT-IN FUNCTIONS


The Python interpreter has a number of functions that are always available for use. These
functions are called built-in functions. For example, print() function prints the given object to the
standard output device (screen) or to the text stream file. Some of the important built-in functions are
given below:

METHOD DESCIPTION
Python abs() Returns absolute value of a number
Python all() Returns true when all elements in iterable is true
Python ascii() Returns String Containing Printable Representation
Python bin() Converts integer to binary string
Python bool() Converts a Value to Boolean
Python bytes() Returns immutable bytes object
Python chr() Returns a Character (a string) from an Integer
Python float() Returns floating point number from number, string
Python format() Returns formatted representation of a value
Python input() Reads and returns a line of string
Python int() Returns integer from a number or string
Python len() Returns Length of an Object
Python list() Creates a list in Python
Python map() Applies Function and Returns a List
Python max() Returns the largest item
Python min() Returns the smallest value
Python next() Retrieves next item from the iterator
Python print() Prints the Given Object
Python reversed() Returns the reversed iterator of a sequence
Python set() Constructs and returns a set
Python sum() Adds items of an Iterable
Python tuple() Returns a tuple

12
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

SAMPLE PROGRAMS

13
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

PROGRAM NO:1
FIND THE SUM OF ALL NUMBERS BELOW
1000 WHICH ARE MULTIPLES OF 3 OR 5

n=1000
s=0
for k in range(1,n+1):
if k%3==0 ork%5==0:
s+=k
print(‘The sum of the number is:’,s)

OUTPUT
The sum of the number: 234168

14
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

PROGRAM NO:2

Program to find the execution time of a


program
N= int( input (“Enter the value of N: “))
t_start=[Link]()
s=[Link](N)
print (“factorial of the number :”,s)
t_end=[Link]()
e=t_end_t_start
print (“The execution time for factorial program:”,e)

OUTPUT

Enter the value of N: 25


Factorial of the number :15511210043330985984000000
The execution time for factorial program: [Link].000022

15
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

PROGRAM NO:3
Program to calculate currency notes
notes = (2000,500,,200,100,50,20,10,,5,2,1)

amount = int(input(“Enter Amount to be paid : “))

for C in notes:
count = amount//C
print(“Note Value : “, C,’\tnumber of notes ‘,count)
amount = amount%C

OUTPUT

Enter Amount to be paid : 34521

16
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

PROGRAM NO:4
Program to find floor division
a = 10
b=3

result1 = a/b
print(“a/b = “, result1)

resul2 = a//b
print(“a/b = “, result2)

OUTPUT

a/b = 3.333333333333335
a/b = 3

17
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

PROGRAMNO:5
Program to find date is valid or not
import datetime
d,m,y=map(int,input(“Enter date:
“).split()”
try:
s=[Link](y,m,d)
print(“Date is valid.”)
except ValueError:
print(“Date is invalid.”)
OUTPUT

Run 1:
Enter date: 10 10 2010
Date is valid.

Run 2:
Enter date: 30 2 2019
Date is invalid.

18
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

PROGRAM NO:6

Python program for logical errors

num1 = int(Input(“Enter number 1 : “))

num2 = int(input(“Enter number 2 :”))

sumValue = (num1 – num2)

print(“The sum of given numbers is

“,sumValue)

OUTPUT

Enter number 1 : 765


Enter number 2 : 23

The sum of given numbers is 742

19
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

PROGRAM NO:7

Create empty dictionary

dict_a = { }

print(“dict_a :”,dict_a)
print(“Total elements: “, len(dict_a))

OUTPUT

dict_a : { }
Total elements: 0

20
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

PROGRAM NO:8
Swapping the values of two integers
X = float(input(‘Enter the value of X: ‘))

Y = float(input(‘Enter the value of Y: ‘))

Temp = x

X=y

Y = temp

Print(‘X :’ , x,’ Y :’,y)

OUTPUT

Enter the value of X: 10


Enter the value of Y: 20

X : 20.0 Y :10.0

21
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

PROGRAM NO:9
Program for Insertion Sort

import sys

def insertion_sort(arr):

n=len(arr)

for i in range(1, n):

key = arr[i]

j = i-1

while j >= 0 and key <


arr[j]:

arr[j+1] = key

return arr

if
__name__==’__main_
_’:

arr =
[24,17,66,33,72,47,68,
41,105,30]

print(“Sorted array: “)

22
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

OUTPUT

Sorted array :

[ 17,24,30,33,41,47,66,68,72,105]

23
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

PROGRAM NO:10
Program to print version information
import sys

print(“Python version: “,
[Link])

print(“python version info:


“, sys.version_info)

print(“Python version
info[0]: “,
sys.version_info[0])

print(“Python version
info[0]: “,
sys.version_info[1])

print(“Python version
info[0]: “,
sys.version_info[2])

print(“Python version
info[0]: “,
sys.version_info[3])

print(“Python version
info[0]: “,
sys.version_info[4])

24
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

OUTPUT
Python version: 3.81 (default, Feb 2 2020, [Link])
[GCC 8.3.0]
Python version info: sys.version_info(major=3, minor=8, micro=1,
Releaslevel=’final’, serial=0)
Python version info[0]: 3
Python version info[1]: 8
Python version info[2]: 1
Python version info[3]: final
Python version info[4]: 0

25
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

PROGRAM NO:11
Declare any variable without assigning any value
num = None

print “value of num: “,num

if (num==None):
print “Nothing”
else:

print “Something”

num = 100

print “value of num: “, num

if (num==None):
print “Nothing”

else:
print “Something"

26
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY
OUTPUT
Value of num: None
Nothing
Value of num: 100
Something

27
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY
PROGRAM NO:12

Program to push three items into the heap and print the
items from the heap

Import heapq
Heap = [ ]
[Link](heap, (‘V’,1))
[Link](heap, (‘V’,2))
[Link](heap, (‘V’,3))
For a in heap:
Print(a)

28
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

OUTPUT
(‘V’, 1)
(‘V’, 2)
(‘V’, 3)

29
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

PROGRAM NO:13
Program to convert Binary to Gray Code
def binary_to_gray(n):
“ “ “Convert Binary to Gray codeword and return it.” “ “
n = int(n,2)
n ^= (n>>1)

return bin(n)[2:]

g = input(‘Enter binary numer: ‘)


b = binary_to_gray(g)
print(‘Gray codeword: ‘ , b)

30
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

OUTPUT
Case 1 :
Enter binary number: 110
Gray codeword: 101

Case 2 :
Enter binary number:1001
Gray codeword: 1101

31
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

PROGRAM NO:14

Program to clone or copy list

myList = [ ]
length = int(input(“Enter number of elements : “))
for i in range(0,length):
value = int(input())
[Link](value)

copyList = [Link]()

print(“Entered List “, myList)


print(“Cloned List “, copylist)

32
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY

OUTPUT

Enter number of elements : 5


10
20
30
40
50
Entered List [10,20,30,40,50]
Cloned List [10,20,30,40,50]

33
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY
PROGRAM NO:15

Program to create Stopwatch


import time

print(“Press Enter to start the stopwatch”)

print(“and, press CTRL + C to stop the stopwatch”)

while True:

try:

input()

start_time = [Link]()

print(“Stopwach started….”)

except KeyboardInterrupt:

print(:Stopwatch stopped….”)

end_time = [Link]()

print(“The total time:” , round(end_time, 2),”seconds”)

break

34
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY
CASE PYHTON
STUDY
Output
Press Enter to start the stopwatch

And,press CTRL + C to stop the stopwatch

Stopwatch started…..

^CStopwatch stopped…..

The total time: 15.81 seconds

35
MSC COMPUTER SCIENCE N A M COLLEGE KALLIKKANDY

You might also like