In this discussion we’re talking about variables, and the difference between explicit
typed variables and implicit typed variables. So, I think it’s essential to
find out what exactly is a variable? According to our text book, a variable is
used to remember a value for later use. For example; Years is a variable which
I can assign a value such as 10 to.
(Years = 10).
Python has two
type of conversions. explicit type conversion and implicit type conversion.
Explicit conversion, is when the user instructs the program to convert the data
type to another data type, while implicit type conversion is when the interpreter
knows what the conversion should be and does it for you automatically.
Here is an example of explicit type
variable conversion:
num_int = 25
num_str = 15
print(“data
type of num_int:”,
type(num_int))
print(“data
type of num_str:”,
type(num_str))
num_str =
int (num_str)
print(“data
type of num_str after
casting:” , type(num_str))
num_sum =
num_int + num_str
print(“sum
of num_int and num_str:”,num_sum)
print(“data
type of the sum:”, type(num_sum))
here is the output when I run the program:
In this
program,
I add
num_str and num_int variable.
I converted
num_str from string(higher) to integer(lower) type using int() function to
perform the addition.
After
converting num_str to an integer value, Python is able to add these two
variables.
I got the
num_sum value and data type to be an integer
Here is another example of an implicit
data type conversion.
num_int = 25
num_flo = 2.4
print(“datatype
of num_int:”, type(num_int))
print(“datatype
of num_flo:”,type(num_flo))
print(“value
of num_int:” ,num_new)
print(“value
of num_flo:”, type(num_new))
here is the output when I run the
program:
I added two
variables num_int and
num_flo, storing the
value in num_new.
In the
output, I saw the data type of num_int
is an integer while the data type of num_flo is a float.
I also saw the num_new has a float data type.
Overall, I don’t
think there’s any differences in performance when it comes to the use of
implicit declaration vs explicit declaration in large scale applications. In
this case, I think it’s a matter of providing the appropriate instruction to
the program to execute.
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4489055007945767"
crossorigin="anonymous"></script>
Comments
Post a Comment
Comments: