Abhigyan Ojha

What Does “if __name__ == ‘__main__'” Do in Python?

Today, let’s discuss something that’s all over the place in many code bases: what does if __name__ == ‘__main__’ do in Python? The statement if __name__ == ‘__main__’: checks if variable __name__ is set to the string value ‘__main__’ which holds only within the main source file from which you initially execute your code. In all other …

What Does “if __name__ == ‘__main__’” Do in Python? Read More »

How to Test Multiple Variables Against a Value in Python?

To test multiple variables x, y, z against a value in Python, use the expression value in {x, y, z}. Checking membership in a set has constant runtime complexity. Thus, this is the most efficient way to test multiple variables against a value. Here’s the code: Exercise: Add a fourth variable and run the modified …

How to Test Multiple Variables Against a Value in Python? Read More »