PYTHON Control Structures note, Study notes of Programming Languages

1. Sequential Control Structures 2. Selection Control Structures 3. Iterative Control Structures

Typology: Study notes

2020/2021

Uploaded on 02/07/2021

Mantu96
Mantu96 🇮🇳

5

(1)

3 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PYTHON Control Structures:
In PYTHON Programming Control Structures are classified into:
1. Sequential Control Structures
2. Selection Control Structures
3. Iterative Control Structures
Control_Flow Pic:
1. Sequential Control Structures:
It get excutes the lines of code in sequential order.
Example:
print("First Line")
print("Second Line")
print("Third Line")
Example:
print("First Line");print("Second Line");print("Third Line")
2. Selection Control Structures: (Conditional Control
Statements)
It is popularly known as Python Decision Making. Python
programming language provides following types of decision making
statements.
1 if statement (One-Way Decisions)
2 if .. else statement (Two-Way Decisions)
3 if .. elif .. else statement (Multi-Way Decisions)
4 Nested if .. else (inner Decisions)
5 Negative Conditions (Using Member-ship operators)
In Conditions, the following Comparison or Relational Operators
Commonly Using:
1 > greater than
2 >= greater than equalto
3 < less than
4 <= less than equalto
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download PYTHON Control Structures note and more Study notes Programming Languages in PDF only on Docsity!

PYTHON Control Structures: In PYTHON Programming Control Structures are classified into:

  1. Sequential Control Structures
  2. Selection Control Structures
  3. Iterative Control Structures Control_Flow Pic:
  4. Sequential Control Structures: It get excutes the lines of code in sequential order. Example: print("First Line") print("Second Line") print("Third Line") Example: print("First Line");print("Second Line");print("Third Line")
  5. Selection Control Structures: (Conditional Control Statements) It is popularly known as Python Decision Making. Python programming language provides following types of decision making statements. 1 if statement (One-Way Decisions) 2 if .. else statement (Two-Way Decisions) 3 if .. elif .. else statement (Multi-Way Decisions) 4 Nested if .. else (inner Decisions) 5 Negative Conditions (Using Member-ship operators) In Conditions, the following Comparison or Relational Operators Commonly Using: 1 > greater than 2 >= greater than equalto 3 < less than 4 <= less than equalto

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")