How to do an Excel if in Python

1 min readExcelGetting StartedFundamentals
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

If-clause in Python are pretty similar to what you could find in excel.

In Excel an if formula would look like this.

=IF(CONDITION, IF_TRUE_WE_DO_THAT, IF_FALSE_WE_DO_THAT)
IF condition in Excel

in Python this translates to

if condition:
	if_true_we_do_that
else:
	if_false_we_do_that
A if clause that looks like an excel if

Example

Let's take the example in which I have two cells, A1 with a value of 2 and A2 with a value of 3.

The idea is to add a logic that returns the max value out of the two cells using if.

In Excel that would translate to

=IF(A1 > A2, A1, A2)
IF clause in excel that returns the biggest value

In Python

For the example I do print the values when it fulfills the condition in Python

if A1 > A2:
	print("A1")
else:
	print("A2")
A if clause that looks like an excel if

Here you are, you know now how to do an if in Python.

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.