Skip to content

Typecasting in Python

Python – Typecasting

As we read Python Data Type earlier, Now we will study about Typecasting in Python. Type cast is also an important role in Python programming and it has some important things like python cast to int, python cast to string, cast to int python, cast to string python, python cast to float, int to string python etc. you can do type casting easily with the help of these type conversion in python. Lets start…!!

Typecasting in python

Typecasting is a technique by which we can convert a data type into another data type.


General syntax for Typecasting :

type (expression)

“type” replace with any known Data type.

Example : Cast to Integers

x = int(5)           # x will be 5
y = int(2.5)         # x will be 2
z = int("8")         # x will be 8

Example :Cast to Floats

x = float(5)           # x will be 5
y = float(2.5)         # x will be 2
z = float("8")         # x will be 8
w = float("5.2")       # x will be 5.2

Example :Cast to Strings

x = str("51")      # x will be 51
y = str(2)         # x will be 2
z = str(3.0)       # x will be 3.0

What is error in Python ?

Errors are the faults or mistakes in the program arises unexpectedly that cause a computer to not work properly. There are some errors in Computer Programming Language.

What are the Types of Errors in Python ?

There are the three main errors in Computer Programming Languages are :

⇒ Logical Errors

⇒ Syntax Errors

⇒ Semantic Errors


⇒ Logical Errors : Logical errors are the strongest of all error types to detect. It doesn’t cause the program to crash or not work properly at all, they cause it to “misbehave” in some way, providing wrong output of some kind.

Logical errors “make sense” as to the computer programming language itself, but they simply don’t fit into the program correctly.

⇒ Syntax Errors :Syntax errors are the errors that is found in the source code of a programs. To differ from logical errors in that they don’t follow a correct sequence in the computer programming language.

⇒ Semantic Errors : Semantic errors are the improper uses of “program statements”. Though different definitions of semantic error exist, we are also saying that the logical errors produce wrong data while the semantic errors produce nothing meaningful at all.


Different between three types of errors ?

Logical ErrorSyntax ErrorSemantic Error
Logic errors have to due with program flow. If you get a wrong operation or a misordered operation, then it’s probably a logic error. Use wrong conditional operator or null reference errors are the good examples of logical error.Syntax errors are the errors like “spelling mistakes and grammatical mistakes” issues. They often stem from types where parentheses characters or single characters are input incorrectly.Semantic errors have to do with meaning/context. It’s like using the wrong word in the wrong place in a human language sentence.