Python program to get the current date using datetime.

In this python basic program, we will get the current date and time using datetime library.

Steps to solve the program
  1. Import datetime library.
  2. Get the current date and time using datetime.datetime.now().
  3. Print the output.
				
					import datetime   
dt = datetime.datetime.now() 

print("Current Date: ",dt)
				
			

Output :

				
					Current Date:  2023-01-29 09:52:33.631419
				
			

generate a random string with a specific length.

convert Decimal to Binary.

Generate a random string with a specific length.

In this python basic program, we will generate a random string with a specific length.

Steps to solve the program
  1. Import string and random library.
  2. Take the length of the string as input through the user.
  3. To generate random string with a specified length use random.choices(string.ascii_letters, k=n), where n is the length.
  4. Print the output.
				
					import string
import random
 
n = int(input("Enter length of the string: "))
 
result = ''.join(random.choices(string.ascii_letters, k=n))
 
# print result
print("The generated random string : ",str(result))
				
			

Output :

				
					Enter length of the string: 8
The generated random string :  HWyGuDWg
				
			

generate random numbers.

get the current date.

Program to generate random numbers.

In this python basic program, we will generate random numbers using random library.

Steps to solve the program
  1. Import random library.
  2. We are going to generate 5 random numbers, so use for loop with range function to generate 5 number.
  3. Use random.random() to generate random numbers inside the loop.
  4. Print the output.
				
					import random

for i in range(5):
    print(random.random())
				
			

Output :

				
					0.9199240457263513
0.9004451014782473
0.7153068440492277
0.6578686372362937
0.9954018599145891
				
			

check for the anagram.

generate a random string with a specific length.

Python program to check for the anagram.

In this python basi program, we will check for the anagram i.e. rearrangement of the letters of a word to another word, using all the original letters once.

Steps to solve the program
  1. Take a word as input and create a variable named combination and assign its value equal to 0.
  2. We have to find the number of words that can be formed by using letters available in the given word.
  3. Create a variable and assign it a value equal to the letters available in the input word.
  4. While the value of that variable is greater than 0 add 1 to combination variable and subtract 1 from the above variable.
  5. Print the output.
				
					word = input("Enter a word: ")
combination = 0
characters = len(word)

while characters>0:
    combination += characters
    characters -= 1

print("Total combinations without repetaation: ",combination)
				
			

Output :

				
					Enter a word: python
Total combinations without repetaation:  21
				
			

check leap year.

generate random numbers.

Python program to check year is a leap year.

In this python basic program, we will check whether the given year is a leap year or not.

Steps to solve the program
  1. Take a year as input through the user.
  2. Use the following condition to check whether the given year is a leap year or not – (year%400 == 0 or year%100 != 0) and year%4 == 0.
  3. Print the respective output.
				
					year = int(input("Enter a year: "))

if (year%400 == 0 or year%100 != 0) and year%4 == 0:
    print(f"{year} is a leap year")
else:
    print(f"{year} is not a leap year")
				
			

Output :

				
					Enter a year: 1700
1700 is not a leap year
				
			

check the prime number.

check for the anagram.

Python program to check the prime number.

In this python basic program, we will check the whether the given number is a prime number.

Steps to solve the program
  1. Take a number as input through the user and create a count variable and assign its value equal to 0.
  2. Use for loop with the range function to check whether a number is a prime number or not.
  3. If a number divides the input number then add 1 to the count variable.
  4. If a number is divisible by 1 and by itself then it is a prime number.
  5. Print the output.
				
					num = int(input("Enter a number: "))
count = 0

for i in range(1,num+1):
    if num%i == 0:
        count += 1
        
if count == 2:
    print(f"{num} is a prime number")
else:
    print(f"{num} is not a prime number")
				
			

Output :

				
					Enter a number: 53
53 is a prime number
				
			

calculate compound interest.

check leap year.

Python program to calculate compound interest.

In this python basic program, we will calculate compound interest using formula p*((1+r/100)**n).

Steps to solve the program
  1. Take the principal amount, rate of interest, and a number of years as input through the user.
  2. Calculate the compound interest using the formula p((1+r/100)**n).
  3. Print the output.
				
					p = int(input("Enter principle amount: "))
r = float(input("Enter interest rate: "))
n = int(input("Enter number of years: "))

amount = p*((1+r/100)**n)

print("Compoud interest: ",amount)
				
			

Output :

				
					Enter principle amount: 10000
Enter interest rate: 10
Enter number of years: 2
Compoud interest:  12100.000000000002
				
			

check given number is palindrome or not.

check the prime number.

Program to check given number is palindrome or not.

In this python basic program we will check whether the given number is palindrome or not.

Steps to solve the program
  1. Take a number as input through the user and create a variable rev and assign its value equal to 0.
  2. Use a while loop to find the reverse of a number.
  3. While num is greater than 0 divide the number by 10 using % to find the remainder.
  4. Multiply the rev by 10 and add the remainder to it.
  5. Now divide the number by 10 using //.
  6. If the reversed number is equal to the input number then it is a palindrome.
				
					n = num = int(input("Enter a number: "))
rev = 0

while n>0:
    rem = n%10
    rev = rev*10+rem
    n = n//10
    
if num == rev:
    print("Given number is a palindrome number")
else:
    print("Given number is not a palindrome number")
				
			

Output :

				
					Enter a number: 121
Given number is a palindrome number
				
			

Fibonacci series between 0 to 50.

calculate compound interest.

Program to get the Fibonacci series between 0 to 50.

In this python basic program, we will get the Fibonacci series between 0 to 50 numbers.

Steps to solve the program
  1. Create two variables and assign their values equal to 0 and 1 respectively.
  2. Create another variable count and assign its value equal to 0.
  3. While the count variable is less than 50 print the num1.
  4. Add num1 and num2 and assign their addition to the n2 variable.
  5. Change the value of num1 with num2 and num2 with n2.
  6. Add 1 to the count variable.
  7. Print the Fibonacci series.
				
					num1 = 0
num2 = 1
count = 0

print("Sequence is: ",end=" ")
while count<50:
    print(num1,end=" ")
    n2 = num1+num2
    num1 = num2
    num2 = n2
    count += 1
				
			

Output :

				
					Sequence is:  0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 
987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 
196418 317811 514229 832040 1346269 2178309 3524578 5702887
9227465 14930352 24157817 39088169 63245986 102334155 165580141 
267914296 433494437 701408733 1134903170 1836311903 2971215073 
4807526976 7778742049 
				
			

reverse a given number.

check given number is palindrome or not.

Python program to reverse a given number

In this python basic program, we will reverse a given number without using some logic.

Steps to solve the program
  1. Take a number as input through the user.
  2. Convert the number into a string by using str() as we cannot use indexing on integers.
  3. Reverse the string i.e. number using indexing.
  4. Print the output.
				
					num = int(input("Enter a number: "))

reverse = str(num)

print("Reverse: ",reverse[::-1])
				
			

Output :

				
					Enter a number: 236
Reverse:  632
				
			

get the factorial of the given number

get the Fibonacci series between 0 to 50.