04-05-2019, 02:00 PM
Python is one of my favourite languages.
I think they only mentioned int('2') to demonstrate that int() can convert string data to integer data.
No, a string is more like a list of characters. If you multiply a string by a number, you get a string:
An integer is a numeric value that rounds to the nearest whole number. If you multiply an integer by another numeric value, you get a numeric value.
type is a great command in Python. Here are some things you can try:
I think they only mentioned int('2') to demonstrate that int() can convert string data to integer data.
Quote:So its a string that can be added, multiplied, divided etc?
No, a string is more like a list of characters. If you multiply a string by a number, you get a string:
Code:
print("l" + "ol" * 5)
An integer is a numeric value that rounds to the nearest whole number. If you multiply an integer by another numeric value, you get a numeric value.
Code:
print(2.7 * 5)
type is a great command in Python. Here are some things you can try:
Code:
>>> type('5')
>>> type(5.0)
>>> type(int(5))
>>> type(int('5'))
>>> type(str(5))
>>> int('5.0')
>>> float('5.0')
>>> type(float('5.0'))
>>> int(float('5.0'))