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
- Using Python3.10 that accepts using bitwise operator '|'.
3048
+
3049
+
```
3050
+
def isNumberPalindrome(number: int | str) -> bool:
3051
+
if isinstance(number, int):
3052
+
number = str(number)
3053
+
return number == number[::-1]
3054
+
3055
+
print(isNumberPalindrome("12321"))
3034
3056
```
3057
+
3058
+
Note: Using slicing to reverse a list could be slower than other options like `reversed` that return an iterator.
3059
+
3060
+
- Result:
3061
+
3035
3062
```
3063
+
True
3064
+
```
3065
+
3036
3066
</b></details>
3037
3067
3038
3068
#### Python - OOP
@@ -3239,6 +3269,28 @@ False
3239
3269
3240
3270
<details>
3241
3271
<summary>What is the __call__ method?</summary><br><b>
3272
+
3273
+
It is used to emulate callable objects. It allows a class instance to be called as a function.
3274
+
3275
+
- Example code:
3276
+
3277
+
```
3278
+
class Foo:
3279
+
def __init__(self: object) -> None:
3280
+
pass
3281
+
def __call__(self: object) -> None:
3282
+
print("Called!")
3283
+
3284
+
f = Foo()
3285
+
f()
3286
+
```
3287
+
3288
+
- Result:
3289
+
3290
+
```
3291
+
Called!
3292
+
```
3293
+
3242
3294
</b></details>
3243
3295
3244
3296
<details>
@@ -3425,6 +3477,24 @@ some_list[:3]
3425
3477
3426
3478
<details>
3427
3479
<summary>How to insert an item to the beginning of a list? What about two items?</summary><br><b>
3480
+
3481
+
- One item:
3482
+
3483
+
```
3484
+
numbers = [1, 2, 3, 4, 5]
3485
+
numbers.insert(0, 0)
3486
+
print(numbers)
3487
+
```
3488
+
3489
+
- Multiple items or list:
3490
+
3491
+
```
3492
+
numbers_1 = [2, 3, 4, 5]
3493
+
numbers_2 = [0, 1]
3494
+
numbers_1 = numbers_2 + numbers_1
3495
+
print(numbers_1)
3496
+
```
3497
+
3428
3498
</b></details>
3429
3499
3430
3500
<details>
@@ -3632,6 +3702,31 @@ list(zip(nums, letters))
3632
3702
3633
3703
<details>
3634
3704
<summary>What is List Comprehension? Is it better than a typical loop? Why? Can you demonstrate how to use it?</summary><br><b>
3705
+
3706
+
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.".
3707
+
3708
+
It's better because they're compact, faster and have better readability.
0 commit comments