Python

Python Program to Generate first N numbers of Pell Series

Python Program to Generate first N numbers of Pell Series

Given the number N, the task is to print the first n numbers of the pell series in Python.

Pell Series:

The Pell Series is a series in which the next number equals the sum of twice the previous number and the number preceding the previous number.

It begins with 1 and 2.

Pell Numbers: 1,2,5,12,29,70,169,…

Consider the following number sequence and try to match it to the definition of the Pell Series:

1 + 2 x 2 = 5

2 + 5* 2 = 12

5 + 12 x 2 = 29

12 + 29*2 = 70

29 + 70*2 = 169

and so on…

Now that you have a clear idea of what the Pell Series are, let’s move on to how we shall write the Python code to generate the same.

Examples:

Example1:

Given number = 12

Input:

The first 12 numbers of the pell series are :
1 2 5 12 29 70 169 408 985 2378 5741 13860

Output:

Example2:

Input:

Given number = 21

Output:

The first 21 numbers of the pell series are :
1 2 5 12 29 70 169 408 985 2378 5741 13860 33461 80782 195025 470832 1136689 2744210 6625109
 15994428 38613965

Program to Generate first N numbers of Pell Series in Python

Below are the ways to generate the first N number of the pell series in Python.

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable temp1 and initialize it to 1.
  • Take another variable temp2 and initialize it to 2.
  • Take the third variable temp3 and initialize it to 0.
  • Now, print the first two numbers in the series, and we’ll print the other generated numbers in the order they were generated.
  • Loop till number -2 using For loop.
  • Add the value of temp1 and twice the temp2 value and initialize the temp3 with this value.
  • Swap the previous number for the previous number’s predecessor (temp1 =temp2).
  • Swap the generated and previously generated numbers (temp2 = temp3).
  • Print the value of temp3.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
numb = 21
# Take a variable temp1 and initialize it to 1.
temp1 = 1
# Take another variable temp2 and initialize it to 2.
temp2 = 2
# Take the third variable temp3 and initialize it to 0.
temp3 = 0
# Now, print the first two numbers in the series,
# and we'll print the other generated numbers in the order they were generated.
print('The first', numb, 'numbers of the pell series are :')
print(temp1, temp2, end=' ')
# Loop till number -2 using For loop.
for i in range(numb-2):
    # Add the value of temp1 and twice the temp2 value
    # and initialize the temp3 with this value.
    temp3 = temp1+2*temp2
    # Swap the previous number for the previous number's predecessor (temp1 =temp2).
    temp1 = temp2
    # Swap the generated and previously generated numbers (temp2 = temp3)
    temp2 = temp3
    # Print the value of temp3.
    print(temp3, end=' ')

Output:

The first 21 numbers of the pell series are :
1 2 5 12 29 70 169 408 985 2378 5741 13860 33461 80782 195025 470832 1136689 2744210 6625109
 15994428 38613965

Method #2: Using For Loop (User Input)

Approach:

  • Give the number N as user input using int(input()) and store it in a variable.
  • Take a variable temp1 and initialize it to 1.
  • Take another variable temp2 and initialize it to 2.
  • Take the third variable temp3 and initialize it to 0.
  • Now, print the first two numbers in the series, and we’ll print the other generated numbers in the order they were generated.
  • Loop till number -2 using For loop.
  • Add the value of temp1 and twice the temp2 value and initialize the temp3 with this value.
  • Swap the previous number for the previous number’s predecessor (temp1 =temp2).
  • Swap the generated and previously generated numbers (temp2 = temp3).
  • Print the value of temp3.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using int(input()) and store it in a variable.
numb = int(input('Enter some random number = '))
# Take a variable temp1 and initialize it to 1.
temp1 = 1
# Take another variable temp2 and initialize it to 2.
temp2 = 2
# Take the third variable temp3 and initialize it to 0.
temp3 = 0
# Now, print the first two numbers in the series,
# and we'll print the other generated numbers in the order they were generated.
print('The first', numb, 'numbers of the pell series are :')
print(temp1, temp2, end=' ')
# Loop till number -2 using For loop.
for i in range(numb-2):
    # Add the value of temp1 and twice the temp2 value
    # and initialize the temp3 with this value.
    temp3 = temp1+2*temp2
    # Swap the previous number for the previous number's predecessor (temp1 =temp2).
    temp1 = temp2
    # Swap the generated and previously generated numbers (temp2 = temp3)
    temp2 = temp3
    # Print the value of temp3.
    print(temp3, end=' ')

Output:

Enter some random number = 12
The first 12 numbers of the pell series are :
1 2 5 12 29 70 169 408 985 2378 5741 13860

Related Programs:

Python Program to Generate first N numbers of Pell Series Read More »

Program to Find the Sum of the Series 1 + x^22 + x^33 + … x^nn

Python Program to Find the Sum of the Series: 1 + x^2/2 + x^3/3 + … x^n/n

This is a mathematical series application in which the user must enter the number of terms up to which the series’ sum is to be found. Following that, we need to know the value of x, which serves as the series’ starting point.

Given number of terms n and x the task is to calculate the sum of the given series in Python

Examples:

Example1:

Input:

total number of terms = 7       x=2

Output:

The total sum of the series with x= 2 and number of terms= 7 is 45.02

Example2:

Input:

Enter the total numbers of terms =17   x=1

Output:

The total sum of the series with x= 1 and number of terms= 17 is 3.44

Program to Find the Sum of the Series: 1 + x^2/2 + x^3/3 + … x^n/n in Python

There are several ways to find the sum of the given series some of them are:

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Method #1:Using for loop (Static input)

Approach:

  • Give the number of terms and x  as static input.
  • Set a totalsum variable which calculates the total sum and initialize it to 1.
  • Calculate the numerator by using x**i where i is iterator value.
  • Find the total of the series using a for loop ranging from 2 to the given number of terms.
  • Print the totalsum of the series, rounded to two decimal places.
  • The Exit of the program.

Below is the implementation:

# Give the number of terms as static input.
numb = 7
# given value of x as static input
x = 2
# Set a totalsum variable which calculates the total sum and initialize it to 0.
totalsum = 1
# Find the total of the series using a for loop ranging from 1 to the number.
for value in range(2, numb+1):
    # Calculate the numerator by using x**i where i is iterator value.
    numer = x**value
    # calculating the total sum
    totalsum = totalsum+(numer/value)
# Print the totalsum of the series, rounded to two decimal places.
print("The total sum of the series with x=", x,
      'and number of terms=', numb, 'is', round(totalsum, 2))

Output:

The total sum of the series with x= 2 and number of terms= 7 is 45.02

Method #2:Using for loop (User input)

Approach:

  • Scan the total number of terms and x  as user input using int(input()).
  • Set a totalsum variable which calculates the total sum and initialize it to 1.
  • Calculate the numerator by using x**i where i is iterator value.
  • Find the total of the series using a for loop ranging from 2 to the given number of terms.
  • Print the totalsum of the series, rounded to two decimal places.
  • The Exit of the program.

Below is the implementation:

# Scan the total number of terms and x  as user input using int(input()).
numb = int(input("Enter the total numbers of terms ="))
# scan the x using int(input()) function
x = int(input('Enter given x = '))
# Set a totalsum variable which calculates the total sum and initialize it to 0.
totalsum = 1
# Find the total of the series using a for loop ranging from 1 to the number.
for value in range(2, numb+1):
    # Calculate the numerator by using x**i where i is iterator value.
    numer = x**value
    # calculating the total sum
    totalsum = totalsum+(numer/value)
# Print the totalsum of the series, rounded to two decimal places.
print("The total sum of the series with x=", x,
      'and number of terms=', numb, 'is', round(totalsum, 2))

Output:

Enter the total numbers of terms =6
Enter given x = 3
The total sum of the series with x= 3 and number of terms= 6 is 204.85

Method #3:Using while loop (User input)

Approach:

  • Scan the total number of terms and x  as user input using int(input()).
  • Set a totalsum variable which calculates the total sum and initialize it to 1.
  • Take a variable say value and initialize it to 2.
  • Using while loop calculate the total sum till value greater than given number
  • Calculate the numerator by using x**i where i is iterator value.
  • Calculate the totalsum by incrementing it value with numerator/value.
  • Increment the value by 1.
  • Print the totalsum of the series, rounded to two decimal places.
  • Exit of program.

Below is the implementation:

# Scan the total number of terms as user input by using int(input()) function.
numb = int(input("Enter the total numbers of terms ="))
# scan the x using int(input()) function
x = int(input('Enter given x = '))
# Take a variable say value and initialize it to 2.
value = 2
# Set a totalsum variable which calculates the total sum and initialize it to 1.
totalsum = 1
# Using while loop calculate the total sum till value greater than given number
while(value <= numb):
    # Calculate the numerator by using x**i where i is iterator value.
    numer = x**value
    # calculating the total sum
    totalsum = totalsum+(numer/value)
    # Increment the value by 1.
    value = value+1
# Print the totalsum of the series, rounded to two decimal places.
print("The total sum of the series with x=", x,
      'and number of terms=', numb, 'is', round(totalsum, 2))

Output:

Enter the total numbers of terms =17
Enter given x = 1
The total sum of the series with x= 1 and number of terms= 17 is 3.44

Method #4:Using While loop( Static Input)

Approach:

  • Give the number of terms and x  as static input.
  • Set a totalsum variable which calculates the total sum and initialize it to 1.
  • Take a variable say value and initialize it to 2.
  • Using while loop calculate the total sum till value greater than given number
  • Calculate the numerator by using x**i where i is iterator value.
  • Calculate the totalsum by incrementing it value with numerator/value.
  • Increment the value by 1.
  • Print the totalsum of the series, rounded to two decimal places.
  • Exit of program.

Below is the implementation:

# Give the number of terms as static input.
numb = 7
# given value of x as static input
x = 2
# Take a variable say value and initialize it to 2.
value = 2
# Set a totalsum variable which calculates the total sum and initialize it to 1.
totalsum = 1
# Using while loop calculate the total sum till value greater than given number
while(value <= numb):
    # Calculate the numerator by using x**i where i is iterator value.
    numer = x**value
    # calculating the total sum
    totalsum = totalsum+(numer/value)
    # Increment the value by 1.
    value = value+1
# Print the totalsum of the series, rounded to two decimal places.
print("The total sum of the series with x=", x,
      'and number of terms=', numb, 'is', round(totalsum, 2))

Output:

The total sum of the series with x= 2 and number of terms= 7 is 45.02

Related Programs:

Python Program to Find the Sum of the Series: 1 + x^2/2 + x^3/3 + … x^n/n Read More »

Program to Find the Sum of the Series 1 + 12 + 13 + ….. + 1N

Python Program to Find the Sum of the Series: 1 + 1/2 + 1/3 + ….. + 1/N

Given a number , the task is to find the sum of series 1 + 1 / 2 + …… + 1 /number in Python.

The inverse of a series is considered to be in Harmonic Progression if it follows the rule of an A.P, i.e. Arithmetic Progression. In general, the terms in a harmonic progression can be written as 1/a, 1/(a + d), 1/(a + 2d), 1/(a + 3d),…. 1/(a + nd).
As the Nth term of AP is (a + (n – 1)d).
As a result, the Nth term of harmonic progression is the reciprocal of the Nth term of AP, which is: 1/(a + (n – 1)d), where “a” is the first term of AP and “d” is the common difference.

Examples:

Example1:

Input:

total number of terms = 17

Output:

The total sum of the series  3.44

Example2:

Input:

Enter the total numbers of terms =26

Output:

The total sum of the series 3.85

Program to Find the Sum of the Series: 1 + 1/2 + 1/3 + ….. + 1/N in Python

There are several ways to find the sum of the given series some of them are:

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Method #1:Using for loop (Static input)

Approach:

  • Give the number of terms as static input.
  • Set a totalsum variable which calculates the total sum and initialize it to 0.
  • Find the total of the series using a for loop ranging from 1 to the number.
  • Print the totalsum of the series, rounded to two decimal places.
  • Exit of program.

Below is the implementation:

# Give the number of terms as static input.
numb = 17
# Set a totalsum variable which calculates the total sum and initialize it to 0.
totalsum = 0
# Find the total of the series using a for loop ranging from 1 to the number.
for value in range(1, numb+1):
  # calculating the total sum
    totalsum = totalsum+(1/value)
# Print the totalsum of the series, rounded to two decimal places.
print("The total sum of the series ", round(totalsum, 2))

Output:

The total sum of the series  3.44

Method #2:Using for loop (User input)

Approach:

  • Scan the total number of terms as user input by using int(input()) function.
  • Set a totalsum variable which calculates the total sum and initialize it to 0.
  • Find the total of the series using a for loop ranging from 1 to the number.
  • Print the totalsum of the series, rounded to two decimal places.
  • Exit of program.

Below is the implementation:

# Scan the total number of terms as user input by using int(input()) function.
numb = int(input("Enter the total numbers of terms ="))
# Set a totalsum variable which calculates the total sum and initialize it to 0.
totalsum = 0
# Find the total of the series using a for loop ranging from 1 to the number.
for value in range(1, numb+1):
  # calculating the total sum
    totalsum = totalsum+(1/value)
# Print the totalsum of the series, rounded to two decimal places.
print("The total sum of the series ", round(totalsum, 2))

Output:

Enter the total numbers of terms =26
The total sum of the series 3.85

Method #3:Using while loop (User input)

Approach:

  • Scan the total number of terms as user input by using int(input()) function.
  • Set a totalsum variable which calculates the total sum and initialize it to 0.
  • Take a variable say value and initialize it to 1.
  • Using while loop calculate the total sum till value greater than given number
  • Calculate the totalsum by incrementing it value with 1/value.
  • Increment the value by 1.
  • Print the totalsum of the series, rounded to two decimal places.
  • The Exit of the program.

Below is the implementation:

# Scan the total number of terms as user input by using int(input()) function.
numb = int(input("Enter the total numbers of terms ="))
# Take a variable say value and initialize it to 1.
value = 1
# Set a totalsum variable which calculates the total sum and initialize it to 0.
totalsum = 0
# Using while loop calculate the total sum till value greater than given number
while(value <= numb):
  # calculating the total sum
    totalsum = totalsum+(1/value)
    # Increment the value by 1.
    value = value+1
# Print the totalsum of the series, rounded to two decimal places.
print("The total sum of the series ", round(totalsum, 2))

Output:

Enter the total numbers of terms =26
The total sum of the series 3.85

Method #4:Using While loop( Static Input)

Approach:

  • Give the number of terms as static input.
  • Set a totalsum variable which calculates the total sum and initialize it to 0.
  • Take a variable say value and initialize it to 1.
  • Using while loop calculate the total sum till value greater than given number
  • Calculate the totalsum by incrementing it value with 1/value.
  • Increment the value by 1.
  • Print the totalsum of the series, rounded to two decimal places.
  • The Exit of the program.

Below is the implementation:

# Give the number of terms as static input.
numb = 26
# Take a variable say value and initialize it to 1.
value = 1
# Set a totalsum variable which calculates the total sum and initialize it to 0.
totalsum = 0
# Using while loop calculate the total sum till value greater than given number
while(value <= numb):
  # calculating the total sum
    totalsum = totalsum+(1/value)
    # Increment the value by 1.
    value = value+1
# Print the totalsum of the series, rounded to two decimal places.
print("The total sum of the series ", round(totalsum, 2))

Output:

The total sum of the series 3.85

Related Programs:

Python Program to Find the Sum of the Series: 1 + 1/2 + 1/3 + ….. + 1/N Read More »

Python Program to Find Minimum Number of Steps to Reach M from N

Python Program to Find Minimum Number of Steps to Reach M from N

Given two numbers m and n the task is to get the lowest number of steps to get from M to N in Python We’ll only use two operations to get from M to N.

  • Multiply the given number by 2.
  • Subtract 1 from the given number.

Examples:

Example1:

Input:

Given Number M=10
Given Number N=6

Output:

The result is 2

Example2:

Input:

Given Number M=12
Given Number N=19

Output:

The result is 7

Program to Find Minimum Number of Steps to Reach M from N in Python

Below are the ways to find the minimum number of steps to reach M from N in Python.

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Method #1: Using While loop (Static Input)

Approach:

  • Give the two numbers m and n as static input and store them in two separate variables.
  • Take a variable result and initialize the variable with 0.
  • Loop till M is greater than N using while loop.
  • Check if M is even or odd using the If statement.
  • If it is true then increment the value of M by 1 and result by 1.
  • Divide the M by 2 after the end of the If statement.
  • Increment the result by 1.
  • Print the result +N-M.
  • The Exit of the Program.

Below is the implementation:

# Give the two numbers m and n as static input and store them in two separate variables.
mNumb = 10
nNumb = 6
# Take a variable result and initialize the variable with 0.
resu = 0
# Loop till M is greater than N using while loop.
while(mNumb > nNumb):
    # Check if M is even or odd using the If statement.
    if (mNumb & 1):
        # If it is true then increment the value of M by 1 and result by 1.
        mNumb += 1
        resu += 1
    # Divide the M by 2 after the end of the If statement.
    mNumb //= 2
    # Increment the result by 1.
    resu += 1
# Print the result +N-M.
resu = resu+nNumb-mNumb
print('The result is', resu)

Output:

The result is 2

Method #2: Using While loop (User Input)

Approach:

  • Give the two numbers m and n as user input using map(), int(), and split() functions.
  • Store them in two separate variables.
  • Take a variable result and initialize the variable with 0.
  • Loop till M is greater than N using while loop.
  • Check if M is even or odd using the If statement.
  • If it is true then increment the value of M by 1 and result by 1.
  • Divide the M by 2 after the end of the If statement.
  • Increment the result by 1.
  • Print the result +N-M.
  • The Exit of the Program.

Below is the implementation:

# Give the two numbers m and n as user input using map(), int(), and split() functions.
# Store them in two separate variables.
mNumb, nNumb = map(int, input('Enter some random numbers M and N =').split())
# Take a variable result and initialize the variable with 0.
resu = 0
# Loop till M is greater than N using while loop.
while(mNumb > nNumb):
    # Check if M is even or odd using the If statement.
    if (mNumb & 1):
        # If it is true then increment the value of M by 1 and result by 1.
        mNumb += 1
        resu += 1
    # Divide the M by 2 after the end of the If statement.
    mNumb //= 2
    # Increment the result by 1.
    resu += 1
# Print the result +N-M.
resu = resu+nNumb-mNumb
print('The result is', resu)

Output:

Enter some random numbers M and N =12 19
The result is 7

Related Programs:

Python Program to Find Minimum Number of Steps to Reach M from N Read More »

Random Choice of Random Module in Python with no Repeat

Random Choice of Random Module in Python with no Repeat

Given the upper limit and lower limit, the task is to generate n natural numbers which are not repeating in Python.

Examples:

Example1:

Input:

Given N=13
Given lower limit range =19
Given upper limit range =45

Output:

The random numbers present in the range from 19 to 45 are :
28 40 24 25 20 44 38 29 21 31 43

Example2:

Input:

Given N=19
Given lower limit range =23
Given upper limit range =41

Output:

The random numbers present in the range from 23 to 41 are : 26 27 40 38 37 41 30 35 36 23 25

Random choice of Random module in Python with no Repeat

Below are the ways to generate n natural numbers which are not repeating in Python.

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Method #1: Using For Loop and randint function (Static Input)

Approach:

  • Import the random module using the import keyword.
  • Give the number n as static input and store it in a variable.
  • Give the lower limit range and upper limit range as static input and store them in two separate variables.
  • Take an empty list (say rndmnumbs) and initialize it with an empty list using [] or list().
  • Loop till n times using For loop.
  • Generate a random number using randint(lowerlimitrange,upperlimitrange) and store it in a variable.
  • Check whether the above random number is present in the list or not using not in operator.
  • If it is not in the list then append the element to the rndmnumbs list using the append() function.
  • Print the rndmnumbs.
  • The Exit of the Program.

Below is the implementation:

# Import the random module using the import keyword.
import random
# Give the number n as static input and store it in a variable.
numbe = 13
# Give the lower limit range and upper limit range as static input
# and store them in two separate variables.
lowerlimitrange = 19
upperlimitrange = 45
# Take an empty list (say rndmnumbs) and initialize it with an empty list
# using [] or list().
rndmnumbs = []
# Loop till n times using For loop.
for m in range(numbe):
        # Generate a random number using randint(lowerlimitrange,upperlimitrange)
    # and store it in a variable.
    randomnumbe = random.randint(lowerlimitrange, upperlimitrange)
    # Check whether the above random number is present in the list or not
    # using not in operator.
    if randomnumbe not in rndmnumbs:
        # If it is not in the list then append the element
        # to the rndmnumbs list using the append() function.
        rndmnumbs.append(randomnumbe)

# Print the rndmnumbs
print('The random numbers present in the range from',
      lowerlimitrange, 'to', upperlimitrange, 'are :')
for q in rndmnumbs:
    print(q, end=' ')

Output:

The random numbers present in the range from 19 to 45 are :
28 40 24 25 20 44 38 29 21 31 43

Method #2: Using For Loop and randint function (User Input)

Approach:

  • Import the random module using the import keyword.
  • Give the number n as user input using int(input()) and store it in a variable.
  • Give the lower limit range and upper limit range as user input using map(),int(),split(),input() functions.
  • Store them in two separate variables.
  • Take an empty list (say rndmnumbs) and initialize it with an empty list using [] or list().
  • Loop till n times using For loop.
  • Generate a random number using randint(lowerlimitrange,upperlimitrange) and store it in a variable.
  • Check whether the above random number is present in the list or not using not in operator.
  • If it is not in the list then append the element to the rndmnumbs list using the append() function.
  • Print the rndmnumbs.
  • The Exit of the Program.

Below is the implementation:

# Import the random module using the import keyword.
import random
# Give the number n as user input using int(input()) and store it in a variable.
numbe = int(input('Enter some random number = '))
# Give the lower limit range and upper limit range as user input
# using map(),int(),split(),input() functions.
# Store them in two separate variables.
lowerlimitrange = int(input('Enter random lower limit range = '))
upperlimitrange = int(input('Enter random upper limit range = '))
# Take an empty list (say rndmnumbs) and initialize it with an empty list
# using [] or list().
rndmnumbs = []
# Loop till n times using For loop.
for m in range(numbe):
        # Generate a random number using randint(lowerlimitrange,upperlimitrange)
    # and store it in a variable.
    randomnumbe = random.randint(lowerlimitrange, upperlimitrange)
    # Check whether the above random number is present in the list or not
    # using not in operator.
    if randomnumbe not in rndmnumbs:
        # If it is not in the list then append the element
        # to the rndmnumbs list using the append() function.
        rndmnumbs.append(randomnumbe)

# Print the rndmnumbs
print('The random numbers present in the range from',
      lowerlimitrange, 'to', upperlimitrange, 'are :')
for q in rndmnumbs:
    print(q, end=' ')

Output:

Enter some random number = 19
Enter random lower limit range = 23
Enter random upper limit range = 41
The random numbers present in the range from 23 to 41 are :
26 27 40 38 37 41 30 35 36 23 25

Related Programs:

Random Choice of Random Module in Python with no Repeat Read More »

Secure Passwords Using Python

Secure Passwords Using Python

Creating a strong password is essential in everyone’s life nowadays in order to keep your accounts safe and secure. Passwords that are simple and easy to guess can be readily hacked. To avoid this problem, we’ll show you how to create your own safe and secure password using simple Python code.

Examples:

Example1:

Input:

Original Password  = Italian

Output:

The new modified password =  |t@l1@n

Example2:

Input:

Original Password = albertos

Output:

The new modified password = @lbert0$

Secure Passwords Using Python

Below are the ways to write a program that creates the secured passwords.

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

So, before we begin writing code, let’s first talk about the idea behind it. In this application, we will replace a number of characters with various symbols. $, &, @, 0, 1, |, and so on.

So the main idea is to accept the user’s password as input, replace its characters with symbols, and then publish the output for the new strong-produced password for the user.

Method #1: Using For Loop and Replace Function (Static Input)

Approach:

  • Give the password as static input and store it in a variable.
  • Create a function createSecurePassword() which accepts the given password as an argument and returns the secure password.
  • Pass the given password as an argument to createSecurePassword() function.
  • Take a variable(say secpassword) and create some random characters as tuple ((‘s’, ‘$’), (‘and’, ‘&’), (‘a’, ‘@’), (‘o’, ‘0’), (‘i’, ‘1’), (‘I’, ‘|’)).
  • Loop using two variables in the secpassword using For loop.
  • Replace the password with the two variables.
  • Return the modified Password.
  • Print the New Modified Password.
  • The Exit of the Program.

Below is the implementation:

# Take a variable(say secpassword) and create some random characters as tuple
# (('s', '$'), ('and', '&'), ('a', '@'), ('o', '0'), ('i', '1'), ('I', '|')).
secpassword = (('s', '$'), ('and', '&'),
               ('a', '@'), ('o', '0'), ('i', '1'),
               ('I', '|'))
# Create a function createSecurePassword() which accepts the given password
# as an argument and returns the secure password.


def createSecurePassword(givenpassword):
    # Loop using two variables in the secpassword using For loop.
    for m, n in secpassword:
      # Replace the password with the two variables.
        givenpassword = givenpassword.replace(m, n)
    # Return the modified Password.
    return givenpassword


# Give the password as static input and store it in a variable.
givenpassword = 'Italian'
print('The original Password = ', givenpassword)
# Pass the given password as an argument to createSecurePassword() function.
modifiedpassword = createSecurePassword(givenpassword)
# Print the New Modified Password.

print('The new modified password = ', modifiedpassword)

Output:

The original Password =  Italian
The new modified password =  |t@l1@n

Method #2: Using For Loop and Replace Function (User Input)

Approach:

  • Give the password as user input using the input() function and store it in a variable.
  • Create a function createSecurePassword() which accepts the given password as an argument and returns the secure password.
  • Pass the given password as an argument to createSecurePassword() function.
  • Take a variable(say secpassword) and create some random characters as tuple ((‘s’, ‘$’), (‘and’, ‘&’), (‘a’, ‘@’), (‘o’, ‘0’), (‘i’, ‘1’), (‘I’, ‘|’)).
  • Loop using two variables in the secpassword using For loop.
  • Replace the password with the two variables.
  • Return the modified Password.
  • Print the New Modified Password.
  • The Exit of the Program.

Below is the implementation:

# Take a variable(say secpassword) and create some random characters as tuple
# (('s', '$'), ('and', '&'), ('a', '@'), ('o', '0'), ('i', '1'), ('I', '|')).
secpassword = (('s', '$'), ('and', '&'),
               ('a', '@'), ('o', '0'), ('i', '1'),
               ('I', '|'))
# Create a function createSecurePassword() which accepts the given password
# as an argument and returns the secure password.


def createSecurePassword(givenpassword):
    # Loop using two variables in the secpassword using For loop.
    for m, n in secpassword:
      # Replace the password with the two variables.
        givenpassword = givenpassword.replace(m, n)
    # Return the modified Password.
    return givenpassword


# Give the password as user input using the input() function and store it in a variable.
givenpassword = input('Enter some random password = ')
print('The original Password = ', givenpassword)
# Pass the given password as an argument to createSecurePassword() function.
modifiedpassword = createSecurePassword(givenpassword)
# Print the New Modified Password.

print('The new modified password = ', modifiedpassword)

Output:

Enter some random password = albertos
The original Password = albertos
The new modified password = @lbert0$

Related Programs:

Secure Passwords Using Python Read More »

Degrees() and Radians() in Python

Given a list, the task is to print the Degrees and Radians value of every element in the given list in Python.

In this article, you will learn about Python’s degrees() and radians() methods. The data will be converted to degrees() and radians() in Python by using the math module. Ship position, etc., will be used for mathematical solving.

Degrees:

The degrees are a unit of angle measurement, and all angels utilize them. If you learn that a circle has 360 degrees, you will be able to use it.

The degree symbol will be obtained by using the Chr(176) technique.

Radians:

In trigonometry, it is a unit of measure of angels that will be used instead of degrees, whereas the circle contains only 360 degrees, a full circle is just over 6 radians. A complete circle has two pi radians.

Examples:

Example1:

Input:

Given list = [8, 1, 9, 12, 45, 1, 7, 3]

Output:

The degree value of the element 8  =  458.3662361046586
The radians value of the element 8  =  0.13962634015954636
The degree value of the element 1  =  57.29577951308232
The radians value of the element 1  =  0.017453292519943295
The degree value of the element 9  =  515.662015617741
The radians value of the element 9  =  0.15707963267948966
The degree value of the element 12  =  687.5493541569879
The radians value of the element 12  =  0.20943951023931956
The degree value of the element 45  =  2578.3100780887044
The radians value of the element 45  =  0.7853981633974483
The degree value of the element 1  =  57.29577951308232
The radians value of the element 1  =  0.017453292519943295
The degree value of the element 7  =  401.07045659157626
The radians value of the element 7  =  0.12217304763960307
The degree value of the element 3  =  171.88733853924697
The radians value of the element 3  =  0.05235987755982989

Example2:

Input:

Given list = [1 ,9 ,7 ,8 ,3 ,6 ,4 ,5 ,11]

Output:

The degree value of the element 1 = 57.29577951308232
The radians value of the element 1 = 0.017453292519943295
The degree value of the element 9 = 515.662015617741
The radians value of the element 9 = 0.15707963267948966
The degree value of the element 7 = 401.07045659157626
The radians value of the element 7 = 0.12217304763960307
The degree value of the element 8 = 458.3662361046586
The radians value of the element 8 = 0.13962634015954636
The degree value of the element 3 = 171.88733853924697
The radians value of the element 3 = 0.05235987755982989
The degree value of the element 6 = 343.77467707849394
The radians value of the element 6 = 0.10471975511965978
The degree value of the element 4 = 229.1831180523293
The radians value of the element 4 = 0.06981317007977318
The degree value of the element 5 = 286.4788975654116
The radians value of the element 5 = 0.08726646259971647
The degree value of the element 11 = 630.2535746439055
The radians value of the element 11 = 0.19198621771937624

Degrees() and Radians() in Python

Below are the ways to print the Degrees and Radians value of every element in the given list in Python.

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Method #1: Using For Loop and Built-in Math Functions (Static Input)

Approach:

  • Import the math function using the import function.
  • Give the list as static input and store it in a variable.
  • Traverse the given list using For loop.
  • Calculate the value of the degree of the given iterator value using math.degrees() function.
  • Calculate the value of the radians of the given iterator value using math.radians () function.
  • Print the degree and radians value of the element.
  • The Exit of the Program.

Below is the implementation:

# Import the math function using the import function.
import math
# Give the list as static input and store it in a variable.
givenlist = [8, 1, 9, 12, 45, 1, 7, 3]
# Traverse the given list using For loop.
for elementval in givenlist:
    # Calculate the value of the degree of the given iterator
    # value using math.degrees() function.
    degreevalu = math.degrees(elementval)
    # Calculate the value of the radians of the given iterator value
    # using math.radians () function.
    radianvalu = math.radians(elementval)
    # Print the degree and radians value of the element.
    print('The degree value of the element', elementval, ' = ', degreevalu)
    print('The radians value of the element', elementval, ' = ', radianvalu)

Output:

The degree value of the element 8  =  458.3662361046586
The radians value of the element 8  =  0.13962634015954636
The degree value of the element 1  =  57.29577951308232
The radians value of the element 1  =  0.017453292519943295
The degree value of the element 9  =  515.662015617741
The radians value of the element 9  =  0.15707963267948966
The degree value of the element 12  =  687.5493541569879
The radians value of the element 12  =  0.20943951023931956
The degree value of the element 45  =  2578.3100780887044
The radians value of the element 45  =  0.7853981633974483
The degree value of the element 1  =  57.29577951308232
The radians value of the element 1  =  0.017453292519943295
The degree value of the element 7  =  401.07045659157626
The radians value of the element 7  =  0.12217304763960307
The degree value of the element 3  =  171.88733853924697
The radians value of the element 3  =  0.05235987755982989

Method #2: Using For Loop and Built-in Math Functions (User Input)

Approach:

  • Import the math function using the import function.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Traverse the given list using For loop.
  • Calculate the value of the degree of the given iterator value using math.degrees() function.
  • Calculate the value of the radians of the given iterator value using math.radians () function.
  • Print the degree and radians value of the element.
  • The Exit of the Program.

Below is the implementation:

# Import the math function using the import function.
import math
# Give the list as user input using list(),map(),input(),and split() functions.
# store it in a variable.
givenlist = list(
    map(int, input('Enter some random List Elements separated by spaces = ').split()))
# Traverse the given list using For loop.
for elementval in givenlist:
    # Calculate the value of the degree of the given iterator
    # value using math.degrees() function.
    degreevalu = math.degrees(elementval)
    # Calculate the value of the radians of the given iterator value
    # using math.radians () function.
    radianvalu = math.radians(elementval)
    # Print the degree and radians value of the element.
    print('The degree value of the element', elementval, ' = ', degreevalu)
    print('The radians value of the element', elementval, ' = ', radianvalu)

Output:

Enter some random List Elements separated by spaces = 1 9 7 8 3 6 4 5 11
The degree value of the element 1 = 57.29577951308232
The radians value of the element 1 = 0.017453292519943295
The degree value of the element 9 = 515.662015617741
The radians value of the element 9 = 0.15707963267948966
The degree value of the element 7 = 401.07045659157626
The radians value of the element 7 = 0.12217304763960307
The degree value of the element 8 = 458.3662361046586
The radians value of the element 8 = 0.13962634015954636
The degree value of the element 3 = 171.88733853924697
The radians value of the element 3 = 0.05235987755982989
The degree value of the element 6 = 343.77467707849394
The radians value of the element 6 = 0.10471975511965978
The degree value of the element 4 = 229.1831180523293
The radians value of the element 4 = 0.06981317007977318
The degree value of the element 5 = 286.4788975654116
The radians value of the element 5 = 0.08726646259971647
The degree value of the element 11 = 630.2535746439055
The radians value of the element 11 = 0.19198621771937624

Related Programs:

Degrees() and Radians() in Python Read More »

Python Program to Union of Set of Tuples

Python Program to Union of Set of Tuples

In this tutorial, we will learn how to calculate the union of sets of tuples in Python. Let us begin by defining the union in set theory.
The set of every element in the collection of sets is the set of the union of sets. In the case of duplicate elements in different sets, the final union will only contain the specific element once. The letter ‘U’ represents the union.
This problem is centered on finding the union of sets of tuples, which indicates that the set is made up of tuple elements.

Examples:

Example1:

Input:

first = {('hello', 5), ('this', 100)}
second = {('this', 100), ('is', 200)}
third = {('hello', 5), ('btechgeeks', 461), ('python', 234)}

Output:

first Union Second =  {('is', 200), ('this', 100), ('hello', 5)}
Second Union third =  {('python', 234), ('is', 200), ('this', 100), ('hello', 5), ('btechgeeks', 461)}
first Union Second Union third =  {('python', 234), ('is', 200), ('this', 100), ('hello', 5), ('btechgeeks', 461)}

Program to Union of Set of Tuples in Python

Below are the ways to find the union of the set of tuples in Python.

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Method #1: Using  or( | ) operator

Approach:

  • Give the set of tuples as static input and store them in Separate Variables.
  • In Python, we may retrieve the union of a set of tuples by using the OR operator (|). In order to acquire the union of two variables, use the OR operator directly between them.
  • Calculate the union using | operator and store it in a variable.
  • Print the union.
  • The Exit of the Program.

Below is the implementation:

# Give the set of tuples as static input and store them in Separate Variables.
first = {('hello', 5), ('this', 100)}
second = {('this', 100), ('is', 200)}
third = {('hello', 5), ('btechgeeks', 461), ('python', 234)}

# In Python, we may retrieve the union of a set of tuples
# by using the OR operator (|). In order to acquire the union of two variables,
# use the OR operator directly between them.
# Calculate the union using | operator and store it in a variable.

reslt1 = second | third
reslt2 = first | second
reslt3 = first | second | third
# Print the union of first and second
print("first Union Second = ", reslt2)
# print the union of second and third
print("Second Union third = ", reslt1)
# print the union of first second and third.
print("first Union Second Union third = ", reslt3)

Output:

first Union Second =  {('this', 100), ('is', 200), ('hello', 5)}
Second Union third =  {('this', 100), ('hello', 5), ('btechgeeks', 461), ('python', 234), ('is', 200)}
first Union Second Union third =  {('this', 100), ('hello', 5), ('btechgeeks', 461), ('python', 234), ('is', 200)}

Method #2: Using union() method

Approach:

  • Give the set of tuples as static input and store them in Separate Variables.
  • The set union() method returns the union of the set variables supplied as parameters. The first set uses the dot operator (.) to call the union() method, and the other set variables are given as arguments.
  • Calculate the union using the union() method and store it in a variable.
  • Print the union.
  • The Exit of the Program.

Below is the implementation:

# Give the set of tuples as static input and store them in Separate Variables.
first = {('hello', 5), ('this', 100)}
second = {('this', 100), ('is', 200)}
third = {('hello', 5), ('btechgeeks', 461), ('python', 234)}

'''The set union() method returns the union of the set variables supplied as parameters.
The first set uses the dot operator (.) to call the union() method, and 
the other set variables are given as arguments.
Calculate the union using the union() method and store it in a variable.'''
reslt1 = second.union(third)
reslt2 = first.union(second)
reslt3 = first.union(second, third)
# Print the union of first and second
print("first Union Second = ", reslt2)
# print the union of second and third
print("Second Union third = ", reslt1)
# print the union of first second and third.
print("first Union Second Union third = ", reslt3)

Output:

first Union Second =  {('is', 200), ('this', 100), ('hello', 5)}
Second Union third =  {('python', 234), ('is', 200), ('this', 100), ('hello', 5), ('btechgeeks', 461)}
first Union Second Union third =  {('python', 234), ('is', 200), ('this', 100), ('hello', 5), ('btechgeeks', 461)}

Related Programs:

Python Program to Union of Set of Tuples Read More »

Program to Count Palindrome Words in a Sentence

Python Program to Count Palindrome Words in a Sentence

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Given a sentence/string the task is to count the number of palindromic words in the given string in Python.

Examples:

Example1:

Input:

Given Sentence =madam how are you

Output:

The total number of palindromic words in the given sentence { madam how are you } are 1

Example2:

Input:

Given Sentence = helleh this issi btechgeeksskeeghcetb pyyp

Output:

The total number of palindromic words in the given sentence { helleh this issi btechgeeksskeeghcetb pyyp } are  4

Program to Count Palindrome Words in a Sentence in Python

Below are the ways to Count Palindrome words in the given sentence in Python

Method #1: Using For Loop (Static Input)

Approach:

  • Give the sentence/string as static input and store it in a variable.
  • Take a variable palcnt which stores the count of palindromic words in the given sentence and initialize its value to 0.
  • Split the given sentence into a list of words using the built-in function split() and store it in a variable.
  • Traverse in this list of words using the For loop.
  • Inside the for loop,Reverse the iterator word using slicing and store it in a variable.
  • Check if this reverse word is equal to the iterator word(Palindrome Condition) using the If conditional statement.
  • If it is true then increment the value of palcnt by 1.
  • Print the palcnt value.
  • The Exit of the Program.

Below is the implementation:

# Give the sentence/string as static input and store it in a variable.
gvnsentence ="helleh this issi btechgeeksskeeghcetb pyyp"
# Take a variable palcnt which stores the count of palindromic words
# in the given sentence and initialize its value to 0.
palcnt = 0
# Split the given sentence into a list of words using
# the built-in function split() and store it in a variable.
sentcewords = gvnsentence.split()
# Traverse in this list of words using the For loop.
for itrword in sentcewords:
    # Inside the for loop,Reverse the iterator word
    # using slicing and store it in a variable.
    reveword = itrword[::-1]
    # Check if this reverse word is equal to the
    # iterator word(Palindrome Condition) using the If conditional statement.
    if(reveword == itrword):
        # If it is true then increment the value of palcnt by 1.
        palcnt = palcnt+1


# Print the palcnt value.
print(
    'The total number of palindromic words in the given sentence {', gvnsentence, '} are ', palcnt)

Output:

The total number of palindromic words in the given sentence { helleh this issi btechgeeksskeeghcetb pyyp } are  4

Method #2: Using For Loop (User Input)

Approach:

  • Give the sentence/string as user input using the input() function and store it in a variable.
  • Take a variable palcnt which stores the count of palindromic words in the given sentence and initialize its value to 0.
  • Split the given sentence into a list of words using the built-in function split() and store it in a variable.
  • Traverse in this list of words using the For loop.
  • Inside the for loop,Reverse the iterator word using slicing and store it in a variable.
  • Check if this reverse word is equal to the iterator word(Palindrome Condition) using the If conditional statement.
  • If it is true then increment the value of palcnt by 1.
  • Print the palcnt value.
  • The Exit of the Program.

Below is the implementation:

# Give the sentence/string as user input using the input() function and store it in a variable.
gvnsentence = input('Enter some random sentence =')
# Take a variable palcnt which stores the count of palindromic words
# in the given sentence and initialize its value to 0.
palcnt = 0
# Split the given sentence into a list of words using
# the built-in function split() and store it in a variable.
sentcewords = gvnsentence.split()
# Traverse in this list of words using the For loop.
for itrword in sentcewords:
    # Inside the for loop,Reverse the iterator word
    # using slicing and store it in a variable.
    reveword = itrword[::-1]
    # Check if this reverse word is equal to the
    # iterator word(Palindrome Condition) using the If conditional statement.
    if(reveword == itrword):
        # If it is true then increment the value of palcnt by 1.
        palcnt = palcnt+1


# Print the palcnt value.
print(
    'The total number of palindromic words in the given sentence {', gvnsentence, '} are ', palcnt)

Output:

Enter some random sentence =madam how are you
The total number of palindromic words in the given sentence { madam how are you } are 1

Related Programs:

Python Program to Count Palindrome Words in a Sentence Read More »

Program to Find the Size of a Tuple

Python Program to Find the Size of a Tuple

Given a tuple, the task is to find the size of the given tuple in Python.

Examples:

Example1:

Input:

Given tuple =(9, 11, 24, 19, 11, 23, 29, 23, 31)

Output:

The length of the given tuple (9, 11, 24, 19, 11, 23, 29, 23, 31) is [ 9 ]

Example2:

Input:

Given tuple =(11, 19, 45, 13, 23, 32, 46, 18, 49,58, 53, 75, 11, 19, 93, 99, 85, 68, 24)

Output:

The length of the given tuple (11, 19, 45, 13, 23, 32, 46, 18, 49, 58, 53, 75, 11, 19, 93, 99, 85, 68, 24) is [ 19 ]

Program to Find the Size of a Tuple in Python

Below are the ways to find the size of the given tuple in Python.

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Method #1: Using len() function

Approach:

  • Give the tuple as static input and store it in a variable.
  • Finding the size or length of a tuple is a rather simple task. The number of objects in a tuple is represented by its size. The syntax for determining the size is len (). The number of elements/objects in the tuple is returned by this function.
  • Calculate the len() using len() function and store it in a variable.
  • Print the length of the tuple.
  • The Exit of the Program.

Below is the implementation:

# Give the tuple as static input and store it in a variable.
gvntuple = (9, 11, 24, 19, 11, 23, 29, 23, 31)
# Finding the size or length of a tuple is a rather simple task.
# The number of objects in a tuple is represented by its size.
# The syntax for determining the size is len ().
# The number of elements/objects in the tuple is returned by this function.
# Calculate the len() using len() function and store it in a variable.
tuplelength = len(gvntuple)
# Print the length of the tuple.
print('The length of the given tuple', gvntuple, 'is [', tuplelength, ']')

Output:

The length of the given tuple (9, 11, 24, 19, 11, 23, 29, 23, 31) is [ 9 ]

Method #2: Using For loop

Approach:

  • Give the tuple as static input and store it in a variable.
  • Take a variable say tuplelength that calculates the given tuple length and initializes its value to 0.
  • Loop through the elements of the tuple using For loop.
  • Inside the For loop increment the value of tuplelength by 1.
  • Print the length of the tuple.
  • The Exit of the Program.

Below is the implementation:

# Give the tuple as static input and store it in a variable.
gvntuple = (9, 11, 24, 19, 11, 23, 29, 23, 31)
# Take a variable say tuplelength that calculates the given tuple
# length and initializes its value to 0.
tuplelength = 0
# Loop through the elements of #Inside the For loop increment the value of tuplelength by 1.the tuple using For loop.
for elemen in gvntuple:
    # Inside the For loop increment the value of tuplelength by 1.
    tuplelength = tuplelength+1

# Print the length of the tuple.
print('The length of the given tuple', gvntuple, 'is [', tuplelength, ']')

Output:

The length of the given tuple (9, 11, 24, 19, 11, 23, 29, 23, 31) is [ 9 ]

Related Programs:

Python Program to Find the Size of a Tuple Read More »