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

AI HHW

Uploaded by

shayaqahmed12
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)
17 views

AI HHW

Uploaded by

shayaqahmed12
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/ 27

Class 10

Python Programs
S No Program Remarks
1 Program to add two numbers
2 Program to print the area of a rectangle
3 Program to calculate the simple interest.
Take input from the user for principal, rate of
interest and time
4 Program to swap two numbers by multiple
assignments.
5 Program to display calendar import calendar
6 Program to check if a number is odd or even

7 Program to check if the character entered by


a user is a consonant or a vowel

8 Program to check if the year inputted by the


user is a leap year

9 Program to print the grade of the student

10 Program to print your name 10 times

11 Program to print sum of first 10 natural


numbers
12 Program to display the Multiplication Table
of the number inputted by the user
13 Program to create and print a list
14 Program to create and print a tuple
15 Program to create and print a dictionary
Program 1
#Program to print total and average of three numbers
a=int(input("Enter first number= "))
b=int(input("Enter second number= "))
c=int(input("Enter third number= "))

Total=a+b+c
Avg=Total/3
print("Total= ", Total)
print("Average= ", Avg)

OUTPUT:
Enter first number= 30
Enter second number= 20
Enter third number= 40
Total = 90
Average=30

Program 2
#Program to find largest of the three numbers

a=int(input("Enter first number= "))


b=int(input("Enter second number= "))
c=int(input("Enter third number= "))
if a>b and a>c:
print(a, "is the largest number")
elif b>a and b>c:
print(b, "is the largest number")
else:
print(c, "is the largest number")

Output:
Enter first number= 12
Enter second number= 34
Enter third number= 1
34 is the largest number

Program 3
# Program to swap two numbers
# Method 1: Using multiple assignment
a=int(input("Enter a number= "))
b=int(input("Enter second number= "))
print("Numbers before swapping")
print("a= ",a)
print("b= ",b)
print("Numbers after swapping")
a,b=b,a # Using multiple assignment
print("a= ",a)
print("b= ",b)
Output:
# Method 1: Using multiple assignment
Enter a number= 9
Enter second number= 7
Numbers before swapping
a= 9
b= 7
Numbers after swapping
a= 7
b= 9
# Method 2: by taking a third variable
a=int(input("Enter a number= "))
b=int(input("Enter second number= "))
print("Numbers before swapping")
print("a= ",a)
print("b= ",b)
print("Numbers after swapping")
c=a
a=b # Using a third variable
b=c
print("a= ",a)
print("b= ",b)

OUTPUT:
Enter a number= 5
Enter second number= 6
Numbers before swapping
a= 5
b= 6
Numbers after swapping
a= 6
b= 5
Program 4
# Program to solve the Quadratic Equation
−𝑏±√𝑏 2 −4𝑎𝑐
#𝑥= 2𝑎

import math
a=int(input("Enter a = "))
b=int(input("Enter b= "))
c=int(input("Enter c= "))
x=(-b+math.sqrt(b*b-4*a*c))/2*a
print("Ans= " , x)

OUTPUT:
Enter a = 2
Enter b= 10
Enter c= 12
Ans = -8.0
Program 5
# Program to find the area and perimeter of the circle
pi=3.14
r=eval(input("Enter the radius= "))
area= pi * r *r
perimeter= 2*pi*r
print("Area of the circle= ", area)
print("Perimeter of the circle= ", perimeter)

OUTPUT:
Enter the radius= 5
Area of the circle= 78.5
Perimeter of the circle= 31.400000000000002
Program 6
# Program to check if the number inputted by the user is even
or odd
num=int(input("Enter a number= "))
if num%2==0:
print("It is an even number")
else:
print("It is an odd number")

OUTPUT:
Enter a number= 10
It is an even number

Enter a number= 5
It is an odd number
Program 7

Program to check if a number is positive, negative or zero

Enter a number= 0
You have entered zero

Enter a number=
-9
You have entered a negative number

Enter a number=
9
You have entered a positive number
Program 8

# Program to print the grade of the student

marks=int(input("Enter the marks of the student=


"))
if marks>=90 and marks<=100:
print("Grade A")
elif marks>=80 and marks<=90:
print("Grade B")
elif marks>=70 and marks<=80:
print("Grade C")
elif marks>=60 and marks<=70:
print("Grade D")
else:
print("Less than 60% you need to Work Hard")

OUTPUT:

Enter first number= 56


Enter second number= 100
Enter third number= 23
100 is the largest number
Program 9

#WAP to calculate the weekly wages of the employees

w_hrs=eval(input("Enter the no of working hours= "))


E_perhour=eval(input("Enter the earning per hour= "))
E_week=w_hrs*E_perhour

if E_week>=30000:
print("Bravo!Your income = " ,E_week)
elif E_week>20000 and E_week<30000:
print("Kudos!You income = " ,E_week)
elif E_week>10000 and E_week<20000:
print("Good Going! You income = " ,E_week)
else:
print("Your income is less than 10000")

OUTPUT:
Enter the no of working hours= 8
Enter the earning per hour= 5000
Bravo!Your income = 40000

Enter the no of working hours= 8


Enter the earning per hour= 3000
Kudos!You income = 24000

Enter the no of working hours= 8


Enter the earning per hour= 2000
Good Going! You income = 16000
Program 10

# Program to print odd & even numbers using for loop from 1-
20
print("Even numbers are ")
for i in range(1,11):
if i%2==0:
print(i)

print("Odd numbers are ")


for i in range(1,11):
if i%2!=0:
print(i)

Even numbers are

2
4
6
8
10

Odd numbers are

1
3
5
7
9
Program 11
Program to show the square of N natural numbers

n=int(input("Enter the value for n= "))


for i in range (1, n+1):
sq=i*i
print("Square of",i,"=", sq)

OUTPUT:
Enter the value for n= 10
Square of 1 = 1
Square of 2 = 4
Square of 3 = 9
Square of 4 = 16
Square of 5 = 25
Square of 6 = 36
Square of 7 = 49
Square of 8 = 64
Square of 9 = 81
Square of 10 = 100
Program 12
# Sum of n natural numbers (using while loop)
# s=1+2+3+........+n
n=int(input("Enter the value for n= "))
s=0
i=1
while i<=n:
s=s+i
i=i+1
print(s)

OUTPUT:
Enter the value for n= 10
1
3
6
10
15
21
28
36
45
55
Program 13
# Program to create a list of numbers and show the sum of all the
items in the list

list=[10,20,30,50,60]
list1=[11, 23, 56,90, 40]
print("Sum of all the items in the list= ", sum(list))
print("Sum of all the items in the list1= ", sum(list1))

OUTPUT:
Sum of all the items in the list= 170
Sum of all the items in the list1= 220
Program 14
# Program to create a list and add the elements using append
method

list1=[34,99]
print(list1)
list1.append(["Python","Hello World"])
print (list1)

OUTPUT:

[34, 99]

[34, 99, ['Python', 'Hello World']]


Program 15
# To show the largest & the smallest number in the list

list1=[99, 100,34, 11, 234, 567,8,1]


print("Largest number in the list= ", max(list1))
print("Smallest number in the list= ", min(list1))

OUTPUT:

Largest number in the list= 567


Smallest number in the list= 1
Program 16
# Create a dictionary of odd numbers between 1 and 10, where the
key is the decimal number and the value is the corresponding in
words.

odd={1:'One',2:"Two", 3: "Three", 5:"Five", 7:"Seven",9:"Nine"}


print(odd)
print(odd.keys())
print(odd.values())

OUTPUT:
{1: 'One', 2: 'Two', 3: 'Three', 5: 'Five', 7: 'Seven', 9: 'Nine'}
dict_keys([1, 2, 3, 5, 7, 9])
dict_values(['One', 'Two', 'Three', 'Five', 'Seven', 'Nine'])
Program 17
# Program to create an array from the given list (Using numpy
package)
import numpy
arr=numpy.array([1,2,3,4,5])
print(arr)

# Accessing the second element


print("Second Element= ", arr[1])

OUTPUT:
[1 2 3 4 5]
Second Element= 2
Program 18
# Applying different arithmetic operations
#Adding 5 to all the elements
import numpy
arr=numpy.array([1,2,3,4,5])
print(arr)

arr1=arr+5
print(arr1)

#Dividing by 5
arr2=arr1/5
print(arr2)

OUTPUT:
[ 6 7 8 9 10]
[1.2 1.4 1.6 1.8 2. ]
Program 19
# Program to create an array from the given list (Using numpy
package)
import numpy
arr=numpy.array([1,2,3,4,5])
print(arr)

#Finding Square of the elements


arr3=arr**2
print(arr3)

OUTPUT:

[ 1 4 9 16 25]
Program 20
# Creating a 2-dimensional zero array (4x3)
#4 rows, 3 columns
import numpy
arr=numpy.zeros((4,3))
print(arr)

OUTPUT:
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
Program 21

# Creating an array with 5 random values


import numpy
arr=numpy.random.random()
print(arr)

OUTPUT:

[0.35989974 0.80022757 0.64536587 0.64983807 0.58682942]


Program 22

# Creating a 2- dimensional constant value array (3,4) having all


7s

import numpy
arr=numpy.full((3,4),7)
print(arr)

OUTPUT:

[[7 7 7 7]
[7 7 7 7]
[7 7 7 7]]
Program 23

# Creating a sequential array from 0 to 30 with gaps of 5

import numpy
arr=numpy.arange((0,30),5)
print(arr)

OUTPUT:

[ 0 5 10 15 20 25]
Program 24

# To create an array and display the type, dimensions, shape, size


and datatype of the given array

import numpy
arr=numpy.array([10,20,30,40,50])
print("Type of the array= ", type(arr))
print("Dimesions of the array= ", arr.ndim)
print("Shape of the array= ", arr.shape)
print("Size of the array= ", arr.size)
print("Datatype of the array= ", arr.dtype)

OUTPUT:

Type of the array= <class 'numpy.ndarray'>


Dimesions of the array= 1
Shape of the array= (5,)
Size of the array= 5
Datatype of the array= int32
Program 25
# Create an array and find the maximum element, minimum
element, sum of all array elements

import numpy
arr=numpy.array([10,34,67,8,9])
print(arr)
print("Maximum element of the array= ", arr.max())
print("Minimum element of the array= ", arr.min())
print("Sum of the elements of the array= ", arr.sum())

OUTPUT:
[10 34 67 8 9]
Maximum element of the array= 67
Minimum element of the array= 8
Sum of the elements of the array= 128

You might also like