Sie sind auf Seite 1von 23

GE8161 PSPP

UNIT2

PART – A

1. Define keywords? List any 4 keywords.

 Keyword are also reserved words that has particular meaning and usage in the language.

 User cannot edit the meaning or usage of the keywords he can only able to use them.

 Keywords cannot be used as a identifier or variable name.

 Following table contains the keywords of the python.

False class finally is return


None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass return
break except in raise

2. What are variables? How to declare the same?

 A variable is a memory location where a programmer can store a value.


 Example: roll_no, amount, name etc.
 Value is either string, numeric etc. Example : "Sara", 120, 25.36
 Variables are created when first assigned.
 Variables must be assigned before being referenced.
 The value stored in a variable can be accessed or updated later.
 No prior declaration required.
 The type (string, int, float etc.) of the variable is determined by Python.
 The interpreter allocates memory on the basis of the data type of a variable.

3. What are the rules for declaring variables?

 Must begin with a letter (a - z, A - B) or underscore (_)


 Other characters can be letters, numbers or _

R. VISWANATH, AP / IT VER 1.0 Page | 1


GE8161 PSPP

 Not a Case Sensitive


 Can be any (reasonable) length
 There are some reserved words which you cannot use as a variable name because Python
uses them for other things.

4. Explain various types of operators available in python.

 Arithmetic Operators
 Comparison Operators
 Logical Operators
 Assignment Operators
 Bitwise Operator
 Conditional Operators or ternary operator
 Membership operators
 Lambda operator
 Identity operator

5. What is named constants? Explain its benefits.

 It is an identifier that represents a permanent value.


 Ex: PI = 3.14
 In maths we use symbol PI value for that PI don’t know by the program language so we
can initialize the PI as named constants.

Benefits of named Constants:

 You don’t have to repeatedly type the same value if its used multiple times in program
 If you want to change the value from 3.14 to 3.14159 then we need to change in only one
place. Its very useful & time saving.
 Descriptive names makes the program more easy to read.

6. Write classifications of python data types with example

R. VISWANATH, AP / IT VER 1.0 Page | 2


GE8161 PSPP

Data types

Numbers None Mapping Sequence

Int
List
Dictionary
Float
Tuple
Complex
String
Bool

7. What is difference between mutable and immutable objects?

Mutable objects:

 These are the objects whose values can be able to change at any time during program
execution after it assigned to variable.
 Examples: list, tuples

Immutable Objects:

 These are the objects whose values cannot be able to change at any time during program
execution after it assigned to variable.
 Example: string,dictionary

8. Define function. State advantages of the same.

 Function is a set of instruction that are used to perform some task (or) process which
frequently occur in main program.
 Functions is a sub-program that contains one or more statements and it performs some task
when called.
 Types:

1. User-defined functions 2. Library functions / PREDEFINED function.

R. VISWANATH, AP / IT VER 1.0 Page | 3


GE8161 PSPP

9. Define module in python

 Modules refer to a file containing Python statements and definitions.


 A file containing Python code, for e.g.: example.py, is called a module and its module name
would be example.
 We use modules to break down large programs into small manageable and organized files.
Furthermore, modules provide reusability of code.
 We can define our most used functions in a module and import it, instead of copying their
definitions into different programs.

10. Write difference between actual and formal parameter

Actual parameters:

 When a function is invoked, you pass a value to the parameter. This value is referred to as
actual parameter or argument.
 In the above example: x, y are actual parameters

Formal parameters:

 The variables defined in the function header are known as formal parameters.
 In the above example: num1, num2 are formal parameters

11. Write a program to calculate area & perimeter of circle using functions.

12. Write a program to swap the content of two variables with using third variable or without
using third variable

R. VISWANATH, AP / IT VER 1.0 Page | 4


GE8161 PSPP

 Please refer ILLUSTRATIVE PROGRAM NO: 1

13. Write a program to check given year is leap year or not

 Please refer ILLUSTRATIVE PROGRAM NO: 2

14. Write a program for simple calculator perform add, sub, mul, product.

15. Write difference between the symbol / and // in python

/ Division x/y Quotient of x and y.


Floor The division of operands where the result is the quotient in
// x// y
Division which the digits after the decimal point are removed.

R. VISWANATH, AP / IT VER 1.0 Page | 5


GE8161 PSPP

16. What is meant by token? Name token available n python

Def: The smallest individual unit element in the program is known as token or lexical unit.

 Python has following tokens:

1. Keywords 4. Operators

2. Identifiers 5. Punctuators

3. Literals 6. Special characters

17. What is the difference between pure arithmetic and mixed arithmetic expression?

 Pure arithmetic expression which has value of pure integer or floating point or complex
numbers
 Mixed expression which is combination of all integer, float, and complex numbers.

PART – B

1. Explain various datatypes available in python with example.


2. Define function and how to declare and use it
3. Explain various function prototypes available in python
4. Explain function with default argument with example
5. Explain various types of operators available.
6. Write a program:
a. Quadratic equation
b. Distance between two points
7. What are variables how to declare and write rules for declaring the variables?
8. Explain briefly about modules in python

R. VISWANATH, AP / IT VER 1.0 Page | 6


GE8161 PSPP

UNIT 3

PART A

1. Write various conditional statements available in python.

CONTROL STRUCTURES

CONDITIONAL STATEMENT UN CONDITIONAL STATEMENT

SELECTION / BRANCHING / LOOPING / ITERATIVE BREAK


DECISION MAKING STATEMENTS STATEMENTS

CONTINUE
IF FOR

IF ELSE PASS
WHILE

IF ELIF ELSE

2. Write difference between break and continue

BREAK CONTINUE
It takes the control to the outside of the It takes the control to the beginning of the
loop loop
Keyword: break Keyword: continue
Mainly used inside loops Mainly used inside loops

3. Write various selection statements in python

1. if
2. ifelse
3. ifelif..else

4. Write various looping statements in python

R. VISWANATH, AP / IT VER 1.0 Page | 7


GE8161 PSPP

1. for
2. while
3. for / while with else

5. Define string and how to declare it.

 String is a sequence of characters. Python treats almost all data as string.


 String may contain alphabets, numbers, and special characters.
 Usually sting value must be enclosed with in either single quote or within double quote.
 But combination not allowed for single value.
 String is immutable in nature i.e once created cannot able to edit when its stored in
variable.
 One string variable can be assigned to another string variable.
 Some valid string declarations are:

letter = "A" letter = 'a'


name = "python" name ='python'

6. Explain how slicing is made on string with syntax

 Slicing operation is used to return / select / slice the particular substring based on
user requirements.
 A segment of the string is called slice.
 Syntax: string_variable_name [start:end:step value]
 Here all the 3 parameters / variables are optional. i.e user may or may not mention
 The default value for start is zero
 The default value for end is -1(minus 1)
 The default value for step is +! Or simply 1(one)
 End value is taken as end_Value = end_Value -1

 You might confused by seeing output as “Welc” by not including “o”

R. VISWANATH, AP / IT VER 1.0 Page | 8


GE8161 PSPP

 Because as we said start value index is zero(0) and start from there print 4 word
continuously. So only “Welc”

7. Is string is mutable or immutable?

 We know that string is immutable in nature. That is we cannot edit the string value
once its assigned to variable.
 If we try to assign or replace a value in the stringits show error that str is not
mutable object.

8. Write various string function available

 String functions and method contain all above seen string functions such as:

1. capitalize 8. isupper()
2. find 9. string.lower()
3. isalnum() 10. string.upper()
4. isalpha() 11. string.lstrip()
5. isdigit() 12. string.rstrip()
6. isspace() 13. string.strip()
7. islower()

R. VISWANATH, AP / IT VER 1.0 Page | 9


GE8161 PSPP

9. Write program for finding square root of number

Please refer illustrative program: 1

10. Write program for GCD of two numbers

Please refer illustrative program: 2

11. Define fruitful functions

 Python functions is said to be fruitful function if it return any value to the calling function.
 A function that don’t return any value is called void function.

12. Explain difference between for and while loop

For While
Syntax: Syntax:
for var_name in While(condition):
range(start:end:stepvalue): #body
#for body
It is count controlled loop It is condition control loop
It is entry controlled loop It is entry controlled loop

13. Explain while loop with else block and its use

 There is a structural similarity between while and else statement. Both have a block of
statement(s) which is only executed when the condition is true.
 The difference is that block belongs to if statement executes once whereas block belongs to
while statement executes repeatedly.
 You can attach an optional else clause with while statement, in this case, syntax will be -

while (expression) :
statement_1
statement_2
......
else :
statement_3
statement_4
......

R. VISWANATH, AP / IT VER 1.0 P a g e | 10


GE8161 PSPP

14. Write difference between counter control and sequential control looping

Counter controlled: loop is the one who’s end value / count known before beginning
of the program / loop. Ex: for loop

Sequential controlled: loop is the one who’s end value / count not known before
beginning of the program / loop. It is decided while execution. Ex: while loop

15. Write syntax for i) for loop 2) while loop

For while
Syntax: Syntax:
for variable_name in range(initial,
final, step value): while (expression) :
statement_1 statement_1
statement_2 statement_2

PART B

1. Explain various selection / branching / conditional statements with example


2. Explain various looping statements available in python
3. What are fruitful function explain with return values and parameter.
4. Explain various scope for variable available in python
5. Explain in detail about recursion with example
6. Define string & explain how to declare and access the string
7. Explain various string functions and method available in python
8. Explain break, continue, pass statements in python
9. Explain various types of operators available in python
10. How to declare the list as array with example

R. VISWANATH, AP / IT VER 1.0 P a g e | 11


GE8161 PSPP

UNIT 4

PART A

1. Define list and write syntax

 List is a structure datatype of python that is collection of sequence of values


belonging of any type.
 List we depicted through source structure [].
 List are mutable: (i.e.) modifiable: you can change the elements of list at any time
you want.
 To create a list, put a number of expression, separated by comma in a square bracket.
 Syntax: var_name=[element1,element2,…..element n].

Ex: list1=[] #empty list


List2=[1,2,3] #list with values of same type
List3=[1,”a”,2,”s”,3,”red”,4,”blue”] #list with values of different types.

Ex2:creating list with for loop:


list=[]
Print(“Enter 10 no:”)
for i in range (10):
list.append(eval(input())

2. How to slice the list

 List slice is same of string slice. From the list slice we can extract sub part of list
based on user requirements.
 We can use Index values to create a sublist from list.
 Syntax:
var_name=list_name[start value : end value: increment/decrement step value]

R. VISWANATH, AP / IT VER 1.0 P a g e | 12


GE8161 PSPP

 Here starting and ending value may be omitted or optional.


 Default start value is: zero
 Default last value is : last index. (here it is 11)
 Default step value is: +1 or simply 1.

3. Write various list methods available

 Python list provides many built-in function and methods for list manipulation.
 These can be applied to list as per below syntax:

Syntax: <listobject> . <methodname>()


 The list has various list class methods. They are

1. append 8. reverse
2. count 9. sort
3. extend 10. del
4. index 11. len
5. insert 12. min
6. pop 13. max
7. remote 14. sum.

R. VISWANATH, AP / IT VER 1.0 P a g e | 13


GE8161 PSPP

4. Define tuples and write syntax

Definition:

 Tuples are like list , but their elements are fixed. Once you created a tuple, you
cannot add /delete/reorder/replace an element in tuple.
 Tuple is immutable in nature.
 Tuples can be define elements separated by comma within parenthesis.
 Syntax: var_name=(element1,element2,….elementn)
 Ex: tuple_values=(1,2,3,4,5,6,,7,8,9,10)
Creating empty tuple :

>>>tuple1=()

Creating tuple with one element :

>>> tuple2=(1)

 If you created tuple with single element python consider it as a value assigned and
tuple2 variable name
 In order to avoid that please include “,” operator next to that element so that python
consider it as a tuple (i.e) tuple2=(1,)

5. Write various tuple function available

 Tuple mainly contain 5 built-in function. They are


1. len()
2. min()
3. max()
4. cmp()
5. sum()

6. Write differences between list & string

Similarity difference between list and string:


 List and string use same function as
o length: len (variable_name)
o indexing: L[i]
o slicing: L[intial,end,step value]
o membership operator: in & not in

R. VISWANATH, AP / IT VER 1.0 P a g e | 14


GE8161 PSPP

o concatenation and replication operators:[+-,concatenate, * -fromreplication]


o Accessing individual elements: L[index].

Difference between list and string:


 Mutability: String are immutable but list is mutable.
 storage:List are stored in memory exactly like string, except that
object are larger than others.

7. Define dictionary

 A dictionary is a containers object that store a collection of key: value pair. It


ensures fast retrieval, deletion and updating of value by using to key
 Dictionary is mutable and unordered collection.
 Here key like index operator (i.e) key must be unique within dictionary. Is also
called map. Where each key is mapped or assigned with value is may be any type
of data

8. Write difference between tuples and list

Similarity:

1. Length fun is used on both tuple and list are same


2. Indexing and slicing are same on both tuples and list
3. Both concatenation and exponentiation operation are possible
4. Both in and not in operation allowed in tuples and list

Difference:

1. Tuples are in mutable set and list are mutable

9. Write syntax & example for dictionary

 You can create a dictionary by enclosing the item inside a pair of curly braces {}.
 Each item consist of key, followed my colon followed by value.
 Each item may be separated with commas.
 Syntax: <dictionary_name>={key1:value,key2:value,…keyn:value}
 Ex: Indian_player_dictionary={1:”dhoni”,2:”kholi”,3:”shewag”,4:”section”}
 To create empty dictionary: <dict_name>={}

R. VISWANATH, AP / IT VER 1.0 P a g e | 15


GE8161 PSPP

10. How to traverse the list

 Traversing a list means accessing & Processing each elements of it


 For loop is mainly used traverse the list.
 Syntax:
for variable_name in list_name:
Process each items
Print or operations.

 Ex:

11. How to traverse the tuples

 Using for loop we can easily able to traverse the individual element in tuple
 Syntax:
for var_name in range(0,len(tuple_name),1):
process each item statement

R. VISWANATH, AP / IT VER 1.0 P a g e | 16


GE8161 PSPP

12. How to traverse the dictionary

 For loop is mainly used to transverse over the items of the dictionary.
 Syntax: for var_name in dictionary_name:
Processing each item tree.

13. Explain various dictionary operations

1. Traversing
2. Adding an element
3. Updating the elements

R. VISWANATH, AP / IT VER 1.0 P a g e | 17


GE8161 PSPP

4. Deleting the elements


5. Checking for presence of key.

14. How to declare 2 dimensional list

15. What is meant by list comprehension

 List comprehension provides a convenient way to create a sequential list of


elements. A list comprehension consist of bracket containing an expression
followed by a for clause, then zero or more for or if clauses.
 It produce a list with the result from evaluating the expression.

PART B

1. Explain in detail about list & its operations


2. Explain various slicing techniques in list
3. Explain how to pass list to function
4. Explain in detail about tuples & its operations
5. Explain tuple function & methods

R. VISWANATH, AP / IT VER 1.0 P a g e | 18


GE8161 PSPP

6. Explain in detail about dictionary and its operations


7. Explain various dictionary functions & methods available
8. Compare and contrast list, tuples, dictionary
9. Explain in detail about 2 dimensional list / matrix declaration and processing
10. Write program for:
a. Matrix multiplication
b. Matrix addition
c. Matrix subtraction
d. Matrix transpose
e. Matrix sum by row wise
f. Matrix sum by column wise
g. Matrix sum by major diagonal

R. VISWANATH, AP / IT VER 1.0 P a g e | 19


GE8161 PSPP

UNIT 5

PART A

1. Explain various modes of file operations

 Files are used to store data permanently. Data used in program are temporary (i.e)
lost when the program terminates or if you closed the application.
 In order to save the data created in a program permanently we need to save it in a
file on a disk.
 Then using that file we can later executed or used for same program.
 To create a file object use open function.
 To read and write data from to file use read and write function.

2. What is the difference between “r”, “w”, and “A” mode

Mode Description

'r' Open a file for reading only.


'w' Open a file for writing only.
'a' Open a file for appending data. Data are
written to the end of the file.
'rb' Open a file for reading binary data.
'wb' Open a file for writing binary data.

3. Write difference between absolute & relative file name.

Absolute File Name:


 It is the one contain file name with its complete part and drive letter.
 For ex: c:\python\unit5.txt in the absolute file name for the unit5 .txt.
 Here c:\python is referred as directory path.
Relative File Name:
 It is the Relative to current working directory. The complete directory path for a
relative filename omitted.
 For ex: unit5 .txt a relative filename. If it’s current, working directory c:\python

4. Define exceptional handling

R. VISWANATH, AP / IT VER 1.0 P a g e | 20


GE8161 PSPP

 Exception handling is a process that enables a program to deal with exceptions and
continue its normal execution.
 When running a program, what happens if user enter a file or value that doesn’t
exist? Suddenly the program will be aborted and raise an error.
 An error that occur at runtime called exception. How can you deal with an exception
so that program can catch the error and prompt to user to enter correct filename (or)
data.

5. Define modules

 As your program gets longer, you may want to split into several files for easier
maintenance.
 We may also want to use a handy function that you have written in a several program
without copying its code into program.
 To support this method, python has a way to put definition in a file and use them in
a program, such a file called module.
 Definition from a module can be imported into another module (or) into main
module (or) for a program execution.
 A module is a file containing python definition and statement. The file name is the
module name with the suffix of “.py”.

6. Define packages

 Packages are a way of structuring python module namespace by using “dotted


module names”.
 For example. The module name A.B designates a submodule named B in a package
named A.
 Just like the use of modules saves the author of different modules from having to
worry about each other global variable name.
 Suppose you want to design a collection of modules is called package for the
uniform handling of files and data.
 Suppose you want create a module and package for the sound file format. Then we
need to create and maintain growing collection of modules for playing various file
formats.
 There may also different operation that you might want to perform on sound and
data (such as mixing, adding, echo, applying equalizer function).

7. Write Syntax for exceptional handling

R. VISWANATH, AP / IT VER 1.0 P a g e | 21


GE8161 PSPP

 Syntax:
try:
<body>
except <exception type 1>:
<handler 1>
except <exception type 2>:
<handler 2>
except <exception type n>:
<handler n>
except :<handler except>
else:
<process else>
finally:
<process finally>

8. Write various file objects attributes

 Mainly three file object attributes are present to check the file opening status and
mode and name.
Attribute Description
file.closed Return true if the file in closed false otherwise
file.mode Return the mode with which file was opened.
file.name Return name of the file

9. Write any fine python file methods

close() Close an open file. It has no effect if the file is already closed.

Separate the underlying binary buffer from the TextIOBase and


detach()
return it.

fileno() Return an integer number (file descriptor) of the file.

flush() Flush the write buffer of the file stream.

isatty() Return True if the file stream is interactive.

R. VISWANATH, AP / IT VER 1.0 P a g e | 22


GE8161 PSPP

Read atmost n characters form the file. Reads till end of file if it is
read(n)
negative or None.

readable() Returns True if the file stream can be read from.

10. What is the use of file seek, tell operations

1. file object.tell ()  Return the current position within a file.


2. file object. seek (offset [,from])  move to new file position.
 Offset argument is optional (i.e default it is Zero (0).
 Anyhow you should mention value which should be >=0.

PART B

1. Explain in detail about text files and its mode of its operations.
2. Explain in detail about reading of data from file
3. Explain in detail about writing data in to file
4. Explain in detail about exceptional handling
5. Explain in detail about modules & packages
6. Explain command line arguments in detail
7. How will you create user defined exceptions
8. Write a program to copy the contents of one file to another file
9. Write a program to find most frequent word occurrence
10. Write a program to find word count & letter count

R. VISWANATH, AP / IT VER 1.0 P a g e | 23

Das könnte Ihnen auch gefallen