Viva Question Ans

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Sample 1:

1. Explain history of Python-creator

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.

2. Which are different operators in Python?

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

3. Explain about special operators in Python.

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

not imply that they are identical.

Operator Meaning Example

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

whether a value or variable is found in a sequence


(string, list, tuple, set and dictionary).
In a dictionary we can only test for presence of key, not the value.

Operator Meaning Example

in True if value/variable is found in the sequence 5 in x

not in True if value/variable is not found in the sequence 5 not in x

4. How to perform read – write operations in Python?

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.

write() : This function writes a fixed sequence of characters to a file.

writelines() : This function writes a list of string.

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)

6. Explain difference between list, set & tuple.

List is a built-in data structure in Python. It is represented as a collection of data points in


square brackets. Lists can be used to store any data type or a mixture of different data types.
Lists are mutable which is one of the reasons why they are so commonly used.
Tuple is a collection of values separated by comma and enclosed in parenthesis. Unlike lists,
tuples are immutable. The immutability can be considered as the identifying feature of
tuples.

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.

7. Why to use dictionaries in Python?


Dictionaries are used to store data values in key:value pairs. A dictionary is a collection
which is ordered, changeable and does not allow duplicates.

8. How to install Python on computer & execute Python program?

Step 1: Select Version of Python to Install.

Step 2: Download Python Executable Installer.

Step 3: Run Executable Installer.

Step 4: Verify Python Was Installed On Windows.

Step 5: Verify Pip Was Installed.

Step 6: Add Python Path to Environment Variables (Optional)

Step 7: Install virtualnv (Optional)

Sample 2:

1. What is looping? Why it is required?


a loop is a programming structure that repeats a sequence of instructions until a
specific condition is met. Programmers use loops to cycle through values, add
sums of numbers, repeat functions, and many other things. ... Two of the most
common types of loops are the while loop and the for loop.
It helps in executing one or more statements up to a desired number of times.

2. State different types of looping structures in Python.


There are two types of loops in Python, for and while

while loop Repeats a statement or group of statements while a given condition


is TRUE. It tests the condition before executing the loop body.

for loop Executes a sequence of statements multiple times and abbreviates the
code that manages the loop variable.

3.Give syntax of each loop in Python.


Syntax of while-

while expression:

statement(s)

syntax of for-

for val in sequence:

Body of for

4. How if-else statement can be used in Python? Give example of


each.
The if-else statement is used to execute both the true part and the false part of
a given condition. If the condition is true, the if block code is executed and
if the condition is false, the else block code is executed.

Example 1 : Program to check whether a person is eligible to vote or not.

1. age = int (input("Enter your age? "))


2. if age>=18:
3. print("You are eligible to vote !!");
4. else:
5. print("Sorry! you have to wait !!");

5. How range() function can be used in for loop?


Python range() function generates the immutable sequence of numbers starting
from the given start integer to the stop integer. It is a built-in function that returns
a range object consists of a series of integer numbers, which we can iterate
using a for a loop.

6. Explain use of pass, break & continue.


The pass statement is used as a placeholder for future code. When
the pass statement is executed, nothing happens, but you avoid getting an error
when empty code is not allowed.

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.

functions in programming is use to bundle a set of instructions that we want


to use repeatedly

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!")

2. What are different types of functions in Python?

There are three types of functions in Python:

 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.

 User-Defined Functions (UDFs), which are functions that users


create to help them out; And

 Anonymous functions, which are also called lambda functions


because they are not declared with the standard def keyword.

4. What is a Python directory ?


If there are a large number of files to handle in our Python program, we can arrange our
code within different directories to make things more manageable. A directory or folder
is a collection of files and subdirectories.

5. What is a Python file?

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.

What are different commands/functions related to directories in Python?

3. What is object & class? Give its syntax and example as per Python.

Python is an object oriented programming language. Almost everything in Python is


an object, with its properties and methods. A Class is like an object constructor, or a
"blueprint" for creating objects.

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: <function Person.greet>


print(Person.greet)

# Output: "This is a person class"


print(Person.__doc__)

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')

# create a new object of Person class


harry = Person()

# Output: <function Person.greet>


print(Person.greet)

# Output: <bound method Person.greet of <__main__.Person object>>


print(harry.greet)

# Calling object's greet() method


# Output: Hello
harry.greet()

Output

<function Person.greet at 0x7fd288e4e160>


<bound method Person.greet of <__main__.Person object at 0x7fd288e9fa30>>
Hello

You might also like