Thursday, March 21, 2024

Explicit and Implicit Typed Variables

 Hey Everyone,

Describe the difference between explicit typed variables and implicit typed variables using examples.

Implicit typed variables, or ‘automatic type conversion’ is when Python automatically converts one data type to another without needing any intervention. Implicit typed variables happen when operations are performed on variables and Python needs to determine the appropriate data type for the result (Praneel, 2023).

Example:

Implicit type cast from integer (int) to float

x = 42

y = 5.0

n = x + y

 

print(n)

 

Output: 47.0

In this example, Python automatically converted the ‘x’ integer into a float, making ‘x’ = 42.0 in order to perform the addition operation. This automatic conversion was done without intervention from the programmer, making it an implicit typed variable.

Implicit typed variables are often easier to work with as programmer’s can focus solely on the intent of their code, rather than the full execution. However, there are limitations to automatic type conversions as the types have to be compatible. Two types that are not compatible for implicit conversions are string types to integer types.

Explicit typed variables, or ‘type casting’ requires the programmer to clearly state the desired data type (Praneel, 2023), often using the built-in functions that Python provides. Type casting, unlike automatic type conversions can convert string types to integer types.

Example:

current_year = 2024

 

user_birth_year_input = input(“what year were you born?”)

user_birth_year = int(user_birth_year_input)

 

user_age = current_year – user_birth_year

 

print(user_age)

 

output: based on the user’s input for the year they were born, an age will be outputted as an integer.

Since all user input obtained using input() is basically a string, a programmer must explicitly convert the input to a numeric type (Miller et al., 2015). By explicitly changing the user’s birth year input into an integer and storing the data into the variable ‘user_birth_year’, Python is able to subtract the user’s birth year from the current year and thus converting a string type into an integer type (Lemonaki, 2022). Without including the int function, Python could not subtract the user’s birth year from the current year because the user’s input would still have a string type.

In large scale applications, explain whether or not explicit declaration has a better impact over implicit declaration.

When determining whether implicit or explicit declaration of variable types would have a better impact for large scale applications, it is important to note that both declarations have their strong suits. Implicit declarations can allow for the programmer to focus on the intent of their code, rather than the full execution of the code which can help programmer’s to write their code more quickly and get their thoughts out while they have them. On the other hand, implicit declaration could feel like a rough draft for large scale applications. Explicit decelerations can help other programmer’s working on the same code or picking up the code after someone else to be able to clearly read each line and understand the type of data being stored. For large scale applications, I imagine that more than one programmer will be working together, which would require clarity and detailed instructions. Due to the needs that large scale applications would require, I think that explicit declarations could help to build a more cohesive, easy to follow, and clearly detailed code in the long run.

References:

Lemonaki, D. (2022, September 15). Python Convert string to Int – How to cast a string in Python. freeCodeCamp.org. https://www.freecodecamp.org/news/python-convert-string-to-int-how-to-cast-a-string-in-python/Links to an external site. 

Miller, B., Vahid, F., & Lysecky, R. (2015). Programming in Python 3. zyBooks.

Praneel, S. (2023, August 28). Understanding implicit and explicit type casting in Python. Mediumhttps://spraneel.medium.com/understanding-implicit-and-explicit-type-casting-in-python-93b248a0125dLinks to an external site. 

No comments:

Post a Comment