Viva Question Ans
Viva Question Ans
Viva Question Ans
The programming language Python was conceived in the late 1980s, and its implementation
was started in December 1989 by Guido van Rossum at CWI in the Netherlands as a
successor to ABC capable of exception handling and interfacing with the Amoeba operating
system.
Operators are special symbols in Python that carry out arithmetic or logical computation.
The value that the operator operates on is called the operand. The different operators are as
follows:
a. Arithmetic operators
b. Comparison operators
c. Logical operators
d. Bitwise operators
e. Assignment operators
f. Special operators
Python language offers some special types of operators like the identity operator or the
membership operator.
Identity operators:
is and is not are the identity operators in Python. They are used to check if two values (or
variables) are located on the same part of the memory. Two variables that are equal does
is True if the operands are identical (refer to the same object) x is True
is not True if the operands are not identical (do not refer to the same object) x is not True
Membership operator:
in and not in are the membership operators in Python. They are used to test
read() : This function reads the entire file and returns a string
readline() : This function reads lines from that file and returns as a string. It
fetch the line n, if it is been called nth time.
readlines() : This function returns a list where each element is single line of
that file.
readlines() : This function returns a list where each element is single line of
that file.
append() : This function append string to the file instead of overwriting the
file.
5. What is Tuple?
Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data
types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all
with different qualities and usage. A tuple is a collection which is ordered
and unchangeable. Tuples are written with round brackets.
Example:
thistuple = ("apple", "banana", "cherry")
print(thistuple)
Set is an unordered collection of distinct immutable objects. A set contains unique elements.
Although sets are mutable, the elements of sets must be immutable. There is no order
associated with the elements of a set. Thus, it does not support indexing or slicing like we do
with lists.
Sample 2:
for loop Executes a sequence of statements multiple times and abbreviates the
code that manages the loop variable.
while expression:
statement(s)
syntax of for-
Body of for
The break statement in Python terminates the current loop and resumes
execution at the next statement
The continue statement in Python returns the control to the beginning of the
while loop. The continue statement rejects all the remaining statements in the
current iteration of the loop and moves the control back to the top of the loop.
The continue statement can be used in both while and for loops.
Sample 3:
1. What is function? Why it is used? Explain with example & syntax in Python.
A function is a block of code which only runs when it is called. You can pass data,
known as parameters, into a function. A function can return data as a result.
Syntax of Function
def function_name(parameters):
"""docstring"""
statement(s)
def greet(name):
"""
Example:
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
Built-in functions, such as help() to ask for help, min() to get the
minimum value, print() to print an object to the terminal,… You can
find an overview with more of these functions here.
A file is some information or data which stays in the computer storage devices.
... Python gives you easy ways to manipulate these files. Generally we divide files in
two categories, text file and binary file. Text files are simple text where as the
binary files contain binary data which is only readable by computer.
6. What are different modes to access files in Python
There are three kinds of mode, that Python provides and how files can be opened:
1. “ r “, for reading.
2. “ w “, for writing.
3. “ a “, for appending.
4. “ r+ “, for both reading and writing.
3. What is object & class? Give its syntax and example as per Python.
The syntax for creating an object is: ClassName object = new ClassName();
syntax is: class MyClass { // class methods constructor() { ... } ...
example of class:
class Person:
"This is a person class"
age = 10
def greet(self):
print('Hello')
# Output: 10
print(Person.age)
Output
10
<function Person.greet at 0x7fc78c6e8160>
This is a person class
Example of object
class Person:
"This is a person class"
age = 10
def greet(self):
print('Hello')
Output