0% found this document useful (0 votes)
3 views

pythonvivaquestions

The document outlines various fundamental concepts in Python programming, including basic data types, list and tuple methods, and dictionary operations. It also covers user input, control flow with if-else statements, and functions for filtering data and manipulating dates. Additionally, it introduces libraries like NumPy and Pandas for data handling and mathematical calculations.

Uploaded by

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

pythonvivaquestions

The document outlines various fundamental concepts in Python programming, including basic data types, list and tuple methods, and dictionary operations. It also covers user input, control flow with if-else statements, and functions for filtering data and manipulating dates. Additionally, it introduces libraries like NumPy and Pandas for data handling and mathematical calculations.

Uploaded by

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

1.

Basic Data Types in Python


Q: What are the basic data types in Python?
A: The basic data types in Python include:
 int: Integer values (e.g., 5, -3).
 float: Floating-point numbers (e.g., 3.14, -0.001).
 str: Strings, which are sequences of characters (e.g., "Hello, World!").
 bool: Boolean values representing True or False.

2. List Methods
Q: What is the purpose of the insert() method in a list?
A: The insert() method adds an element at a specified index in a list. For
example, list.insert(1, 'a') inserts 'a' at index 1.Q: How does
the remove() method differ from pop()?
A: The remove() method removes the first occurrence of a specified value
from the list, while pop() removes an element at a specified index (or the last
element if no index is provided) and returns it.

3. Tuple Methods
Q: Why are tuples considered immutable?
A: Tuples are immutable because once they are created, their elements
cannot be changed, added, or removed. This property makes them useful for
fixed collections of items.Q: How can you check if an item exists in a
tuple?
A: You can check for an item in a tuple using the in keyword. For
example, item in my_tuple returns True if item exists in my_tuple.

4. Dictionary Methods
Q: What is a dictionary in Python, and how is it different from a list?
A: A dictionary is an unordered collection of key-value pairs where each key
must be unique. Unlike lists, which are ordered collections indexed by
integers, dictionaries use keys to access values.Q: What does
the get() method do?
A: The get() method retrieves the value associated with a specified key in a
dictionary. It returns None if the key does not exist instead of raising an error.

5. Menu Program for Arithmetic Operations


Q: How do you implement user input in Python?
A: User input can be implemented using the input() function, which reads a
line from input and returns it as a string. You can convert it to other types as
needed (e.g., using int()).

6. Positive/Negative Number Check


Q: How does the if-else statement work in Python?
A: The if-else statement evaluates a condition; if it's True, it executes the
code block under if; otherwise, it executes the code block under else. For
example:

if number > 0:
print("Positive")
else:
print("Negative")

7. Filtering Even Numbers


Q: What is the purpose of the filter() function?
A: The filter() function constructs an iterator from elements of an iterable
for which a function returns True. It's commonly used to filter items based on
specific criteria.

8. Printing Date and Time


Q: How do you import and use the datetime module in Python?
A: You can import the datetime module using import datetime. To get the
current date and time, you can use:

now = datetime.datetime.now()
print(now)

9. Adding Days to Current Date


Q: How do you manipulate dates using Python's datetime module?
A: You can manipulate dates using the timedelta class from the datetime
module to add or subtract days from a date:
from datetime import datetime, timedelta
today = datetime.now()
new_date = today + timedelta(days=5)

10. Character Count in String


Q: How would you approach counting characters in a string using a
dictionary?
A: You can iterate through each character in the string and use a dictionary
to count occurrences:
char_count = {}
for char in string:
char_count[char] = char_count.get(char, 0) + 1

11. Character Frequency in File


Q: How do you read from a file in Python?
A: You can read from a file using the built-in open() function along with
methods like read(), readline(), or readlines(). For example:

with open('file.txt', 'r') as file:


content = file.read()

12. NumPy Array Properties


Q: What is NumPy, and why is it used in Python programming?
A: NumPy is a powerful library for numerical computing in Python that
provides support for large multi-dimensional arrays and matrices along with
mathematical functions to operate on them.

13. Concatenating DataFrames


Q: What is a DataFrame? How does it differ from other data
structures like lists or dictionaries?
A: A DataFrame is a two-dimensional labeled data structure similar to a table
or spreadsheet. It allows for more complex data manipulation compared to
lists or dictionaries due to its ability to handle heterogeneous data types and
perform operations across rows and columns.

14. Reading CSV Files with Pandas


Q: What is CSV, and why is it commonly used for data storage?
A: CSV (Comma-Separated Values) is a simple file format used for storing
tabular data where each line corresponds to a row and each value is
separated by commas. It’s widely used due to its simplicity and compatibility
with many applications.

15. Area of Circle Calculation


Q: How does importing the math module help with mathematical
calculations in Python?
A: The math module provides access to mathematical functions defined by
the C standard library, such as calculating square roots or trigonometric
functions. For area calculation, we can use:

import math
area = math.pi * radius ** 2

You might also like