Python Programming Tips


How to Add Two Matrices in Python

The following program demonstrates how two matrices can be added in python using the list data structure. Matrix addition works by adding corresponding entries in the matrices based on their position. The following sample program adds two 3×3 matrices, but the program can be easily modified for any size matrix. You just need to change […]

How to Reverse a Number in Python

The following python program reverses a given multi-digit number. In this example, we simply print the digits in reverse order without removing redundant zeros. For example, if the input number is "1000", the reversed number displayed will be "0001" instead of "1". number = int(input("Enter a number: ")) reverse_number = ” while number > 0: […]

Python Program to Draw a Square and Chess Board Using Turtle

For simple drawing, python provides a built-in module named turtle. This behaves like a drawing board and you can use various drawing commands to draw over it. The basic commands control the movement of the drawing pen itself. We start with a very basic drawing program and work our way through to draw a complete […]

How to Generate Cryptographically Secure Random Numbers in Python

Generating a cryptographically secure random number is very easy in python 3.6 and above. You can use the new secrets module and the function randbelow() for it. This function returns a random number below the specified value. The following python 3.6+ program demonstrates the use of secrets module for secure random numbers, import secrets # […]

Python Program to Check for Armstrong Number

What is an Armstrong number? An Armstrong number is an n-digit number such that the sum of it digits raised to the power n is the number itself. For example, 153 is a 3 digit Armstrong number since, 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 Another example […]