Practical Implementation of Primitive Datatype in one of the Python IDEs, Jupyter Notebook #Part-2

kanak Gupta Mar 25 2021 · 1 min read
Share this

String possesses immutable quality. Immutable means we cannot change the original value of an object by using the index. Have a look at the given example

Input

string[0]='l'

Output

---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

<ipython-input-33-b92d8add3c1a> in <module>

----> 1 string[0]='l'

TypeError: 'str' object does not support item assignment

  Python provides so many in-built methods for string class. Have a look below by just using the object.tab key and find many methods.  

Python also provides docstring we can use it by simply pressing  shift+tab key 

We can perform all arithmetic operations on the same data types as earlier discussed but what if in the case of adding a string with an integer, is it possible ?? Let's see by the example and try to understand

Input

print(10 + 'rs')

# Convert integer into string using single quotes

print('add string with integer =','10'+'rs')

# Another way to convert int into the string using the keyword 'str'

print('2nd way to write',str(10)+'rs')

# float to integer also vice-versa using keywords 'int','float'

a = 15

print('convert integer to float-',float(a))

b = 18.5

print('convert float to integer-',int(b))

'''Original values of a and b didn't change after the conversion

so we can say integers,floats is also immutable data types'''

print("Get values of a and b",a,b)

Output

---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

<ipython-input-42-dcf139eb9f64> in <module>

----> 1 10 + 'rs'

TypeError: unsupported operand type(s) for +: 'int' and 'str'

add string with integer = 10rs

2nd way to write 10rs

convert integer to float- 15.0

convert float to integer- 18

15 18.5

We cannot convert a string into a numeric form but the reverse can be possible.

From the above example, we can say Yes, we can add different datatypes by converting one into another and this conversion is known as Type-casting in python.

Thanking you, 

Part-3 related to non-primitive datatypes will be uploaded very soon.

Comments
Read next