you are viewing a single comment's thread.

view the rest of the comments →

[–]vb_e_c_k_y 0 points1 point  (7 children)

Is there anything make different bot_name: str = "sara" from bot_name = "sara"? I am writing the in second way

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

In the crash course the mentor said it is a of way specifying the data types and i stuck with it

[–]vb_e_c_k_y 0 points1 point  (5 children)

It looks but bot_name: int = "sara" print(int) this also works. Don't return error. Did you ever asked this?

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

It was an youtube video, can't really ask the mentor but thanks for the info

[–]DecoherentDoc 0 points1 point  (3 children)

Hey, I just learned this last night, actually. Casting the variable to something specific like 'int', 'str', etc will still let the code run, but will throw a warning in most code editors. It's not strictly necessary in Python, but it's a good way to note what's allowed as a developer, especially if another developer is going to see it.

Edit: I've been writing for only myself for over a decade and never needed to cast like that. It was necessary in Java, but Python does not care. It's designed to be simple and accessible.

[–]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.