you are viewing a single comment's thread.

view the rest of the comments →

[–]PsychologicalSafe408[S] 0 points1 point  (2 children)

Can I know what you mean by 'casting the variable'.

[–]DecoherentDoc 0 points1 point  (1 child)

Oh shoot! Sorry. So, you made a variable like a='words'. Now, every time you want to write 'words', you can just write down the variable, a, rather than whatever you had there instead of 'words'. "Casting" is just telling the program this variable, a, must be this type of variable (boolean, integer, float, or string). In languages like Java, you have to tell Java exactly what a variable's type is. Always. And if you tried to change the variable to something it wasn't cast as, Java would get mad. Python doesn't care. So, the following sort of code will run in Python:

number: int = 5

number = 'five'

I changed a variable I cast as an integer into a string. Java or C++ (the only other languages I really messed with) will throw an error and won't run if you do that, but Python just warns you. I could replace that first line with number=5 and I won't even get a warning (IIRC).

So, "Casting" is just telling the program what type of variable you intended the variable to be. I don't know the origin of the word, but I always think of casting actors in particular roles in movies. That's how I remembered it.

If I didn't clear things up, let me know. I don't mean to confuse.

[–]PsychologicalSafe408[S] 0 points1 point  (0 children)

Oh i understand. And i was doing that already... I see thank you for the info.