Learn By Doing – Learn Python By Example -2

Learn By Doing – Learn Python By Example -2

If you have gone through our previous blog Learn By Doing – Learn Python By Example -1 then this is the continuation to that article.

KtmBytes Python By Example : Program to check if a string is palindrome or not

# Program to check if a string is palindrome or not

str = input("string : ")

# reverse the string
rev_str = reversed(str)

# check if the string is equal to its reverse
if list(str) == list(rev_str):
print("The string is a palindrome.")
print("The string is not a palindrome.")

KtmBytes Python By Example : Sum of the series 1/2+2/3+3/4+…+n/n+1 with a given n input by console when n>0 in python

# sum of the series 1/2+2/3+3/4+...+n/n+1 with a given n input by console
 # when n>0 in python

num=int(input("no of terms:"))
for i in range(1,num+1):
sum +=float(float(i)/(i+1))

KtmBytes Python By Example : Random Number generator

#roll a dice random numbers
#import random to generate random numbers
import random
while roll_again=="yes" or roll_again=="y":
print("rolling the dice ............")
print("the values are..........")
roll_again=input("Do you want to roll the dice again?")
if roll_again=="n":

KtmBytes Python By Example : Fabonicci Series

#Fabonicci Series

num=input("How many no. of terms? :")

if num==0:
print("Please enter a positive integer")
elif num == 1:
print("Fibonacci sequence upto",num,":")
print("Fibonacci sequence:")
for i in range (0,num):

ntemp =n1+ n2
n1 = n2
n2 = ntemp
i += 1

KtmBytes Python By Example : Bubble Sort Algorithm in Python

#bubble sort algorithm

# creating an empty list
arr = []

# number of elemetns as input
n = int(input("Enter number of elements : "))

# iterating till the range
for i in range(0, n):
ele = int(input("input {0} elements :".format(n)))

arr.append(ele) # adding the element

print("The list is ",arr)

def bubbleSort(arr):
n = len(arr)

# Traverse through all array elements
for i in range(n):

# Last i elements are already in place
for j in range(0, n-i-1):

# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]


print ("Sorted array is:")
for i in range(len(arr)):
print ("%d" %arr[i])