-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_boolean_functions.py
More file actions
96 lines (82 loc) · 4.23 KB
/
Copy path08_boolean_functions.py
File metadata and controls
96 lines (82 loc) · 4.23 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#======================================================================================
# BOOLEAN FUNCTIONS
#--------------------------------------------------------------------------------------
# Boolean: Can be either True or False
# used to handle logic and decision-making
# python uses bool to check conditions (yes/no, right/wrong).
#======================================================================================
#======================================================================================
# Boolean from values
#======================================================================================
print(True)
print(False)
print(type(True))
#======================================================================================
# Functions & Methods that return True or False as output
#--------------------------------------------------------------------------------------
# bool(value) --built-in function / output: bool
# True -- if the value is non-empty or non-zero
# False -- if the value is empty or zero
#======================================================================================
print(bool(123)) #True
print(bool("hello")) #True
print(bool(1)) #True
print(bool(0)) #False
print(bool()) #False
print(bool("")) #False
print(bool(None)) #False
#======================================================================================
# any() & all() are built-in functions used with lists, tuples, sets etc.
# They return True or False
#======================================================================================
email = "BOb@123"
phone = "23456"
username = "David Wilson"
print(any([email, phone, username]))
#--------------------------------------------------------------------------------------
# any()--True if at least one value is True
#--------------------------------------------------------------------------------------
# ex: allow registration if any field is filled
print(any([email, phone, username]))
#--------------------------------------------------------------------------------------
# all()--True only if all values are True
#--------------------------------------------------------------------------------------
# ex: allow registration only if all fields is filled
print(all([email, phone, username]))
#--------------------------------------------------------------------------------------
# isinstance(value, type)--checks if a value belongs to certain data
# type.(built-in function / output:bool)
#--------------------------------------------------------------------------------------
print(isinstance(2, int))
#--------------------------------------------------------------------------------------
# startswith(substring)--checks if the string begins with a specific
# word.(str method / output: bool)
#--------------------------------------------------------------------------------------
txt = "Hello"
print(txt.startswith("H"))
#--------------------------------------------------------------------------------------
# endswith(substring)--checks if the string ends with a specific
# word.(str method / output: bool)
#--------------------------------------------------------------------------------------
print(txt.endswith("o"))
#--------------------------------------------------------------------------------------
# 'substring' in 'sting' -- checks if a word exists in the string.
#--------------------------------------------------------------------------------------
print("o" in txt)
#--------------------------------------------------------------------------------------
# isalpha()--checks if the string has only letters.
#--------------------------------------------------------------------------------------
country = "USA"
print(country.isalpha())
country = "USA1"
print(country.isalpha())
#--------------------------------------------------------------------------------------
# isnumeric()--checks if the string has only numbers.
#--------------------------------------------------------------------------------------
phone = "0987612345"
print(phone.isnumeric())
# . and - considered as special character that's why it is showing false in output
phone = "123-456-789"
print(phone.isnumeric())
price = "4.50"
print(price.isnumeric())