0% found this document useful (0 votes)
5 views15 pages

Important Python Program With Easy Explaination

The document provides detailed instructions for creating various Python programs, including generating multiplication tables, calculating factorials, and checking if a number is positive, negative, or zero. Each program is explained line-by-line, detailing the purpose and function of each line of code. Additionally, it includes examples of expected input and output for clarity.

Uploaded by

artempmailj4ecap
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)
5 views15 pages

Important Python Program With Easy Explaination

The document provides detailed instructions for creating various Python programs, including generating multiplication tables, calculating factorials, and checking if a number is positive, negative, or zero. Each program is explained line-by-line, detailing the purpose and function of each line of code. Additionally, it includes examples of expected input and output for clarity.

Uploaded by

artempmailj4ecap
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

1.

Create a python program to take a number as an input and program will


display the table of the given number

Output of the program

Line-by-Line Explanation
Line 1
Line 2
number = int(input("Enter the Number:"))

input("Enter the Number:") function is used to

 Displays the message “Enter the Number:” on the screen.


 Takes input from the user.
 By default, input is taken as text (string).

int(...) function is used to

 Converts the input value from string to integer.

number =

 Stores the integer value entered by the user in the variable number.

✅Example:
If the user enters 5, then
number = 5
Line 3
for i in range(1,11):

for

 Used to start a loop.

 Loop variable that changes its value in each iteration.

range (1,11)

 Generates numbers from 1 to 10


 (11 is excluded)

 Indicates the start of the loop body.

✅This loop will run 10 times, with i values:

1, 2, 3, ..., 10

Line 4
print(number," x ",i," = ",number*i)

print()function is used to

 Displays output on the screen.

number," x ",i," = "

 Prints the multiplication format (like a table).

number * i

 Calculates the multiplication of number and i.

✅Example when number = 5 and i = 3

5 x 3 = 15
2. Create a python program to take a number as an input and program will
display the table of the given number using while loop

Output of the Program

Line-by-Line Explanation
Line 1
number = int(input("Enter the Number:"))

 Displays the message “Enter the Number:”


 Takes input from the user
 Converts the input into an integer using int()
 Stores the value in the variable number

✅Example: If user enters 6

number = 6
Line 2
i = 1

 Initializes the loop control variable i


 The loop will start counting from 1

Line 3
while i<=10:

 Starts a while loop


 The loop will run as long as the condition i <= 10 is true
 When i becomes greater than 10, the loop stops

Line 4
print(number," x ",i," = ",number*i)

 Prints the multiplication table format


 Multiplies number with the current value of i

✅Example when number = 6 and i = 4

6 x 4 = 24

Line 5
i = i + 1

 Increases the value of i by 1


 This step is very important
 Without this line, the loop will run forever (infinite loop)

How the Loop Works (Step-by-Step)


Iteration i Condition (i ≤ 10) Output

1 1 True 6x1=6

2 2 True 6 x 2 = 12

... ... ... ...


Iteration i Condition (i ≤ 10) Output

10 10 True 6 x 10 = 60

11 11 False Loop stops

Sample Input
Enter the Number: 6

Sample Output
6 x 1 = 6
6 x 2 = 12
...
6 x 10 = 60

3. Create a python program to take a number as an input and program will


calculate and display the factorial of the number.

Output of the Program


Line-by-Line Explanation
Line 1
number = int(input("Enter the Number:"))

 Displays the message “Enter the Number:”


 Takes input from the user
 Converts input into an integer
 Stores the value in the variable number

✅Example:
If user enters 5, then

number = 5

Line 2
fact = 1

 Initializes the variable fact


 fact will store the factorial result
 Factorial always starts with 1, not 0

Line 3
for i in range(1,number+1):

 Starts a for loop


 i is the loop variable
 range(1, number+1) generates numbers from 1 to number

 number + 1 is used because the last value is excluded

✅If number = 5, values of i will be:

1, 2, 3, 4, 5

Line 4
fact = fact * i

 Multiplies the current value of fact with i


 Stores the updated value back in fact
 This line calculates the factorial step by step
How Factorial is Calculated (Example: number = 5)

i fact calculation fact value

1 1×1 1

2 1×2 2

3 2×3 6

4 6×4 24

5 24 × 5 120

Line 5
print("Factorial of a ",number," is =",fact)

 Displays the final factorial result


 Prints both the input number and its factorial

Sample Input
Enter the Number: 5

Sample Output
Factorial of a 5 is = 120
4. Create a python program to take a number as an input and program will
calculate and display the factorial of the number using while loop.

Line-by-Line Explanation
number = int(input("Enter the Number:"))

✔ Displays a message, takes input from the user, converts it into an integer, and stores it in
number.

fact = 1

✔ initializes the variable fact to store the factorial result.

i=1

✔ Initializes the loop counter variable i with value 1.

while i <= number:

✔ Starts a while loop that runs as long as i is less than or equal to number.

fact = fact * i

✔ Multiplies the current value of fact with i and stores the result back in fact.
i=i+1

✔ Increases the value of i by 1 for the next loop iteration.

print("Factorial of a ", number, " is =", fact)

✔ Prints the final factorial of the entered number.

5. Write a python program to display all even numbers from 20 to 40.


Line-by-Line Explanation
Line 1:

for i in range(20, 41):

✔ Starts a for loop


✔ i is the loop variable
✔ range(20, 41) generates numbers from 20 to 40
✔ (41 is excluded)

Line 2:

if i % 2 == 0:

✔ Checks whether the current value of i is even

✔ % is the modulus operator (gives remainder)

✔ If remainder is 0, the number is even

Line 3:

print(i)

✔ Prints the value of i if it is even

✅Output

20
22
24
26
28
30
32
34
36
38
40
6. Write a python program to display all Odd numbers from 30 to 50.
Line-by-Line Explanation
Line 1:
i = 30
✔ Initializes the variable i with value 30.
Line 2:
while i <= 50:
✔ Starts a while loop
✔ The loop will continue running as long as i is less than or equal to 50
Line 3:
if i % 2 == 1:
✔ Checks whether the current value of i is odd
✔ % is the modulus operator
✔ If the remainder when divided by 2 is 1, the number is odd
Line 4:
print(i)
✔ Prints the value of i if it is an odd number
Line 5:
i=i+1
✔ Increases the value of i by 1 for the next loop iteration
✔ Prevents an infinite loop

✅Output
31
33
35
37
39
41
43
45
47
49
7. Write a Python program that takes a number as input and checks whether the number
is positive, negative, or zero.

Line-by-Line Explanation
Line 1:
number = int(input("Enter the number:"))
✔ Displays a message and takes a number from the user
✔ Converts the input into an integer
✔ Stores the value in the variable number
Line 2
if number > 0:
✔ Checks whether the entered number is greater than 0

Line 3:
print("Number is positive")
✔ Prints the message if the number is positive
Line 4:
if number < 0:
✔ Checks whether the entered number is less than 0

Line 5:
print("Number is negative")
✔ Prints the message if the number is negative

Line 6:
if number == 0:
✔ Checks whether the entered number is equal to 0

Line 7:
print("Number is Zero")
✔ Prints the message if the number is zero

8. Write a Python program that takes a number as input and checks whether the number
is positive, negative, or zero using if-elif-else.
Line-by-Line Explanation
Line 1:
number = int(input("Enter the number:"))
✔ Displays a message and takes a number from the user
✔ Converts the input into an integer
✔ Stores the value in the variable number

Line 2:
if number > 0:
✔ Checks whether the entered number is greater than 0

Line 3:
print("Number is positive")
✔ Prints this message if the condition is true

Line 4:
elif number < 0:
✔ Checks whether the number is less than 0
✔ This condition is checked only if the first if condition is false

Line 5:
print("Number is negative")
✔ Prints this message if the number is negative

Line 6:
else:
✔ Executes when all above conditions are false

Line 7:
print("Number is Zero")
✔ Prints this message when the number is equal to zero

You might also like