








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
1. Sequential Control Structures 2. Selection Control Structures 3. Iterative Control Structures
Typology: Study notes
1 / 14
This page cannot be seen from the preview
Don't miss anything!









PYTHON Control Structures: In PYTHON Programming Control Structures are classified into:
5 == equal 6 != not equal Python 'if' Statement It executes a set of statements conditionally, based on the value of a logical expression. Syntax: if expression : statement_ statement_ .... Example: num = 3 if num > 0: print(num, "It is a Positive Number.") print("This is always printed.") num = - if num > 0: print(num, "It is a Positive Number.") print("This is also always printed.") Example: num=input("Enter any Number: ") if int(num) > 0: print(num, "It is a Positive number.") Example: x=int(input("Enter Any Number: ")) if x>0: print("Value is +VE"); print("It is always get Executed"); y=int(input("Enter Any Number: ")) if y<0: print("Value is -VE"); print("It is always get Executed");
print("This is always Printed.") else: print(num, "It is a Negative Number.") rint("This is also Printed.") Example: num = int(input("Enter a number: ")) if (num % 2) == 0: print("is Even") else: print("is Odd") Python if...elif...else The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE. Syntax if Expression1: statement(s)_ statement(s)_ elif Expression2: statement(s)_ statement(s)_ elif Expression3: statement(s)_ statement(s)_ else: statement(s)_ statement(s)_ NOTE: Core Python does not supports switch or case statements as in other languages. Example: num=input("Enter any Number: ") print(type(num)) if int(num) > 0:
print(num, "It is a Positive number.") elif int(num)<0: print(num, "It is a Negative number.") else: print(num, "It is a ZERO.") Example: a=200;b= if b>a: print("b is bigger") elif a==b: print("Both are equal") else: print("a is Bigger") Example: x=int(input("Enter Any Number: ")) y=int(input("Enter Any Number: ")) if x>0 and y>0: print("X and Y are Positives: ") elif x==0 and y==0: print("Values are ZEROS") else: print("Values are Negatives") print("Thank U") Example: grade=int(input("Enter Your Marks: ")) if grade >= 90: print("Your Grade is A+") elif grade >=80: print("Your Grade is A") elif grade >=70: print("Your Grade is B+") elif grade >=60: print("Your Grade is B") elif grade >=50: print("You are Pass") else:
else : Statement_ Statement_ .... else : Statement_ Statement_ Example: num = int(input("Enter a number: ")) if num >= 0: if (num == 0): print("ZERO") else: print("Positive number") else: print("Negative number") Example: x=int(input("Enter Any Number: ")) if x!=0: if x<0: print("-VE") else: print("+Ve") else: print("ZERO") Example: x=int(input("Enter Any Number: ")) if x==0: print("ZERO") else: if x>0: print("+VE") else: print("-VE")
Example: x=int(input("Enter Any Number: ")) if x>=0: if not x==0: print("+VE") else: print("ZERO") else: print("-VE") Example: grade=int(input("Enter Your Marks: ")) if grade >= 90: print("Your Grade is A+") else: if grade >=80: print("Your Grade is A") else: if grade >=70: print("Your Grade is B+") else: if grade >=60: print("Your Grade is B") else: if grade >=50: print("You are Pass") else: print("Sorry You Failed") Example: year =int(input("Enter Any Year: ")) if (year % 4) == 0: if (year % 100) == 0: if (year % 400) == 0: print("is a leap year") else: print("is not a leap year") else: print("is a leap year")
Example3: x=int(input("Enter Any Number: ")) if x>0: print("Hey Positive") print("Thank U") Example4: x=int(input("Enter Any Number: ")) if x>0: print("Hey Positive") print("Thank U") if x<0: print("Hey Negative") print("Thank U") #The above example get executed successfully, #But it is not satisfying syntax #It is hardcoding, It never recomended #In realtime industry...!! Example4: x=int(input("Enter Any Number: ")) if x>0: print("Hey Positive") print("Thank U") else: print("Hey Negative") print("Thank U") Example5: x=int(input("Enter Any Number: ")) if x%2==0: print("Hey Even") print("Thank U") else: print("Hey Odd") print("Thank U") Example6:
x=int(input("Enter Any Number: ")) y=int(input("Enter Any Number: ")) if x%2==0 and y%2==0: print("Hey Even Numbers") print("Thank U") else: print("Hey Odd Numbers") print("Thank U") Example7: x=int(input("Enter Any Number: ")) if x>0: print("X is Positive Value") elif x<0: print("X is Negative Value") else: print("ZERO") Example8: x=int(input("Enter Any Number: ")) y=int(input("Enter Any Number: ")) if x>0 and y>0: print("X & Y are Positive Value") elif x==0 and y==0: print("X & Y are ZEROS") else: print("Negative Values") Example9: Marks=int(input("Enter Valid Marks: ")) if Marks>=90 and Marks<=100: print("Grade is A+") elif Marks>=80 and Marks<89: print("Grade is A") elif Marks>=70 and Marks<79: print("Grade is B+") elif Marks>=60 and Marks<69: print("Grade is B") elif Marks>=50 and Marks<59:
Example12: x=int(input("Enter Any Number: ")) if x>=0: if x==0: print("ZERO") else: print("+VE Number") else: print("Negative Number") NOTE: The above logic develop in minimum 5 other ways Example13: Marks=int(input("Enter Valid Marks: ")) if Marks>=90 and Marks<=100: print("Grade is A+") else: if Marks>=80 and Marks<=89: print("Grade is A") else: if Marks>=70 and Marks<=79: print("Grade is B+") else: if Marks>=60 and Marks<=69: print("Grade is B") else: if Marks>=50 and Marks<=59: print("Grade is C") else: if Marks>=40 and Marks<=49: print("JUST PASS") else: print("Invalid Marks/Failed") Example15: x=int(input("Enter Any Number: ")) if not x>0:
print("Negative Value") else: print("Positive Value")