Okay, first things first.

There is no such thing as "variable declaration" or "variable initialization" in Python.

There is simply what we call "assignment", but should probably just call "naming".

Assignment means "this name on the left-hand side now refers to the result of evaluating the right-hand side, regardless of what it referred to before (if anything)".

foo = 'bar' # the name 'foo' is now a name for the string 'bar'
foo = 2 * 3 # the name 'foo' stops being a name for the string 'bar',
# and starts being a name for the integer 6, resulting from the multiplication

As such, Python's names (a better term than "variables", arguably) don't have associated types; the values do. You can re-apply the same name to anything regardless of its type, but the thing still has behaviour that's dependent upon its type. The name is simply a way to refer to the value (object). This answers your second question: You don't create variables to hold a custom type. You don't create variables to hold any particular type. You don't "create" variables at all. You give names to objects.

Second point: Python follows a very simple rule when it comes to classes, that is actually much more consistent than what languages like Java, C++ and C# do: everything declared inside the class block is part of the class. So, functions (def) written here are methods, i.e. part of the class object (not stored on a per-instance basis), just like in Java, C++ and C#; but other names here are also part of the class. Again, the names are just names, and they don't have associated types, and functions are objects too in Python. Thus:

class Example:
    data = 42
    def method(self): pass

Classes are objects too, in Python.

So now we have created an object named Example, which represents the class of all things that are Examples. This object has two user-supplied attributes (In C++, "members"; in C#, "fields or properties or methods"; in Java, "fields or methods"). One of them is named data, and it stores the integer value 42. The other is named method, and it stores a function object. (There are several more attributes that Python adds automatically.)

These attributes still aren't really part of the object, though. Fundamentally, an object is just a bundle of more names (the attribute names), until you get down to things that can't be divided up any more. Thus, values can be shared between different instances of a class, or even between objects of different classes, if you deliberately set that up.

Let's create an instance:

x = Example()

Now we have a separate object named x, which is an instance of Example. The data and method are not actually part of the object, but we can still look them up via x because of some magic that Python does behind the scenes. When we look up method, in particular, we will instead get a "bound method" (when we call it, x gets passed automatically as the self parameter, which cannot happen if we look up Example.method directly).

What happens when we try to use x.data?

When we examine it, it's looked up in the object first. If it's not found in the object, Python looks in the class.

However, when we assign to x.data, Python will create an attribute on the object. It will not replace the class' attribute.

This allows us to do object initialization. Python will automatically call the class' __init__ method on new instances when they are created, if present. In this method, we can simply assign to attributes to set initial values for that attribute on each object:

class Example:
    name = "Ignored"
    def __init__(self, name):
        self.name = name
    # rest as before

Now we must specify a name when we create an Example, and each instance has its own name. Python will ignore the class attribute Example.name whenever we look up the .name of an instance, because the instance's attribute will be found first.

One last caveat: modification (mutation) and assignment are different things!

In Python, strings are immutable. They cannot be modified. When you do:

a = 'hi '
b = a
a += 'mom'

You do not change the original 'hi ' string. That is impossible in Python. Instead, you create a new string 'hi mom', and cause a to stop being a name for 'hi ', and start being a name for 'hi mom' instead. We made b a name for 'hi ' as well, and after re-applying the a name, b is still a name for 'hi ', because 'hi ' still exists and has not been changed.

But lists can be changed:

a = [1, 2, 3]
b = a
a += [4]

Now b is [1, 2, 3, 4] as well, because we made b a name for the same thing that a named, and then we changed that thing. We did not create a new list for a to name, because Python simply treats += differently for lists.

This matters for objects because if you had a list as a class attribute, and used an instance to modify the list, then the change would be "seen" in all other instances. This is because (a) the data is actually part of the class object, and not any instance object; (b) because you were modifying the list and not doing a simple assignment, you did not create a new instance attribute hiding the class attribute.

Answer from Karl Knechtel on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_variables.asp
Python Variables
Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how can i assign a value to a variable later in python?
r/learnpython on Reddit: How can I assign a value to a variable later in Python?
June 18, 2022 -

I am new to Python, comming from other programming languages and with them, you could declare a variable and give it value later on like in this example:

string phrase;

phrase = "Hi mom";

Console.WriteLine(phrase);

//output: Hi mom

I am wondering how can I achieve something similar in Python as it would be very useful for the project I am doing. All help is appreciated!

Discussions

How are variables and class fields supposed to be declared in Python? - Stack Overflow
There is no such thing as "variable declaration" or "variable initialization" in Python. There is simply what we call "assignment", but should probably just call "naming". More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to assign input value to a variable in python?
I am just a beginner to python ! I wanna know what function can be used to create such a program that asks to input certain value and then stores the value in a variable More on sololearn.com
๐ŸŒ sololearn.com
3
1
How do I assign a Values to Variables in Python?
Welcome to Just Answer, my name is Rob. Please hold on for a moment while I look into this.In Python, variables do not require explicit declaration to allocate memory space. The declaration occurs automatically when a value is assigned to a variable. More on justanswer.com
๐ŸŒ justanswer.com
0
April 6, 2019
I'm trying to change a Global Variable inside of a Async Function, Any Thoughts?
Just for future reference: global variables are bad. Global variables inside async functions are so much worse. also, using a number as a boolean value is a horrible idea. Just use a bool. If you need more than 2 options, use an Enum . More on reddit.com
๐ŸŒ r/learnpython
4
1
June 2, 2024
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ assign-a-variable
How to assign a variable in Python - Python Morsels
July 18, 2026 - You can set the value of a variable in Python by using the equals sign (=) to make an assignment statement.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-variables
Python Variables - GeeksforGeeks
1. Basic Assignment: Variables are assigned values using the = operator. ... 2. Dynamic Typing: Python is dynamically typed, so the same variable can store different data types during execution.
Published: 2 weeks ago
๐ŸŒ
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu โ€บ notebooks โ€บ chapter02.01-Variables-and-Assignment.html
Variables and Assignment โ€” Python Numerical Methods
When programming, it is useful to be able to store information in variables. A variable is a string of characters and numbers associated with a piece of information. The assignment operator, denoted by the โ€œ=โ€ symbol, is the operator that is used to assign values to variables in Python.
๐ŸŒ
Imperialcollegelondon
imperialcollegelondon.github.io โ€บ python-novice-mix โ€บ 02-variables โ€บ index.html
Programming in Python: Variables and Assignment
November 8, 2018 - The variable is created when a value is assigned to it. Here, Python assigns an age to a variable age and a name in quotes to a variable first_name.
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-var.html
Python Variables and Assignment
A Python variable is a named place to store a value. A variable is created with an "assignment" = (one equal sign), with the variable's name on the left and the value it should store on the right:
Find elsewhere
Top answer
1 of 6
243

Okay, first things first.

There is no such thing as "variable declaration" or "variable initialization" in Python.

There is simply what we call "assignment", but should probably just call "naming".

Assignment means "this name on the left-hand side now refers to the result of evaluating the right-hand side, regardless of what it referred to before (if anything)".

foo = 'bar' # the name 'foo' is now a name for the string 'bar'
foo = 2 * 3 # the name 'foo' stops being a name for the string 'bar',
# and starts being a name for the integer 6, resulting from the multiplication

As such, Python's names (a better term than "variables", arguably) don't have associated types; the values do. You can re-apply the same name to anything regardless of its type, but the thing still has behaviour that's dependent upon its type. The name is simply a way to refer to the value (object). This answers your second question: You don't create variables to hold a custom type. You don't create variables to hold any particular type. You don't "create" variables at all. You give names to objects.

Second point: Python follows a very simple rule when it comes to classes, that is actually much more consistent than what languages like Java, C++ and C# do: everything declared inside the class block is part of the class. So, functions (def) written here are methods, i.e. part of the class object (not stored on a per-instance basis), just like in Java, C++ and C#; but other names here are also part of the class. Again, the names are just names, and they don't have associated types, and functions are objects too in Python. Thus:

class Example:
    data = 42
    def method(self): pass

Classes are objects too, in Python.

So now we have created an object named Example, which represents the class of all things that are Examples. This object has two user-supplied attributes (In C++, "members"; in C#, "fields or properties or methods"; in Java, "fields or methods"). One of them is named data, and it stores the integer value 42. The other is named method, and it stores a function object. (There are several more attributes that Python adds automatically.)

These attributes still aren't really part of the object, though. Fundamentally, an object is just a bundle of more names (the attribute names), until you get down to things that can't be divided up any more. Thus, values can be shared between different instances of a class, or even between objects of different classes, if you deliberately set that up.

Let's create an instance:

x = Example()

Now we have a separate object named x, which is an instance of Example. The data and method are not actually part of the object, but we can still look them up via x because of some magic that Python does behind the scenes. When we look up method, in particular, we will instead get a "bound method" (when we call it, x gets passed automatically as the self parameter, which cannot happen if we look up Example.method directly).

What happens when we try to use x.data?

When we examine it, it's looked up in the object first. If it's not found in the object, Python looks in the class.

However, when we assign to x.data, Python will create an attribute on the object. It will not replace the class' attribute.

This allows us to do object initialization. Python will automatically call the class' __init__ method on new instances when they are created, if present. In this method, we can simply assign to attributes to set initial values for that attribute on each object:

class Example:
    name = "Ignored"
    def __init__(self, name):
        self.name = name
    # rest as before

Now we must specify a name when we create an Example, and each instance has its own name. Python will ignore the class attribute Example.name whenever we look up the .name of an instance, because the instance's attribute will be found first.

One last caveat: modification (mutation) and assignment are different things!

In Python, strings are immutable. They cannot be modified. When you do:

a = 'hi '
b = a
a += 'mom'

You do not change the original 'hi ' string. That is impossible in Python. Instead, you create a new string 'hi mom', and cause a to stop being a name for 'hi ', and start being a name for 'hi mom' instead. We made b a name for 'hi ' as well, and after re-applying the a name, b is still a name for 'hi ', because 'hi ' still exists and has not been changed.

But lists can be changed:

a = [1, 2, 3]
b = a
a += [4]

Now b is [1, 2, 3, 4] as well, because we made b a name for the same thing that a named, and then we changed that thing. We did not create a new list for a to name, because Python simply treats += differently for lists.

This matters for objects because if you had a list as a class attribute, and used an instance to modify the list, then the change would be "seen" in all other instances. This is because (a) the data is actually part of the class object, and not any instance object; (b) because you were modifying the list and not doing a simple assignment, you did not create a new instance attribute hiding the class attribute.

2 of 6
46

This might be 6 years late, but in Python 3.5 and above, you can give a hint about a variable type like this:

variable_name: type_name

or this:

variable_name # type: shinyType

This hint has no effect in the core Python interpreter, but many tools will use it to aid the programmer in writing correct code.

So in your case(if you have a CustomObject class defined), you can do:

customObj: CustomObject

See this type hints question or this article on how to use static type checking for more info.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ assign-function-to-a-variable-in-python
Assign Function to a Variable in Python - GeeksforGeeks
June 11, 2026 - Python ยท def show(): print("GFG") ... the show() function ยท Note: When assigning a function to a variable, use the function name without parentheses....
๐ŸŒ
Pluralsight
pluralsight.com โ€บ blog โ€บ tech guides & tutorials
Python Variables and Assignment | Pluralsight
The new line prompt >>> is empty. But Python actually did a few things: A variable named one was created. The value 1 was assigned to the variable one.
๐ŸŒ
Medium
yuvrajscorpio.medium.com โ€บ variables-assignment-in-python-8e81cc5bb182
Variables & Assignment in Python. Python Basics | by Yuvraj Kevit | Medium
April 29, 2025 - Should not be a reserved keyword in Python (like class, for, if, etc.). Syntax: Variable assignment is done using the = operator.
๐ŸŒ
Tutorial Teacher
tutorialsteacher.com โ€บ python โ€บ python-variables
Python Variables: A Beginner's Guide to Declaring, Assigning, and Naming Variables in Python
Unlike C# or Java, it's not necessary to explicitly define a variable in Python before using it. Just assign a value to a variable using the = operator e.g. variable_name = value.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ how-to-assign-values-to-variables-in-python-and-other-languages
Assigning Values to Variables in Python
July 11, 2025 - In this method, we will directly assign the value in Python but in other programming languages like C, and C++, we have to first initialize the data type of the variable. So, In Python, there is no need for explicit declaration in variables as compared to using some other programming languages.
๐ŸŒ
Real Python
realpython.com โ€บ python-variables
Variables in Python: Usage and Best Practices โ€“ Real Python
April 15, 2026 - Variables in Python are symbolic names pointing to objects or values in memory. You define variables by assigning them a value using the assignment operator.
๐ŸŒ
Cisco
ipcisco.com โ€บ home โ€บ python variables
Python Variables | Python Variable Names | How to Assign Value?โ‹† IpCisco
August 5, 2022 - So, how can we create variables in Python? We can do this easily by assigning a value to a variable. When we assing this value, the variable is automatically created.
๐ŸŒ
LabEx
labex.io โ€บ questions โ€บ how-to-assign-a-value-to-a-variable-in-python-270256
How to assign a value to a variable in Python | LabEx
April 16, 2026 - Python is a dynamically-typed language, which means that you don't need to explicitly declare the data type of a variable. Python will automatically determine the data type based on the value you assign to the variable.
๐ŸŒ
JustAnswer
justanswer.com โ€บ computer-programming โ€บ c90qh-assign-values-variables-python.html
How do I assign a Values to Variables in Python?
April 6, 2019 - In Python, assign values using the equals sign, e.g., x = 5. Variable names must start with a letter or underscore and avoid keywords. For multiple assignments, use commas: x, y = 1, 2. To assign the same value to several variables, chain ...
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-assign-a-value-to-a-python-variable-398139
How to assign a value to a Python variable | LabEx
In this case, the variables a, b, and c are all assigned the value 10. Python provides a set of augmented assignment operators that allow you to perform an operation and assignment in a single step.