Computer Programming 1
Computer Programming 1
x=5
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x
print(x is z)
print(x is y)
# returns False because x is not the same object as y, even if they have
the same content
print(x == y)
Output:
True
False
True
x = ["apple", "banana"]
print ("banana" in x)
# returns True because a sequence with the value "banana" is in the list
Output = True
not in Returns True if a sequence with the specified value is not present
in the object x not in y
x = ["apple", "banana"]
Output = True
Equals: k == p
Not Equals: k != p
Less than: k < p
Less than or equal to: k <= p
Greater than: k > p
Greater than or equal to: k >= p
Elseif (elif)
The elif keyword means "if the previous conditions were not true, then try this
condition".
a = 65
b = 65
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")…………………… a and b are equal
Else
The else keyword outputs anything which isn't captured by the preceding
conditions
k = 79
p = 12
if p > k:
print("p is greater than k")
elif a == b:
print("p and k are equal")
else:
print("k is greater than p")……………… k is greater than p
And
The and keyword is used to combine conditional statements.
a = 105
b = 90
c = 150
if a > b and c > a:
print("Both conditions are True")……….. Both conditions are true
Or
The or keyword is used to combine conditional statements:
a = 105
b = 90
c = 150
if a > b or a > c:
print("At least one of the conditions is True")… Both conditions are
true
Not
The not keyword is used to reverse the result of the conditional statement:
a = 33
b = 200
if not a > b:
print("a is NOT greater than b")……... a is NOT greater than b
Nested If
You can have if statements inside if statements, this is
called nested if statements.
k = 34
if k > 8:
print("Above ten,")
if k > 20:
print("and also above 20!")
else:
print("but not above 20.")