Modue1 PYTHON Sample QB.docx
Modue1 PYTHON Sample QB.docx
PART A
DATA,
EXPRESSIONS
1.Define python AND
STATEMENTS
Python is an object-oriented, high level language, interpreted, dynamic and
multipurpose programming language.
5.List
the standard data types in
python.
Python has five standard data
Types:
●Numbers
●Strings
●List,
●Tuples
●Dictionary
6.What is meant by python numbers?
Number data types store numeric values. Number objects are created when
you assign a value to them.
Python supports four different numerical types :
● int (signed integers)
● long (long integers, they can also be represented in octal and
● hexadecimal)
● float (floating point real values)
● complex (complex numbers)
15.whatare expressions?
An expression is a combination of values, variables, operators, and calls to
functions. If you type an expression at the Python prompt, the interpreter evaluates it and
displays the result:
>>> 1 + 1=2
16.What is a statement?
A statement is an instruction that the Python interpreter can execute. When you type a
statement on the command line, Python executes it. Statements don‘t produce any result.
For example, a = 1 is an assignment statement. if statement, for statement, while
statement etc. are other kinds of statements.
18.What is docstring?
Doc string is short for documentation string. It is a string that occurs as the first
statement in a module, function, class, or method definition. It is used to explain in brief,
what a function does.
● Arithmetic Operators
● Comparison (Relational) Operators
● Assignment Operator
● Logical Operators
● Bitwise Operators
● Membership Operators
● Identity Operator
PART B
statements
elif
statement:
statements
else:
statements
5.Write the syntax and usage of for loop
For Loop is used to iterate a variable over a sequence(i.e., list or string) in
the order that they appear.Syntax:
while
<expression>:
Body
10.What is len function and explain how it is used on strings with an example.
The len function, when applied to a string, returns the number or character in a string.
Example:
>>>book=‘Problem Solving and Python Programming‘
>>>l
en(book)
38
>>>
12.What are the two operators that are used in string functions?
The in operator tests for membership.
>>>‘V‘ in ‗VRB‘
True
>>>‘S‘ in ‗VRB‘
>>>False
The not in operator returns the logical opposite results of in operator.
>>>‘x‘ not in ‗VRB‘
True
15.How to split strings and what function is used to perform that operation?
The str.split() method is used to split strings up.
>>>book=‘Problem Solving and Python Programming‘
>>>print(book.split())
[‗Problem‘, ‗Solving‘, ‗and‘, ‗Python‘, ‗Programing‘]
PART B
10. What is the output of print tuple[1:3] if tuple = ( 'abcd', 786 , 2.23, 'john', 70.2
)? In the given command, tuple[1:3] is accessing the items in tuple using indexing.
It will print elements starting from 2nd
till 3rd. Output will be (786, 2.23).
>>>a<
b True
13.What are the built-in functions that are used in Tuple?
● all()- returns true if all elements of the tuple are true or if tuple is empty
● any()- returns true if any element of tuple is true
● len()- returns the length in the tuple
● max()- returns the largest item in tuple
● min()- returns the smallest item in tuple
● sum()- returns the sum of all elements in tuple
14.What is the output of print tuple + tinytuple if tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
and tinytuple = (123, 'john')?
It will print concatenated tuples. Output will be ('abcd', 786, 2.23, 'john',
70.200000000000003, 123, 'john').
6.Which method is used to read the contents of a file which is already created?
The read method reads data from the file. With no arguments, it reads
the entire contents of the file:
>>> text = f.read()
>>> print text
Now is the timeto close the file
7.What is a text file? Give an example for a text file.
A text file is a file that contains printable characters and whitespace,
organized into lines separated by newline characters.
To demonstrate, we‘ll create a text file with three lines of text separated by newlines:
>>> f = open("test.dat","w")
>>> f.write("line one\nline two\nline three\n")
>>> f.close()
17.Which method is used to read the contents of a file which is already created?
The read method reads data from the file. With no arguments, it reads
the entire contents of the file:
>>> text = f.read()
>>> print text
Now is the timeto close the file
23.List some few common Exception types and explain when they occur.
● ArithmeticError- Base class for all errors that occur for numeric calculations.
● OverflowError- Raised when a calculation exceeds maximum limit for
a numeric type.
● ZeroDivisionError- Raised when division or modulo by zero takes
o place.
● ImportError- Raised when an import statement fails.
● IndexError- Raised when an index is not found in a sequence.
● RuntimeError- Raised when a generated error does not fall into any category.
24. Write a simple program which illustrates
Handling Exceptions. w x=int(input(―Please
enter a number:‖))
break
except ValueError:
print(―Oops! That was no valid number. Try again…‖)
PART B