How to Use an If, Elif, and Else Statement in Python
Categories: Tech | Pubby Cash Received:
0
Do you know how to use an if statement in Python? If you do not know how you should read this article because it will teach you how. First of all, the if statement is written using the if keyword in Python. Python also supports the logical conditions of mathematics. Here is what an if statement can look like.
a = 1
b = 2
if a < b:
print("a is less than b.")
The code above basically means that a is equal to 1 and b is equal to 2. The if statement is saying that a is less than b. Since 1 is less than 2, this would print a is less than b.. You must also make sure that the print statement is indented after the if statement because Python relies on indentation. If you do not indent, an error will appear. There is also an elif statement. This statement means that if the previous conditions were not true, then try this condition. Here is what an elif statement could look like.
a = 3
b = 3
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
The code above means that if b is greater than a, print b is greater than a. However, b is not greater than a. Since it is not, it moves on to the elif statement. The elif is saying that if a and be are equal, print a and b are equal. Since a and b are equal, this would print a and b are equal. Finally, there is the else statement. The else statement means that it catches anything which isn't caught by the preceding conditions. Here is what an else statement could look like
a = 2
b = 1
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"
The code above means that if b is greater than a, print b is greater than a. However, it is not, so it then sees if a and b are equal. Since a and b are not equal, the else statement sees if a is greater than b. Since 2 is greater than 1, this would print a is greater than b. Hopefully, after reading this article, you have a good idea of how the if, elif, and the else statement work....
Read more












This may look confusing if you have never heard of this formula, but I will try my best to explain it. n is the number of different items that you can choose. r is the number of items that you are allowed to choose. ! just means you are multiplying it by the same value each time but subtracting one each time it is multiplied. You do this until it gets to one. For example, 3! is equal to 3*(3-1)*(3-2) or 3*2*1. This would be equal to 6. Let's try out this formula with the real Powerball numbers. This formula would be
There are 31 numbers to choose from and you can only choose 5 of those 31 numbers. After calculating this, our final answer was 169,911 or about 170,000. That means out of about 170,000 people, only one person would win the grand prize. You can also write this as a decimal, by dividing 1 by 170,000. That means that there is a 0.00058824% chance of winning if you are playing real Powerball. However, when playing on Gopubby.com, out of ten numbers you pick three. The formula for this situation would be
After calculating this there is a one out of 120 chance of winning the grand prize or a 0.833% chance of winning. Gopubby.com also offers a small prize winner, which means guessing 2 of the 3 numbers correctly. The formula for this situation is
Considering the winning numbers are a set of three numbers, which offers three possible combinations of two numbers, so, 45/3 = 15. There would be a one out of 15 or 6.67% chance to win the small prize. As you can see, winning a lottery on Gopubby.com is a lot easier than winning one on Powerball.
Now, I...