-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path6_if.py
More file actions
94 lines (80 loc) · 2.04 KB
/
6_if.py
File metadata and controls
94 lines (80 loc) · 2.04 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Author : Ahmed Shaaban
Date : Sep 13, 2021
Purpose: demonstrating if condition
"""
x=4
if (x>0):
print("x is larger than zero")
#%%
# on blocks
# if block statment "must" be indented.
decision = True
if (decision == False):
print("We are using Python")
print("We still inside the if block")
print("Hi, I am outside the if condition") # this is the only one that should appear
#%% Quick if
mydecision=True
if mydecision :
print("yes we are true")
#%% alternative quick if
if True:
print("yes this is true")
#%% if and else if
x = 0
if (x > 0):
print("x is positive")
elif (x < 0):
print("x is negative")
else:
print("neither positive nor negative")
#%% tip on elif
# what is wrong with the following if statement.
x= 91
if (x > 100):
print("x is larger than 100")
elif (x > 90):
print("x is larger than 90")
elif (x >80):
print("x is larger than 80")
elif (x >70):
print("x is larger than 70")
else:
print("x is unknown")
# if the if statment is not mutually exclusive, only the first true statment will be exectuted.
#%% The first solution to the the not mutually exclusive statement
x= 91
if (x > 100):
print("x is larger than 100")
if (x > 90):
print("x is larger than 90")
if (x >80):
print("x is larger than 80")
if (x >70):
print("x is larger than 70")
#%% The second solution to the the not mutually exclusive statement
x= 81
if (x > 100):
print("x is larger than 100")
elif (x>90 and x<= 100):
print("x is larger than 90 and less than or equal 100")
elif (x>80 and x<= 90):
print("x is larger than 80 and less than or equal 90")
elif (x>70 and x<=80):
print("x is larger than 70 and less than or equal 80")
else:
print("x is unknown")
#%% to understand what is happening
print(x>80 and x<=90)
#%% nested if condition
x=-1.
if (isinstance(x, int)):
if (x < 0):
print("x is int less than 0")
#%% another way
x=1
if (isinstance(x, int) and x < 0):
print("x is int less than 0")