Why is type casting required?

Addition of two number

>>> num1 = input("Enter the num1\t")

Enter the num1 11

>>> num2 = input("Enter the num2\t")

Enter the num2 22

>>> num3 = num1 + num2

>>> print(num3)

1122 #If you check here it performed concatenation operation not addition.


The process of converting the one data type to another data type is called  type casting.


There are two types of type casting

  1. Implicit Type Casting

  2. Explicit Type Casting


1. Implicit Type Casting
 Python interpreter does by itself without any user interaction is called implicit Type Casting.

>>> 10 + 10
20
>>> 10 + 10.0
20.0 #to avoid loss of data.



2. Explicit Type Casting:
It is done by the user.

- Function used to explicit conversion. 

  1. int()

  2. float()

  3. bool()

  4. complex()

  5. str()

  6. list()

  7. tuple()

  8. set()

  9. dict()


Example: Now,We will do the above addition program using explicit conversion.

>>> num1 = input("Enter the num1\t")

Enter the num1 11

>>> num2 = input("Enter the num2\t")

Enter the num2 22

>>> num3 = int(num1) + int(num2)

>>> print(num3)

33


>>>int(‘100’)

100
>>>float(‘100’)

100.0
>>>complex(‘100’)

(100+0j)

>>>bool(‘100’)

True


Bool return ‘False’ only in five condition
bool(),bool(‘’),bool(None),bool(False),bool(0)


Operation

>>>tuple(‘100’) #'int' object is not iterable when we try with 100.

(‘1’, ‘0’, ‘0’)

>>>list(‘100’)

[‘1’, ‘0’, ‘0’]

>>>set(‘100’) #unordered And Not allow duplicates

{‘1’, ‘0’}

>>>dict(‘100’)

>>>dict({"10"})

{'1': '0'}

>>> dict({"10","20"})

{'2': '0', '1': '0'}

>>>int(100.0)

100
>>>int(100+0j)
#can't convert complex to int


Why float to int possible but not from complex?

  This happens because complex numbers are a combination of real numbers and imaginary parts.
List To Set
>>> s = [1,2,3,4,5,2,3]

>>> set(s)

{1, 2, 3, 4, 5}


Set To List

>>> a = {1, 2, 3, 4, 5}

>>> list(a)

[1, 2, 3, 4, 5]


Python Namespace and Scope

We previously saw variables and identifiers. We give the normal name. This normal name we given to object. Let see example

>>> 200 #object

>>> id(200)

4301853568

>>> a = 200 #a is normal variable 

>>> id(a)

4301853568

>>> b = 100

>>> id(b)

4301850368

 

A namespace is a system to have a unique name for each and every object in Python. An object might be a variable or a method.


 Python interpreter understands what exact method or variable one is trying to point to in the code, depending upon the namespace. So, the division of the word itself gives little more information. Its Name (which means name, an unique identifier) + Space(which talks something related to scope). Here, a name might be of any Python method or variable and space depends upon the location from where is trying to access a variable or a method.


*Types of namespaces:

  1. Builtin namespaces (id,print) dir('bultins')

  2. Global namespace

  3. Local Namespace


# var1 is in the global namespace 

var1 = 5

def some_func(): 

# var2 is in the local namespace 

var2 = 6


Categories: Python Tags: #Python,

Comments