What is life without conditions?
So you have learnt to tell the computer, Number_of_apples_raja_has=5 and also learnt to ask it to do some math problems for you. BTW, Number_of_apples_raja_has=5 is called as assignment. You are assigning the value 5 to the variable Number_of_apples_raja_has. We can think of assignment as one kind of sentence in the Python language.
Let's change the above problem statement slightly.
Problem: Raja has 5 apples. Rani has 4 apples. Who has more apples?
Can you write a Python program to solve this?
Drag the text to the correct variable assignment
You will start by saying
raja_apples=5
rani_apples=4
But after this what? If you don't know some words in a langauge you cannot explain an idea. Similarly, since you don't know how to tell a computer to check for a condition you are stuck.
You can't obviously say:
If raja_apples is more than rani_apples then Raja is the winner. Otherwise Rani is the winner.
You can't say the above because the computer does not know English and it cannot understand the above sentence. So lets see how we can create a similar sentence in Python. In Python such sentences are represented by "if else" statements.
You have to follow some rules to write such sentences. The condition sentence is written in Python as follows.
if condition:
statements
else:
statements
So the sentence is constructed as follows.
You start with 'if'
Then write the condition
Then end with ':' to say to the computer that the condition is over.
Then hit 'enter' key on your keyboard. Next is very important in Python language. You have to indent the next line. That is, hit 'space' 4 times or hit 'tab' once. Then you can write assignment statements which you learnt earlier. You can write as many as you want. Everytime you hit enter and go to a new line, make sure the indentation matches.
After you are done with 'if', you can move to 'else'. If you don't have an else condition you can stop here.
Else sentence is easier.
You just type 'else'(note the indentation),
then ':'
hit 'enter',
hit 'tab' and type your statements.
Let us try some simple if statements. Notice the "tab"(spaces) before the print.
if 3<5:
print("3 lesser")
Now try:
if 3<5:
print("3 lesser")
else:
print("3 not lesser")
Lets now look at the conditions supported in Python. They are
Equals: a == b. (That is, in English we say a is equal to b. In Python it's a == b)
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
More than: a > b
More than or equal to: a >= b
A little hard no? It's ok. Once you practice a few it will become easy.
Now let's see with this new knowledge how can we write the sentence 'If raja_apples is more than rani_apples then Raja is the winner. Otherwise Rani is the winner.' in Python
Try this out below. You should get Raja is the winner as the output. Remember to indent. Replace the ?? with the correct terms.
raja_apples=5
rani_apples=4
if ?? > ??:
print("Raja is the winner")
??:
print("Rani is the winner")See how cleanly and easily we are able to convert an English sentence into Python. 'If' became '
'is more than' became '>'
'then' became '
'Otherwise' became '
That's all. This is why Python is such an easy language to learn.
- This is how you write all programs.
- Understand the problem in your language(English/Telugu).
- Think of a solution in your langauge.
- Convert the sentences into Python language.
- Feed it to computer and let the computer solve the problem for you.
Let's try this out with another small example.
Problem: Take a number. Check if it is even or odd.
Let's think in English first:
- Store num from the user
- If
when the number by 2 is zero then print even - Otherwise, print odd
Now, let's convert these English sentences to Python. Replace the ?? with the right values.
num=5
if num%2==0:
print("??")
else:
print("??") See how we converted our English thoughts:
- "If" became if
- "reminder when the number divided by 2 became num % 2
- "is zero" became ==0(notice the double =)
- "then" became :
- "print" became print
- "Otherwise" became else:
Problem: Take a temperature value. Tell if it's hot or cold weather.
Let's think in English first:
- Store temperature from the user
- If temperature is
25 degrees then print hot weather - Otherwise, print cold weather
Now, let's convert these English sentences to Python:
temp=30
if temp>
print("
else:
print("
See how we converted our English thoughts:
- "If" became if
- "more than" became >
- "25" became 25
- "then" became :
- "print" became print
- "Otherwise" became else:
Before we finish with conditions, let's look at couple of final concepts.
Let's go back to the Raja, Rani, apples problem. We saw how we can represent "If Number_of_apples_raja_has is more than Number_of_apples_rani_has then Raja is the winner. Otherwise Rani is the winner." in Python. But what if I add another condition. "If Number_of_apples_raja_has is more than Number_of_apples_rani_has then Raja is the winner. Otherwise Rani is the winner. If both have equal number of apples, then it's a tie".
You could do something like
if Number_of_apples_raja_has > Number_of_apples_rani_has:
print("Raja is the winner")
if Number_of_apples_rani_has > Number_of_apples_raja_has:
print("Rani is the winner")
if Number_of_apples_rani_has == Number_of_apples_raja_has:
print("It's a tie")But Python has a more elegant language construct, 'elif'.
So, a new vocabulary. See, just like in languages, when something becomes hard to explain in the world, we invent new words :)
Just like 'else', 'elif' is also optional. You can also have any number of 'elif's. Python will look at all the elif conditions and find the the first TRUE one and execute those statements.
The syntax(language rules) of elif is
if condition1:
statement1(s)
elif condition2:
statement2(s)
elif condition3:
statement3(s)
else:
statement4(s)Now that we new a word in Python language lets use it to write the solution to the above problem. We can write "If Number_of_apples_raja_has is more than Number_of_apples_rani_has then Raja is the winner. Otherwise Rani is the winner. If both have equal number of apples, then it's a tie" in Python as below. Replace the ?? with the right keywords and execute the program.
Number_of_apples_raja_has=5
Number_of_apples_rani_has=4
if Number_of_apples_raja_has > Number_of_apples_rani_has:
print("Raja is the winner")
?? Number_of_apples_rani_has > Number_of_apples_raja_has:
print("Rani is the winner")
??:
print("It's a tie")Also, just like any other language you can write different sentences with the same meaning. For example a different solution to the above problem but using '<'(less than) can also be written. Why don't you try it out. Some other excercises you should try out before moving on:
- Store marks from different subjects. Find their total. If total is above or equal to 80, give A grade. Else, give B grade.
- Check if an year is a leap year or not.
- Create your own problems and solve them.
For the final concept in conditions, lets look at logical operators.
Problem: Find if a number is divisible by 5 and 11.
num=55
if(num%5==0):
print("Divisble by 5")
if(num%55==0):
print("Divisble by 11")What will the above program do? It will output:
Divisible by 5
Divisible by 11
But we want to print:
Divisible by 5 and 11
So how can we do it.