Python

5 Python Conditional statements 2 – Simple If..else

simple If..else statement

Simple If..else statement: code

cities = ['LA', 'Chicago', 'kansas city', 'columbia','Boston']
for city in cities:
if city == 'Boston':
print(city.upper())
else:
print(city.title())

Results:

 La
Chicago
Kansas City
Columbia
BOSTON

if statement is an expression that can be evaluated as True or False and is called a conditional test.

If a conditional test evaluates to True, Python executes the code following the if statement. If the test evaluates to False, Python ignores the code following the if statement.

The simplest conditional test checks whether the value of a variable is equal to the value of interest.

== sign – check for True or False . Example.

if city == 'bmw':
print(city.upper())

Note: Conditional tests are CASE sensitive. CASE == case ‘False’, but CASE == CASE is True.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s