π₯ Mastering Type Casting & User Input
Hey everyone! Today in my Python journey, I explored something super important β type casting and user input. At first, I tried adding a float and a string, like 3.4 + '2.1', and boom β Python threw an error. Thatβs when I learned Python is very strict about data types.
So I discovered explicit type casting, where we manually convert data types. For example, using float() to convert a string into a float, or int() to turn a numeric string into an integer. Once both values are in the same type, operations work perfectly. I also practiced converting numbers back into strings using str() for concatenation.
Then came an interesting part β float to int conversion. I noticed Python doesnβt round; it simply cuts off the decimal part. That was a key learning moment. I also learned about implicit type casting, where Python automatically converts types, like when adding an int and a float. Even booleans surprised me β True behaves like 1 and False like 0 in calculations!
Next, I worked with user input and discovered that input() always returns a string. So whenever we need numerical operations, we must convert the input using int() or float(). Finally, I built a small average calculator and used round() to format the output to two decimal places.
Overall, todayβs lesson made me much more confident about handling data types in Python. Small concept β but super powerful for writing clean, error-free code. π
#python #Day1 #DataScience #bhartiinsan
Top comments (0)