# How To Fix SyntaxError: invalid syntax in if statement in Python

The error SyntaxError: invalid syntax in if statement in Python occurs when the syntax of the if statement is incorrect, such as capitalizing If, miswriting the “==” operator, or forgetting to put a colon. In this article, we will show the causes and solutions.

## Why does the SyntaxError: invalid syntax error occur in the If statement?

When learning Python, it will be elementary to encounter basic syntax errors in Python, the SyntaxError: invalid syntax error in the If statement can happen because we capitalize the letter If, using the “=” sign instead of the “==” . Or maybe we forgot the colon at the end of the sentence. Let’s take a look at a few examples that create errors.

Python is a case-sensitive language, so when capitalizing If word, we will get SyntaxError: invalid syntax error.

Error Example:

price1 = 199
price2 = 299

# Error occurs when capitalizing "If"
If price1 == price2:
print("price 1 equals price 2")

Output:

SyntaxError: invalid syntax

Errors can also occur when using the wrong operator equals comparison conditions. The example below uses the “=” operator for comparison.

Error Example:

price1 = 199
price2 = 299

# Use "=" operator for comparison
if price1 = price2:
print("price 1 equals price 2")

Output:

SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

Sometimes forgetting to put a colon at the end of the “if” statement will cause the error

Error Example:

price1 = 199
price2 = 299

# Forget to put a colon at the end of the "if" statement
if price1 == price2
print("price 1 equals price 2")

Output:

SyntaxError: expected ':'

## Solution for SyntaxError: invalid syntax in if statement in Python

We have understood a few causes of errors. The solution is straightforward. Pay attention to the syntax when you write the if statement.

### Lowercase “if”

Python is a case-sensitive language, so the correct syntax is that we should lowercase this if word. Then the Error will be fixed

Example:

price1 = 199
price2 = 299

# Lowercase this "if" word
if price1 == price2:
print("price 1 equals price 2")
else:
print("price 1 not equals price 2")

Output:

price 1 not equals price 2

### Use the correct comparison operator

To compare in Python, we use the operator “==“, not the operator “=” because the sign “=” is an assignment in Python.

Example:

price1 = 199
price2 = 199

# Use the operator "==" to compare
if price1 == price2:
print("price 1 equals price 2")
else:
print("price 1 not equals price 2")

Output:

price 1 equals price 2

### Don’t forget the colon.

To avoid errors, you should remember to put a colon at the end of the statement and the end of the “else” word.

Example:

price1 = 199
price2 = 199

# Remember to put a colon at the end of the statement
if price1 == price2:
print("price 1 equals price 2")
else:
print("price 1 not equals price 2")

Output:

price 1 equals price 2

## Summary

The article has covered a few simple syntax errors in the if statement that Python beginners often encounter. Pay attention to the syntax while writing the statement. We will never encounter the same Error. We hope you understand and fix the Error soon.