Among the most popular of these are methods for quickly finding the square root, modulus, and rounding a number. A list of such operations is shown in the following table, where you can find the purpose of each of them: The operationĪlso, in order to process numerical data more efficiently, special methods have been added to Python that allow many complex operations to be performed in just one action. In addition to the arithmetic operations listed above, you can perform bit operations on numbers, which involve their binary representation. To familiarize yourself with the basic types of operations that are performed on numeric values, see the following small table: Operation Thus, you can add, subtract, multiply, divide, find the remainder of a division, and raise numeric values of any variety. Manipulations on numeric values in the Python programming language are performed thanks to a variety of different operations, whose symbolic designations tend to coincide with their counterparts in traditional mathematics. X = hex(4152) # number converted to hexadecimal The following code snippet demonstrates how all six of the above methods work: i = int(67.23) # a real number is truncated to an integerį = float('1304') # the string becomes a real numberĬ = complex(2, 6) # complex number is formed The last three functions let you convert a number or variable to decimal, octal, or hexadecimal respectively. To do this, there are methods such as int, float, complex, bin, oct, and hex. Using the standard Python language tools, you can quite easily change the type identity of almost any variable. X = 0x1FE9 # number in hexadecimal system The program below creates and displays data in various systems: b = 0b110101 # number in binary system To start working with these numbers you must initialize the variables with constants that have the prefixes 0b, 0o and 0x for the type you choose. In addition to the decimal number system, Python supports binary, octal and hexadecimal conversions. To do this, use real and image: c = (1+2j) It is possible to get the real and imaginary parts of numbers separately. The following code fragment demonstrates the creation of the complex variable c and displays all information about it: c = 1 + 2j # complex number Also, at the end of the imaginary part of a complex number there must be the letter j. Such information is represented as a pair of values: the real and imaginary parts, which are separated by the symbol of the addition operation. The Python programming language also supports working with complex numbers. This example shows the creation of the variable f: f = 3.14 # real number To initialize an object that contains a real number, you must assign it a value, with integer and fractional parts separated by a dot character. By working with such values in a program, you can get more accurate data in arithmetic calculations. In order to write a real number in Python, there are floating point numbers. Below is an example of an integer assignment in a program that creates an integer variable named i and then displays its value and type: i = 42 # integer To represent it, numeric values are used, which are usually assigned to specific variables. The easiest and most common type of number in Python, as in many other programming tools, is integer data type.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |