You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* New questions and spell check (bregman-arie#181)
Added new questions related with KVM, Libvirt and DNF
* New answers
* Adding a note about which way is faster: Union vs Bitwise
* Add comment about slicing vs reversed
- Using Python3.10 that accepts using bitwise operator '|'.
3050
+
3051
+
```
3052
+
def isNumberPalindrome(number: int | str) -> bool:
3053
+
if isinstance(number, int):
3054
+
number = str(number)
3055
+
return number == number[::-1]
3056
+
3057
+
print(isNumberPalindrome("12321"))
3036
3058
```
3059
+
3060
+
Note: Using slicing to reverse a list could be slower than other options like `reversed` that return an iterator.
3061
+
3062
+
- Result:
3063
+
3037
3064
```
3065
+
True
3066
+
```
3067
+
3038
3068
</b></details>
3039
3069
3040
3070
#### Python - OOP
@@ -3241,6 +3271,28 @@ False
3241
3271
3242
3272
<details>
3243
3273
<summary>What is the __call__ method?</summary><br><b>
3274
+
3275
+
It is used to emulate callable objects. It allows a class instance to be called as a function.
3276
+
3277
+
- Example code:
3278
+
3279
+
```
3280
+
class Foo:
3281
+
def __init__(self: object) -> None:
3282
+
pass
3283
+
def __call__(self: object) -> None:
3284
+
print("Called!")
3285
+
3286
+
f = Foo()
3287
+
f()
3288
+
```
3289
+
3290
+
- Result:
3291
+
3292
+
```
3293
+
Called!
3294
+
```
3295
+
3244
3296
</b></details>
3245
3297
3246
3298
<details>
@@ -3427,6 +3479,24 @@ some_list[:3]
3427
3479
3428
3480
<details>
3429
3481
<summary>How to insert an item to the beginning of a list? What about two items?</summary><br><b>
3482
+
3483
+
- One item:
3484
+
3485
+
```
3486
+
numbers = [1, 2, 3, 4, 5]
3487
+
numbers.insert(0, 0)
3488
+
print(numbers)
3489
+
```
3490
+
3491
+
- Multiple items or list:
3492
+
3493
+
```
3494
+
numbers_1 = [2, 3, 4, 5]
3495
+
numbers_2 = [0, 1]
3496
+
numbers_1 = numbers_2 + numbers_1
3497
+
print(numbers_1)
3498
+
```
3499
+
3430
3500
</b></details>
3431
3501
3432
3502
<details>
@@ -3634,6 +3704,31 @@ list(zip(nums, letters))
3634
3704
3635
3705
<details>
3636
3706
<summary>What is List Comprehension? Is it better than a typical loop? Why? Can you demonstrate how to use it?</summary><br><b>
3707
+
3708
+
From [Docs](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions): "List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.".
3709
+
3710
+
It's better because they're compact, faster and have better readability.
0 commit comments