Do I Have to Set Variable Types?

I have a question about the variable declaration
do I have to write the type of the variable in Python as we do in Java ?
ex: String name , int num and char x?
Thank you.

1 Like

Hi @aljohara_r8,

Python automatically handles variable types. You can even change a variable’s type, like this:

num_eggs = 1 # int
num_eggs = "one" # string, same variable

Python variables behave like a variable in Java with the type Object

That is, any variable can refer to any value regardless of type, and to figure out which type something has you would inspect the value, not the variable

Python doesn’t care nearly as much about types as Java does. Python does what the code says. If something isn’t possible, it’ll fail and an exception is raised.


I know this is not the right place to see post this but I do not know how to open a thread and neither the bug report allows you to share a screenshot; which makes it quite difficult to explain.

Is this a bug? I know that \ makes the value next to it not convert it special. I’m just typing the current date…
Also I was wondering if the error is because having introduced numbers in a string. I’ve done a very quick search and haven’t encountered anything. I suppose I will have to Google better next time…

Which lesson is this?

Not an error to use a string. It makes / printable characters otherwise Python would evaluate it as division.

Is it possible this is a lesson on the datetime class?

from datetime import datetime

now =
todays_date = f"{now.month}/{}/{now.year}"

No. This is the lesson from the Python 2 lecture 5: Variables, from the first module.

Edit: I’m so dumbbbbb. I have to use print() that code is for only saying that the variable is x and therefore the interpreter do not compile :laughing::sweat: :man_facepalming:

1 Like