Python Tutorial for Beginners - 5. Conditionals and Booleans - If, Else, and Elif Statements

Table of Contents

  1. Run Code 1 in Python
  2. Run Code 2 in Python
  3. Run Code 3 in Python
  4. Run Code 4 in Python
  5. Run Code 5 in Python
  6. Run Code 6 in Python
  7. Run Code 7 in Python
  8. Run Code 8 in Python
  9. Run Code 9 in Python
  10. Run Code 10 in Python
  11. Run Code 11 in Python
  12. Run Code 12 in Python
  13. Run Code 13 in Python
  14. Run Code 14 in Python
  15. Run Code 15 in Python
  16. Run Code 16 in Python
  17. Run Code 17 in Python
  18. Run Code 18 in Python

Run Code 1 in Python

1
2
3
4
5
6
7
8
9
10
11
12
13
# Comparisons:
# Equal:            ==
# Not Equal:        !=
# Greater Than:     >
# Less Than:        <
# Greater or Equal: >=
# Less or Equal:    <=
# Object Identity:  is

language = 'Python'

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

start.py

Run Code 2 in Python

1
2
3
4
5
6
language = 'Python'

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

start.py

Run Code 3 in Python

1
2
3
4
5
6
7
8
9
10
language = 'Java'

if language == 'Python':
    print('Language is Python')
elif language == 'Java':
    print('Language is java')
elif language == 'JavaScritp':
    print('Language is JavaSrtipt')
else:
    print('No match')

start.py

Run Code 4 in Python

1
2
3
4
5
6
7
user = 'Admin'
logged_in = True

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

start.py

Run Code 5 in Python

1
2
3
4
5
6
7
user = 'Admin'
logged_in = False

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

start.py

Run Code 6 in Python

1
2
3
4
5
6
7
user = 'Admin'
logged_in = False

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

start.py

Run Code 7 in Python

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

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

start.py

Run Code 8 in Python

1
2
3
4
5
6
a = [1, 2, 3]
b = [1, 2, 3]

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

start.py

Run Code 9 in Python

1
2
3
4
5
6
a = [1, 2, 3]
b = [1, 2, 3]

print(id(a))
print(id(b))
print(a == b)

start.py

Run Code 10 in Python

1
2
3
4
5
6
a = [1, 2, 3]
b = [1, 2, 3]

print(id(a))
print(id(b))
print(id(a) == id(b))

start.py

Run Code 11 in Python

1
2
3
4
5
6
a = [1, 2, 3]
b = a

print(id(a))
print(id(b))
print(id(a) == id(b))

start.py

Run Code 12 in Python

1
2
3
4
5
6
7
8
9
10
11
12
13
# False Values:
    # False
    # None
    # Zero of any numeric type
    # Any empty sequence. For example, '', (), [].
    # Any empty mapping. For example, {}.

condition = False

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

start.py

Run Code 13 in Python

1
2
3
4
5
6
condition = None

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

start.py

Run Code 14 in Python

1
2
3
4
5
6
condition = 0

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

start.py

Run Code 15 in Python

1
2
3
4
5
6
condition = 10

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

start.py

Run Code 16 in Python

1
2
3
4
5
6
condition = []

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

start.py

Run Code 17 in Python

1
2
3
4
5
6
condition = {}

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

start.py

Run Code 18 in Python

1
2
3
4
5
6
condition = 'Test'

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

start.py