The Big If
Comparators
3<4 5>=5 10==10 12!=13
Boolean operators
True or False
(4 < 5) and (6 >= 6)
this() and nor that()
Conditional statements
if this_might_be_true(): print "This really is true."elif print "None of the above."
If Statement
if statement is the easiest method for making a decision in Python, it simply states that is something is true, python should perform the steps that follow.
Syntax:
if condition:
statement(s)
Header
➱ If the condtion is true, then the indented statement gets executed.
➱ The indented statement implies that its execution is dependent on the header.
➱ There is no limit on the number of statements that can appear under an if block.
-
'if' is keyword,followed by a condition and ended with 'colon'
-
We can use condition with bracket() also, but not mandatory.
-
Statements(Block of code) are indented inside the 'if' statement and are excuted only if condition evaluates to true, otherwiswe the control skips the bock of statements inside the if statements and only statement2 will be excuted.
Example:
#program to demonstrate if statement.
age = int(input("Enter age to continue:"))
if age >= 18:
print("You are an Adult")
print("You are not an Adult")
If we want the program to display some appropriate message or to exceute certain set if instructions in case the condition becomes FALSE. Here comes the 'else' statement.
else statement can be use with if statement to exceute a block of code when the condition is false.
✦ if-elif-else Statement
if-elif-else statement is used in the situations where a user can decide among multiple options. As soon as one of the conditions controlling the 'of' true, then statement/statements associated with that 'if' are executed, an the reset of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed,control flows from top to down.
Syntax:
if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)
example:
#this program will accept percentage (%) of a student and display grade
percentage = float(input("enter % of the student"))
if percentage > 85:
print('A')
elif percentafe >70 and percentage <=85:
print('B')
elif percentage >60 and percentage <=70:
print('C')
elif percentage >45 and percentage >=60:
print('D')
else:
print('E')
✦ Loops
Looping constructs provide the facility to excute a set of statements in a program repetitevely, based on a condition. The statements in a loop are executed again and again as long as particular logiacal condition remains true. this conditions is checked through a variable calle control variable. When the conitio beomes false, loop terminates. It is the responsibilities of the programmer to ensure that this condition eventually does becomes false. ➣ A loop statement allows us to execute a statements or group of statements multiple time ➣ Types of LOOP
1. for loop 2. while loopit is also known as 'indefinite loops'
(i) for loop
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
example:
>>> pets=["bunny","tuffy","maggie"]
>>> for x in pets:
print(x)
* Nested for Loops
basically loop inside a loop. The "inner loop" will be executed one time for each iteration of the "outer loop"
example:
pet=["bunny"]color=["camourflag"]for x in pet: for y in color: print(x,y)
(ii)The while Loop
With the while loop we can execute a set of statements as long as a condition is true.
i = 1
while i < 6:
print(i)
i += 1
No comments: