DEV Community

Nkemchor Duru
Nkemchor Duru

Posted on

PYTHON PROGRAMMING- Lesson 3: Type Casting and Input Function

For the purpose of this lesson we will be looking at certain aspects

  1. Type Checking
  2. Type Casting (implicit and Explicit Type Casting)
  3. Input function *TYPE CHECKING:How to check the type of a data variable * Using the type function we can check whether the given variable data type is an integer, float, string or boolean. e.g x=200 print (type(x))---->Integer y= "adaobi" print(type(y))---> string z= 120.23 print (type(z))----> floating point

TYPE CASTING
We have implicit and Explicit type casting
Implicit type casting (also called type coercion) is when Python automatically converts one data type to another during an operation, without you needing to do anything.
e.g
x=10 ====> Integer
y=20.5 ====> Float
z=x+y =====> conversion
print (type(z))===> float as the final result is 30.5 which is a decimal number.
Explicit Type Casting
In this case a user has to convert from one data type to another.
For example the int() function will convert any data type to an integer.
a=10.4
a=int(a)
print (a)===> if this code is run the output will be 10. Therefore the number has moved from being a decimal point to an integer.
To convert from an integer to a float
a=10
a=float(a)
print (a)==> if this is run the result will be 10.0

In python, it is important to note that "10"+"10" is considered a string not an integer because of the presence of quotation marks. In python + operator between strings means string CONCATENATION so in other words they are joined together "10" + "10"= 1010
In the event that you type into your IDE
a="10"
b="20"
if you go ahead to say c=a+ b you will get "1020" because both numbers are strings so there is a concatenation.
So the right thing to do is
a="10"
b="20"
a=int(a)
b=int(b)
c=a+b
print (c)==> Result is 30 in this case because the string has been converted to the integer by virtue of the int() function

To convert to string
a=20
b=10
a=str(a)
b=str(b)
c=a+b
print (c)==> The result will be "2010"

To convert into a boolean value
a=0
b=1
a=bool(a)
b=bool(b)
print (a)===>false (returns zero as false
Print(b)===> True (returns non zero values as true)
*INPUT FUNCTION *
What does input() do?
It pauses the program and waits for the user to type something.
Whatever the user types is returned as a string.
e.g
a=Input ("Please Enter number 1")
b=Input ("Please Enter number 2")
c=a+b
print (c)
The result is that in the terminal you will be asked to enter the value of number 1 and number 2. If it is 200 and 100 that is entered the result will be 200100 But in the event that
a=int(a)
b=int(b) is inserted into the line of code it will be 300 because 200+100 is 300 as the string had been converted to an integer making it possible for an addition of the two values.
This same code can be written in this shorter form. e.g a=int(input("Please enter Number 1:"))
b=int(input("Please enter number 2:"))

The following was discussed in this tutorial
==>How to check data type using type function
==> Type casting(implicit and explicit)
==> Input Function

The next topic will be on Strings in Python

Top comments (0)