Python

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 »

How to Draw a Christmas Tree in Python (3 Different Ways)

Drawing a Christmas tree with asterisks (*) in Python isn’t probably the most impressive Christmas present, but it offers a nice challenge that tests your understanding of loops. This guide teaches you how to draw Christmas trees in Python in three different ways that incorporate loops and recursion. Let’s jump into it! Drawing Christmas Trees

How to Draw a Christmas Tree in Python (3 Different Ways) Read More »