Use Python If Statement

How to Use the Python If Statement

Use Python If Statement


Your Python knowledge depends on your usage of Python “If” statement. If your knowledge isn’t enough, try these If statement examples below.

The basic of logical programming is the if statement. These if statements may give you an edge against other programmers, and may also add to your Python programming skills.

If you want to know more about Python’s if else statement or logical conditions, this is the page you are looking for.

How Does it Work?

Basically, all programming languages, especially Python begin their conditional statements with if. Without it, they are not considered as logical. Yet, conditions are a set of programmer-identified rules which check whether a particular event is true or false. It simply checks the validity of the event.

Check this if format:


if an event is True:
Execute some commands…


The if statement is stand-alone. But in other conditions, elif and else can back it up to add other rules on place. Likewise, you can also add statements like “not“, “and“, “or“, and “in” with the if condition of Python.

How to Use Python’s If Else and If Statements

The if conditions tells Python to execute a set of commands as long as an event is true:


if 5 > 3:
print(“Valid”)
Output: Valid


Yet, a combination of the if else condition is more useful if another set of commands are needed to execute, if the first one else is false.


a = 10
b = 3
if a == b:
print(“They’re the same”)
else:
print(“They’re not the same”)
Output: They’re not the same


You can simply check the equality of the two variables (above) by having Python return a Boolean value. For example, printing a==b returns False:


a = 10
b = 3
print(a==b)
Output: False


Using Python’s if…elif…else Conditions

Javascript uses else if, but Python uses elif. Yet, Python’s conditional statement ends with else. Also, if you want to validate another event before ending your conditions, then you need to use elif statement.


a = 10
b = 3
if b == a:
print(a + b)
elif b * a == 30:
print(b – a)
else:
print(“impossible”)
Output: -7


The code above executes the command within the if statement if it’s event is true. If not, it executes the elif statement, otherwise, the results will be the else statement.

How to Use the “in”, “and” and “or” Python Keywords with If Statement

The use of in keyword with if statement checks if an item is present in a list or an array:


myList = [‘Python’, ‘JavaScript’, ‘Hello’]
if (‘Python’) in myList:
print(“It’s in the list”)
Output: It’s in the list


You can also add the and expression with if. This checks more than a single item:


myList = [‘Python’, ‘Javascript’, ‘Hello’]
if (‘Python’ and ‘Hello’) in myList:
print(“Hello Python”)
Output: Hello Python


To check if either item is on the list, you can use the or keyword:


myList = [‘Python’, ‘Javascript‘, ‘Hello’]
if (‘Python’ or ‘Bags’) in myList:
print(“One of them is on the list”)
Output: One of them is on the list


Use Python IF with the For Loop

The if condition also controls what happens in a for loop. You can set conditions while looping through a list or an array with a Python for loop.

Check the example below.


myList = myList = [‘Python’, ‘Javascript‘, ‘Hello’]
myList2 = [“Fish”, “Gold”, “Bag”]
if len(myList) == 3:
for items in myList:
print(items)
else:
for items2 in myList2:
print(items2)


The code only checks whether the length of myList is exactly three and loops through it if the statement is true, if not, it executes the else statement; this outputs each item in myList2.

You can also the above code to print all items in either list with exactly four wordcounts:


myList = [‘Python’, ‘Javascript’, ‘Hello’, ‘Books’, ‘Pizza’, ‘Four’]
myList2 = [“Fish”, “Gold”, “Bag”]
for items in (myList + myList2):
if len(items) == 4:
print(items)


The above code concatenates the two lists. Then, it checks if there are items with exactly four-word counts in both lists. It will loop them out if the statement is true.

Logical Statements and Automated Programs

There are more Python functions and statements that you can play with. You can also use if Statement with Python’s Lambda function, or in a Python List Comprehension.

Logical statements are the building blocks of many coded programs today. Like we said earlier, conditional statements add to your Python programming skills. With this, you can tweak things the way you want them.

Nowadays, machine learning and game development depend on these conditional statements for task automation. This allows their users to have their desired outputs.

So, as a computer engineer, learning more about them and how they work is essential to coding dynamic and responsive modern tech programs.


Use Python If Statement

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts