Python Programs
Python Programs
def findArea(r):
PI = 3.142
return PI * (r*r);
# Driver method
print("Area is %.6f" % findArea(5));
Output
Area is 78.550000
2.
else:
greater = y
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
num1 = 54
num2 = 24
Output
Note: To test this program, change the values of num1 and num2 .
This program stores two number in num1 and num2 respectively. These
numbers are passed to the compute_lcm() function. The function returns the
L.C.M of two numbers.
In the function, we first determine the greater of the two numbers since the
L.C.M. can only be greater than or equal to the largest number. We then
use an infinite while loop to go from that number and beyond.
In each iteration, we check if both the numbers perfectly divide our number.
If so, we store the number as L.C.M. and break from the loop. Otherwise,
the number is incremented by 1 and the loop continues.
while(y):
x, y = y, x % y
return x
num1 = 54
num2 = 24
print("The L.C.M. is", compute_lcm(num1, num2))
Run Code
N,X=2,3
print(CalculatePower(N,X))
N,X=3,4
print(CalculatePower(N,X))
Output:
8
81
if p == 0:
return 1
print(calc_power(4, 2))
Output
16
1 * 15 = 15
3 * 5 = 15
5 * 3 = 15
15 * 1 = 15
Source Code
# Python Program to find the factors of a number
num = 320
print_factors(num)
Run Code
Output
1
2
4
5
8
10
16
20
32
40
64
80
160
320
Note: To find the factors of another number, change the value of num .
my_string = "Programiz"
my_char = "r"
for i in my_string:
if i == my_char:
count += 1
print(count)
Run Code
Output
In the above example, we have found the count of 'r' in 'Programiz' . The
for-loop loops over each character of my_string and the if condition checks
if each character of my_string is 'r' . The value of count increases if there
is a match.
print(my_string.count(my_char))
Run Code
Output
2
count() counts the frequency of the character passed as parameter.
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
(-b ± (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)
Source Code
# Solve the quadratic equation ax**2 + bx + c = 0
Output
Enter a: 1
Enter b: 5
Enter c: 6
The solutions are (-3+0j) and (-2+0j)
We have imported the cmath module to perform complex square root. First,
we calculate the discriminant and then find the two solutions of the
quadratic equation.
You can change the value of a , b and c in the above program and test this
program.
x = 5
y = 10
Output
In this program, we use the temp variable to hold the value of x temporarily. We
then put the value of y in x and later temp in y . In this way, the values get
exchanged.
x = 5
y = 10
x, y = y, x
print("x =", x)
print("y =", y)
Run Code
If the variables are both numbers, we can use arithmetic operations to do the same.
It might not look intuitive at first sight. But if you think about it, it is pretty easy to
figure it out. Here are a few examples
Addition and Subtraction
x = x + y
y = x - y
x = x - y
x = x * y
y = x / y
x = x / y
Source Code
# Python program to convert decimal into other number systems
dec = 344
Output
Note: To test the program for other decimal numbers, change the
value of dec in the program.
In this program, we have used built-in
functions bin() , oct() and hex() to convert the given decimal
number into respective number systems.
These functions take an integer (in decimal) and return a string.
Python Program to Add Two
Matrices
In this program, you'll learn to add two matrices using Nested
loop and Next list comprehension, and display it.
For example X = [[1, 2], [4, 5], [3, 6]] would represent a 3x2
matrix. First row can be selected as X[0] and the element in first
row, first column can be selected as X[0][0] .
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for r in result:
print(r)
Run Code
Output
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
for r in result:
print(r)
Run Code
Output:
[17, 15, 4]
[10, 12, 9]
Output
0 21
1 44
2 35
3 11
Using enumerate() , we can print both the index and the values.
Pass two loop variables index and val in the for loop. You can
give any name to these variables.
Print the required variables inside the for loop block.
Output
1 21
2 44
3 35
4 11
Output
0 21
1 44
2 35
3 11
Python Array
Creating Python Arrays
To create an array of numeric values, we need to import
the array module. For example:
import array as arr
a = arr.array('d', [1.1, 3.5, 4.5])
print(a)
Run Code
Output
u Py_UNICODE Unicode 2
f float float 4
d double float 8
Output
First element: 2
Second element: 4
Last element: 8
Output
Output
numbers.append(4)
print(numbers) # Output: array('i', [1, 2, 3, 4])
Output
print(numbers)
Run Code
Output
Output
numbers.remove(12)
print(numbers) # Output: array('i', [10, 11, 12, 13])
print(numbers.pop(2)) # Output: 12
print(numbers) # Output: array('i', [10, 11, 13])
Run Code
Output
Check this page to learn more about Python array and array
methods.
Python Lists Vs Arrays
In Python, we can treat lists as arrays. However, we cannot
constrain the type of elements stored in a list. For example:
If you create arrays using the array module, all elements of the
array must be of the same numeric type.
Output
def checkAllElementsEqual(lst):
return len(set(lst)) == 1
print(checkAllElementsEqual([0,1,2,3,4]))
print(checkAllElementsEqual([0,0,0,0,0]))
#Output:
False
True
When working with collections of data in a Python program, it’s possible you
want to check if all elements in an array are equal.
Arrays in Python are called lists, and we can easily check if all elements in a list
are equal.
To check if all items in a list are equal, the easily way is to convert the list to a set
and check the set’s length. If the length of the set is 1, then we know that all
elements are equal.
Below is a simple function in Python of how to check if all elements of a list are
the same.
def checkAllElementsEqual(lst):
return len(set(lst)) == 1
print(checkAllElementsEqual([0,1,2,3,4]))
print(checkAllElementsEqual([0,0,0,0,0]))
#Output:
False
True
There are a few other ways you can check if all elements in a list are equal in
Python which you can read about below.
To check if all items in a list are equal with a loop, we just check if all elements
are equal to the first element.
Below is a Python function which will check if all elements of a list are equal
with a for loop.
def checkAllElementsEqual(lst):
for x in lst:
if lst[0] != x:
return False
return True
print(checkAllElementsEqual([0,1,2,3,4]))
print(checkAllElementsEqual([0,0,0,0,0]))
#Output:
False
True
Copy
Using count() to Check if All Items in a List are the
Same in Python
Another way we can check if all items in a list are equal is with the help of
the count() function.
The count() function in Python gives us the count of how many times a
particular value is found in a list.
If a list has all equal values, then the count of the first value should equal the
length of the list.
Below is how to check if all elements in a list are equal with the help of
the count() function.
def checkAllElementsEqual(lst):
return lst.count(lst[0]) == len(lst)
print(checkAllElementsEqual([0,1,2,3,4]))
print(checkAllElementsEqual([0,0,0,0,0]))
#Output:
False
True
all() returns True is all values in a list are True, and False is not all values in a list
are True.
We can check if all values in a list are equal to the first element and pass this
to all()
Below is a Python function which will check if all elements of a list are equal
with the all() function.
def checkAllElementsEqual(lst):
return all(x == lst[0] for x in lst)
print(checkAllElementsEqual([0,1,2,3,4]))
print(checkAllElementsEqual([0,0,0,0,0]))
#Output:
False
True
Hopefully this article has been useful for you to check if all elements in a list are
equal using Python.
Method Discussed :
Method 1 :
Otherwise, return 1.
if (j == m):
return 0
return 1
# Driver code
arr1 = [11, 12, 13, 21, 30, 70]
arr2 = [11, 30, 70, 12]
m = len(arr1)
n = len(arr2)