Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

10/9/2020 Day-5 [ 09-10-2020]

In [ ]: #Agenda of Today Class:


1. Software Installatations
2. Python Introduction
3. Python Basics
4. Python Varibales and Data Types
5. Python Keywords and Operators
6. Python Conditional Statements
7. iteration in python
problem sloving on them

In [ ]: #Conditional Statements in Python:


1. What are the conditional statements?
Conditional statements in python perform different computations or actions based the given conditions
to be
True of False.
why we use ?
To implement decision making and condition checking operations.
what are those?
1. if ..else statements
2. else if as elif
3. Nested if statement.

In [ ]: #If statement: (its used for decision making operations. it runs the code only the condition is True)
#syntax:
if condition/expression:
block of statements
else:
block of statements (its executed only if condition is false)

In [5]: #example:
x=5
y=10
if x > y:
print("x is less than y")
else:
print("x is greater than y")

x ix greater than y

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day-5 (Conditional Statements in Python)/Day-5 %5B 09-10-2020%5D.ipynb?download=false 1/8


10/9/2020 Day-5 [ 09-10-2020]

In [7]: a = 10
b = 10
if a < b:
print(" a is less than b")
elif a==b:
print(" a is equal to b")
else:
print("a is greater than b")
#Note: Here we got the wrong result by else block blindly execution.
# to correct the previus error made by "else block" we can use "elif"

a is equal to b

In [ ]: #How to execute conditional statement with minimal code:


#Syntax:
Statement A if condition Statement B else Statement C

In [20]: a,b= 10,6


"a is less than b" if a < b else " a is greater tha b"

Out[20]: ' a is greater tha b'

In [27]: #Example:
SavingAmt = 5000
withdrawAmt = int(input("amout to be withdraw:")) #type conversion string to integer values
if withdrawAmt > SavingAmt:
print("insuffient Balance")
elif SavingAmt==withdrawAmt
SavingAmt = SavingAmt-withdrawAmt #int() float() str()
print("Account Balance:"+str(SavingAmt))
#print("Zero Balance")
else:
SavingAmt = SavingAmt-withdrawAmt
print("Account Balance:"+str(SavingAmt)) #Type conversion integer to string

amout to be withdraw:3000
Account Balance:2000

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day-5 (Conditional Statements in Python)/Day-5 %5B 09-10-2020%5D.ipynb?download=false 2/8


10/9/2020 Day-5 [ 09-10-2020]

In [ ]: #Nested If:
#Syntax:
if condition #1:
if condition #1.1:
if condition #1.2:
[statements to be execute if 1 and 1.1 and 1.2 conditions are True]
else:
statement to execute if 1 and 1.1. are False
else:
alternate statements to execute

In [36]: #Example:
#time = int(input("Enter the Time")
time = int(input("enter time"))
if time>=6 and time <12:
print("Hello Good Morning")
else:
if (time==12):
print("Noon")
else:
if time > 12 and time <= 17:
print("afternoon")
else:
if time > 17 and time <= 20:
print("Good Evening")
else:
if time > 20 and time <24 or time >=0 and time<6:
print("Good Night")
else:
print("invalid Time!")

enter time21
Good Night

In [ ]: #Tasks: (Conditional statements)


1. Using if condition find the biggest of 3 numbers.
2. Using Nested if conditions make a logic for Username and password validation and prints the display messag
e for successfully
logged users.
2. Find the grade of students marks and percentage upto 5 members and prints the grade of highest percentage

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day-5 (Conditional Statements in Python)/Day-5 %5B 09-10-2020%5D.ipynb?download=false 3/8


10/9/2020 Day-5 [ 09-10-2020]

In [ ]: #Iterations in Python: (Loops in Python)


# what are loops?
Loops can execute a block of code number of times until a certain condition is met.
#what are those ?
1. for loop
2. while loop
# What is the use of for loop?
for loop is used iterate over every element of a sequence/data structures.
#Note: For loop is used when we know the fixed number of iterations.
#What is the use of while loop?
Its execute the code block multiple times until a certain condition is met.

In [ ]: #How to implement for loop?


for val in sequence: # ":" is called indentation symbol
body for for loop

In [43]: #Example:
s = "Python Programming"
print(type(s))
for i in s:
print(i,end=" ")

<class 'str'>
P y t h o n P r o g r a m m i n g

In [42]: li = [10,20,500,1000,30,60,90]
sum = 0
for val in li:
sum = sum+val
print(sum,end=" ")

1710

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day-5 (Conditional Statements in Python)/Day-5 %5B 09-10-2020%5D.ipynb?download=false 4/8


10/9/2020 Day-5 [ 09-10-2020]

In [57]: #for loop along used with range() function:

#We can generate a sequence of numbers using range() function.


# if u want generate 1 to numbers
#Note: range function object is "lazy" is in the sense its means its does not store the values in memor
y.
list(range(0,10))

Out[57]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [55]: #Syntax:
#range(start,stop,step-size)
print(list(range(10,101,5)),end=" ")

[10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]

In [59]: print(list(range(2,20,3)))

[2, 5, 8, 11, 14, 17]

In [62]: #Example: to print the list even numbers upto 100 numbers
for i in range(0,100):
if i %2 ==0:
print(i,end=" ")

0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74
76 78 80 82 84 86 88 90 92 94 96 98

In [65]: li = ["abc","xyz","pqr","friday","sunday"]
print(len(li))
for name in range(len(li)):
print(li[name])

5
abc
xyz
pqr
friday
sunday

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day-5 (Conditional Statements in Python)/Day-5 %5B 09-10-2020%5D.ipynb?download=false 5/8


10/9/2020 Day-5 [ 09-10-2020]

In [64]: #For loop with else statement:


numbers = [0,1,2,3,4,5,6,7,8,9]
for i in numbers:
print(i)
else:
print("No items are left")

0
1
2
3
4
5
6
7
8
9
No items are left

In [70]: #Break and Continue statement:


#Break : Break statement is a unique function in for loop that allows you to break or terminate the executio
n of the for loop.
months = ["jan","feb","mar","april","may","june"]
for m in months:
if m =="may":
break #Its break the loop at this iteration.
print(m)
else:
print("this is end")

jan
feb
mar
april

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day-5 (Conditional Statements in Python)/Day-5 %5B 09-10-2020%5D.ipynb?download=false 6/8


10/9/2020 Day-5 [ 09-10-2020]

In [75]: #Ex:
for x in range(10,50):
if x==15:
break
print(x)
else:
print("this is end point")

10
11
12
13
14

In [ ]: #Continue Statement:

Continue function, will terminate the current iteration of the for loop BUT will
continue execution of the remaiming iterations.

In [78]: for i in range(10,20):


if i==15:
continue
print(i,end=" ")
else:
print("No elements are left")

10 11 12 13 14 16 17 18 19 No elements are left

In [4]: a,b,c=10,20,30
print(a,b,c)

10 20 30

In [10]: x,y,z=input("enter numbers:").split()


print(x,y,z)

enter numbers:10 20 30
10 20 30

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day-5 (Conditional Statements in Python)/Day-5 %5B 09-10-2020%5D.ipynb?download=false 7/8


10/9/2020 Day-5 [ 09-10-2020]

In [ ]:

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day-5 (Conditional Statements in Python)/Day-5 %5B 09-10-2020%5D.ipynb?download=false 8/8

You might also like