Author name: Vikram Chiluka

Python sympy.gcd() Method

Python SymPy Module:

SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.

Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.

SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.

Installation:

pip install sympy

Python sympy.gcd() Method:

We find the greatest common divisor of two numbers that are passed as arguments to the sympy.gcd() method.

Syntax:

  sympy.gcd(num1, num2)

Return Value:

The greatest common divisor value is returned by the gcd() function.

sympy.gcd() Method in Python

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

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Pass the above given two numbers as arguments to the gcd() function to get the greatest common divisor(gcd) value for the given two numbers.
  • Store it in another variable.
  • Print the GCD of the given two numbers.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Give the first number as static input and store it in a variable.
gvn_num1 = 20
# Give the second number as static input and store it in another variable.
gvn_num2 = 3
# Pass the above given two numbers as arguments to the gcd() function to get the 
# greatest common divisor(gcd) value for the given two numbers.
# Store it in another variable.
rslt = gcd(gvn_num1, gvn_num2)

# Print the GCD of the given two numbers
print("The GCD of the given two numbers {",gvn_num1,",",gvn_num2,"} = ", rslt)

Output:

The GCD of the given two numbers { 20 , 3 } = 1

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

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the first number as user input using the int(input()) function and store it in a variable.
  • Give the second number as user input using the int(input()) function and store it in another variable.
  • Pass the above given two numbers as arguments to the gcd() function to get the greatest common divisor(gcd) value for the given two numbers.
  • Store it in another variable.
  • Print the GCD of the given two numbers.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Give the first number as user input using the int(input()) function
# and store it in a variable.
gvn_num1 = int(input("Enter some random number = "))
# Give the second number as user input using the int(input()) function
# and store it in another variable.
gvn_num2 = int(input("Enter some random number = "))
# Pass the above given two numbers as arguments to the gcd() function to get the 
# greatest common divisor(gcd) value for the given two numbers.
# Store it in another variable.
rslt = gcd(gvn_num1, gvn_num2)

# Print the GCD of the given two numbers
print("The GCD of the given two numbers {",gvn_num1,",",gvn_num2,"} = ", rslt)

Output:

Enter some random number = 60
Enter some random number = 4
The GCD of the given two numbers { 60 , 4 } = 4

Python sympy.gcd() Method Read More »

Python sympy.Rational() Method

Python SymPy Module:

SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.

Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.

SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.

Installation:

pip install sympy

Python sympy.Rational() Method:

We can obtain the rational form of any float value passed as an argument to Rational() function of the sympy module.

Syntax:

 sympy.Rational(value)

Return Value:

The rational form of the given float value is returned by the Rational() function.

sympy.Rational() Method in Python

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

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the number(as float) as static input and store it in a variable.
  • Pass the above given float number as an argument to the Rational() function to get the Rational value for the given float number.
  • Store it in another variable.
  • Print the Rational value for the given float number.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Give the number(as float) as static input and store it in a variable.
gvn_num = 0.5

# Pass the above given float number as an argument to the Rational() function to get the 
# Rational value for the given float number.
# Store it in another variable.
rslt = Rational(gvn_num)

# Print the Rational value for the given float number.
print("The Rational value for the given float number{",gvn_num,"} = ", rslt)

Output:

The Rational value for the given float number{ 0.5 } = 1/2

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

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the number(as float) as user input using the input() function and store it in a variable.
  • Pass the above given float number as an argument to the Rational() function to get the Rational value for the given float number.
  • Store it in another variable.
  • Print the Rational value for the given float number.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Give the number(as float) as user input using the input() function 
# and store it in a variable.
gvn_num = input("Enter some random float number = ")

# Pass the above given float number as an argument to the Rational() function to get the 
# Rational value for the given float number.
# Store it in another variable.
rslt = Rational(gvn_num)

# Print the Rational value for the given float number.
print("The Rational value for the given float number{",gvn_num,"} = ", rslt)

Output:

Enter some random float number = 1.2
The Rational value for the given float number{ 1.2 } = 6/5

Python sympy.Rational() Method Read More »

Python sympy.lcm() Method

Python SymPy Module:

SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.

Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.

SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.

Installation:

pip install sympy

Python sympy.lcm() Method:

We find the least common multiple(lcm) of two numbers that are passed as arguments to the sympy.lcm() method.

Syntax:

  sympy.lcm(num1, num2)

Return Value:

The least common multiple value is returned by the lcm() function.

sympy.lcm() Method in Python

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

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Pass the above given two numbers as arguments to the lcm() function to get the least common multiple(lcm) value for the given two numbers.
  • Store it in another variable.
  • Print the LCM of the given two numbers.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Give the first number as static input and store it in a variable.
gvn_num1 = 10
# Give the second number as static input and store it in another variable.
gvn_num2 = 6
# Pass the above given two numbers as arguments to the lcm() function to get the 
# least common multiple(lcm) value for the given two numbers.
# Store it in another variable.
rslt = lcm(gvn_num1, gvn_num2)

# Print the LCM of the given two numbers
print("The LCM of the given two numbers {",gvn_num1,",",gvn_num2,"} = ", rslt)

Output:

The LCM of the given two numbers { 10 , 6 } = 30

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

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the first number as user input using the int(input()) function and store it in a variable.
  • Give the second number as user input using the int(input()) function and store it in another variable.
  • Pass the above given two numbers as arguments to the lcm() function to get the least common multiple(lcm) value for the given two numbers.
  • Store it in another variable.
  • Print the LCM of the given two numbers.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Give the first number as user input using the int(input()) function
# and store it in a variable.
gvn_num1 = int(input("Enter some random number = "))
# Give the second number as user input using the int(input()) function
# and store it in another variable.
gvn_num2 = int(input("Enter some random number = "))

# Pass the above given two numbers as arguments to the lcm() function to get the 
# least common multiple(lcm) value for the given two numbers.
# Store it in another variable.
rslt = lcm(gvn_num1, gvn_num2)

# Print the LCM of the given two numbers
print("The LCM of the given two numbers {",gvn_num1,",",gvn_num2,"} = ", rslt)

Output:

Enter some random number = 5
Enter some random number = 7
The LCM of the given two numbers { 5 , 7 } =  35

Python sympy.lcm() Method Read More »

Python sympy.cofactors() Method

Python SymPy Module:

SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.

Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.

SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.

Installation:

pip install sympy

Python sympy.cofactors() Method:

We can use the sympy.cofactors() method to get the cofactors of two numbers that are supplied as parameters to this function.

Syntax:

 sympy.cofactors(num1, num2)

Return Value:

A tuple of cofactors is returned by the cofactors() function.

sympy.cofactors() Method in Python

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

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Pass the above given two numbers as arguments to the cofactors() function to get the cofactors of the given two numbers.
  • Store it in another variable.
  • Print the cofactors of the given two numbers.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Give the first number as static input and store it in a variable.
gvn_num1 = 10
# Give the second number as static input and store it in another variable.
gvn_num2 = 6

# Pass the above given two numbers as arguments to the cofactors() function 
# to get the cofactors of the given two numbers.
# Store it in another variable.
rslt = cofactors(gvn_num1, gvn_num2)

# Print the cofactors of the given two numbers.
print("The cofactors of the given two numbers{",gvn_num1,",",gvn_num2,"} = ", rslt)

Output:

The cofactors of the given two numbers{ 10 , 6 } = (2, 5, 3)

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

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the first number as user input using the int(input()) function and store it in a variable.
  • Give the second number as user input using the int(input()) function and store it in another variable.
  • Pass the above given two numbers as arguments to the cofactors() function to get the cofactors of the given two numbers.
  • Store it in another variable.
  • Print the cofactors of the given two numbers.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Give the first number as user input using the int(input()) function
# and store it in a variable.
gvn_num1 = int(input("Enter some random number = "))
# Give the second number as user input using the int(input()) function
# and store it in another variable.
gvn_num2 = int(input("Enter some random number = "))

# Pass the above given two numbers as arguments to the cofactors() function 
# to get the cofactors of the given two numbers.
# Store it in another variable.
rslt = cofactors(gvn_num1, gvn_num2)

# Print the cofactors of the given two numbers.
print("The cofactors of the given two numbers{",gvn_num1,",",gvn_num2,"} = ", rslt)

Output:

Enter some random number = 15
Enter some random number = 20
The cofactors of the given two numbers{ 15 , 20 } = (5, 3, 4)

Python sympy.cofactors() Method Read More »

Python sympy.sec() Method

Python SymPy Module:

SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.

Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.

SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.

Installation:

pip install sympy

Python sympy.sec() Method:

We can determine the value of secant theta using the sec() method of the sympy module.

Syntax:

  sympy.sec()

Return Value:

The value of secant theta is returned by the sec() function.

sympy.sec() Method in Python

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

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass some random theta value to the sec() function to get the secant value of the given angle theta.
  • Store it in a variable.
  • Print the secant value of the given angle theta.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Pass some random theta value to the sec() function to get the 
# secant value of the given angle theta.
# Store it in a variable.
rslt = sec(pi / 4)
    
# Print the secant value of the given angle theta
print("The secant value of the given angle theta = ", rslt)

Output:

The secant value of the given angle theta = sqrt(2)

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

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the theta value(angle) as user input using input() function and store it in a variable.
  • Pass the above-given theta value as an argument to the sec() function to get the secant value of the given angle theta.
  • Store it in a variable.
  • Print the secant value of the given angle theta.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Give the theta value(angle) as user input using input() function 
# and store it in a variable.
gvn_theta = input("Enter some random theta value = ")

# Pass the above given theta value as an argument to the sec() function to get the 
# secant value of the given angle theta.
# Store it in a variable.
rslt = sec(gvn_theta)
    
# Print the secant value of the given angle theta
print("The secant value of the given angle theta = ", rslt)

Output:

Enter some random theta value = pi/6
The secant value of the given angle theta = 2*sqrt(3)/3

Python sympy.sec() Method Read More »

Python sympy.LessThan() Method

Python SymPy Module:

SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.

Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.

SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.

Installation:

pip install sympy

Python sympy.LessThan() Method:

We can create a mathematical expression using the less-than(<) operator with the help of the sympy.LessThan() method.

Syntax:

  sympy.LessThan(variable_1, variable_2)

Return Value:

A mathematical expression is returned by the LessThan() function.

sympy.LessThan() Method in Python

Example1

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass the symbols to be used as arguments to the symbols() function and store them in corresponding variables.
  • Create a mathematical expression with the less-than(<) operator by using the LessThan() function and store it in a variable.
  • Here it creates the x <= y mathematical expression.
  • Print the result mathematical expression with the less-than(<) operator.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Pass the symbols to be used as arguments to the symbols() function
# and store them in corresponding variables.
x, y = symbols('x y')

# Create a mathematical expression with the less-than(<) operator by using the
# LessThan() function and store it in a variable.
# Here it creates x <= y expression.
rslt_expsn = LessThan(x, y)

# Print the result mathematical expression with the less-than(<) operator.
print(rslt_expsn)

Output:

x <= y

Example2

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass the symbols to be used as arguments to the symbols() function and store them in corresponding variables.
  • Create a mathematical expression with the less-than(<) operator by using the LessThan() function and store it in a variable.
  • Here it creates the y<= 15 mathematical expression.
  • Print the result mathematical expression with the less-than(<) operator.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Pass the symbols to be used as arguments to the symbols() function
# and store them in corresponding variables.
x, y = symbols('x y')

# Create a mathematical expression with the less-than(<) operator by using the
# LessThan() function and store it in a variable.
# Here it creates y <= 15 expression.
rslt_expsn = LessThan(y, 15)

# Print the result mathematical expression with the less-than(<) operator.
print(rslt_expsn)

Output:

y <= 15

Python sympy.LessThan() Method Read More »

Python sympy.GreaterThan() Method

Python SymPy Module:

SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.

Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.

SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.

Installation:

pip install sympy

Python sympy.GreaterThan() Method:

We can create a mathematical expression using the greater than operator with the help of the sympy.GreaterThan() method.

Syntax:

  sympy.GreaterThan(variable_1, variable_2)

Return Value:

A mathematical expression is returned by the GreaterThan() function.

sympy.GreaterThan() Method in Python

Example1

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass the symbols to be used as arguments to the symbols() function and store them in corresponding variables.
  • Create a mathematical expression with the greater than operator by using the GreaterThan() function and store it in a variable.
  • Here it creates the x >= y mathematical expression.
  • Print the result mathematical expression with the greater-than operator.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Pass the symbols to be used as arguments to the symbols() function
# and store them in corresponding variables.
x, y = symbols('x y')

# Create a mathematical expression with the greater than operator by using the
# GreaterThan() function and store it in a variable.
# Here it creates x >= y expression.
rslt_expsn = GreaterThan(x, y)

# Print the result mathematical expression with the greater-than operator.
print(rslt_expsn)

Output:

x >= y

Example2

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass the symbols to be used as arguments to the symbols() function and store them in corresponding variables.
  • Create a mathematical expression with the greater than operator by using the GreaterThan() function and store it in a variable.
  • Here it creates the y>= 20 mathematical expression.
  • Print the result mathematical expression with the greater-than operator.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Pass the symbols to be used as arguments to the symbols() function
# and store them in corresponding variables.
x, y = symbols('x y')

# Create a mathematical expression with the greater than operator by using the
# GreaterThan() function and store it in a variable.
# Here it creates y >= 20 expression.
rslt_expsn = GreaterThan(y, 20)

# Print the result mathematical expression with the greater than operator.
print(rslt_expsn)

Output:

y >= 20

Python sympy.GreaterThan() Method Read More »

Python code.compile_command() Function

Python code Module:

The code module contains functions for implementing read-eval-print loops in Python. There are two classes and two convenience functions available that can be used to build applications that give an interactive interpreter prompt.

The code module includes a variety of functions for emulating the behaviour of the standard interpreter’s interactive mode.

The compile command works in the same way as the built-in compile function, but it does some additional checks(tests) to ensure that you provide it with a complete Python statement.

Python code.compile_command() Function:

We can use the code.compile_command() method to compile a single or numerous lines of code in order to check for syntax errors.

Syntax:

 code.compile_command(code)

Return Value:

An object or compilation error if occurred is returned by the compile_command() function.

code.compile_command() Function in Python

 

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

Approach:

  • Import compile_command function from code module using the import keyword.
  • Give the code as static input and store it in a variable.
  • Pass the above-given code as an argument to the compile_command() function
    to compile the given code.
  • The Exit of the Program.

Below is the implementation:

# Import compile_command function from code module using the import keyword
from code import compile_command

# Give the code as static input and store it in a variable.
code = 'x = 3 y = 4; print(x*y)'

# Pass the above given code as an argument to the compile_command() function
# to compile the given code
compile_command(code)

Output:

 File "<input>", line 1
x = 3 y = 4; print(x*y)
^
SyntaxError: invalid syntax

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

Approach:

  • Import compile_command function from code module using the import keyword.
  • Give the code as user input using the input() function and store it in a variable.
  • Pass the above-given code as an argument to the compile_command() function
    to compile the given code.
  • The Exit of the Program.

Below is the implementation:

# Import compile_command function from code module using the import keyword
from code import compile_command

# Give the code as user input using the input() function and store it in a variable.
code = input("Enter some random code:\n")
print()

# Pass the above given code as an argument to the compile_command() function
# to complie the given code
compile_command(code)

Output:

Enter some random code:
+x=5

File "<input>", line 1
SyntaxError: can't assign to operator

 

 

 

 

Python code.compile_command() Function Read More »

Python linecache.getline() Function

Python linecache Module:

The linecache module allows you to read any line from a Python source file while attempting to optimise internally using a cache, which is useful in the case where numerous lines are read from a single file. The traceback module uses this to retrieve source lines for inclusion in the formatted traceback.

Python linecache.getline() Function:

We can obtain a specific line by providing a line number using the linecache.getline() method. One use case for this method is to locate/find the syntax error in the specified line number within a large file by using the linecache.getline() function.

Syntax:

linecache.getline(file_name, line_number)

Return Value:

The content(data) of the given text file is returned by the getline() fnction.

Let us take the text file samplefile.txt as an example here.

samplefile.txt:

Hello this is Python-programs
Good morning all
Welcome to the Python coding platform
Hurry up!!!!

linecache.getline() Function in Python

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

Approach:

  • Import linecache module using the import keyword
  • Pass some random text file name, line number as arguments to the getline() function of the linecache module to get the content of the given line number.
  • Store it in a variable.
  • Print the content of the given line.
  • The Exit of the Program.

Below is the implementation:

# Import linecache module using the import keyword
import linecache 

# Pass some random text file name, line number as arguments to the 
# getline() function of the linecache module to get the content of the 
# given line number.
# Store it in a variable.
rslt = linecache.getline('samplefile.txt', 2)

# Print the content of the given line.
print("The content of the given line-2 is:")
print(rslt)

Output:

The content of the given line-2 is:
Good morning all

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

Approach:

  • Import linecache module using the import keyword.
  • Give the line number as user input using the int(input()) function and store it in a variable.
  • Pass some random text file name, above given line number as arguments to the getline() function of the linecache module to get the content of the given line number.
  • Store it in a variable.
  • Print the content of the given line.
  • The Exit of the Program.

Below is the implementation:

# Import linecache module using the import keyword
import linecache 

# Give the line number as user input using the int(input()) function 
# and store it in a variable.
gvn_line = int(input("Enter some random number = "))

# Pass some random text file name, above given line number as arguments to the 
# getline() function of the linecache module to get the content of the 
# given line number.
# Store it in a variable.
rslt = linecache.getline('samplefile.txt', gvn_line)

# Print the content of the given line.
print("The content of the given line {",gvn_line,"} is:")
print(rslt)

Output:

Enter some random number = 1
The content of the given line { 1 } is:
Hello this is Python-programs

Python linecache.getline() Function Read More »

Python Numpy matrix.cumsum() Function

NumPy Library 

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it in our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.cumsum() Function: 

Using the matrix.cumsum() function of the NumPy module, we can find the cumulative sum of a given matrix and returns the output as a 1-Dimensional matrix.

Syntax:

 matrix.cumsum()

Return Value:

The cumulative sum of a given matrix is returned by the cumsum() function.

Cumulative sum:

The cumulative sum denotes “how much so far.” The cumulative sum is defined as the sum of a given sequence that grows or increases with more additions.

Numpy matrix.cumsum() Function in Python

For 1-Dimensional (1D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing some random 1D matrix as an argument to it and store it in a variable
  • Apply cumsum() function on the given matrix to get the cumulative sum of a given matrix.
  • Store it in another variable
  • Print the cumulative sum of a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing 
# some random 1D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[1, 4, 2]')
            
# Apply cumsum() function on the given matrix to get the cumulative sum of a given matrix.
# Store it in another variable
rslt = gvn_matrx.cumsum()
# Print the cumulative sum of a given matrix.
print("The cumulative sum of a given matrix:")
print(rslt)

Output:

The cumulative sum of a given matrix:
[[1 5 7]]

Explanation:

Here it prints 1, adds 1+4 = 5, and adds that result to the next element i.e, 5+2=7

For 2-Dimensional (2D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing some random 2D matrix as an argument to it and store it in a variable
  • Apply cumsum() function on the given matrix to get the cumulative sum of a given matrix.
  • Store it in another variable
  • Print the cumulative sum of a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing 
# some random 2D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[1, 2; 4, 5]')
            
# Apply cumsum() function on the given matrix to get the cumulative sum of a given matrix.
# Store it in another variable
rslt = gvn_matrx.cumsum()
# Print the cumulative sum of a given matrix.
print("The cumulative sum of a given matrix:")
print(rslt)

Output:

The cumulative sum of a given matrix:
[[ 1 3 7 12]]

Python Numpy matrix.cumsum() Function Read More »