Python - Change data types | Type Conversion - Passionate Geekz

Breaking

Where you can unleash your inner geek.

Friday, 5 July 2019

Python - Change data types | Type Conversion

Python defines type conversion functions to directly convert one data type to another which is useful in day to day and competitive programming. This article is aimed at providing the information about certain conversion functions.
  • int(a,base) : This function converts any data type to integer. ‘Base’ specifies the base in which string is if data type is string.
  • float() : This function is used to convert any data type to a floating point number

Note:

[highlight bgcolor=”#000000″ txtcolor=”#eeee22″]

str + int→ Error

str * str→ Error

str + str→ str

str * int→ strstrstr… int multiple

[/highlight][highlight bgcolor=”#000000″ txtcolor=”#eeee22″][/highlight]

Example

print("5" + "5")
print((int("5") + int("5")) * 10)
print(int("5") + int("5") * 10)
print(int("5" + "5") * 10 )
print(("5" + "5") * 10 )

output :

55
100
55
550
55555555555555555555

 

No comments:

Post a Comment