Software engineering and personal development

Tag: python (Page 5 of 10)

How to Include Multiple Conditions at Once in Python

Similar to the case of using any(), we can also use a method that allows us to check whether all conditions are met. This can also greatly reduce the complexity of the code since you do not need to use multiple and checks.

Let us see this with an example.

Let us assume that we have the following conditions where we are checking whether we have more than 50 points in each school course:

 math_points = 51
 biology_points = 78
 physics_points = 56
 history_points = 72
 ​
 my_conditions = [math_points > 50, biology_points > 50,
                  physics_points > 50, history_points > 50]

Now passing all of them means that each condition should be met. To help us with that, we can simply use all(), as can be seen in the following snippet:

Continue reading

How to Quickly Check Whether at Least One Condition is Met in Python

In many cases, we may have multiple conditions that we want to check, and going through each one of them can be a clutter.

First and foremost, we should keep in mind that we write code for other humans to read it. These are our colleagues that work with us, or people who use our open source projects.

As such, checking whether at least one condition is met can be done using a very quick way by using the method any().

Continue reading

How to Quickly Simply “if” Statements in Python

There can be cases when we want to check whether a value is equal to one of the multiple other values.

One way that someone may start using is using or and adding multiple checks. For example, let us say that we want to check whether a number is 11, 55, or 77. Your immediate response may be to do the following:

 m = 1
 ​
 if m == 11 or m == 55 or m == 77:
     print("m is equal to 11, 55, or 77")

There is fortunately another quick way to do that.

Continue reading
« Older posts Newer posts »

© 2024 Fatos Morina

Theme by Anders NorenUp ↑