0% found this document useful (0 votes)
9 views1 page

Python Practice Questions for Beginners

Uploaded by

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

Python Practice Questions for Beginners

Uploaded by

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

Python Coding Practice Questions

1. Write a Python program to input a string and print its length and type.

2. Write a program to take an integer as input and check if it is even or odd.

3. Write a program to convert a float number into an integer and a string.

4. Write a program to input two numbers and print their sum, difference, product, and quotient.

5. Write a program to find the remainder and quotient when dividing two numbers.

6. Write a Python program to calculate x raised to the power y (x^y) without using the ** operator.

7. Write a program to create a list of 5 numbers entered by the user.

8. Write a Python program to find the maximum and minimum elements in a list without using max()
or min().

9. Write a program to reverse a list without using the reverse() function.

10. Write a Python program to count how many times an element appears in a list.

11. Write a program to create a tuple of numbers and print only the even numbers.

12. Write a Python program to find the sum of elements in a tuple.

13. Write a program to check if an element exists in a tuple.

14. Write a Python program to create a dictionary of 3 students with their marks.

15. Write a program to find the student with the highest marks in a dictionary.

16. Write a Python program to add a new key-value pair in an existing dictionary.

17. Write a Python program to count the frequency of characters in a string using a dictionary.

18. Write a Python program to create two sets and find their union and intersection.

19. Write a program to remove duplicate elements from a list using a set.

20. Write a Python program to check if one set is a subset of another.

Common questions

Powered by AI

Dictionaries in Python can be used to manage structured data by storing records as key-value pairs, such as student names paired with their marks . This allows for efficient data retrieval, updates, and analytical operations like finding students with the highest marks. Using dictionary methods, data can be rapidly accessed and manipulated, facilitating insights such as average scores and performance trends.

To count the frequency of each character in a string using a dictionary, iterate through the string and use the character as a key in the dictionary, incrementing the value for each occurrence . This approach allows for quick lookup and update operations, making it efficient for large strings. The use of dictionaries leverages Python's hash table implementation for fast access, which is an advantage over other data structures that don't support these operations as efficiently.

To determine if an integer is even or odd in Python, you can use the modulus operator `%` to check the remainder when the integer is divided by 2 . An integer is even if `number % 2 == 0`, otherwise it is odd. This technique is efficient because modulus is a constant-time operation (O(1)), making it very fast for simple numerical determinations.

In Python, numerical data can be effectively input, stored, and manipulated using lists, tuples, dictionaries, and sets. Lists allow dynamic resizing and easy iteration; tuples, being immutable, are used for fixed data sequences and allow iteration. Dictionaries are useful for storing key-value pairs where numerical data can be associated with identifiers, and sets are efficient for membership tests and duplicate elimination . Each data structure offers unique advantages depending on the requirements for mutability, access speed, or organization.

In Python, the union of two sets can be computed using the `|` operator or the `union()` method, and the intersection can be found using the `&` operator or `intersection()` method . These operations are meaningful as they allow for comprehensive analysis and comparison of datasets, facilitating tasks like merging distinct datasets (union) and identifying commonalities (intersection). Such set operations are foundational in data processing, filtering, and developing algorithms for intersection-based problem-solving.

Tuples, as immutable sequences, enhance efficiency and reliability in Python by preventing in-place modifications, which is advantageous in scenarios involving static datasets that should remain constant throughout execution . Immutable data structures help prevent accidental data changes and allow tuples to be used as keys in dictionaries, which require hashable types. They are particularly useful in cases where data integrity is critical and performance benefits are needed due to faster access times compared to lists.

To find the maximum and minimum values in a list without using built-in functions, iterate through the list while maintaining two variables to track the maximum and minimum values found during the iteration . Initialize these variables with the first element of the list and update them as you find larger or smaller elements. This approach runs in O(n) time complexity, where n is the number of elements in the list, making it linear and efficient for large lists.

To calculate the power of a number x raised to y in Python without using the ** operator, you can use a loop to multiply x by itself y times . For instance, a simple iterative approach involves initializing a variable (e.g., `result = 1`) and multiplying it by x in a loop that runs y times. Alternatively, recursive methods or logarithmic multiplication strategies can be employed for more efficient power calculations, particularly when y is large.

Using sets to remove duplicate elements from a list in Python is based on the property of sets having only unique elements. By converting a list to a set, duplicates are inherently removed . This method is efficient in terms of simplicity and speed for deduplication. However, it has limitations, such as losing the original order of elements and potentially increased memory usage, as converting large lists to sets requires additional space.

You can reverse a list in Python without using the `reverse()` function by using slicing (`list[::-1]`) or by employing a two-pointer approach that swaps the first and last elements repeatedly until you reach the center of the list . The slicing method is concise and readable but may use additional memory for large lists, whereas the two-pointer approach is in-place and can be more memory-efficient. The choice depends on the balance between readability, memory usage, and the specific constraints of the application.

You might also like