Frequently Asked Questions

Variables in Python

  • Is True the same as 1 in Python?

    Sort of. Booleans are technically a subtype of integers in Python, so true equals 1 and False equals 0 when used in math. Try print (True + True) and you’ll get 2. Odd, but worth knowing. 

  • How do you delete a variable in Python?

    Use the del statement: del variable_name. After deletion, attempting to access the variable raises a NameError. This is useful for freeing memory or removing a name from scope intentionally. 

  • What is the difference between == and is for variables?

    == checks if two variables have the same value, while is checks if they point to the same object in memory. For example, [1,2] == [1,2] is True, but [1,2] is [1,2] is False because they are two different list objects. Use is only when checking identity (e.g. x is None). 

  • Can Python variables change type after assignment?

    Yes. Python allows reassigning a variable to a completely different type — x = 5 followed by x = "hello" is valid. This is called dynamic typing. While flexible, it can reduce readability, so use type hints and keep variable roles consistent. 

  • What is the difference between a local and a global variable?

    A local variable exists only inside the function where it is created and cannot be accessed outside. A global variable is defined at the module level and can be read anywhere in the file. To modify a global variable inside a function, use the global keyword. 

  • Does Python require you to declare a variable type?

    No. Python is dynamically typed, so you simply assign a value and Python infers the type automatically. You can optionally add type hints (x: int = 5) for readability and tooling support, but they are not enforced at runtime. 

Data Types in Python

  • Why does “5” + 5 throw an error but “5” + “5” doesn’t?

    Because + means something different depending on the type. Between two strings, it joins them together ("55"). Between a string and an integer, Python doesn’t know what you want, so it refuses rather than guessing wrong. 

  • What’s the difference between int and float?

    An int has no decimal point (10), a float does (10.0). Even 10.0 is treated as a float, not an int, despite looking like a whole number. 

  • Can a variable change its data type later?

    Yes, and this actually happens a lot in real code. If x=5 and later you write x=”five”, Python doesn’t complain. x is now a string. This flexibility is convenient, but it can also cause bugs if you lose track of what a variable currently holds. 

  • Do I need to tell Python what data type a variable is?

    No. Python figures it out automatically based on the value you assign. This is called dynamic typing. Languages like C or Java make you declare the type upfront; Python doesn’t. 

Scroll to Top