November 2022

Python Increment Operator (++) and Decrement Operator (–)

Python does not have traditional increment and decrement operators, like ++ or –. Instead, Python uses augmented assignment operators, which combine the assignment operator (=) with a mathematical operation, such as addition (+=) or subtraction (-=). For example, to increment a variable x by 1, you can use the augmented assignment operator x += 1 […]

Python Increment Operator (++) and Decrement Operator (–) Read More »

Python ‘if…else’ in a List Comprehension (Examples)

You can place an if…else statement into a list comprehension in Python. For example: Notice that the if…else statement in the above expression is not traditional if…else statement, though. Instead, it’s a ternary conditional operator, also known as the one-line if-else statement in Python. Example Given a list of numbers, let’s construct a list of

Python ‘if…else’ in a List Comprehension (Examples) Read More »

Python dict.get(‘key’) vs dict[‘key’] — Don’t Use Square Brackets

In Python, a dictionary is a collection of key-value pairs. Both dictionary.get(‘key’) and dictionary[‘key’] are used to retrieve the value associated with a given key in a dictionary. However, there is a clear difference between the two: To put it short, dictionary.get(‘key’) is the safer way to get value from a dictionary. You shouldn’t use

Python dict.get(‘key’) vs dict[‘key’] — Don’t Use Square Brackets Read More »