999 Python Super Tips (35 → 44)
999 Python Super Tips (35 → 44)

999 Python Super Tips (35 → 44)

Tags
Published
November 13, 2022
Author
Mai The Dung

35. An “if-elif” block can exist without the else block at the end

However, “elif” cannot stand on its own without an “if” step before it.
notion image

36. Check whether 2 strings are anagrams using sorted()

notion image

37. Get the value of a character in Unicode

notion image

38. Get keys and values of a dictionary in a single line

notion image

39. Add a value in the first position in a list

If you use append(), you are going to insert new values from the right.
We can also use insert() to specify the index and the element where we want to insert this new element. In our case, we want to insert it in the first position, so we use 0 as the index
notion image

40. Lambda functions can only be in one line

You cannot have lambdas in more than one line.
notion image

41. Conditionals statements in lambda should always include the “else” part

notion image
Noe that this is a feature of the conditional expression and not of the lambda itself.

42. filter() returns a new object

notion image

43. map() returns a new object

notion image

44. range() starts by default at 0

So you don’t need to include it at all.
notion image