Question 31: Print the number in words for Example: 1234 => One Two Three Four. Download hole Program / Project code, by clicking following link: Compare tuple with list ? Tuples and lists are both used for storing collections of items in Python, but they have some key differences. Here's a comparison table highlighting seven important differences between tuples and lists:
Aspect Tuples Lists Syntax Created using parentheses: `(item1, item2, ...)`. Created using square brackets: `[item1, item2, ...]`. Mutability Immutable - Elements cannot be changed after creation. Mutable - Elements can be modified after creation. Elements Typically stores heterogeneous elements (e.g., different data types). Typically stores homogeneous elements (e.g., same data type). Performance Slightly faster for iteration and access due to immutability. Slightly slower for iteration and access due to mutability. Size Generally consumes less memory than lists. Consumes more memory compared to tuples. Use Cases Suitable for situations where data should not change (e.g., dictionary keys). Suitable for situations where data may need to be modified. Operations Limited to basic operations like indexing, slicing, and counting elements. Supports a wide range of operations, including appending, removing, and sorting.
Keep in mind that the choice between tuples and lists depends on the specific requirements of your program. If you need an ordered collection of items that should not change, tuples are a good choice due to their immutability. If you need to work with a dynamic collection that requires frequent modification, lists are more suitable. Programming Code: Following code write in: BP_P31.py # Print number to words
# Exp. 123 => one two three
# import lib.
import inflect
num = inflect.engine()
words = num.number_to_words(1234)
print(words)
words = num.number_to_words(num.ordinal(1234)) # ordinal
print(words)
# Thanks for Reading.
Output:
Compare tuple with list ?
Tuples and lists are both used for storing collections of items in Python, but they have some key differences. Here's a comparison table highlighting seven important differences between tuples and lists:
Aspect | Tuples | Lists |
---|---|---|
Syntax | Created using parentheses: `(item1, item2, ...)`. | Created using square brackets: `[item1, item2, ...]`. |
Mutability | Immutable - Elements cannot be changed after creation. | Mutable - Elements can be modified after creation. |
Elements | Typically stores heterogeneous elements (e.g., different data types). | Typically stores homogeneous elements (e.g., same data type). |
Performance | Slightly faster for iteration and access due to immutability. | Slightly slower for iteration and access due to mutability. |
Size | Generally consumes less memory than lists. | Consumes more memory compared to tuples. |
Use Cases | Suitable for situations where data should not change (e.g., dictionary keys). | Suitable for situations where data may need to be modified. |
Operations | Limited to basic operations like indexing, slicing, and counting elements. | Supports a wide range of operations, including appending, removing, and sorting. |
Keep in mind that the choice between tuples and lists depends on the specific requirements of your program. If you need an ordered collection of items that should not change, tuples are a good choice due to their immutability. If you need to work with a dynamic collection that requires frequent modification, lists are more suitable.
# Print number to words
# Exp. 123 => one two three
# import lib.
import inflect
num = inflect.engine()
words = num.number_to_words(1234)
print(words)
words = num.number_to_words(num.ordinal(1234)) # ordinal
print(words)
# Thanks for Reading.
Output:
0 Comments