Python Tutorial - 5 -Conditionals and Booleans: If, else, elif statements

Conditionals and Booleans: If, else, Elif Statements

/*

  • File: conditional-booleans_if-else-elif.md
  • Project: 5_conditional-booleans
  • File Created: Saturday, 11th March 2023 5:14:43 pm
  • Author: Hanlin Gu (hg_fine_codes@163.com)

  • Last Modified: Saturday, 11th March 2023 8:50:51 pm
  • Modified By: Hanlin Gu (hg_fine_codes@163.com>)
    */

1. Conditionals

1.1. if statement

if True:
    print('Conditional was True')

Output:

Conditional was True

The print is only executed when the statement of if is True. When the statement is False, it will not process the print() function.

if False:
    print('Conditional was True')

Output:

Conditionals are usually not hard coded as above. One usually write some code that evaluated to True or False like a boolean statement.

language = 'Python'

if language == 'Python':
    print('Conditional was True')

Output:

Conditional was True
# Comparison:
# Equal:            ==
# Not equal:        !=
# Greater than:     >
# Less than:        <
# Greater or equal: >=
# Less or equal:    <=
# Object identity:  is

The object identity is checks whether the two variables have the same object in memory.

1.2. Else statement

language = 'Python'

if language == 'Python':
    print('Language is Python')
else:
    print('No Match')

Output:

Language is Python
language = 'Java'

if language == 'Python':
    print('Language is Python')
else:
    print('No Match')

Output:

No Match

1.3. Elif: check multiple statements

elif statement could check for multiple statements.

# Check if the language is either Python or Java or JavaScript
# if none of those return 'No Match'

language = 'Java'

if language == 'Python':
    print('Language is Python')
elif language == 'Java':
    print('Language is Java')
elif language == 'JavaScript':
    print('Language is JavaScript')
else:
    print('No Match')

Output:

Language is Java

Python doesn’t have switch case.

2. Boolean Operations

and, or, and not

  • and

and: only both of the statements are true returns true.

user = 'Admin'
logged_in = True

if user == 'Admin' and logged_in:
    print('Admin Page')
else:
    print('Bad Creds')

Output:

Admin Page
user = 'Admin'
logged_in = False

if user == 'Admin' and logged_in:
    print('Admin Page')
else:
    print('Bad Creds')

Output:

Bad Creds
  • or

or: at least one of the statements is true will return true.

user = 'Admin'
logged_in = False

if user == 'Admin' or logged_in:
    print('Admin Page')
else:
    print('Bad Creds')

Output:

Admin Page
  • not

not: switch a boolean

user = 'Admin'
logged_in = False

if not logged_in:
    print('Please Log In')
else:
    print('Welcome')

Output:

Please Log In

3. Object identity: is

is means the two variables are actually the same object in memory. a is b is essentially id(a) == id(b)

a = [1, 2, 3]
b = [1, 2, 3]

print(a == b)
print(a is b)

Output:

True
False

One can find the identity by id() function.

a = [1, 2, 3]
b = [1, 2, 3]

print(id(a))
print(id(b))
print(a is b)

Output:

2355797972480
2355798349632
False
a = [1, 2, 3]
b = a

print(id(a))
print(id(b))
print(a is b)

Output:

2168446473728
2168446473728
True

4. False Values

For the list of False values below, the statement is False. But for everything else it will return as True

# False Values:
    # False
    # None
    # Zero of any numeric type
    # Any empty sequency. For example, '', [], ().
    # Any empty mapping (Dictionary). For example, {}.
condition = False

if condition:
    print('Evaluated to True')
else:
    print('Evaluated to False')

Output:

Evaluated to False

Notice that, if condition = 0 or 0.0, it is also a False statement.

condition = 0

if condition:
    print('Evaluated to True')
else:
    print('Evaluated to False')

Output:

Evaluated to False
condition = ' '

if condition:
    print('Evaluated to True')
else:
    print('Evaluated to False')

Output:

Evaluated to True
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值