Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Computer Programming 1

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Computer Programming 1

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

PYTHON PROGRAMMING

 Logical operators (and or not)

x=5

print(not(x > 3 and x < 10))

# returns False because not is used to reverse the result

print(x > 3 or x < 4)

# returns True because one of the conditions are true (5 is greater


than 3, but 5 is not less than 4)

print(x > 3 and x < 10)

# returns True because 5 is greater than 3 AND 5 is less than 10

 Identity operators (is is not)

is Returns True if both variables are the same object


x is y

Is not Returns True if both variables are not the same


object

x = ["apple", "banana"]

y = ["apple", "banana"]

z=x

print(x is z)

# returns True because z is the same object as x

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)

# to demonstrate the difference betweeen "is" and "==": this comparison


returns True because x is equal to y

Output:
True
False
True

 Membership operators (in not in)

in Returns True if a sequence with the specified value is present in the


object x in y

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"]

print ("pineapple" not in x)

# returns True because a sequence with the value "pineapple" is not in


the list

Output = True

Python Conditions and If statements


Python supports the usual logical conditions from mathematics:

 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

Python relies on indentation (whitespace at the beginning of a line) to define


scope in the code. An If statement without indentation will raise an error.

If statement: is used to check if a condition is true


k = 4
p = 68
if p > k:
print("p is greater than k") ………………...p is greater than k

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.")

You might also like