0% found this document useful (0 votes)
6 views3 pages

Python Test Questions and Exercises

The document contains a collection of practical and theory questions related to Python programming. It includes questions about printing strings and lists, list operations, string slicing and formatting, functions, data types like tuples and dictionaries, modules and libraries.

Uploaded by

arkayinterview
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Python Test Questions and Exercises

The document contains a collection of practical and theory questions related to Python programming. It includes questions about printing strings and lists, list operations, string slicing and formatting, functions, data types like tuples and dictionaries, modules and libraries.

Uploaded by

arkayinterview
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

# Practical Questions:

1. String = "Hello Hi Python"


Print the given string as group of 3 separate strings.

2. fruits = ["grape", "apple", "mango"]


Print all the items in the given list using for loop.

3. List1 = [1, 2, 3]
list2 = [2, 4]

append list2 in list1.

4. what will be the output of the given code?

b = [1, 3, 4, 6, 7, 10]

st = "Python tutorial"
sliceportion = slice(0, 4)
print(b[sliceportion])
print(st[sliceportion])

5. What will be the output of the given code?

str = " Python tutorial"

str = [Link](" P")


print(str)

6. What will be the output of the following code?

a = ["Python", "tutorial", "and", "test"]

a = " ".join(a)
print(a)

7. Replace “_” from all the items in the list with “-” and print
the list.

columns = ["First_name", "Middle_name", "Last_name"]

8. What will be the output of the following code?

myStr = "I am writing a Python program"


x = len(myStr)
print(x)

9. What will be the output of the following code?

bikes = ['trek', 'redline', 'giant']


ans='trek' in bikes
print(ans)

10. Sort the given list in ascending order.

A = [3, 2, 1]

[Link] will be the output of the following code?

products = ['table', 'chair', 'sofa', 'bed', 'lamp']

prices = [50, 20, 200, 150, 10]

for product, price in zip(products, prices):

print('Product: {}, Price: {}'.format(product, price))

[Link] will be the output of the following code?

numbers = [1, 2, 3, 4, 5, 6]
list(filter(lambda x: x % 2, numbers))

13. What will be the output of the following code?

cities = ('Madrid', 'Valencia', 'Barcelona', 'Munich',


'Stuttgart')
tuple(filter(lambda x: [Link](('M', 'V')), cities))

14. What will be the output of the following code?


import datetime as dt
now = [Link]()
print([Link])
print([Link])

15. What will be the output of the following code?

def division(num1, num2=2):

print(num1/num2)

division(16,4)
division(16)

# Theory Questions

1. What type of language is python? Programming or scripting?

2. What is pep 8?
3. What are decorators in Python?
4. What is the difference between .py and .pyc files?
5. Is python case sensitive?

6. Is indentation required in python?

7. What is the difference between Python Arrays and lists?

8. What is __init__?

9. What is a lambda function?

10. What is self in Python?

11. What does [::-1} do?

12. How do you write comments in python?

13. How to comment multiple lines in python?

14. What are docstrings in Python?

15. What does this mean: *args, **kwargs? And why would we use it?

16. What are Python libraries? Name a few of them.

17. What is split used for?


18. How to import modules in python?

19. What are unit tests in Python?


20. How to use map(), filter() and reduce() functions in python?

Common questions

Powered by AI

Applying the method `str.strip(' P')` to the string ' Python tutorial' trims the leading character ' ' and the leading 'P' from the string, resulting in 'ython tutorial'. This illustrates the use of the `strip` method to remove specific characters from the beginning and end of a string in Python .

The `filter` function in Python is used to construct a list of elements from an iterable for which a specified function returns true. Given `numbers = [1, 2, 3, 4, 5, 6]`, `filter(lambda x: x % 2, numbers)` filters out even numbers since the lambda function checks for odd numbers (`x % 2` is true for odds), returning `[1, 3, 5]`. This approach effectively applies functional programming principles by utilizing anonymous functions as predicates .

To slice a list or a string in Python, you can use the `slice()` function. The `slice(0, 4)` indicates that the elements from the 0th index up to, but not including, the 4th index should be extracted. For a list `b = [1, 3, 4, 6, 7, 10]`, `b[slice(0, 4)]` results in `[1, 3, 4, 6]`. For a string `st = 'Python tutorial'`, `st[slice(0, 4)]` results in 'Pyth' .

`*args` and `**kwargs` in Python are used to pass a variable number of arguments to functions. `*args` allows passing a non-keyworded, variable number of arguments as a tuple, while `**kwargs` allows for a keyworded, variable number of arguments as a dictionary. This functionality is beneficial for creating flexible functions that can handle different sets and numbers of arguments, thereby enhancing reusability and abstraction levels in code design .

Python's `zip` function pairs elements from multiple lists together, providing a convenient way to iterate over tuples of paired elements. For example, given `products = ['table', 'chair', 'sofa', 'bed', 'lamp']` and `prices = [50, 20, 200, 150, 10]`, iterating with `zip(products, prices)` results in tuple pairs that can be used to generate outputs like 'Product: table, Price: 50', illustrating the creation of associative arrays or dictionaries-like structures in Python .

`.py` files contain the source code of Python programs, whereas `.pyc` files contain the bytecode compiled from the source code for execution by the Python interpreter. `.pyc` files are automatically generated to improve startup speed by skipping the source compilation process. This distinction implies that while debugging or inspecting code, `.py` files are necessary, but `.pyc` files enhance performance by reducing runtime compilation overhead .

To replace underscores with hyphens in a list of strings, you can use a list comprehension. For a list like `columns = ['First_name', 'Middle_name', 'Last_name']`, a comprehension `[col.replace('_', '-') for col in columns]` will result in `['First-name', 'Middle-name', 'Last-name']`. This process involves iterating over each string in the list and applying the `replace` method to each one, demonstrating both list comprehensions and string manipulation .

In Python, one can check for the existence of an item within a list using the `in` keyword. For instance, to check if 'trek' is in the list of bikes `bikes = ['trek', 'redline', 'giant']`, the expression `'trek' in bikes` returns `True`. This capability is crucial for decision-making processes where the presence or absence of an item affects the flow of program execution .

The `join` method in Python is used to concatenate elements of a list into a single string with a specified separator. For example, given a list `a = ['Python', 'tutorial', 'and', 'test']`, using `' '.join(a)` produces the string 'Python tutorial and test'. This method is useful for combining a list of words into a sentence or a CSV line .

Python's `lambda` functions are small anonymous functions defined with the `lambda` keyword instead of `def`. They can take any number of arguments, but have a single expression. When used with the `filter` function, such as in `list(filter(lambda x: x % 2, numbers))` for `numbers = [1, 2, 3, 4, 5, 6]`, the `lambda` function checks each number for oddness (`x % 2`), resulting in a list `[1, 3, 5]`. This demonstrates the use of `lambda` for concise, inline functions particularly in functional operations .

You might also like