Python Examples | Basic to Advanced Level

1.  Python program to display calendar of given month of the year. import calendar # To ask month and year from the user yy = int(input(“Enter year: “)) mm = int(input(“Enter month: “)) # display the calendar print(calendar.month(yy, mm))


2. Program to find the ASCII value of the given character c = input(“Enter a character: “) print(“The ASCII value of ‘” + c + “‘ is”,ord(c)) ord() function to convert a character to an integer (ASCII value). This function actually returns the Unicode code point of that character

3.Program make a simple calculator that can add, subtract, multiply and divide using functions. # This function adds two numbers def add(x, y): return x + y # This function subtracts two numbers def subtract(x, y): return x – y # This function multiplies two numbers def multiply(x, y): return x * y # This function divides two numbers def divide(x, y): return x / y print(“Select operation.”) print(“1.Add”) print(“2.Subtract”) print(“3.Multiply”) print(“4.Divide”) # Take input from the user choice = input(“Enter choice(1/2/3/4):”) num1 = int(input(“Enter first number: “)) num2 = int(input(“Enter second number: “)) if choice == ‘1’: print(num1,”+”,num2,”=”, add(num1,num2)) elif choice == ‘2’: print(num1,”-“,num2,”=”, subtract(num1,num2)) elif choice == ‘3’: print(num1,”*”,num2,”=”, multiply(num1,num2)) elif choice == ‘4’: print(num1,”/”,num2,”=”, divide(num1,num2)) else: print(“Invalid input”)


4.Program make a simple calculator that can add, subtract, multiply and divide using functions. my_str = my_str.casefold() # reverse the string rev_str = reversed(my_str) # check if the string is equal to its reverse if list(my_str) == list(rev_str): print(“It is palindrome”) else: print(“It is not palindrome”)


5. Python of  Fibonacci series: the sum of two elements defines the next.
a, b = 0, 1 while a < 10:   print(a)   a, b = b, a+b The keyword argument end can be used to avoid the newline after the output, a, b = 0, 1 while a < 10: print(a, end=’,’) a, b = b, a+b


6.Iterating through a string Using for Loop h_letters = []
for letter in ‘human’:
h_letters.append(letter)
print(h_letters)


7.Python Program to Find the Square Root num = float(input(‘Enter a number: ‘)) num_sqrt = num ** 0.5 print(‘The square root of %0.3f is %0.3f’%(num ,num_sqrt))


8.Python Program to generate a random number between 0 and 9 # import the random module import random print(random.randint(0,9))


9.Python program to count the number of even and odd numbers from a series of numbers. numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Declaring the tuple count_odd = 0 count_even = 0 for x in numbers: if not x % 2: count_even+=1 else: count_odd+=1 print(“Number of even numbers :”,count_even) print(“Number of odd numbers :”,count_odd)


10.Python program to check whether an alphabet is a vowel or consonant. l = input(“Input a letter of the alphabet: “) if l in (‘a’, ‘e’, ‘i’, ‘o’, ‘u’): print(“%s is a vowel.” % l) elif l == ‘y’: print(“Sometimes letter y stand for vowel, sometimes stand for consonant.”) else: print(“%s is a consonant.” % l)


11.Python program to create the multiplication table (from 1 to 10) of a number. n = int(input(“Input a number: “)) # use for loop to iterate 10 times for i in range(1,11): print(n,’x’,i,’=’,n*i)


12. Python program to construct the following pattern, using a nested loop number. 1 22 333 4444 55555 666666 7777777 88888888 999999999 for i in range(10): print(str(i) * i)


13.Python  program splits a string into characters. s = “Its to easy” words = s.split() print(words)


14.Python program check Armstrong numbers in certain interval

Ex : 153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number. lower = int(input(“Enter lower range: “)) upper = int(input(“Enter upper range: “)) for num in range(lower, upper + 1): # order of number order = len(str(num)) # initialize sum sum = 0 # find the sum of the cube of each digit temp = num while temp > 0: digit = temp % 10 sum += digit ** order temp //= 10 if num == sum: print(num)


15.python program to calculate the area of a regular polygon. from math import tan, pi
n_sides = int(input(“Input number of sides: “))
s_length = float(input(“Input the length of a side: “))
p_area = n_sides * (s_length ** 2) / (4 * tan(pi / n_sides))
print(“The area of the polygon is: “,p_area)

16.Python Program to take the temperature in Celsius and convert it to Fahrenheit. celsius=int(input(“Enter the temperature in celcius:”)) f=(celsius*1.8)+32 print(“Temperature in farenheit is:”,f)


17.Python Program to Check if a Number is a Perfect Number n = int(input(“Enter any number: “)) sum1 = 0 for i in range(1, n): if(n % i == 0): sum1 = sum1 + i if (sum1 == n): print(“The number is a Perfect number!”) else: print(“The number is not a Perfect number!”)



Leave a Comment