Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python Viva Questions

The document contains a series of Python viva questions covering fundamental concepts such as indentation, types of operators, data structures (lists, tuples, dictionaries), functions, strings, loops, and control statements. It explains the characteristics and differences between various data types and provides examples of string methods and operations. Additionally, it discusses how to iterate over collections and the use of negative indexing.

Uploaded by

akshayba001
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Viva Questions

The document contains a series of Python viva questions covering fundamental concepts such as indentation, types of operators, data structures (lists, tuples, dictionaries), functions, strings, loops, and control statements. It explains the characteristics and differences between various data types and provides examples of string methods and operations. Additionally, it discusses how to iterate over collections and the use of negative indexing.

Uploaded by

akshayba001
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PYTHON VIVA QUESTIONS

1. What is indentation in Python, and why is it important?


Indentation refers to the spaces at the beginning of a code line. Where in other
programming languages the indentation in code is for readability only, the
indentation in Python is very important.

Python uses indentation to indicate a block of code.

Python will give you an error if you skip the indentation.

2. Is Python code compiled or interpreted?


Python code is interpreted
3. What are the different types of operators in Python?
 Arithmetic Operators: +, -, *, /, %, //, **
 Comparison Operators: ==, !=, >, <, >=, <=
 Logical Operators: and, or, not
 Assignment Operators: =, +=, -=, *=, /=, etc.
 Bitwise Operators: &, |, ^, <<, >>, ~
 Membership Operators: in, not in
 Identity Operators: is, is not
4. What is a list in Python?
A list in Python is an ordered, mutable (changeable), and indexed collection of
elements. Lists can hold items of different data types, including integers,
strings, and even other lists.
5. What is the difference between a tuple and a list?
 List:
 Mutable (can be modified).
 Defined using square brackets [].
 Can grow and shrink dynamically.
 Tuple:
 Immutable (cannot be modified after creation).
 Defined using parentheses ().
 More memory-efficient and faster than lists due to immutability.
6. What is a dictionary in Python?
A dictionary in Python is an unordered, mutable collection of key-value pairs.
It is indexed by keys, which are unique within a dictionary.
my_dict = {"name": "John", "age": 25, "city": "New York"}
7. What are functions in Python?
A function is a block of code that only runs when it is called. Functions allow
you to reuse code, and they can take inputs (parameters) and return an output.
def display(name):
return "Hello, " + name

print(display("Divya"))
8. What is a string in Python?
A string is a sequence of characters enclosed within single quotes (') or double quotes
("). In Python, strings are immutable, meaning their contents cannot be modified after
creation.
9. How do you check the length of a string in Python?
You can use the built-in len() function to check the length of a string, which
returns the number of characters in the string.
10. What are some common string methods in Python?
 upper(): Converts all characters to uppercase.
 lower(): Converts all characters to lowercase.
 capitalize(): Capitalizes the first character of the string.
 strip(): Removes leading and trailing spaces.
 replace(old, new): Replaces all occurrences of a substring with a new substring.
 split(): Splits a string into a list of substrings.
 join(): Joins a list of strings into a single string.
 find(sub): Returns the index of the first occurrence of a substring, or -1 if not
found.
 count(sub): Returns the number of occurrences of a substring.
11. What is string slicing in Python?
String slicing allows you to extract a portion of a string using the syntax
string[start:end:step], where:
 start: The starting index (inclusive).
 end: The ending index (exclusive).
 step: The step between indices (optional).
12. What is the difference between + and * operators when used with strings?
 + is used for concatenation of two strings.
 * is used for repeating a string a given number of times.
13. What are escape sequences in Python strings?
 \n - Newline
 \t - Tab
 \\ - Backslash
 \' - Single quote
 \" - Double quote
14. How do you check if a string contains only digits in Python?
You can use the isdigit() method, which returns True if all characters in the string are
digits, and False otherwise.
15. How do you convert a string to an integer in Python?
You can use the int() function to convert a string that represents a number into an
integer.
16. What are loops in Python?
Loops in Python allow you to execute a block of code repeatedly, either for a fixed
number of times or until a condition is met. Python has two primary types of loops:
for loop and while loop.
for item in iterable:
# code block
while condition:
# code block
17. How does the range() function work in a for loop?
The range() function generates a sequence of numbers, which is often used in a for
loop for iteration. The syntax of range() is:
range(start, stop, step)
 start: The starting value (default is 0).
 stop: The end value (exclusive).
 step: The increment value (default is 1).
18. What is a break statement in Python?
The break statement is used to exit from a loop prematurely, regardless of whether the
loop condition is met or not. It is typically used when a specific condition is met
inside the loop
19. How can you iterate over a list or string using a for loop?
my_list = [1, 2, 3, 4, 5]
for item in my_list:
print(item) # Output: 1, 2, 3, 4, 5
20. How Negative Indexing Works
my_list = [10, 20, 30, 40, 50]

# Access elements using positive indices


print(my_list[0]) # Output: 10
print(my_list[4]) # Output: 50

# Access elements using negative indices


print(my_list[-1]) # Output: 50 (last element)
print(my_list[-2]) # Output: 40 (second to last element)
print(my_list[-3]) # Output: 30

You might also like