Python object to number

Python Type Conversion

In programming, type conversion is the process of converting data of one type to another. For example: converting int data to str .

There are two types of type conversion in Python.

  • Implicit Conversion — automatic type conversion
  • Explicit Conversion — manual type conversion

Python Implicit Type Conversion

In certain situations, Python automatically converts one data type to another. This is known as implicit type conversion.

Example 1: Converting integer to float

Let’s see an example where Python promotes the conversion of the lower data type (integer) to the higher data type (float) to avoid data loss.

integer_number = 123 float_number = 1.23 new_number = integer_number + float_number # display new value and resulting data type print("Value:",new_number) print("Data Type:",type(new_number))

In the above example, we have created two variables: integer_number and float_number of int and float type respectively.

Читайте также:  Javascript External Script

Then we added these two variables and stored the result in new_number .

As we can see new_number has value 124.23 and is of the float data type.

It is because Python always converts smaller data types to larger data types to avoid the loss of data.

  • We get TypeError , if we try to add str and int . For example, ’12’ + 23 . Python is not able to use Implicit Conversion in such conditions.
  • Python has a solution for these types of situations which is known as Explicit Conversion.

Explicit Type Conversion

In Explicit Type Conversion, users convert the data type of an object to required data type.

We use the built-in functions like int() , float() , str() , etc to perform explicit type conversion.

This type of conversion is also called typecasting because the user casts (changes) the data type of the objects.

Example 2: Addition of string and integer Using Explicit Conversion

num_string = '12' num_integer = 23 print("Data type of num_string before Type Casting:",type(num_string)) # explicit type conversion num_string = int(num_string) print("Data type of num_string after Type Casting:",type(num_string)) num_sum = num_integer + num_string print("Sum:",num_sum) print("Data type of num_sum:",type(num_sum))
Data type of num_string before Type Casting: Data type of num_string after Type Casting: Sum: 35 Data type of num_sum:

In the above example, we have created two variables: num_string and num_integer with str and int type values respectively. Notice the code,

num_string = int(num_string)

Here, we have used int() to perform explicit type conversion of num_string to integer type.

After converting num_string to an integer value, Python is able to add these two variables.

Finally, we got the num_sum value i.e 35 and data type to be int .

Key Points to Remember

  1. Type Conversion is the conversion of an object from one data type to another data type.
  2. Implicit Type Conversion is automatically performed by the Python interpreter.
  3. Python avoids the loss of data in Implicit Type Conversion.
  4. Explicit Type Conversion is also called Type Casting, the data types of objects are converted using predefined functions by the user.
  5. In Type Casting, loss of data may occur as we enforce the object to a specific data type.

Table of Contents

Источник

int() Function in Python

Python Certification Course: Master the essentials

To convert a specified value (any number or string) into an integer object, we use int() in python. The conversion of one data type into the other data type is known as type casting or type conversion.

Syntax of int() in Python

The syntax of the method, int() in Python is very simple:

The value parameter is required, but the base parameter is optional.

Parameters of int() in Python

The method — int() in python takes two parameters, one optional and the other required. The parameters given in int in python are:

  1. value: The value parameter is a required parameter. The value is the number or string to be converted into an integer object.
  2. base: The base parameter is optional. The default value specified is 10. The base-10 value resembles the number having base address 10 , i.e. decimal numbers.

We can specify the base type of the number other than 10 , such as

  • 8 for octal number (to convert octal number into an equivalent decimal number).
  • 16 for hexadecimal number (to convert a hexadecimal number into an equivalent decimal number).
  • 2 for binary number (to convert the binary number into an equivalent decimal number).

Return Values of int() in Python

The int in python returns the integer object value of the given string or floating number. If we do not give any parameter in the method, it will return 0 .

Exception of int() in Python

The int() in python raises an error whenever we pass a data type other than a string or an integer. For example, if we try to typecast a list using int in python. The following error will be printed:

This exception raised by int() in python is known as TypeError. Refer to this article’s topic — More Example to know more about the exceptions.

Example

Let us convert a floating-point number into an integer using the method int() in python.

What is int() in Python?

As we know, converting one data type into the other is known as type casting or type conversion.

We can convert any string into an integer using the built-in python method called int() method. The int() method takes a string or integer data type and converts the given data into an integer number.

what is int in python

How to Use int() in Python?

To convert a specified value (any number or string) into an integer object, we use int in python. The int in python is a built-in method that converts a string or a number into an integer. We can also use int in python to convert binary numbers to decimal numbers, hexadecimal numbers to decimal numbers, and octal numbers into decimal numbers.

The int method raises an error when a non-string or non-integer data type is given as a parameter. The error raised is termed as TypeError.

More Examples

Let us take a few examples to better understand the int method in python.

Example for Exception in int

Let us try to convert a list into an integer using int in python. As we have learned, we can only convert a string or a number into an integer.

How int() works in Python?

Let us try to convert a string into an integer using int in python.

How int() works for binary, octal, and hexadecimal?

Let us try to convert a binary, octal, and hexadecimal number into integer numbers using int in python.

int() for custom objects

Let us try to convert an object called Student into an integer number using int in python.

Internally the int in python calls int() method, which is the method of an object. So we can convert an object into an integer object using int in python.

We only need to override the int() method for a class to return an integer class.

Conclusion

  • To convert a specified value (any number or string) into an integer object, we use int in python.
  • The conversion of one data type into the other is known as type casting or type conversion.
  • The int() in python takes two parameters and returns an equivalent integer number. Its syntax is: int(value, base)
  • The value parameter is required, but the base parameter is optional.
  • Whenever we pass a data type other than a string or an integer, the int in python raises an error. The raised error is known as TypeError.

Learn More:

Источник

int() Function in Python

Python Certification Course: Master the essentials

To convert a specified value (any number or string) into an integer object, we use int() in python. The conversion of one data type into the other data type is known as type casting or type conversion.

Syntax of int() in Python

The syntax of the method, int() in Python is very simple:

The value parameter is required, but the base parameter is optional.

Parameters of int() in Python

The method — int() in python takes two parameters, one optional and the other required. The parameters given in int in python are:

  1. value: The value parameter is a required parameter. The value is the number or string to be converted into an integer object.
  2. base: The base parameter is optional. The default value specified is 10. The base-10 value resembles the number having base address 10 , i.e. decimal numbers.

We can specify the base type of the number other than 10 , such as

  • 8 for octal number (to convert octal number into an equivalent decimal number).
  • 16 for hexadecimal number (to convert a hexadecimal number into an equivalent decimal number).
  • 2 for binary number (to convert the binary number into an equivalent decimal number).

Return Values of int() in Python

The int in python returns the integer object value of the given string or floating number. If we do not give any parameter in the method, it will return 0 .

Exception of int() in Python

The int() in python raises an error whenever we pass a data type other than a string or an integer. For example, if we try to typecast a list using int in python. The following error will be printed:

This exception raised by int() in python is known as TypeError. Refer to this article’s topic — More Example to know more about the exceptions.

Example

Let us convert a floating-point number into an integer using the method int() in python.

What is int() in Python?

As we know, converting one data type into the other is known as type casting or type conversion.

We can convert any string into an integer using the built-in python method called int() method. The int() method takes a string or integer data type and converts the given data into an integer number.

what is int in python

How to Use int() in Python?

To convert a specified value (any number or string) into an integer object, we use int in python. The int in python is a built-in method that converts a string or a number into an integer. We can also use int in python to convert binary numbers to decimal numbers, hexadecimal numbers to decimal numbers, and octal numbers into decimal numbers.

The int method raises an error when a non-string or non-integer data type is given as a parameter. The error raised is termed as TypeError.

More Examples

Let us take a few examples to better understand the int method in python.

Example for Exception in int

Let us try to convert a list into an integer using int in python. As we have learned, we can only convert a string or a number into an integer.

How int() works in Python?

Let us try to convert a string into an integer using int in python.

How int() works for binary, octal, and hexadecimal?

Let us try to convert a binary, octal, and hexadecimal number into integer numbers using int in python.

int() for custom objects

Let us try to convert an object called Student into an integer number using int in python.

Internally the int in python calls int() method, which is the method of an object. So we can convert an object into an integer object using int in python.

We only need to override the int() method for a class to return an integer class.

Conclusion

  • To convert a specified value (any number or string) into an integer object, we use int in python.
  • The conversion of one data type into the other is known as type casting or type conversion.
  • The int() in python takes two parameters and returns an equivalent integer number. Its syntax is: int(value, base)
  • The value parameter is required, but the base parameter is optional.
  • Whenever we pass a data type other than a string or an integer, the int in python raises an error. The raised error is known as TypeError.

Learn More:

Источник

Оцените статью