Author Archive


How to Check Whether a String is a Palindrome Using Python

Checking for a palindrome string in python is trivial due to built-in functions and powerful string indexing and slicing. The following python program checks whether a string input by user is a palindrome, input_str = input("Please enter a string: ").lower() if input_str == input_str[::-1]: print("{} is a palindrome".format(input_str)) else: print("{} is NOT a palindrome".format(input_str)) Even […]

Python Program to Find Largest of 4 Numbers

The following python program uses the built-in function max() to find the largest of 4 numbers. The max function can take any number of arguments and hence can be used to find maximum of 3 or 5 numbers as well, num1 = 10 num2 = 20 num3 = 30 num4 = 40 print(max(num1,num2,num3,num4)) # prints […]

How to Find Factors of a Given Number Using Python

The following python program prints all the positive factors of a given input number. This program demonstrates the use of functions, modulus operator, list data structure and if statement. def get_all_factors(n): factors = [] for i in range(1,n+1): if n%i == 0: factors.append(i) return factors number = int(input("Please enter a number: ")) list_of_factors = get_all_factors(number) […]

How to Check Whether a Given Number is Prime in Python

A prime number is a natural number greater than 1 which has no positive divisors other than 1 and the number itself. For example, 3 is a prime number since the only positive divisors of 3 are 1 and 3 itself. The number 10 is NOT a prime number since it can be divided by […]

How to Download a Webpage in Python

The following python program demonstrates the use of urllib module to download a webpage to a local folder. Please note that this program downloads the webpage html content only, it doesn’t download the linked images or other resources. The following program also demonstrates use of exception handling. # python 3 only import urllib.request from urllib.error […]