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

Naincy Mod 1 Python

Uploaded by

Komalgupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Naincy Mod 1 Python

Uploaded by

Komalgupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Name :- Naincy Jaiswal

Roll Number :- 22CS011107, Section :- B

Programming Practices II (YCS 4104)

LAB 1

1) Write a Python program to print “Hello Python”.

Program :- print("Hello Python")

Output :-

Hello Python

Explanation :-

• The message “Hello Python” is displayed using the print keyword


available in Python.

2) Write a Python program to do arithmetical operation :

Addition. Program :- a=int(input("Enter the 1st number :- "))

b=int(input("Enter the 2nd number :- ")) c=a+b print("Sum

of",a,"&",b,"is =",c)
Output :-

Enter the 1st number :- 5

Enter the 2nd number :- 7


Sum of 5 & 7 is = 12 Explanation

:-

• The first number is accepted from the user and stored in variable
‘a’.
• The second number is accepted from the user and stored in
variable ‘b’.
• The sum of both the numbers ‘a’ & ‘b’ is calculated & stored in
variable ‘c’.
• The value of ‘c’ is then displayed.

3) Write a Python program to do arithmetical operation :

Subtraction. Program :- a=int(input("Enter the larger number :- "))

b=int(input("Enter the smaller number :- ")) c=a-b

print("Difference between",a,"&",b,"is =",c)

Output :-

Enter the larger number :- 7


Enter the smaller number :- 4

Difference between 7 & 4 is = 3 Explanation

:-

• The larger number is accepted from the user & stored in variable
‘a’.
• The smaller number is accepted from the user & stored in variable
‘b’.
• The smaller number is subtracted from the larger one & the
difference is stored in variable ‘c’.
• The value of the variable ‘c’ is then displayed.

4) Write a Python program to do arithmetical operation :

Multiplication. Program :- a=int(input("Enter the 1st number :- "))

b=int(input("Enter the 2nd number :- ")) c=a*b

print("Product of",a,"&",b,"is =",c)

Output :-

Enter the 1st number :- 5

Enter the 2nd number :- 4

Product of 5 & 4 is = 20
Explanation :-

• The first number is accepted from the user & stored in variable ‘a’.
• The second number is accepted from the user & stored in variable

‘b’.
• Both the numbers are multiplied & the product is stored in variable
‘c’.
• The value of the variable ‘c’ is then displayed.

5) Write a Python program to do arithmetical operation : Division.

Program :- a=int(input("Enter the larger


number :- ")) b=int(input("Enter the smaller
number :- ")) c=a/b d=a//b print("Result of
Division is =",c) print("Result

of Floor Division is =",d)

Output:-

Enter the larger number :- 10 Enter

the smaller number :- 4

Result of Division is = 2.5


Result of Floor Division is = 2

Explanation :-

• The larger number is accepted from the user & stored in variable
‘a’.

• The smaller number is accepted from the user & stored in variable
‘b’.
• Firstly, normal division is performed by using the / operator and
the result obtained is stored in the variable ‘c’.
• Then, floor division is performed by using the // operator & the
quotient obtained is stored in the variable ‘d’.
• The values of both division & floor division are displayed
thereafter.

6) Write a Python program to find the area of a triangle. Program

:-
a=int(input("Enter the 1st side of the triangle :- "))

b=int(input("Enter the 2nd side of the triangle :- "))

c=int(input("Enter the 3rd side of the triangle :- ")) s=(a+b+c)/2

area=(s*(s-a)*(s-b)*(s-c))**0.5 print("Area of the triangle is

=",area) Output :-
Enter the 1st side of the triangle :- 10

Enter the 2nd side of the triangle :- 15

Enter the 3rd side of the triangle :- 20


Area of the triangle is = 72.61843774138907 Explanation

:-

• The length of the first side of the triangle is accepted from the user
& stored in ‘a’.
• The length of the second side of the triangle is accepted from the
user & stored in ‘b’.
• The length of the third side of the triangle is accepted from the
user
& stored in ‘c’.
• All the lengths are added and the result is halved. This is stored in
a variable ‘s’ which is known as semi-perimeter.
• Then to calculate the area, the value of ‘s’ is multiplied with the
difference which is obtained when the lengths of all the sides are
subtracted from the semi-perimeter. The square root of the above
result is the area of the triangle.
• The area of the triangle is then displayed.

7) Write a Python program to check Prime Number. Program


:-

a=int(input("Enter a number :- "))

c=0 for b in range(1,a+1): if(a

%b==0): c+=1 if(c==2):

print("Prime Number") else:

print("Not a Prime Number")

Output :-

Enter a number :- 5

Prime Number

Enter a number :- 9

Not a Prime Number

Explanation :-

• A number is accepted from the user & stored in variable ‘a’.


• A variable ‘c’ is initialized to 0.
• A for loop with index variable ‘b’ is iterated from 1 to a+1 with an
increment of 1.
• Within the body of the loop for each iteration, it is checked
whether the given number when divided by the current value of ‘b’
gives 0 as the remainder.
• If the remainder is 0, the value of ‘c’ is increased by 1.
• At the end of the for loop, if the value of c is 2 then the given
number is prime.
• A relevant message is displayed stating whether the given number
is prime or not.

8) Write a Python program to swap two variables. Program

:-
a=int(input("Enter the value of A :- ")) b=int(input("Enter
the value of B :- ")) print("Before Swapping :-") print("A
=",a) print("B =",b) t=a a=b b=t print("After
Swapping :-") print("A =",a) print("B

=",b)

Output :-

Enter the value of A :- 6

Enter the value of B :- 7

Before Swapping :-

A=6

B=7

After Swapping :-

A=7
B=6

Explanation :-

• First, the value of A & after that the value of B is accepted from
the user & stored in their respective variables.
• Before Swapping, the values of A & B are displayed.
• The value of ‘a’ is stored in a variable ‘t’.
• Then, the value of ‘b’ is assigned in variable ‘a’. • After that, the
current value of ‘t’ is assigned in variable ‘b’.
• Now, the numbers are swapped. The values of both A & B are
displayed again.

9) Write a Python program to check Leap Year. Program

:-
a=int(input("Enter a year :- "))

if(a%100==0): if(a%400==0):

print("Leap Year")
else: print("Not a
Leap Year") else: if(a%4==0):

print("Leap Year")
else: print("Not a Leap

Year")

Output :-

Enter a year :- 1900

Not a Leap Year

Enter a year :- 2024

Leap Year

Explanation :-

• A year is accepted from the user & stored in variable ‘a’.

• First of all, it is checked whether the year is divisible by 100.


• If the year is divisible by 100, then it is further checked whether
the year is divisible by 400 or not. If the year is divisible by 400,
then it is a leap year else not.
• If the year is not divisible by100, then it is checked whether it is
divisible by 4 or not. If it is divisible by 4, then it is a leap year
else not.
• A relevant message is displayed for the condition which satisfies
the given year.
10) Write a Python program to check if a number is odd or

even. Program :- a=int(input("Enter a number :- ")) if(a


%2==0): print("Even Number") else: print("Odd

Number") Output

:-

Enter a number :- 5

Odd Number

Enter a number :- 6

Even Number

Explanation :-

• An integer number is accepted from the user as input & stored in


variable ‘a’.
• Now, it is checked whether on dividing the given number by 2, the
remainder is 0 or not.

• If the remainder is 0, then the message “Even Number” is


displayed else the message “Odd Number” is displayed.

You might also like