-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooleanPrecedenceShortC.py
More file actions
executable file
·69 lines (49 loc) · 1.19 KB
/
BooleanPrecedenceShortC.py
File metadata and controls
executable file
·69 lines (49 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 25 22:18:38 2021
@author: maherme
"""
#%%
# Precedence is: not --> and --> or
print(True or True and False)
print(True or (True and False))
print((True or True) and False) # This is different due to precedence
#%%
# Short-Circuiting:
# True or Y --> True, whatever Y was
# False and Y --> False, whatever Y was
a = 10
b = 2
if a/b > 2:
print('a is at least twice b')
#%%
# This code will fail:
a = 10
b = 0
if a/b > 2:
print('a is at least twice b')
#%%
# We can fix the problem above using short-circuiting:
a = 10
b = 0
if b > 0 and a/b > 2:
print('a is at least twice b')
#%%
# We can use even a better syntax:
a = 10
b = 0
if b and a/b > 2: # If b = None, in this case will not fail
print('a is at least twice b')
#%%
import string
name = 'Bob'
if name[0] in string.digits:
print("Name cannot start with a digit.")
#%%
# The code above will fail if name is empty, let's fix that:
name = ''
# if name is not None and len(name) > 0 and name[0] in string.digits:
if name and name[0] in string.digits: # This syntax is equivalent to the commented above
print("Name cannot start with a digit.")
#%%