18 Python Basic Data Types
- Numbers
- integers
- floating point numbers
- complex numbers
- strings
- boolean
18.1 Find variable class name or type
use type function.
6]: a = 5
In [
7]: type(a)
In [7]: int Out[
18.2 Converting/Casting between types
use object functions to change to different types.
int
float
bool
str
complex
18.3 + operator works with same types
8]: print("hello" + " world")
In [
hello world
9]: print(10+1)
In [11
10]: print(10 + "8")
In [---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-38035139b14a> in <module>
----> 1 print(10 + "8")
TypeError: unsupported operand type(s) for +: 'int' and 'str'
11]: print("hello" + 10)
In [---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-9d2092f62421> in <module>
----> 1 print("hello" + 10)
TypeError: can only concatenate str (not "int") to str
How to solve this problem? Change their types using object changing functions.
12]: print(10 + int("8"))
In [18
13]: print("hello" + str(10))
In [ hello10