0% found this document useful (0 votes)
6 views9 pages

w2-Day1-Batch1 (05-05-2025) - Jupyter Notebook

The document is a Jupyter Notebook that covers conditional statements, looping statements, and control statements in Python programming. It includes examples of simple if, if-else, elif, nested if-else, for loops, while loops, and control statements like break and continue. Additionally, it presents tasks for practice related to month names, arithmetic operations, factorials, prime numbers, and palindromes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

w2-Day1-Batch1 (05-05-2025) - Jupyter Notebook

The document is a Jupyter Notebook that covers conditional statements, looping statements, and control statements in Python programming. It includes examples of simple if, if-else, elif, nested if-else, for loops, while loops, and control statements like break and continue. Additionally, it presents tasks for practice related to month names, arithmetic operations, factorials, prime numbers, and palindromes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

5/5/25, 8:09 PM w2-Day1-Batch1(05-05-2025) - Jupyter Notebook

1 ### 1. Conditional Statements:


2 - 1. Simple if
3 - 2. if-else
4 - 3. elif
5 - 4. elif ladder
6 - 5. nested if-else

elif:
syntax:

if(condition1): True
statement
elif(condition2): True
statement
else:
statement

In [1]: 1 # Write a program to find the biggest three numbers?


2 n1 = int(input("Enter n1 value: "))
3 n2 = int(input("Enter n2 value: "))
4 n3 = int(input("Enter n3 value: "))
5 if(n1>n2 and n1>n3):
6 print('n1 is biggest of n2 and n3')
7 elif(n2>n3):
8 print('n2 is biggest of n1 and n3')
9 else:
10 print('n3 is biggest of n1 and n2')

Enter n1 value: 23
Enter n2 value: 56
Enter n3 value: 10
n2 is biggest of n1 and n3

localhost:8888/notebooks/SOI-Batch1-(April-2025)/week2/w2-Day1-Batch1(05-05-2025).ipynb 1/9
5/5/25, 8:09 PM w2-Day1-Batch1(05-05-2025) - Jupyter Notebook

elif ladder:
syntax:

if(condition1):
statement
elif(condition2):
statement
elif(condition3):
statement
"
"
"
elif(conditionn):
statement
else:
statement

localhost:8888/notebooks/SOI-Batch1-(April-2025)/week2/w2-Day1-Batch1(05-05-2025).ipynb 2/9
5/5/25, 8:09 PM w2-Day1-Batch1(05-05-2025) - Jupyter Notebook

In [3]: 1 # Write a program to print the week day of a given input number?
2 day = int(input(("Enter day number: ")))
3 if(day == 1):
4 print('sunday')
5 elif(day == 2):
6 print('Monday')
7 elif(day == 3):
8 print('Tuesday')
9 elif(day == 4):
10 print('wednesday')
11 elif(day == 5):
12 print('thursday')
13 elif(day == 6):
14 print('Friday')
15 elif(day == 7):
16 print('saturday')
17 else:
18 print('Your enter invalid week number!...')
19 ​

Enter day number: 10


Your enter invalid week number!...

netsed if-else:
syntax:

if(condition):
if(condition):
statement
else:
statement
else:
statement

localhost:8888/notebooks/SOI-Batch1-(April-2025)/week2/w2-Day1-Batch1(05-05-2025).ipynb 3/9
5/5/25, 8:09 PM w2-Day1-Batch1(05-05-2025) - Jupyter Notebook

In [7]: 1 # example program on nested if-else.


2 # To develop the simple login page using nested if-else.
3 username ='[email protected]'
4 password = 'python@123'
5 u1 = input("enter username: ")
6 if(username == u1):
7 p1 = input("enter password: ")
8 if(password == p1):
9 print('Login Successfully!..')
10 else:
11 print('Incorrect password try again!...')
12 else:
13 print('Incorrect username try again!...')

enter username: [email protected]


enter password: python@123
Login Successfully!..

1 ### Looping Statements:


2 - Definition:
3 - To repeate the same steps until the condition becomes false.
4 - in python two looping statements:
5 - 1. for loop
6 - 2. while loop
7 #### for loop:
8 - for is a keyword to iterate the statements.
9 - for syntax:
10 for variable_name in range(start,stop,step):
11 statements
12 - start -> 0 -> it is optional
13 - stop ->size-1
14 - step -> 1 -> it is optional

localhost:8888/notebooks/SOI-Batch1-(April-2025)/week2/w2-Day1-Batch1(05-05-2025).ipynb 4/9
5/5/25, 8:09 PM w2-Day1-Batch1(05-05-2025) - Jupyter Notebook

In [16]: 1 # To print the 1 to 5 natural numbers using forloop?


2 # output: 1 2 3 4 5
3 for i in range(1,6):
4 print(i,end=" ")
5 ​

1 2 3 4 5

for loop execution process:


iteration1:

i = 1 in range [1,2,3,4,5]: True


print(1 )

iteration2:

i=2 in range [1,2,3,4,5]: True


print(2 )

i = 6 in range [1,2,3,4,5] : condition is False


exit from the for loop

In [17]: 1 # To print the 0 to 10 natural numbers?


2 for i in range(0,11):
3 print(i,end=" ")

0 1 2 3 4 5 6 7 8 9 10

In [20]: 1 # To print the 1 to 10 natural numbers using steping value as 3?


2 for i in range(1,11,3):
3 print(i,end=" ")

1 4 7 10

localhost:8888/notebooks/SOI-Batch1-(April-2025)/week2/w2-Day1-Batch1(05-05-2025).ipynb 5/9
5/5/25, 8:09 PM w2-Day1-Batch1(05-05-2025) - Jupyter Notebook

In [21]: 1 # To print the 1 to n natural number using given n range?


2 n = int(input("Enter n range: "))
3 for i in range(1,n+1):
4 print(i,end=" ")

Enter n range: 30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

In [23]: 1 # To print the 1 to n natural numbers in ascending order given n range?


2 n1 = int(input("Enter n range: "))
3 for i in range(1,n1+1):
4 print(i,end=" ")

Enter n range: 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

In [24]: 1 # To print the 1 to n natural numbers in descending order?


2 # input: m= 5
3 # output: 5 4 3 2 1
4 m = int(input("Enter m range: "))
5 for i in range(m,0,-1):
6 print(i,end=" ")

Enter m range: 5
5 4 3 2 1

In [25]: 1 for i in range(100,1,-3):


2 print(i,end=' ')

100 97 94 91 88 85 82 79 76 73 70 67 64 61 58 55 52 49 46 43 40 37 34 31 28 25 22 19 16 13 10 7 4

1 ### while loop:


2 - syntax:
3 while(condition):
4 statement
5 any updation
6 - we don't know the range

localhost:8888/notebooks/SOI-Batch1-(April-2025)/week2/w2-Day1-Batch1(05-05-2025).ipynb 6/9
5/5/25, 8:09 PM w2-Day1-Batch1(05-05-2025) - Jupyter Notebook
7 - but we condition
8 - while loop is working on condition
9 - It is mainly working on numbers related programs.

In [28]: 1 # while loop example:


2 # To count the number digits in a given number using while loop?
3 # input: n=4578
4 # output: number of digits is : 4
5 n = int(input("Enter a number: "))
6 count = 0
7 while(n!=0):
8 count = count+1
9 n = n//10
10 print('number of digits is:',count)

Enter a number: 02345


number of digits is: 4

1 - while loop execution process:


2 - Iteration1 :
3 n=4578 , count = 0
4 while(4578!=0): True
5 count = count+1 => 0+1 => 1=> count=1
6 n = 4578//10 ->
7 10)4578(457 -> // operator
8 4570
9 -----
10 8 -> % operator
11 n = 457
12 - Iteration2 :
13 n=457 , count=1
14 while(457!=0): true
15 count=count+1 => 1+1 => 2
16 n = n//10 => 457//10 -> 45
17 - Iteration3:
18 n = 45 , count=2
19 while(45!=0): True
20 count = count+1 => 2+1=> 3
21 n = n//10 => 45//10 -> 4
22 - Iteration4:
localhost:8888/notebooks/SOI-Batch1-(April-2025)/week2/w2-Day1-Batch1(05-05-2025).ipynb 7/9
5/5/25, 8:09 PM w2-Day1-Batch1(05-05-2025) - Jupyter Notebook
23 n=4, count=3
24 while(4!=0):True
25 count = count+1-> 3+1 -> 4
26 n=n//10 -> 4//10 ->
27 10)4(0 -> //
28 0
29 ---
30 4-> %
31 n=0
32 - Iteration5:
33 n=0,count =4
34 while(0!=0): false
35 - finally we print digit count is : 4

1 ### Control Statements:


2 - To control the flow of execution of a program.
3 - break:
4 - If the condition is True ,then stop the execution.
5 - continue:
6 - If the condition is True,then skip the value and continue execution until condition false.

In [29]: 1 # example program on break statement


2 for i in range(1,10):
3 if(i==5):
4 break;
5 else:
6 print(i,end=" ")

1 2 3 4

localhost:8888/notebooks/SOI-Batch1-(April-2025)/week2/w2-Day1-Batch1(05-05-2025).ipynb 8/9
5/5/25, 8:09 PM w2-Day1-Batch1(05-05-2025) - Jupyter Notebook

In [30]: 1 # example on continue statement?


2 for i in range(1,10):
3 if(i==5):
4 continue
5 else:
6 print(i,end=" ")

1 2 3 4 6 7 8 9

1 - Task1:
2 - To print the month names of a given input number?
3 - sample : input: 1
4 - output: january
5 - Task2:
6 - To perform all airthmetic operations using elif ladder of given input chioce? 1-> addition, 2-> subtraction
7 - to two input numbers.
8 - sample : enter your choice : 2
9 a = 2 , b= 5
10 - output: 2-5 => -3
11 - Task3:
12 - To print the factorial of given number?
13 - Task4:
14 - TO check the whether the given number is prime number or not?
15 - Task5:
16 - To print the reverse of given number?
17 - Task6:
18 - To check whether the given number or palindrome or not?

localhost:8888/notebooks/SOI-Batch1-(April-2025)/week2/w2-Day1-Batch1(05-05-2025).ipynb 9/9

You might also like