This is the 18th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

int()function

The method of converting data to an integer type is also simple: the int() function. It is used the same way as STR (), just putting what you want to convert in parentheses, like this: int(what to convert).

So let’s try it out, figure out the sum of the two variables number1 and number2. Int () function to convert data type; 2. Operator + calculation; 3. Print () function

number1 = '618'

number2 = '1111'

print(int(number1) + int(number2))
Copy the code

Int () casts only string data that meets the integer specification. Int () casts only string data that meets the integer specification.

First, integer strings such as ‘618’ and ‘1111’ can be cast by int().

Finally, decimal strings cannot be cast using the int() function due to Python syntax rules.

ValueError: invalid literal for int() with base 10: ‘6.18’)

print(int('6.18'))


ValueError: invalid literal for int() with base 10: '6.18'
Copy the code

Does this mean that floating-point numbers cannot be converted to integers?

No, you can’t use the int() function, though, for floating-point strings. But floating-point numbers can be cast by int().

print(int(6.18))
Copy the code

The above code string is a statement that casts the floating point number 6.18. But why is the output 6?

You see, the int() function essentially converts data to integers. So for floating-point numbers, int() does the rounding. However, unlike our usual rounding of decimals, the int() function erases the zeros and prints the integer part.

Finally, let’s summarize the int() function.

However, in this case, the string is a decimal, such as ‘3.8’. I don’t want it to be an integer 3, I want it to be a decimal 3.8, so what do I do?

This brings us to the last point in the class, the float() function.

float()function

The first use of the float() function is to put the data to be converted in parentheses, like this: float(data).

Second, the float() function also converts integers and strings to floating-point types. But at the same time, if the data inside the parentheses is a string, it must be a number.

So, after the previous STR () and int() exercises, is the float() function a little clearer?

Now, let’s do some code to practice.

width = 40.0
length = 120.0
weight = "20"
print(width)
print(length)
print(float(weight))
Copy the code

In the previous problem,widhtwithlenghtThe variable is already floating point,weightIs a stringType (variable)To see the data type), so the pre-print pair willweightforfloat(weight)Convert. By now, you know how to convert data types. Now, let’s summarize these three functions using a diagram.

How’s that? Having mastered data types, data splicing and data conversion, you have the sense of accomplishment to become a leader?

So let’s take a look at what you’ve learned.

But that’s just the beginning, as you’ll learn more complex code and commands and learn more about how to communicate with computers.