Author name: Vikram Chiluka

Program to Find the Sum of Series 3+7+13+21.....+N

Python Program to Find the Sum of Series 3+7+13+21…..+N

In the previous article, we have discussed Python Program to Find the Sum of Series 9+99+999…..+N

Given a number N and the task is to find the sum of the given series (3+7+13+21…..+N) for the given number in Python.

Examples:

Example1:

Input:

Given Number = 9

Output:

The total sum of the series till the given number { 9 } =  339

Example2:

Input:

Given Number = 15

Output:

The total sum of the series till the given number { 15 } =  1375

Program to Find the Sum of Series 3+7+13+21…..+N in Python

Below are the ways to find the sum of the given series (3+7+13+21…..+N)  for the given number in Python:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Loop from 2 to the given number+1 using the for loop.
  • Inside the loop, subtract 1 from the iterator value and multiply it with the iterator and add 1 to this result.
  • Store it in another variable say p.
  • Add the value of above p to the above-initialized resltsum and store it in the same variable resltsum.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 9
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Loop from 2 to the given number+1 using the for loop.
for itr in range(2, gvn_numb+2):
  # Inside the loop, subtract 1 from the iterator value and multiply it with the
  # iterator and add 1 to this result.
  # Store it in another variable say p.
    p = 1+(itr*(itr-1))
  # Add the value of above p to the above-initialized resltsum and store it in the same
  # variable resltsum.
    resltsum += p
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

The total sum of the series till the given number { 9 } =  339

Method #2: Using For loop (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Loop from 2 to the given number+1 using the for loop.
  • Inside the loop, subtract 1 from the iterator value and multiply it with the iterator and add 1 to this result.
  • Store it in another variable say p.
  • Add the value of above p to the above-initialized resltsum and store it in the same variable resltsum.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function and 
# store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Loop from 2 to the given number+1 using the for loop.
for itr in range(2, gvn_numb+2):
  # Inside the loop, subtract 1 from the iterator value and multiply it with the
  # iterator and add 1 to this result.
  # Store it in another variable say p.
    p = 1+(itr*(itr-1))
  # Add the value of above p to the above-initialized resltsum and store it in the same
  # variable resltsum.
    resltsum += p
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

Enter some Random Number = 15
The total sum of the series till the given number { 15 } = 1375

Access the big list of Python Programming Code Examples with actual logical code asked in Programming and Coding Interviews for Python and stand out from the crowd.

Python Program to Find the Sum of Series 3+7+13+21…..+N Read More »

Program to Find the Sum of Series 9+99+999.....+N

Python Program to Find the Sum of Series 9+99+999…..+N

In the previous article, we have discussed Python Program to Remove the last Word from the String/Sentence

Given a number N and the task is to find the sum of the given series (9+99+999…..+N) for the given number in Python.

Examples:

Example1:

Input:

Given Number =6

Output:

The total sum of the series till the given number { 6 } =  1111104

Example2:

Input:

Given Number =11

Output:

The total sum of the series till the given number { 11 } =  111111111099

Program to Find the Sum of Series 9+99+999…..+N in Python

Below are the ways to find the sum of the given series (9+99+999…..+N) for the given number in Python

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Take another variable say k and initialize its value with 9.
  • Iterate from 1 to the given number using the for loop.
  • Inside the loop, add the value of k to the above-initialized resltsum and store it in the same variable resltsum.
  • Calculate the value of (k*10)+9 and store it in the same variable k.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 6
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Take another variable say k and initialize its value with 9.
k = 9
# Iterate from 1 to the given number using the for loop.
for itr in range(1, gvn_numb+1):
  # Inside the loop, add the value of k to the above-initialized resltsum and store
  # it in the same variable resltsum.
    resltsum += k
  # Calculate the value of (k*10)+9 and store it in the same variable k.
    k = (k*10)+9
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

The total sum of the series till the given number { 6 } =  1111104

Method #2: Using For loop (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Take another variable say k and initialize its value with 9.
  • Iterate from 1 to the given number using the for loop.
  • Inside the loop, add the value of k to the above-initialized resltsum and store it in the same variable resltsum.
  • Calculate the value of (k*10)+9 and store it in the same variable k.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function and
# store it in a variable.
gvn_numb = int(input("Enter some random Number = "))
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Take another variable say k and initialize its value with 9.
k = 9
# Iterate from 1 to the given number using the for loop.
for itr in range(1, gvn_numb+1):
  # Inside the loop, add the value of k to the above-initialized resltsum and store
  # it in the same variable resltsum.
    resltsum += k
  # Calculate the value of (k*10)+9 and store it in the same variable k.
    k = (k*10)+9
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

Enter some random Number = 11
The total sum of the series till the given number { 11 } = 111111111099

The best way to learn Python for Beginners is to practice as much as they can taking help of the Sample Python Programs For Beginners. Using them you can develop code on your own and master coding skills.

Python Program to Find the Sum of Series 9+99+999…..+N Read More »

Program to get First Element of each Tuple in a List

Python Program to get First Element of each Tuple in a List

In the previous article, we have discussed Python Program for Modulo of Tuple Elements

Tuple in Python:

A tuple is an immutable list of objects. That means the elements of a tuple cannot be modified or changed while the program is running.

Given some random number of tuples in a list and the task is to get the first element of each tuple in a given tuple list.

For example

let the tuple list = [(1, 2),(3, 4)]

The first element of tuples in the given list = [1, 3]

Examples:

Example1:

Input:

Given Tuple list = [(1, 2), (3, 4)]

Output:

The first element of the each tuple in a given tuplelist [(1, 2), (3, 4)] = [1, 3]

Example2:

Input:

Given Tuple list = [('p', 'q', 'r'), ('s', 't'), ('u', 'v')]

Output:

The first element of the each tuple in a given tuplelist [('p', 'q', 'r'), ('s', 't'), ('u', 'v')] = ['p', 's', 'u']

Program to get the First Element of each Tuple in a List in Python

Below are the ways to get the first element of each tuple in a given tuple list:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the tuple list as static input and store it in a variable.
  • Take an empty new list to store the first element of each tuple in a given tuple list.
  • Loop in the given input tuple list using the for loop.
  • Inside the loop, append the first element of each tuple in a given tuple list to the above initialized new list using the append() function.
  • Print the new list to get the first element of each tuple in a given tuple list.
  • The Exit of the program.

Below is the implementation:

# Give the tuple list as static input and store it in a variable.
gvn_tupl_lst = [(1, 2), (3, 4)]
# Take an empty new list to store the first element of each tuple in a given tuple list.
fst_tupl_elemnts = []
# Loop in the given input tuple list using the for loop.
for elemnt in gvn_tupl_lst:
  # Inside the loop, append the first element of each tuple in a given tuple list to
    # the above initialized new list using the append() function.
    fst_tupl_elemnts.append(elemnt[0])
# Print the new list to get the first element of each tuple in a given tuple list.
print("The first element of the each tuple in a given tuplelist",
      gvn_tupl_lst, "=", fst_tupl_elemnts)

Output:

The first element of the each tuple in a given tuplelist [(1, 2), (3, 4)] = [1, 3]

Method #2: Using For loop (User Input)

Approach:

  • Give the number of tuples in a tuple list as user input using the int(input())  function and store it in a variable.
  • Take an empty list say “gvn_tupl_lst” and store it in another variable.
  • Iterate in the above-given number of times using the for loop.
  • Inside the loop, enter the tuple elements using tuple(),map(),input(),and split() functions and Store it in a variable.
  • Append the above elements to the “gvn_tupl_lst”.
  • Take an empty new list to store the first element of each tuple in a given tuple list.
  • Loop in the given input tuple list using the for loop.
  • Inside the loop, append the first element of each tuple in a given tuple list to the above initialized new list using the append() function.
  • Print the new list to get the first element of each tuple in a given tuple list.
  • The Exit of the program.

Below is the implementation:

# Give the number of tuples in a tuple list as user input using the int(input())
# function and store it in a variable
numtuples=int(input('Enter some random number of tuples = '))
#Take an empty list say "gvn_tupl_lst" and  store it in another variable.
gvn_tupl_lst=[]
#Iterate in the above-given number of times using the for loop.
for t in range(numtuples):
# Inside the loop, enter the tuple elements using tuple(),map(),input(),and split() functions
# and Store it in a variable. 
  tupleelemts=tuple(map(int,input('Enter random tuple elements = ').split()))
# Append the above elements to the "gvn_tupl_lst".
  gvn_tupl_lst.append(tupleelemts)
# Take an empty new list to store the first element of each tuple in a given tuple list.  
fst_tupl_elemnts = []
# Loop in the given input tuple list using the for loop.
for elemnt in gvn_tupl_lst:
  # Inside the loop, append the first element of each tuple in a given tuple list to
    # the above initialized new list using the append() function.
    fst_tupl_elemnts.append(elemnt[0])
# Print the new list to get the first element of each tuple in a given tuple list.
print("The first element of the each tuple in a given tuplelist",
      gvn_tupl_lst, "=", fst_tupl_elemnts)

Output:

Enter some random number of tuples = 2
Enter random tuple elements = 7 8 9
Enter random tuple elements = 4 5 6
The first element of the each tuple in a given tuplelist [(7, 8, 9), (4, 5, 6)] = [7, 4]

If you are new to the Python Programming Language then practice using our Python Programming Examples for Beginners as our expert team has designed them from scratch.

Python Program to get First Element of each Tuple in a List Read More »

Program to Remove the last Word from the StringSentence

Python Program to Remove the last Word from the String/Sentence

In the previous article, we have discussed Python Program to Print Palindrome Numbers in a Range

Given a string/sentence and the task is to remove the last word from the given sentence.

join() :

The string method join()  takes all of the items in an iterable and returns a string after joining them all together. Iterable types include list, tuple, string, dictionary, and set.

pop() Function in Python:

Python’s list pop() function removes and returns the final value from a List or the supplied index value.

Examples:

Example1:

Input:

Given String = "Hello this is btechgeeks"

Output:

The given string{ Hello this is btechgeeks } after removal of last word = Hello this is

Example2:

Input:

Given String = "Good Morning btechgeeks Hello everyone"

Output:

The given string{ Good Morning btechgeeks Hello everyone } after removal of last word =  Good Morning btechgeeks Hello

Program to Remove the last Word from the String/Sentence in Python

Below are the ways to remove the last word from a given string/sentence:

Method #1: Using pop() Function (Static Input)

Approach:

  • Give the string/sentence as static input and store it in a variable.
  • Convert the given string into a list of words using the list() and split() functions and store it in another variable.
  • Apply pop() function on the above list of words.
  • Convert the list of words into the string using the join() function and store it in another variable.
  • Print the String after removal of the last word from the above-given string.
  • The Exit of the program.

Below is the implementation:

# Give the string/sentence as static input and store it in a variable.
gven_strng = 'Hello this is btechgeeks'
# Convert the given string into a list of words using the list() and split() functions
# and store it in another variable.
str_lst = list(gven_strng.split())
# Apply pop() function on the above list of words.
str_lst.pop()
# Convert the list of words into the string using the join() function and store it in
# another variable.
modified_str = ' '.join(str_lst)
# Print the String after removal of the last word from the above-given string.
print("The given string{", gven_strng,
      "} after removal of last word = ", modified_str)

Output:

The given string{ Hello this is btechgeeks } after removal of last word =  Hello this is

Method #2: Using pop() Function (User Input)

Approach:

  • Give the string/sentence as user input using the input() function and store it in a variable.
  • Convert the given string into a list of words using the list() and split() functions and store it in another variable.
  • Apply pop() function on the above list of words.
  • Convert the list of words into the string using the join() function and store it in another variable.
  • Print the String after removal of the last word from the above-given string.
  • The Exit of the program.

Below is the implementation:

# Give the string/sentence as user input using the input() function and
# store it in a variable.
gven_strng = input("Enter some random string = ")
# Convert the given string into a list of words using the list() and split() functions
# and store it in another variable.
str_lst = list(gven_strng.split())
# Apply pop() function on the above list of words.
str_lst.pop()
# Convert the list of words into the string using the join() function and store it in
# another variable.
modified_str = ' '.join(str_lst)
# Print the String after removal of the last word from the above-given string.
print("The given string{", gven_strng,
      "} after removal of last word = ", modified_str)

Output:

Enter some random string = Good Morning btechgeeks hello every one
The given string{ Good Morning btechgeeks hello every one } after removal of last word = Good Morning btechgeeks hello every

Enhance your coding skills with our list of Python Basic Programs provided and become a pro in the general-purpose programming language Python in no time.

Python Program to Remove the last Word from the String/Sentence Read More »

Program for Modulo of Tuple Elements

Python Program for Modulo of Tuple Elements

In the previous article, we have discussed Python Program to find Sum of Tuple Items

Given a tuple and the task is to find the modulo of the given tuple elements.

zip() function in python:

The zip() function returns a zip object, which is an iterator of tuples in which the first item in each provided iterator is coupled together, and so on.

If the lengths of the provided iterators differ, the length of the new iterator is determined by the iterator with the fewest items.

Examples:

Example1:

Input:

Given First Tuple= (1, 10, 12, 3, 9)
Given Second Tuple=  (10, 9, 8, 7, 6)

Output:

The modulus of the given tuple elements (1, 10, 12, 3, 9) and (10, 9, 8, 7, 6) is:
(1, 1, 4, 3, 3)

Example2:

Input:

Given First Tuple= (12, 15, 18, 20, 64)
Given Second Tuple= (5, 6, 1, 7, 8)

Output:

The modulus of the given tuple elements (12, 15, 18, 20, 64) and (5, 6, 1, 7, 8) is:
(2, 3, 0, 6, 0)

Program for Modulo of Tuple Elements in Python

Below are the ways to find the modulo of the given tuple elements:

Method #1: Using zip() Function (Static Input)

Approach:

  • Give the first tuple as static input and store it in a variable.
  • Give the second tuple as static input and store it in another variable.
  • Loop in the first and second tuples using the zip() function, apply modulus function to the first tuple iterator value with the second tuple iterator value and convert this statement to a tuple using the tuple() function.
  • Print the modulo of the given tuple elements.
  • The Exit of the program.

Below is the implementation:

# Give the first tuple as static input and store it in a variable.
fst_tupl = (1, 10, 12, 3, 9)
# Give the second tuple as static input and store it in another variable.
secnd_tupl = (10, 9, 8, 7, 6)
# Loop in the first and second tuples using the zip() function, apply modulus function to
# the first tuple iterator value with the second tuple iterator value and convert this
# statement to a tuple using the tuple() function.
rslt_tupl = tuple(ele1 % ele2 for ele1, ele2 in zip(fst_tupl, secnd_tupl))
# Print the modulo of the given tuple elements.
print("The modulus of the given tuple elements",
      fst_tupl, "and", secnd_tupl, "is:")
print(rslt_tupl)

Output:

The modulus of the given tuple elements (1, 10, 12, 3, 9) and (10, 9, 8, 7, 6) is:
(1, 1, 4, 3, 3)

Method #2: Using zip() Function (User Input)

Approach:

  • Give the first tuple as user input using tuple(),map(),input(),and split() functions and Store it in a variable.
  • Give the second tuple as user input using tuple(),map(),input(),and split() functions and Store it in another variable.
  • Loop in the first and second tuples using the zip() function, apply modulus function to the first tuple iterator value with the second tuple iterator value and convert this statement to a tuple using the tuple() function.
  • Print the modulo of the given tuple elements.
  • The Exit of the program.

Below is the implementation:

# Give the first tuple as user input using tuple(),map(),input(),and split() functions and
# Store it in a variable.
fst_tupl = tuple(map(int, input( 'Enter some random tuple Elements separated by spaces = ').split()))
# Give the second tuple as user input using tuple(),map(),input(),and split() functions and
# Store it in another variable.
secnd_tupl = tuple(map(int, input( 'Enter some random tuple Elements separated by spaces = ').split()))
# Loop in the first and second tuples using the zip() function, apply modulus function to
# the first tuple iterator value with the second tuple iterator value and convert this
# statement to a tuple using the tuple() function.
rslt_tupl = tuple(ele1 % ele2 for ele1, ele2 in zip(fst_tupl, secnd_tupl))
# Print the modulo of the given tuple elements.
print("The modulus of the given tuple elements",
      fst_tupl, "and", secnd_tupl, "is:")
print(rslt_tupl)

Output:

Enter some random tuple Elements separated by spaces = 12 15 18 20 64
Enter some random tuple Elements separated by spaces = 5 6 1 7 8
The modulus of the given tuple elements (12, 15, 18, 20, 64) and (5, 6, 1, 7, 8) is:
(2, 3, 0, 6, 0)

Remediate your knowledge gap by attempting the Python Code Examples regularly and understand the areas of need and work on them.

Python Program for Modulo of Tuple Elements Read More »

Program to find Sum of Tuple Items

Python Program to find Sum of Tuple Items

In the previous article, we have discussed Python Program to Find the Sum of Series 1+X+X^2/2…+X^N/N

Given a tuple and the task is to find the sum of the given tuple elements.

Tuple in Python:

A tuple is an immutable list of objects. That means the elements of a tuple cannot be modified or changed while the program is running.

Examples:

Example1:

Input:

Given Tuple = (12, 1, 45, 20, 10, 15, 35)

Output:

The Sum of the given tuple elements (12, 1, 45, 20, 10, 15, 35) = 138

Example2:

Input:

Given Tuple = (30, 23, 65, 13, 45, 67, 89)

Output:

The Sum of the given tuple elements (30, 23, 65, 13, 45, 67, 89) = 332

Program To Find Sum of Tuple Items in Python

Below are the ways to find the sum of given tuple elements:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the tuple as static input and store it in a variable.
  • Take a variable say sum_tupl and initialize its value to 0.
  • Loop in the given tuple using the for loop.
  • Add the iterator value to the above-initialized sum_tupl and store it in the same variable sum_tupl.
  • Print sum_tupl to get the sum of given tuple elements.
  • The Exit of the program.

Below is the implementation:

# Give the tuple as static input and store it in a variable.
gven_tupl = (12, 1, 45, 20, 10, 15, 35)
# Take a variable say sum_tupl and initialize its value to 0.
sum_tupl = 0
# Loop in the above given tuple using the for loop.
for elemt in gven_tupl:
  # Add the iterator value to the above-initialized sum_tupl and store it in the same
    # variable sum_tupl.
    sum_tupl = sum_tupl + elemt
# Print sum_tupl to get the sum of given tuple elements.
print("The Sum of the given tuple elements", gven_tupl, "=", sum_tupl)

Output:

The Sum of the given tuple elements (12, 1, 45, 20, 10, 15, 35) = 138

Method #2: Using For loop (User Input)

Approach:

  • Give the tuple as user input using tuple(),map(),input(),and split() functions and Store it in a variable.
  • Take a variable say sum_tupl and initialize its value to 0.
  • Loop in the given tuple using the for loop.
  • Add the iterator value to the above-initialized sum_tupl and store it in the same variable sum_tupl.
  • Print sum_tupl to get the sum of given tuple elements.
  • The Exit of the program.

Below is the implementation:

# Give the tuple as user input using tuple(),map(),input(),and split() functions
# and Store it in a variable.
gven_tupl = tuple(map(int, input( 'Enter some random tuple Elements separated by spaces = ').split()))
# Take a variable say sum_tupl and initialize its value to 0.
sum_tupl = 0
# Loop in the above given tuple using the for loop.
for elemt in gven_tupl:
  # Add the iterator value to the above-initialized sum_tupl and store it in the same
    # variable sum_tupl.
    sum_tupl = sum_tupl + elemt
# Print sum_tupl to get the sum of given tuple elements.
print("The Sum of the given tuple elements", gven_tupl, "=", sum_tupl)

Output:

Enter some random tuple Elements separated by spaces = 25 34 89 75 16 3 2
The Sum of the given tuple elements (25, 34, 89, 75, 16, 3, 2) = 244

Method #3: Using sum() Function (Static Input)

Approach:

  • Give the tuple as static input and store it in a variable.
  • Calculate the sum of given tuple elements using the sum() function and store it in another variable.
  • Print the sum of the above-given tuple elements.
  • The Exit of the program.

Below is the implementation:

# Give the tuple as static input and store it in a variable.
gven_tupl = (10, 20, 45, 63, 72, 91)
# Calculate the sum of given tuple elements using the sum() function and
# store it in another variable.
sum_tupl = sum(gven_tupl)
# Print the sum of the above-given tuple elements.
print("The Sum of the given tuple elements", gven_tupl, "=", sum_tupl)

Output:

The Sum of the given tuple elements (10, 20, 45, 63, 72, 91) = 301

Method #4: Using sum() Function (User Input)

Approach:

  • Give the tuple as user input using tuple(),map(),input(),and split() functions and Store it in a variable.
  • Calculate the sum of given tuple elements using the sum() function and store it in another variable.
  • Print the sum of the above-given tuple elements.
  • The Exit of the program.

Below is the implementation:

# Give the tuple as user input using tuple(),map(),input(),and split() functions and
# Store it in a variable.
gven_tupl = tuple(map(int, input( 'Enter some random tuple Elements separated by spaces = ').split()))
# Calculate the sum of given tuple elements using the sum() function and
# store it in another variable.
sum_tupl = sum(gven_tupl)
# Print the sum of the above-given tuple elements.
print("The Sum of the given tuple elements", gven_tupl, "=", sum_tupl)

Output:

Enter some random tuple Elements separated by spaces = 1 2 6 7 9 6
The Sum of the given tuple elements (1, 2, 6, 7, 9, 6) = 31

Explore more Example Python Programs with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.

Python Program to find Sum of Tuple Items Read More »

Program to Print Palindrome Numbers in a Range

Python Program to Print Palindrome Numbers in a Range

In the previous article, we have discussed Python Program to get First Element of each Tuple in a List

Palindrome:

If a number reads the same both forward and backward, it is called a Palindrome number. And the insane part is that it doesn’t just apply to numbers. Even if a string reads the same in both directions, it is always a Palindrome!

Given lower limit and upper limit range and the task is to print the palindrome numbers in a given range.

Examples:

Example1:

Input:

Given lower limit = 50
Given upper limit= 130

Output:

The palindrome numbers between 50 and 130 are:
55 66 77 88 99 101 111 121

Example2:

Input:

Given lower limit = 1
Given upper limit= 300

Output:

The palindrome numbers between 1 and 300 are:
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 111 121 131 141 151 161 171 181 191 202 212 222 232 242 252 262 272 282 292

Program to Print Palindrome Numbers in a Range in Python

Below are the ways to print the palindrome numbers in a given range:

Method #1: Using While Loop (Static Input)

Approach:

  • Give the lower limit range as static input and store it in a variable.
  • Give the upper limit range as static input and store it in another variable.
  • Loop from lower limit range to upper limit range using For loop.
  • Inside the loop, give the iterator value as the number of the for loop.
  • Take another variable say duplicate_num and initialize it with the given number
  • Set the variable reverse_number to 0.
  • Loop while number > 0.
  • Multiply reverse_number by 10 and add the remainder to reverse_number like below
  • reverse_number = (reverse_number * 10) + remainder
  • Divide the given number by 10 to remove the last digit.
  • If the duplicate_num and reverse_number are equal then it is a palindrome.
  • Print “duplicate_num “to get the above palindrome number.
  • The Exit of the Program.

Below is the implementation:

# Give the lower limit range as static input and store it in a variable.
gvn_lowrlmt = 50
# Give the upper limit range as static input and store it in another variable.
gvn_upprlmt = 130
print("The palindrome numbers between",
      gvn_lowrlmt, "and", gvn_upprlmt, "are:")
# Loop from lower limit range to upper limit range using For loop.
for m in range(gvn_lowrlmt, gvn_upprlmt+1):
    # Inside the loop, give the iterator value as the number of the for loop.
    given_num = m
    # taking another variable to store the copy of original number
    # and initialize it with given num
    duplicate_num = given_num
    # Take a variable reverse_number and initialize it to null
    reverse_number = 0
    # using while loop to reverse the given number
    while (given_num > 0):
        # implementing the algorithm
        # getting the last digit
        remainder = given_num % 10
        reverse_number = (reverse_number * 10) + remainder
        given_num = given_num // 10
   # if duplicate_num and reverse_number are equal then it is palindrome
    if(duplicate_num == reverse_number):
        # Print "duplicate_num "to get the above palindrome number.
        print(duplicate_num, end=" ")

Output:

The palindrome numbers between 50 and 130 are:
55 66 77 88 99 101 111 121

Method #2: Using While loop (User Input)

Approach:

  • Give the lower limit range as user input using the int(input()) function and store it in a variable.
  • Give the upper limit range as user input using the int(input()) function and store it in another variable.
  • Loop from lower limit range to upper limit range using For loop.
  • Inside the loop, give the iterator value as the number of the for loop.
  • Take another variable say duplicate_num and initialize it with the given number
  • Set the variable reverse_number to 0.
  • Loop while number > 0.
  • Multiply reverse_number by 10 and add the remainder to reverse_number like below
  • reverse_number = (reverse_number * 10) + remainder
  • Divide the given number by 10 to remove the last digit.
  • If the duplicate_num and reverse_number are equal then it is a palindrome.
  • Print “duplicate_num “to get the above palindrome number.
  • The Exit of the Program.

Below is the implementation:

# Give the lower limit range as user input using the int(input()) function
# and store it in a variable.
gvn_lowrlmt = int(input("Enter some random Number = "))
# Give the upper limit range as user input using the int(input()) function 
# and store it in another variable.
gvn_upprlmt = int(input("Enter some random Number = "))
print("The palindrome numbers between",
      gvn_lowrlmt, "and", gvn_upprlmt, "are:")
# Loop from lower limit range to upper limit range using For loop.
for m in range(gvn_lowrlmt, gvn_upprlmt+1):
    # Inside the loop, give the iterator value as the number of the for loop.
    given_num = m
    # taking another variable to store the copy of original number
    # and initialize it with given num
    duplicate_num = given_num
    # Take a variable reverse_number and initialize it to null
    reverse_number = 0
    # using while loop to reverse the given number
    while (given_num > 0):
        # implementing the algorithm
        # getting the last digit
        remainder = given_num % 10
        reverse_number = (reverse_number * 10) + remainder
        given_num = given_num // 10
   # if duplicate_num and reverse_number are equal then it is palindrome
    if(duplicate_num == reverse_number):
        # Print "duplicate_num "to get the above palindrome number.
        print(duplicate_num, end=" ")

Output:

Enter some random Number = 1
Enter some random Number = 300
The palindrome numbers between 1 and 300 are:
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 111 121 131 141 151 161 171 181 191 202 212 222 232 242 252 262 272 282 292

Dive into numerous Python Programming Language Examples for practice and get the best out of the tutorial and learn python one step at a time.

Python Program to Print Palindrome Numbers in a Range Read More »

Program to Find Sum of Series (1+(1+2)+(1+2+3)+...till N)

Python Program to Find Sum of Series (1+(1+2)+(1+2+3)+…till N)

In the previous article, we have discussed Python Program to Swap Upper Diagonal Elements with Lower Diagonal Elements of Matrix.

Given a number N and the task is to find the sum of the given series (1+(1+2)+(1+2+3)+…till N) for the given number in Python.

Examples:

Example1:

Input:

Given Number = 25

Output:

The total sum of the series till the given number { 25 } =  2925

Example2:

Input:

Given Number = 18

Output:

The total sum of the series till the given number { 18 } =  1140

Program to Find Sum of Series (1+(1+2)+(1+2+3)+…till N) in Python

Below are the ways to find the sum of the given series (1+(1+2)+(1+2+3)+…till N) for the given number in Python

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Take another variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Inside the loop, iterate from 1 to the value of k using the for loop.
  • Add the iterator value to the above-initialized resltsum and store it in the same variable resltsum.
  • Increment the value of k by 1 outside the for loop.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 25
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Take another variable say k and initialize its value with 1.
k = 1
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
  # Inside the loop, iterate from 1 to the value of k using the for loop.
    for itr in range(1, k+1):
      # Add the iterator value to the above-initialized resltsum and store it in the same
        # variable resltsum.
        resltsum += itr
  # Increment the value of k by 1 outside the for loop.
    k += 1
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

The total sum of the series till the given number { 25 } =  2925

Method #2: Using While loop (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Take another variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Inside the loop, iterate from 1 to the value of k using the for loop.
  • Add the iterator value to the above-initialized resltsum and store it in the same variable resltsum.
  • Increment the value of k by 1 outside the for loop.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function and
# store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Take another variable say k and initialize its value with 1.
k = 1
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
  # Inside the loop, iterate from 1 to the value of k using the for loop.
    for itr in range(1, k+1):
      # Add the iterator value to the above-initialized resltsum and store it in the same
        # variable resltsum.
        resltsum += itr
  # Increment the value of k by 1 outside the for loop.
    k += 1
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

Enter some Random Number = 18
The total sum of the series till the given number { 18 } = 1140

Find a comprehensive collection of Examples of Python Programs ranging from simple ones to complex ones to guide you throughout your coding journey.

 

Python Program to Find Sum of Series (1+(1+2)+(1+2+3)+…till N) Read More »

Program to Find the Sum of Series 1+X+X^22...+X^NN

Python Program to Find the Sum of Series 1+X+X^2/2…+X^N/N

In the previous article, we have discussed Python Program to Find the Sum of Series 1+X+X^3…+X^N

Given the numbers, N and X and the task is to find the sum of the given series (1+X+X^2/2…+X^N/N) for the given numbers in Python.

Examples:

Example1:

Input:

Given Number = 7
Given X value = 6

Output:

The total sum of the series till the given number N and x value { 7 6 } =  49743.05714285714

Example2:

Input:

Given Number = 4
Given X value = 3

Output:

The total sum of the series till the given number N and x value { 4 3 } =  37.75

Program to Find the Sum of Series 1+X+X^2/2…+X^N/N in Python

Below are the ways to find the sum of the given series (1+X+X^2/2…+X^N/N) for the given numbers in Python :

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Give the value of X as static input and store it in another variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 1.0 (to get in float format).
  • Take another variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Inside the loop, calculate the value of X raised to the power k divided by k using the pow() function and store it in a variable say p.
  • Add the above value of p to the resltsum and store it in the same variable resltsum .
  • Increment the value of k by 1 and store it in the same variable k.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 7
# Give the value of X as static input and store it in another variable.
gvn_x_val = 6
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 1.0 (to get in float format).
resltsum = 1.0
# Take another variable say k and initialize its value with 1.
k = 1
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
 # Inside the loop, calculate the value of X raised to the power k divided by k using the
    # pow() function and store it in a variable say p.
    p = pow(gvn_x_val, k)/k
  # Add the above value of p to the resltsum and store it in the same variable resltsum.
    resltsum += p
 # Increment the value of k by 1 and store it in the same variable k.
    k += 1
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number N and x value {", gvn_numb, gvn_x_val, "} = ", resltsum)

Output:

The total sum of the series till the given number N and x value { 7 6 } =  49743.05714285714

Method #2: Using While loop (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Give the value of X as user input using the int(input()) function and store it in another variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 1.0 (to get in float format).
  • Take another variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Inside the loop, calculate the value of X raised to the power k divided by k using the pow() function and store it in a variable say p.
  • Add the above value of p to the resltsum and store it in the same variable resltsum .
  • Increment the value of k by 1 and store it in the same variable k.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function and
# store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Give the value of X as user input using the int(input()) function and
# store it in another variable.
gvn_x_val = int(input("Enter some Random Number = "))
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 1.0 (to get in float format).
resltsum = 1.0
# Take another variable say k and initialize its value with 1.
k = 1
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
 # Inside the loop, calculate the value of X raised to the power k divided by k using the
    # pow() function and store it in a variable say p.
    p = pow(gvn_x_val, k)/k
  # Add the above value of p to the resltsum and store it in the same variable resltsum.
    resltsum += p
 # Increment the value of k by 1 and store it in the same variable k.
    k += 1
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number N and x value {", gvn_numb, gvn_x_val, "} = ", resltsum)

Output:

Enter some Random Number = 4
Enter some Random Number = 3
The total sum of the series till the given number N and x value { 4 3 } = 37.75

If you are learning Python then the Python Programming Example is for you and gives you a thorough description of concepts for beginners, experienced programmers.

Python Program to Find the Sum of Series 1+X+X^2/2…+X^N/N Read More »

Program to Find the Sum of Series 1+X+X^3...+X^N

Python Program to Find the Sum of Series 1+X+X^3…+X^N

In the previous article, we have discussed Python Program to Find Sum of Series 1/2-2/3+3/4-4/5+5/6…+N/N+1

Given the numbers, N and X and the task is to find the sum of the given series (1+X+X^3…+X^N) for the given numbers in Python.

Examples:

Example1:

Input:

Given Number = 4
Given X value =  8

Output:

The total sum of the series till the given number N and x value { 4 8 } =  520

Example2:

Input:

Given Number = 5
Given X value = 11

Output:

The total sum of the series till the given number N and x value { 5 11 } =  162393

Program to Find the Sum of Series 1+X+X^3…+X^N in Python

Below are the ways to find the sum of the given series (1+X+X^3…+X^N) for the given numbers in Python :

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Give the value of X as static input and store it in another variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Take another variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Inside the loop, calculate the value of X raised to the power k using the pow() function and store it in a variable say p.
  • Add the above value of p to the resltsum and store it in the same variable.
  • Increment the value of k by 2 and store it in the same variable k.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 4
# Give the value of x as static input and store it in another variable.
gvn_x_val = 8
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Take another variable say k and initialize its value with 1.
k = 1
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
    # Inside the loop, calculate the value of X raised to the power k using the pow() function
    # and store it in a variable say p.
    p = pow(gvn_x_val, k)
 # Add the above value of p to the resltsum and store it in the same variable.
    resltsum += p
 # Increment the value of k by 2 and store it in the same variable k.
    k += 2
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number N and x value {", gvn_numb, gvn_x_val, "} = ", resltsum)

Output:

The total sum of the series till the given number N and x value { 4 8 } =  520

Method #2: Using While loop (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Give the value of X as user input using the int(input()) function and store it in another variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Take another variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Inside the loop, calculate the value of X raised to the power k using the pow() function and store it in a variable say p.
  • Add the above value of p to the resltsum and store it in the same variable.
  • Increment the value of k by 2 and store it in the same variable k.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function and
# store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Give the value of X as user input using the int(input()) function and
# store it in another variable.
gvn_x_val = int(input("Enter some Random Number = "))
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Take another variable say k and initialize its value with 1.
k = 1
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
    # Inside the loop, calculate the value of X raised to the power k using the pow() function
    # and store it in a variable say p.
    p = pow(gvn_x_val, k)
 # Add the above value of p to the resltsum and store it in the same variable.
    resltsum += p
 # Increment the value of k by 2 and store it in the same variable k.
    k += 2
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number N and x value {", gvn_numb, gvn_x_val, "} = ", resltsum)

Output:

Enter some Random Number = 5
Enter some Random Number = 11
The total sum of the series till the given number N and x value { 5 11 } = 162393

Grab the opportunity and utilize the Python Program Code Examples over here to prepare basic and advanced topics too with ease and clear all your doubts.

Python Program to Find the Sum of Series 1+X+X^3…+X^N Read More »