0
Using Python int, bool, str, and float properly
okay so ive been trying to figure out ways to use each syntax for just in case or so I know when its important to use it. but anyway the question is when do I use the functions int, float, str, and bool? I saw it used in the exercises but I didnt really understand.. It would also help if I had an example or a practical
1 Resposta
+ 10
Those functions return certain data types, which you may want to carry out operations on.
For example, you ask for user input, let's say his age. The input() method returns a string, which is the most capable data type, it can store any character. However, since you want to deal with a number, not a string, you have to convert it to int or float. Then you can use those functions.
It can be the other way round, too. For example, if you have a number and want to sort its digits in an order, you can convert it to string, use its sorted() method and convert the result back to integer.
Check out my code on Kaprekar numbers to see this in action:
https://code.sololearn.com/cv426gaf4R5E/?ref=app