Skip to content

Commit a2db75e

Browse files
authored
Feature/new python answers (bregman-arie#191)
* 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
1 parent f85dacd commit a2db75e

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

README.md

+95
Original file line numberDiff line numberDiff line change
@@ -3033,8 +3033,38 @@ You can then assign a function to a variables like this `x = my_function` or you
30333033
<details>
30343034
<summary>Write a function to determine if a number is a Palindrome</summary><br><b>
30353035

3036+
- Code:
3037+
3038+
```
3039+
from typing import Union
3040+
3041+
def isNumberPalindrome(number: Union[int, str]) -> bool:
3042+
if isinstance(number, int):
3043+
number = str(number)
3044+
return number == number[::-1]
3045+
3046+
print(isNumberPalindrome("12321"))
3047+
```
3048+
3049+
- 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"))
30363058
```
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+
30373064
```
3065+
True
3066+
```
3067+
30383068
</b></details>
30393069

30403070
#### Python - OOP
@@ -3241,6 +3271,28 @@ False
32413271

32423272
<details>
32433273
<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+
32443296
</b></details>
32453297

32463298
<details>
@@ -3427,6 +3479,24 @@ some_list[:3]
34273479

34283480
<details>
34293481
<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+
34303500
</b></details>
34313501

34323502
<details>
@@ -3634,6 +3704,31 @@ list(zip(nums, letters))
36343704

36353705
<details>
36363706
<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.
3711+
3712+
- For loop:
3713+
3714+
```
3715+
number_lists = [[1, 7, 3, 1], [13, 93, 23, 12], [123, 423, 456, 653, 124]]
3716+
odd_numbers = []
3717+
for number_list in number_lists:
3718+
for number in number_list:
3719+
if number % 2 == 0:
3720+
odd_numbers.append(number)
3721+
print(odd_numbers)
3722+
```
3723+
3724+
- List comprehesion:
3725+
3726+
```
3727+
number_lists = [[1, 7, 3, 1], [13, 93, 23, 12], [123, 423, 456, 653, 124]]
3728+
odd_numbers = [number for number_list in number_lists for number in number_list if number % 2 == 0]
3729+
print(odd_numbers)
3730+
```
3731+
36373732
</b></details>
36383733

36393734
<details>

0 commit comments

Comments
 (0)