0% found this document useful (0 votes)
9 views

Conditional - While - Loop - Jupyter Notebook

The document discusses various conditional statements and loops in Python like if, if-else, nested if, if-elif-else statements and while loops. It provides examples of using these statements and loops to check conditions and repeat code. It also discusses infinite loops and how to break out of them.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Conditional - While - Loop - Jupyter Notebook

The document discusses various conditional statements and loops in Python like if, if-else, nested if, if-elif-else statements and while loops. It provides examples of using these statements and loops to check conditions and repeat code. It also discusses infinite loops and how to break out of them.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

5/31/23, 4:59 AM conditional_while_loop - Jupyter Notebook

Indentation : It is one of the most important feature in


Python. It indicates the block of code.
In [4]: if 5<6:
print('hii')
print('out of if')

hii
out of if

Conditional Statement :- It is used to the check the flow


of control according to the given condition.
Single/Simple if
if-else
Nested if
if-elif-else

if statement : The if statement is used to test a particular condition


and if the condition is true, it executes a block of code known as if-
block.
Syntax: if expression:
statement
statement

localhost:8888/notebooks/Desktop/New folder/conditional_while_loop.ipynb 1/8


5/31/23, 4:59 AM conditional_while_loop - Jupyter Notebook

In [6]: age = int(input("Enter your age: "))


if age >=18:
print("Eligible to vote")

Enter your age: 15

In [7]: age = int(input("Enter your age: "))


if age >=18:
print("Eligible to vote")

Enter your age: 22


Eligible to vote

if-else statement : If the condition is true, then the if-block is


executed. Otherwise, the else-block is executed.
Syntax:
if condition:
#block of statements
else:
#another block of statements (else-block)

localhost:8888/notebooks/Desktop/New folder/conditional_while_loop.ipynb 2/8


5/31/23, 4:59 AM conditional_while_loop - Jupyter Notebook

In [8]: age = int(input("Enter your age: "))


if age >=18:
print("Eligible to vote")
else:
print("you are still a kid")

Enter your age: 22


Eligible to vote

In [9]: age = int(input("Enter your age: "))


if age >=18:
print("Eligible to vote")
else:
print("you are still a kid")

Enter your age: 5


you are still a kid

localhost:8888/notebooks/Desktop/New folder/conditional_while_loop.ipynb 3/8


5/31/23, 4:59 AM conditional_while_loop - Jupyter Notebook

Nested if : to check more than one condition at a time.


Syntax:
if condition:
# block of statements

if condition:
# block of statements

else:
# block of statements
else:
# block of statements

In [11]: n = int(input("enter a number: "))


if n%2==0:
if n>0:
print("Even & +ve")
else:
print("Even and -ve")
else:
if n>0:
print("Odd and +ve")
else:
print("Odd and -ve")

enter a number: -5
Odd and -ve

if-elif-else: The elif statement enables us to check multiple conditions


and execute the specific block of statements depending upon the
true condition among them. We can have any number of elif
localhost:8888/notebooks/Desktop/New folder/conditional_while_loop.ipynb 4/8
5/31/23, 4:59 AM conditional_while_loop - Jupyter Notebook

statements in our program depending upon our need.


Syntax:
if expression 1:
# block of statements

elif expression 2:
# block of statements

elif expression 3:
# block of statements

else:
# block of statements

In [12]: n = int(input("Enter any number: "))


if n%2 ==0 and n>0:
print("Even and +ve")
elif n%2 ==0 and n<0:
print("Even and -ve")
elif n%2 !=0 and n>0:
print("Odd and +ve")
elif n%2 !=0 and n<0:
print("Odd and -ve")
else:
print("Number is 0")

Enter any number: 88


Even and +ve

localhost:8888/notebooks/Desktop/New folder/conditional_while_loop.ipynb 5/8


5/31/23, 4:59 AM conditional_while_loop - Jupyter Notebook

Q. Wapp to check whether the entered character is vowel or


consonant.

In [17]: ch = input("Enter any character: ")


if ch.upper() in 'AEIOU':
print("it is a vowel")
else:
print("it is a consonant")

Enter any character: o


it is a vowel

In [18]: ch = input("Enter any character: ")


if ch.lower() in 'aeiou':
print("it is a vowel")
else:
print("it is a consonant")

Enter any character: P


it is a consonant

Loop: Something that repeats continuously.

While Loop
Python while loop is used to run a block code until a certain conditio
n is met.

The syntax of while loop is:

initialization
while condition:
#body of while
increment/decrement

1. A while loop evaluates the condition


2. If the condition evaluates to True, the code inside the while l
oop is executed.
3. condition is evaluated again.
4. This process continues until the condition is False.
5. When condition evaluates to False, the loop stops.

localhost:8888/notebooks/Desktop/New folder/conditional_while_loop.ipynb 6/8


5/31/23, 4:59 AM conditional_while_loop - Jupyter Notebook

In [19]: i = 1
while i<=6:
print(i)
i+=1

1
2
3
4
5
6

localhost:8888/notebooks/Desktop/New folder/conditional_while_loop.ipynb 7/8


5/31/23, 4:59 AM conditional_while_loop - Jupyter Notebook

Infinite while Loop in Python

If the condition of a loop is always True, the loop runs for infinite
times (until the memory is full). For example,

In [1]: age = 32

# the test condition is always True
while age > 18:
print('You can vote')
break

You can vote

The 👆 condition always evaluates to True. Hence, the loop body will
run for infinite times.

Q. Wapp to print table of enter number using while loop.

In [20]: num = int(input("Enter the number: "))


i = 1
while i<=10:
print(num,'X',i,'=',num*i)
i+=1

Enter the number: 7


7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70

In [ ]: ​

localhost:8888/notebooks/Desktop/New folder/conditional_while_loop.ipynb 8/8

You might also like