CONDITIONAL STATEMENTS
Decision making is anticipation of conditions occurring while execution
of the program and specifying actions taken according to the conditions.
Following is the general form of a typical decision making structure found
in most of the programming languages −
Python programming language
assumes
zero and
any non-
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:
•If statements:
•An if statement consists of a boolean expression
followed by one or more statements.
•If-else statements:
•An if statement can be followed by an optional else
statement, which executes when the boolean expression
is FALSE.
•Nested if else statements:
•You can use one if or else if statement inside
another if or else if statement(s).
CONDITIONAL STATEMENTS
CONDITIONAL STATEMENTS
If statements:
Syntax:
if (expression):
statement(s)
Ex:
val=5
if (val==5):
print("I am five")
OUTPUT:
CONDITIONAL STATEMENTS
If statements:
EX: Program to check the given number is Negative
num=float(input("Enter the number:: "))
if num<0:
print("The number {0} is Negative: ".format(num))
OUTPUT:
CONDITIONAL STATEMENTS
If-else statements:
Syntax:
if (expression):
statement(s)
else:
statement(s)
Ex:
val=6
if (val==5):
print("I am five")
else:
print(“I am not five”)
OUTPUT:
CONDITIONAL STATEMENTS
If-else statements:
EX: Program to check the given number is Positive or Negative
num=float(input("Enter the number:: "))
if num<0:
print("The number {} is Negative: ".format(num))
else:
print("The number {} is Positive: ".format(num))
OUTPUT:
If-elif-else statements:
Syntax:
CONDITIONAL STATEMENTS
if (expression):
statement(s)
elif (exp):
statement(s)
else:
statement(s)
Ex:
val=10
if val>10:
print("Value is greater than 10")
elif (val<10):
print("value is less than 10")
else:
print("Value is equal to 10")
OUTPUT:
CONDITIONAL STATEMENTS
If-elif-else statements:
EX: Program to check the given Year is Leap or Not
year=int(input("Enter the year:: "))
if year%400==0 and year%100==0:
print("The year {0} is Leap year. ".format(year))
elif year%4==0 and year%100!=0:
print("The year {0} is Leap year. ".format(year))
else:
print("The year {0} is NOT Leap year. ".format(year))
OUTPUT:
Nested If-else statements:
Syntax:
if expression1:
statement(s)
CONDITIONAL STATEMENTS
if expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
else:
statement(s)
var = 100
if var < 200:
print (“Value is less than 200”)
if var == 150:
print (“Which is 150” )
elif var == 100:
print (“Which is 100”)
else :
print (“This is inner else”)
else:
print (“This is out else part”)
CONDITIONAL STATEMENTS
Nested If-else statements:
EX: Program to check the largest number of given three numbers
num1=float(input("Enter the First number:: "))
num2=float(input("Enter the Second number:: "))
num3=float(input("Enter the Third number:: "))
if num1>num2:
if (num1>num3):
print("The number {} is the largest number.".format(num1))
else:
print("The number {} is the largest number.".format(num3))
elif (num2>num3):
print("The number {} is the largest number.".format(num2))
else:
print("The number {} is the largest number.".format(num3))
Example:
1. Write a program to find small number of two numbers (if)
2. Write a Program to input age and check whether the person is eligible to vote or not.
3. Write a program to find biggest number among three numbers.
Python For Loops
Python For Loops
• 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
Print each fruit in a fruit list:
fruits = ["apple", "kiwi", "cherry"]
for x in fruits:
print(x)
Python While Loops
Looping Through a String
Even strings are iterable objects, they contain a sequence of
characters:
Example
Loop through the letters in the word "banana":
for x in "banana":
print(x)
The pass Statement
• if statements cannot be empty, but if you for some reason have
an if statement with no content, put in the pass statement to
avoid getting an error.
• Example
a = 33
b = 200
if b > a:
pass
The break Statement
• With the break statement we can stop the loop even if the
while condition is true:
• Example
Exit the loop when i is 3:
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
The break Statement
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
The continue Statement
• With the continue statement we can stop the current iteration,
and continue with the next:
• Example
Continue to the next iteration if i is 3:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
The continue Statement
The continue Statement
The range() Function
Example
Using the range() function:
for x in range(6):
print(x)
for x in range(2, 6):
print(x)
Nested Loops
• A nested loop is a loop inside a loop.
• The "inner loop" will be executed one time for each iteration
of the "outer loop":
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)

Python basics Notes as per aktu syllabus unit-1

  • 1.
    CONDITIONAL STATEMENTS Decision makingis anticipation of conditions occurring while execution of the program and specifying actions taken according to the conditions. Following is the general form of a typical decision making structure found in most of the programming languages − Python programming language assumes zero and any non- non-null values as TRUE, and if it is either zero or null, then it is assumed as FALSE value.
  • 2.
    Python programming languageprovides following types of decision making statements: •If statements: •An if statement consists of a boolean expression followed by one or more statements. •If-else statements: •An if statement can be followed by an optional else statement, which executes when the boolean expression is FALSE. •Nested if else statements: •You can use one if or else if statement inside another if or else if statement(s). CONDITIONAL STATEMENTS
  • 3.
    CONDITIONAL STATEMENTS If statements: Syntax: if(expression): statement(s) Ex: val=5 if (val==5): print("I am five") OUTPUT:
  • 4.
    CONDITIONAL STATEMENTS If statements: EX:Program to check the given number is Negative num=float(input("Enter the number:: ")) if num<0: print("The number {0} is Negative: ".format(num)) OUTPUT:
  • 5.
    CONDITIONAL STATEMENTS If-else statements: Syntax: if(expression): statement(s) else: statement(s) Ex: val=6 if (val==5): print("I am five") else: print(“I am not five”) OUTPUT:
  • 6.
    CONDITIONAL STATEMENTS If-else statements: EX:Program to check the given number is Positive or Negative num=float(input("Enter the number:: ")) if num<0: print("The number {} is Negative: ".format(num)) else: print("The number {} is Positive: ".format(num)) OUTPUT:
  • 7.
    If-elif-else statements: Syntax: CONDITIONAL STATEMENTS if(expression): statement(s) elif (exp): statement(s) else: statement(s) Ex: val=10 if val>10: print("Value is greater than 10") elif (val<10): print("value is less than 10") else: print("Value is equal to 10") OUTPUT:
  • 8.
    CONDITIONAL STATEMENTS If-elif-else statements: EX:Program to check the given Year is Leap or Not year=int(input("Enter the year:: ")) if year%400==0 and year%100==0: print("The year {0} is Leap year. ".format(year)) elif year%4==0 and year%100!=0: print("The year {0} is Leap year. ".format(year)) else: print("The year {0} is NOT Leap year. ".format(year)) OUTPUT:
  • 9.
    Nested If-else statements: Syntax: ifexpression1: statement(s) CONDITIONAL STATEMENTS if expression2: statement(s) elif expression3: statement(s) else: statement(s) else: statement(s) var = 100 if var < 200: print (“Value is less than 200”) if var == 150: print (“Which is 150” ) elif var == 100: print (“Which is 100”) else : print (“This is inner else”) else: print (“This is out else part”)
  • 10.
    CONDITIONAL STATEMENTS Nested If-elsestatements: EX: Program to check the largest number of given three numbers num1=float(input("Enter the First number:: ")) num2=float(input("Enter the Second number:: ")) num3=float(input("Enter the Third number:: ")) if num1>num2: if (num1>num3): print("The number {} is the largest number.".format(num1)) else: print("The number {} is the largest number.".format(num3)) elif (num2>num3): print("The number {} is the largest number.".format(num2)) else: print("The number {} is the largest number.".format(num3))
  • 11.
    Example: 1. Write aprogram to find small number of two numbers (if) 2. Write a Program to input age and check whether the person is eligible to vote or not. 3. Write a program to find biggest number among three numbers.
  • 12.
    Python For Loops PythonFor Loops • 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.
  • 13.
    Example Print each fruitin a fruit list: fruits = ["apple", "kiwi", "cherry"] for x in fruits: print(x)
  • 14.
  • 15.
    Looping Through aString Even strings are iterable objects, they contain a sequence of characters: Example Loop through the letters in the word "banana": for x in "banana": print(x)
  • 16.
    The pass Statement •if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error. • Example a = 33 b = 200 if b > a: pass
  • 17.
    The break Statement •With the break statement we can stop the loop even if the while condition is true: • Example Exit the loop when i is 3: i = 1 while i < 6: print(i) if i == 3: break i += 1
  • 18.
    The break Statement fruits= ["apple", "banana", "cherry"] for x in fruits: print(x) if x == "banana": break
  • 19.
    The continue Statement •With the continue statement we can stop the current iteration, and continue with the next: • Example Continue to the next iteration if i is 3: i = 0 while i < 6: i += 1 if i == 3: continue print(i)
  • 20.
    fruits = ["apple","banana", "cherry"] for x in fruits: if x == "banana": continue print(x) The continue Statement The continue Statement
  • 21.
    The range() Function Example Usingthe range() function: for x in range(6): print(x)
  • 22.
    for x inrange(2, 6): print(x)
  • 23.
    Nested Loops • Anested loop is a loop inside a loop. • The "inner loop" will be executed one time for each iteration of the "outer loop": adj = ["red", "big", "tasty"] fruits = ["apple", "banana", "cherry"] for x in adj: for y in fruits: print(x, y)