Python Programme For Class IX Practical

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

Q1.

This program adds two numbers

Ans.

# This program adds two numbers

num1 = 1.5

num2 = 6.3

# Add two numbers

sum = num1 + num2

# Display the sum

print(“The sum of num1 & num2:”, sum)

Q2. Store input numbers and sum of this

Ans.

# Store input numbers

num1 = input('Enter first number: ')

num2 = input('Enter second number: ')

# Add two numbers

sum = float(num1) + float(num2)

# Display the sum

print(“The sum of two numbers” sum)


Q3. Python Program to find the area of triangle

Ans.

# Python Program to find the area of triangle

a=5

b=6

c=7

# Uncomment below to take inputs from the user

# a = float(input('Enter first side: '))

# b = float(input('Enter second side: '))

# c = float(input('Enter third side: '))

# calculate the semi-perimeter

s = (a + b + c) / 2

# calculate the area

area = (s*(s-a)*(s-b)*(s-c)) ** 0.5

print(“The area of the triangle is”, area)

Q4. Python program to swap two variables

Ans.

# Python program to swap two variables

x=5
y = 10

# To take inputs from the user

#x = input('Enter value of x: ')

#y = input('Enter value of y: ')

# create a temporary variable and swap the values

temp = x

x=y

y = temp

print("The value of x after swapping",x)

print("The value of y after swapping",y)

Q5. Python Program to calculate the square root

Ans.

# Python Program to calculate the square root

# Note: change this value for a different result

num = 8

# To take the input from the user

#num = float(input('Enter a number: '))

num_sqrt = num ** 0.5

print("The square root of is",num_sqrt)


Q6. Python Program to convert temperature in celsius to Fahrenheit

Ans.

# Python Program to convert temperature in celsius to fahrenheit

# change this value for a different result

celsius = 37.5

# calculate fahrenheit

fahrenheit = (celsius * 1.8) + 32

print(“The temperature in Fahrenheit”,fahrenheit)

Q7. Python Program to find the largest number among the two input
numbers

Ans.

# Python program to find the largest number among the three input
numbers

# change the values of num1, num2 and num3

# for a different result

num1 = 10

num2 = 14

# uncomment following lines to take two numbers from user

#num1 = float(input("Enter first number: "))


#num2 = float(input("Enter second number: "))

if (num1 >= num2):

print("The largest number is", num1)

else:

print("The largest number is", num2)

Q8. Python Program to find the largest number among the three input
numbers

Ans.

# Python program to find the largest number among the three


input numbers

# change the values of num1, num2 and num3

# for a different result

num1 = 10

num2 = 14

num3 = 12

# uncomment following lines to take three numbers from user

#num1 = float(input("Enter first number: "))

#num2 = float(input("Enter second number: "))

#num3 = float(input("Enter third number: "))


if (num1 >= num2) and (num1 >= num3):

largest = num1

elif (num2 >= num1) and (num2 >= num3):

largest = num2

else:

largest = num3

print("The largest number is", largest)

Q9. Python Program to find the Positive or Negative numbers

Ans.

num = float(input("Enter a number: "))

if num > 0:

print("Positive number")

elif num == 0:

print("Zero")

else:

print("Negative number")

Q10. Python Program to check if the input number is odd or even.

Ans.
# Python program to check if the input number is odd or even.

# A number is even if division by 2 gives a remainder of 0.

# If the remainder is 1, it is an odd number.

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

if (num % 2) == 0:

print("The Even No. is :",num)

else:

print("The Odd No. is :",num)

Q11. Python Program to display numbers from 1 to 5

Ans.

# program to display numbers from 1 to 5

# initialize the variable

i=1

n=5

# while loop from i = 1 to 5

while i <= n:

print(i)

i=i+1
Q12. Python Program to calculate the sum of numbers until the user
enters zero

Ans.

# program to calculate the sum of numbers

# until the user enters zero

total = 0

number = int(input('Enter a number: '))

# add numbers until number is zero

while number != 0:

total += number # total = total + number

# take integer input again

number = int(input('Enter a number: '))

print('total =', total)

Q13. Python Program to use of range() to define a range of values

Ans.

# use of range() to define a range of values

values = range(4)

# iterate from i = 0 to i = 3
for i in values:

print(i)

Q14. Python Program to use of for loop with else

Ans.

digits = [0, 1, 5]

for i in digits:

print(i)

else:

print("No items left.")

Q15. Python Program to check if a number is prime or not

Ans.

# Program to check if a number is prime or not

num = 29

# To take input from the user

#num = int(input("Enter a number: "))

# define a flag variable

flag = False

if num == 1:
print(num, "is not a prime number")

elif num > 1:

# check for factors

for i in range(2, num):

if (num % i) == 0:

# if factor is found, set flag to True

flag = True

# break out of loop

break

# check if flag is True

if flag:

print(num, "is not a prime number")

else:

print(num, "is a prime number")

Q16. Python Program to find the factorial of a number provided by


the user.

Ans.

Python program to find the factorial of a number provided by the user.

# change the value for a different result


num = 7

# To take input from the user

#num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero

if num < 0:

print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

print("The factorial of 0 is 1")

else:

for i in range(1,num + 1):

factorial = factorial*i

print("The factorial of",num,"is",factorial)

Q17. Python Program to Access Python List Elements

Ans.

languages = ["Python", "Swift", "C++"]

# access item at index 0

print(languages[0]) # Python

# access item at index 2


print(languages[2]) # C++

Q18. Python Program to Access Negative Indexing in Python

Ans.

languages = ["Python", "Swift", "C++"]

# access item at index 0

print(languages[-1]) # C++

# access item at index 2

print(languages[-3]) # Python

Q19.(i) Python Program ‘list’ using For loop

Ans.

#Using For loop

thislist = ["apple", "banana", "cherry"]

for i in range(len(thislist)):

print(thislist[i])

Q19.(ii) Python Program ‘list’ using While loop

Ans.
#Using While loop

thislist = ["apple", "banana", "cherry"]

i=0

while i < len(thislist):

print(thislist[i])

i=i+1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy