coding180 icon
Coding180
Python If Statement: Syntax, Examples, and Exercises for Beginner

Python If Statement: Syntax, Examples, and Exercises for Beginner

Home > Tutorials > Python for beginners > Python Conditional Looping


If statements are one of the fundamental concepts in computer programming. They allow your program to make decisions based on certain conditions and execute specific actions accordingly. In Python, if statements are used extensively, making it a vital concept to learn for all aspiring programmers.

In this article, we will explore the basic syntax of Python if statements, provide practical examples of how they are used, and end with some exercises for you to test your knowledge.

Syntax of Python If Statements

The syntax of an if statement in Python is straightforward. It starts with the keyword "if" followed by a condition in parentheses. The condition can be any expression that evaluates to a Boolean value, which means it can either be True or False. If the condition is true, the code inside the if statement block will be executed.

Here is an example:

x = 5
if x > 3:
    print("x is greater than 3")

In this example, the condition is "x > 3", which evaluates to True since x is equal to 5, and 5 is greater than 3. Therefore, the code inside the if statement block will be executed, and the output will be "x is greater than 3".

Now let's look at a more complicated example:

age = 25
if age < 18:
    print("You are too young to vote.")
else:
    print("You are eligible to vote."

In this example, the condition is "age < 18", which evaluates to False since age is equal to 25, and 25 is not less than 18. Therefore, the code inside the if statement block will not be executed. Instead, the code in the else block will be executed, and the output will be "You are eligible to vote."

Python If Statement: Examples

Let's look at some practical examples of how Python if statements are used.

Example 1: Checking if a number is even or odd

number = int(input("Enter a number: "))
if number % 2 == 0:
    print(number, "is even.")
else:
    print(number, "is odd.")

In this example, we take input from the user and store it in the variable "number". We then use the modulus operator (%) to check if the number is divisible by 2. If the remainder is 0, the number is even, and the code inside the if block will be executed; otherwise, the code inside the else block will be executed.

Example 2: Checking if a string contains a specific character

sentence = "The quick brown fox jumps over the lazy dog."
letter = "a"
if letter in sentence:
    print("The sentence contains the letter 'a'.")
else:
    print("The sentence does not contain the letter 'a'.")

In this example, we have a string "sentence" that contains the phrase "The quick brown fox jumps over the lazy dog." We then check if the letter "a" is present in the sentence using the "in" keyword. If the letter is present, the code inside the if block will be executed; otherwise, the code inside the else block will be executed.

Python If Statement: Exercises for Beginners

Now it's time to test your knowledge with some exercises. Try to solve each problem and check your answers with the solutions provided.

Exercise 1: Write a program that takes two numbers from the user and prints the larger number.

num_1 = int(input("Enter the first number: "))
num_2 = int(input("Enter the second number: "))

if num_1 > num_2:
    print(num_1, "is larger than", num_2)
else:
    print(num_2, "is larger than", num_1)

Exercise 2: Write a program that takes a year from the user and checks if it is a leap year or not.

year = int(input("Enter a year: "))

if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
    print(year, "is a leap year.")
else:
    print(year, "is not a leap year.")

Conclusion

Python if statements are essential in programming as they allow you to create decision-making structures in your code. By understanding the syntax and examples of using if statements in Python, you can begin creating more complex


user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel