# Learn By Doing – Learn Python By Example -2 #### KtmBytes Python By Example :Program to check if a string is palindrome or notCode:

``````# 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.")
else:
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 pythonCode:

``````# 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:"))
sum=0.0
for i in range(1,num+1):
sum +=float(float(i)/(i+1))
print(sum)``````

#### KtmBytes Python By Example :Random Number generator

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

#### KtmBytes Python By Example :Fabonicci Series

``````#Fabonicci Series

num=input("How many no. of terms? :")
num=int(num)
n1,n2=0,1

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

print(n1)
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]

bubbleSort(arr)

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