Python variable assignment is different from some of the popular languages like c, c++ and java. There is no declaration of a variable, just an assignment statement.
Let us see why?
When we declare a variable in C or alike languages, this sets aside an area of memory for holding values allowed by the data type of the variable. The memory allocated will be interpreted as the data type suggests. If it’s an integer variable the memory allocated will be read as an integer and so on. When we assign or initialize it with some value, that value will get stored at that memory location. At compile time, initial value or assigned value will be checked. So we cannot mix types. Example: initializing a string value to an int variable is not allowed and the program will not compile.
But Python is a dynamically typed language. It doesn’t know about the type of the variable until the code is run. So declaration is of no use. What it does is, It stores that value at some memory location and then binds that variable name to that memory container. And makes the contents of the container accessible through that variable name. So the data type does not matter. As it will get to know the type of the value at run-time.
<class 'int'> <class 'str'>
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Scope resolution when a function is called or defined
- PyQt5 QSpinBox - Getting the object by which action is called
- Python | How to dynamically change text of Checkbutton
- How to Dynamically Load Modules or Classes in Python
- Create Classes Dynamically in Python
- wxPython - Dynamically enable and disable tools in toolbar using button
- Dynamically Resize Buttons When Resizing a Window using Tkinter
- Why is python best suited for Competitive Coding?
- Why import star in Python is a bad idea
- 12 Reasons Why You Should Learn Python in 2019
- Why is Python the Best-Suited Programming Language for Machine Learning?
- Why do people prefer Selenium with Python?
- Why Python Is Used For Developing Automated Trading Strategy?
- 10 Reasons Why You Should Choose Python For Big Data
- Why do we Need Immutables in Python ?
- Why and how are Python functions hashable?
- Why is Numpy faster in Python?
- Why does Python automatically exit a script when it’s done?
- Why 0.3 - 0.2 is not equal to 0.1 in Python?
- ML | Why Logistic Regression in Classification ?