REVIEW- IF construct

Good Morning Boys

The Google meet link for today's class is

Meeting ID

------------------------------------------------------------------------------------------------------------
By the end of the session, you will be able to


  • Understand the basic decision construct in Python
  • Understand and apply the basic IF statement
  • Understand and apply the basic IF -else statement.
  • Understand and apply the basic IF-elif-else  statement
  • Apply the concepts learnt to solve the given problems.
--------------------------------------------------------------------------------------------------------------

Python - Decision Making

Decision making is anticipation of conditions occurring while execution of the program and specifying actions taken according to the conditions.
Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise.
Following is the general form of a typical decision making structure found in most of the programming languages −
Decision making statements in Python
Python programming language assumes any non-zero and non-null values as TRUE, and if it is either zero or null, then it is assumed as FALSE value.
Python programming language provides following types of decision making statements. Click the following links to check their detail.
Sr.No.Statement & Description
1if statements
An if statement consists of a boolean expression followed by one or more statements.
2if...else statements
An if statement can be followed by an optional else statement, which executes when the boolean expression is FALSE.
3nested if statements
You can use one if or else if statement inside another if or else if statement(s).

Let us go further −

Single Statement Suites

If the suite of an if clause consists only of a single line, it may go on the same line as the header statement.
Here is an example of a one-line if clause −
#!/usr/bin/python

var = 100
if ( var == 100 ) : print "Value of expression is 100"
print "Good bye!"
When the above code is executed, it produces the following result −
Value of expression is 100
Good bye!

Let us watch this video to take a recap of the topic on IF statement




----------------------------------------------------------------------------------------------------------------------



For this session, the following questions have to be done by you. I shall take up the solution after you have tried your hands at given questions:


Q1. Write a Python program to convert temperatures  from celsius to fahrenheit. 
 Formula : c/5 = f-32/9 

(where c = temperature in celsius and f = temperature in fahrenheit )


Conversion formula:
T(℉) = T(℃) x 9/5 + 32
Or,
T(℉) = T(℃) x 1.8 + 32
See this example:
  1. # Collect input from the user  
  2. cel = float(input('Enter temperature in Celsius: '))  
  3.   
  4. # calculate temperature in Fahrenheit  
  5. fah = (cel * 1.8) + 32  
  6. print(cel," degree celcius= ", fah," degrees Fahrenheit")  
Output:


EXERCISE

Q2: Accept two integer numbers from a user and return their product and  if the product is greater than 1000, then return their sum


Q3. Accept marks in 5 subjects from the user and find the average marks obtained. (average marks=sum of marks obtained/5)


Comments

Post a Comment

Popular posts from this blog

HOLIDAY HW