Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Assignment-1

Subject: Python Programming(OE-II) Branch: MMT


Date of submission :1/11/21 Section: B.tech IVyr-Sem1
KARTHIK NAKKA 17261A1812

1. Illustrate the different types of control flow statements available in


Python with flowcharts.
A. Program’s control
flow is the order in which the program’s code executes. The control flow of a Python
program is regulated by conditional statements, loops, and function calls . Python
has three types of control structures:-
Sequential Statements - a set of statements whose execution process happens in a
sequence.
Selection Statements - The selection statement allows a program to test several
conditions and execute instructions based on which condition is true.
Repetition Statements - A repetition statement is used to repeat a group(block) of
programming instructions.

2. Describe the features of python.


A.
1. Easy Language
2. Readable
3. Interpreted language
4. Dynamically-typed language
5. Object-Oriented
6. Popular and Large Community Support
7. Free and Open-Source
8. Large Standard Library
9. Platform-Independent
10. Entensible and Embeddible
11. GUI Programming Support
3. Write a program to find the sum of all Odd and Even numbers up to a
number specified by the user.
A.
Program:-
n=int(input("Specify the number :"))
a=1;c=2;b=0;d=0;
while a<n+1:
b=b+a;
a=a+2;
while c<n+1:
d=d+c;
c=c+2;
print("Sum of odd numbers upto specified num is :",b)
print("Sum of even numbers upto specified num is :",d)
Output:-
Specify the number :5
Sum of odd numbers upto specified num is : 9
Sum of even numbers upto specified num is : 6

4.How does try-except statement work? Demonstrate with an example python code
A.
The python try-except statement helps handle exceptions, the try-except statement runs the
code under the ‘try’ statement . If this code doesn’t execute successfully, the program will
stop at the line that caused the error and the “except” code will run.
Ex: try:
print(variable)
except:
Print(‘error returned’)

5.Explain the following file built-in functions and method with clear syntax, description and
illustration: a) open( ) b) file( ) c) seek( ) d) tell( ) e)read( )
A.
a). open(): This built-in function returns a file object on a successful opening of the file.
Syntax :- file_object = open(file_name,access_mode=’r’,buffering=1)
The file_name is a string containing the name of the file to open.
The access_mode is not given , it defaults automatically to ‘r’.
The buffering is used to indicate the type of buffering that should be performed when
accessing the file.
b). file(): This built-in function came into later versions of python and can be used as a
substitute for the open() function as the functionality of both the functions are the same.
c). seek(): This function moves the file pointer to different positions within the file.
Syntax :- file.seek(off,whence)
off bytes offset from
whence beginning , current location or end of file
d). tell(): It is a complimentary method to seek()
Syntax :- file.tell()
e). read(): It is a file input method which is used to read bytes directly into a string, reading
at most number of times indicated.
Syntax :- file.read(number of times)

6.Compare List, tuple and Dictionary.


A. Lists provide sequential storage through an index offset and access to single or
consecutive elements through slices. Lists are flexible container objects which hold an
arbitrary number of python objects. Lists are mutable datatypes and can be converted into
tuples. Lists are enclosed in square brackets[].
Ex:- listname = [‘Karthik’,’17261A1812’,’Nakka’,26]
Tuples are another container type extremely similar in nature to lists, Tuples are also
flexible and can hold arbitrary number of python objects. Tuples can also be converted into
lists. Tuples are enclosed in parentheses().
Ex:- tuplename = (‘March’,2021,(’MGIT’,’MMT’),’Python_3.10’)
Dictionaries are the sole mapping type in python, mapping objects have a one-to-many
correspondence between hashable values (keys) and the objects they represent (values),
Dictionaries are mutable and is another container type which can store any number of python
objects including other container types. Dictionaries are enclosed in curly braces{}.
Ex:- dictionaryname = {‘Name’:’Karthik’,’RollNo’:’17261A1812’}

X X X

You might also like