Skip to main content

Type-Casting And Dynamic-Typing

Python is equipped with some of the amazing and easy to use methods which are already built-in keeping the ease of understanding and fun-to-learn.

Some of these amazing properties are going to be discussed today in the blog.


Type-Casting

Typecasting is one of the simple topics of Python which which is very much easy to understand yet is very efficient in the day-to-day lives of Python programmers.

Def: The explicit conversion of an operand to a specific type is called typecasting. Explicit conversion refers to the type of conversion of datatype into which the user wants to change his or her expression into. Note that these types of conversion are user defined.

Typecasting in Python can be explained as follows:
                    <datatype> (expression)
where <datatype> refers to the datatype into which the user wants to type-cast.

Example: 
When we use input() function, Python always takes the input in string. Now, let's say we want to make a calculator, so here we need the input in integers or in our case let it be in floats.

So, our code for the type-casting part will be like:
           a   =   float(input("Enter a number:"))

Dynamic-Typing

Dynamic-Typing refers to super flexibility of changing the values of the variables in Python.

Def: A variable pointing to a value of a certain type, can be made to point to a value/object of different type. This phenomenon is known as dynamic typing.

Dynamic typing in Python can be explained as follows:
                n = "I am Raj."
                print(n)
                n = 16
                print(n)
where the first value of n is a string and it gives out a string when being printed, whereas the second value of n is an integer and it gives out a integer when being printed.

See the output of the given code below:
                I am Raj.
                16

Example:
We may face many instances when we need to swap the values of  the variables in our Python program. We can do so very easily just with the help of dynamic-typing. You can understand what I mean to say in a better way if you look at the example below:

                x = int(input("Enter a number:"))
                y = int(input("Enter another number:"))
                x,y=y,x

©SGPython

Comments

Post a Comment

Popular posts from this blog

Factorial of a Number by a User-Defined Function (PCTS)

 Factorial of a Number There are a number of ways to find the factorial of a number (obviously, it going to a whole number 😆). Out of all of them today, I'm going to show you the user-defined code I created to find the factorial of any whole number. Trust me, it's cool 😎 just give it a try...

For Loops

 The for loop By  polygraphus  on  shutterstock Def: The for loop is also known as the counting loop and is designed to process the items of any sequence, such as a list or a string, one by one. General form of for loop: The general form of the for loop is given below:      for <variable> in <sequence>:          statements to repeat