simple python program with output, Exercises of Computer science

For high school projects. Here is a project on python with output.You can take it for a reference.

Typology: Exercises

2019/2020
On special offer
30 Points
Discount

Limited-time offer


Uploaded on 01/01/2020

poisonous-prince
poisonous-prince 🇮🇳

1 document

1 / 50

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CERTIFICATE
The project report entitled PYTHON TUTORIAL Submitted
by ASHWNAI SINGH of class XII Science for the CBSE Senior
Secondary Examination 2016-17, Class XII for Computer
Science at ………………………………………………………. has been
examined.
SIGNATURE OF EXAMINER
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
Discount

On special offer

Partial preview of the text

Download simple python program with output and more Exercises Computer science in PDF only on Docsity!

CERTIFICATE

The project report entitled “ PYTHON TUTORIAL ” Submitted

by ASHWNAI SINGH of class XII Science for the CBSE Senior

Secondary Examination 2016-17, Class XII for Computer

Science at ………………………………………………………. has been

examined.

SIGNATURE OF EXAMINER

D E C L A R AT I O N

I hereby declare that the project work entitled

“ PYTHON TUTORIAL ”, submitted to Department

of Computer Science,

……………………………………………. is prepared by me.

Class XII (Science)

CONTENTS

___________________________

  1. USER DEFINED MODULE FUNCTION
  2. TEXT FILE
  3. WORKING DESCRIPTION
  4. SOURCE CODE
  5. INPUT/OUTPUT INTERFACE
  6. BIBLIOGRAPHY

MODULE FUNCTIONS

# FUnctions
def l_s(ar,item):
i=
while i < len(ar) and ar[i]!=item :
i+=
if i< len(ar):
return i
else:
return False
def b_s(ar,item):
beg=
last=len(ar)-
while(beg<=last):
mid= (beg+last)/
if (item == ar[mid]):
return mid
elif (item >ar[mid]):
beg=mid+
i=i-
def s_sort(liste):
for curpos in range(len(liste)):
minpos=curpos #starting with current position
for scanpos in range(curpos+1,len(liste)):
if liste[scanpos]list[i+1]):
temp=list[i]
list[i]=list[i+1]
list[i+1]=temp
i=i+
#print " List after pass",(i), ":",list
def b_sort(list):
for num in range(len(list)-1):
pass
swapelements(list)
def i_sort(ar):
for i in range(1,len(ar)):
v=ar[i]
j=i
while ar[j-1]> v and j>=1:
ar[j]=ar[j-1]
j-=
#Insert the value at its correct postion
ar[j]=v
def traverse(ar):
size =len(ar)
for i in range(size):
print ar[i],
def isempty(stk):
if stk==[]:
return True
else:
return False
def push(stk,item):
stk.append(item)
top=len(stk)-
def pop(stk):
def cls(n):
print " "*n
def qu_isempty(qu):
if qu==[]:
return True
else:
return False
def qu_enqueue(qu,item):
qu.append(item)
if len(qu)==1:
front=rear=
else:
rear=len(qu)-
def qu_peek(qu):
if qu_isempty(qu):
return "Underflow"
else:
front=
return qu[front]
def qu_dequeue(qu):
if qu_isempty(qu):
return "Underflow"
else:
item=qu.pop(0)
if len(qu)==0: #if it was single element queue
front=rear=None
return item
def qu_display(au):
if qu_isempty(qu):
print" QueUE Empty"
elif len(qu)==1:
print [qu],"<== front,rear"
else:
front=
rear=len(qu)-
print qu[front],"<- front"
for a in range(1,rear):
print qu[a]
print qu[rear],"<-rear"
def s():
print "---------------------------------------------------------------"

____________________Some Exceptions______________________ EXCEPTION NAME DESCRIPTION OverflowError() Raised when a calculation exceeds maximum limit for a numeric type. 0ZeroDivisonError() Raised when division or modulo by zero takes place for all numeric types. EOFError() Raised when there is no input from either the raw_input() or input() function and the end of file is reached. ImportError() Raised when an import statement fails. IndexError() Raised when an index is not found in a sequence. NameError() Raised when an identifier is not found in the local or global namespace. IOError() Raised when an input/ output operation fails, such as the print statement or the open() function when trying to open a file that does not exist. TypeError() Raised when an operation or function is attempted that is invalid for the specified data type. ValueError() Raised when the built-in function for a data type has the valid type of arguments, but the arguments have invalid values specified. ______________________Generators______________________ A generator is simply a function which returns an object on which you can call next, such that for every call it returns some value, until it raises a StopIteration exception, signaling that all values have been generated. Such an object is called an iterator. Normal functions return a single value using return, just like in Java. In Python, however, there is an alternative, called yield. Using yield anywhere in a function makes it a generator

EXAMPLE:

>>> def myGen(n): ... yield n ... yield n + 1 ... >>> g = myGen(6) >>> next(g) 6 >>> next(g) 7

2. R_W.txt

__________________Opening and Closing____________________ The first thing to do when you are working with files in Python isto open thefile. When you open the files, you can specify with parameters how you want to open them. The "r" is for reading, the "w" for writing and the "a" for appending. This opens the filename for reading. By default, the file is opened with the "r" parameter. fh = open("filename_here", "r") This opens the fhe file for writing. It will create the file if it doesn't exist, and if it does, it will overwrite it. fh =open("filename_here", "w") This opens the fhe file in appending mode. That means, it will be open for writing and everything will be written to the end of the file.

file.The readlines function reads all rows and retains the newlines character that is at the end of every row. eg. fh = open("filename", "r") content = fh.readlines() print content.rstrip() print content[:-1]

Writting from files

The functions for writing are write and writelines ---Write--- To write a fixed sequence of characters to a file eg. fh = open("hello.txt","w") write("Hello World") ----Writeline---- With the writeline function you can write a list of strings to a file eg. fh = open("hello.txt", "w") lines_of_text = ["a line of text", "another line of text", "a third line"] fh.writelines(lines_of_text)

3. M_FUN.txt

_________________Function defined in module___________________ A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference. Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code. Example: The Python code for a module named aname normally resides in a file named aname.py. Here's an example of a simple module, support.py def print_func( par ): print "Hello : ", par return The import Statement You can use any Python source file as a module by executing an import statement in some other Python source file. The import has the following syntax: import module1[, module2[,... moduleN] When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches before importing a module. For example, to import the module hello.py, you need to put the following command at the top of the script -

Import module support

>>>import support

Return a copy of s, but with upper case letters converted to lower case. 6.string.find(s, sub[, start[, end]]): Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end]. Return -1 on failure. Defaults for start and end and interpretation of negative values is the same as for slices. 7.string.islower(): This method checks if the string is in lowercase and returns true if all the characters are in lowercase. 8.string.isupper(): This method checks if all the characters in the string are in uppercase. If any character is in lower case, it would return false otherwise true.

5. D_T.txt

____________________DATA STRUCTURES____________________

Python offers 5 different types of data structure.

  1. ARRAY: Array refers to a named list of finite number n of similar data elements. Each of the data elements can be referenced respectively by a set of consecutive numbers, usually 0,1,2,3,4... n. e.g. A array ar containing 10 elements will be referenced as ar[0] , ar[1] , ar[2]... ar[9]
  2. STACKS: Stacks data structure refer to list stored and accessed in a special way, where LIFO(Last In First Out) technique is followed. In stack insertion and deletion take place at only one end called the top.
3. QUEUES:

Queues data structure are FIFO(First In First Out ) lists , where take place at "rear" end of queue deletions take place at the "front" end of the queue.

  1. LINKED LIST: Linked lists are special list of some data elements linked to one another. The logical ordering is represented by having each element pointing to next element. Each element is called a 'node' which has the parts. The INFO part which stores the information and the reference pointer part i.e stores reference of next element. 5.Trees: Trees are multilevel data structures having a hierarchical relationship amongst its element called 'node'. Topmost node is called node of the tree and bottom most node of tree is called leaves of tree. Each node have some reference pointers pointing to node below it.

6. ABOUT.txt

_______________________ABOUT___________________________

This project has been created to fulfill the requirement of the CBSE Senior Secondary Examination Class XII for Computer science. This project is been created by Ashwani singh of class XII under the guidance of Mr. Jaideep sir .This project is created to teach a new beginner how to code with python. WORKING DESCRIPTION