Data Science Explorer

[Basic Grammar] Python If ... Else 본문

Python

[Basic Grammar] Python If ... Else

grace21110 2023. 10. 20. 20:38
반응형
  • Python Conditions and If statements 
    • Equals: a == b
    • Not Equals: a != b
    • Less than: a < b
    • Less than or equal to: a <= b
    • Greater than: a > b
    • Greater than or equal to: a >= b

Those are usually used in IF statement and loops. For IF statement, we use if keywords.

 

Example

a = 33
b = 95
if b > a:
  print("b is greater than a")

** In IF statement, indentation is very important, so you should keep the correct indentation for the statements. ** 

** Do not forget ":" when you give IF statement!**

 

  • Elif 

It is like "if the previous conditons were not true, then try this condition."

a = 89
b = 89
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")

 

  • Else 

It is anything that is not caught by the preceding conditions. 

 

Example 

a = 883
b = 99
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")

 

  • And 

It is a logical operator, and is used to see if both conditions are true.

 

Example

Test if a is greater than b, AND if c is greater than a:

a = 200
b = 21
c = 450
if a > b and c > a:
  print("Both conditions are True")

 

  • Or 

It is also a logical operator, and is used to check if at least one of the conditions is true. 

a = 200
b = 33
c = 500
if a > b or a > c:
  print("At least one of the conditions is True")

 

  • Not 

It is used to reverse the result of the conditional statement. 

 

Example 

Test if a is NOT greater than b:

a = 33
b = 200
if not a > b:
  print("a is NOT greater than b")

 

  • Nested If 

You can have if statements inside if statements.

 

Example 

x= 45 

if x > 10: 
	print ("Above ten,")
   	if x > 20: 
    	print ("and also above 20!")
    else: 
    	print ("but not above 20.")

 

  • The pass statement 

In general, if statement cannot be empty, but if it is empty, you can use pass statement to avoid getting an error. 

 

Example 

a = 11
b = 23

if b > a:
  pass

 

Exercises 

1. Write a Python program that compares two numbers, x and y. If x is greater than y, print "x is greater than y." If they are equal, print "x and y are equal." Otherwise, print "y is greater than x."

x = 22
y = 90 

if x > y: 
	print ("x is greater than y.")
    
elif x == y: 
	print("x and y are equal.")

else: 
	print("y is greater than x.")

 

2. Write a Python program that checks if the value of a is greater than b, and if the value of c is greater than a. If both conditions are true, print "Both conditions are met," otherwise, print "At least one condition is not met."

a = 42
b = 27
c = 55

if a > b and c > a: 
	print ("Both conditions are met")
else: 
	print ("At least one condition is not met.")

3. Write a Python program to determine if a given number num is positive or negative. If num is greater than zero, print "Positive." If num is less than or equal to zero, check if it is less than or equal to -10. If so, print "Negative and less than or equal to -10." Otherwise, print "Negative but greater than -10."

num = -12

if num > 0: 
	print ("Positive")
	if num <= 0 : 
    	print ("Negative and less than or equal to -10.")
    else: 
    	print ("Negative but greater than -10.")

'Python' 카테고리의 다른 글

[Basic Grammar] Python For Loops  (1) 2023.10.21
[Basic Grammar] Python While Loops  (0) 2023.10.20
[Basic Grammar] Python Casting  (0) 2023.10.20
[Basic Grammar] Python Numbers  (0) 2023.10.20
[Basic Grammar] Python Data Types  (0) 2023.10.20