0% found this document useful (0 votes)
4 views

5. type casting.ipynb - Colab

The document discusses type casting in programming, explaining how to convert between different data types such as int, float, and str. It covers both implicit and explicit type casting, providing examples of each, as well as the behavior of boolean values. Additionally, it highlights common errors that occur when attempting to convert incompatible types.

Uploaded by

noneed86590
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

5. type casting.ipynb - Colab

The document discusses type casting in programming, explaining how to convert between different data types such as int, float, and str. It covers both implicit and explicit type casting, providing examples of each, as well as the behavior of boolean values. Additionally, it highlights common errors that occur when attempting to convert incompatible types.

Uploaded by

noneed86590
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

2/5/25, 11:35 AM 5. type casting.

ipynb - Colab

keyboard_arrow_down Topic : Type casting


a = 5
type(a)
int

add Code add Text

b = 5.5
type(b)
float

c = "hello"
type(c)
str

"2" + 5 adding str to int is not allowed


Cell In[4], line 1
"2" + 5 adding str to int is not allowed
^
SyntaxError: invalid syntax

# type casting/type conversion>> the process of changing the data ty


# why? while executing/computing using operatoers, there can be mism

a = "2"
print(type(int(a)))
<class 'int'>

a = 3.5
print(int(a))
3

# int to float
a = 3
print(float(a))
3.0
3

# flaot to int
a = 3.5
print(int(a))
3

# str to int
a = "2"
print(int(a))
2

https://colab.research.google.com/drive/1Ez88HKQWJHAY0WN3ByvZmXnxXImMfSOe?authuser=2 1/3
2/5/25, 11:35 AM 5. type casting.ipynb - Colab

# str to flaot

a = "3.5"
print(float(a))
3.5

# character cannot convert into numbers


a = "Mohit"
type(a)
int(a)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[22], line 4
2 a = "Mohit"
3 type(a)
----> 4 int(a)

ValueError: invalid literal for int() with base 10: 'Mohit'

# int to str
a = 2
type(a)
str(a)
'2'

# float to str
a = "3.5"
print(type(str(a)))
<class 'str'>

# typecasting >> implicit and explicit

# implicit typecasting >> implicit conversion is handled automatical


a = 5
b = 3.5
print(a+b)
print(type(a+b))
8.5
<class 'float'>

# explicit typecasting>> Explicit type conversion in programming is


a = "5"
b = 10
print(int(a) + b)
print(type(int(a) + b))
15
<class 'int'>

https://colab.research.google.com/drive/1Ez88HKQWJHAY0WN3ByvZmXnxXImMfSOe?authuser=2 2/3
2/5/25, 11:35 AM 5. type casting.ipynb - Colab

a = "Mohit "
b = "Kumar"
a + b # addition of str to str is allowed
'Mohit Kumar'

# bool
bool(0)
False

bool(1)
True

bool("Hello")
True

bool(100) # put any value other than 0 be always true


True

bool(-1)
True

bool("hello") # non empty str


False

bool("") # empty str will be false.


False

https://colab.research.google.com/drive/1Ez88HKQWJHAY0WN3ByvZmXnxXImMfSOe?authuser=2 3/3

You might also like