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

Python Spyder (1)

python programe codes for class 10th

Uploaded by

sainijatin208
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)
27 views

Python Spyder (1)

python programe codes for class 10th

Uploaded by

sainijatin208
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/ 6

Example 1: Add Two Numbers With User Input

# 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 {0} and {1} is {2}'.format(num1, num2, sum))

Example 2: For Sqaure 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 %0.3f is %0.3f'%(num ,num_sqrt))

Example 3: 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 %0.2f' %area)


Example 4: Kilometers to Miles

# Taking kilometers input from the user

kilometers = float(input("Enter value in kilometers: "))

# conversion factor

conv_fac = 0.621371

# calculate miles

miles = kilometers * conv_fac

print('%0.2f kilometers is equal to %0.2f miles' %(kilometers,miles))

Example 5: Convert temperature in celsius to fahrenheit

# change this value for a different result

celsius = 37.5

# calculate fahrenheit

fahrenheit = (celsius * 1.8) + 32

print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit' %(celsius,fahrenheit))

Example 6: 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("{0} is Even".format(num))

else:

print("{0} is Odd".format(num))

Example 7: Python program to check if year is a leap year or not

year = 2000

# To get year (integer input) from the user

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

# divided by 100 means century year (ending with 00)

# century year divided by 400 is leap year

if (year % 400 == 0) and (year % 100 == 0):

print("{0} is a leap year".format(year))

# not divided by 100 means not a century year


# year divided by 4 is a leap year

elif (year % 4 ==0) and (year % 100 != 0):

print("{0} is a leap year".format(year))

# if not divided by both 400 (century year) and 4 (not century year)

# year is not leap year

else:

print("{0} is not a leap year".format(year))

Example 8: # 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 == 0 or 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")

Example 9: # 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)

Example 10: # Function to find HCF the Using Euclidian algorithm

def compute_hcf(x, y):

while(y):

x, y = y, x % y

return x

hcf = compute_hcf(300, 400)

print("The HCF is", hcf)

Example 11: # Python Program to find the L.C.M. of two input number

def compute_lcm(x, y):

# choose the greater number

if x > y:

greater = x

else:

greater = y

while(True):

if((greater % x == 0) and (greater % y == 0)):

lcm = greater

break

greater += 1
return lcm

num1 = 54

num2 = 24

print("The L.C.M. is", compute_lcm(num1, num2))

Example 12: # Python Program to find the factors of a number

# This function computes the factor of the argument passed

def print_factors(x):

print("The factors of",x,"are:")

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

if x % i == 0:

print(i)

num = 320

print_factors(num)

Example 13: def jpeg_res(filename):

""""This function prints the resolution of the jpeg image file passed into it"""

# open image for reading in binary mode

with open(filename,'rb') as img_file:

# height of image (in 2 bytes) is at 164th position

img_file.seek(163)

# read the 2 bytes

a = img_file.read(2)

# calculate height

height = (a[0] << 8) + a[1]

# next 2 bytes is width

a = img_file.read(2)

# calculate width

width = (a[0] << 8) + a[1]

print("The resolution of the image is",width,"x",height)

jpeg_res("img1.jpg")

Example 14: # Program to add two matrices using list comprehension

X = [[12,7,3],
[4 ,5,6],

[7 ,8,9]]

Y = [[5,8,1],

[6,7,3],

[4,5,9]]

result = [[X[i][j] + Y[i][j] for j in range(len(X[0]))] for i in range(len(X))]

for r in result:

print(r)

Example 15: # Function to print binary number using recursion

def convertToBinary(n):

if n > 1:

convertToBinary(n//2)

print(n % 2,end = '')

# decimal number

dec = 34

convertToBinary(dec)

print()

You might also like