# declare score as integer
score = int

# declare rating as character
rating = chr

Above two statement, assigns the function int, chr, not declaring the variable with the default value. (BTW, chr is not a type, but a function that convert the code-point value to character)

Do this instead:

score = 0    # or   int()
rating = ''  # or   'C'   # if you want C to be default rating

NOTE score is not need to be initialized, because it's assigned by score = input("Enter score: ")

Answer from falsetru 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. x = 5 y = "John" print(x) print(y) Try it Yourself ยป ยท Variables do not need to be declared with any particular type, and can even change type after they have been set. x = 4 # x is of type int x = "Sally" # x is now of type str print(x) Try it Yourself ยป
Discussions

Creating a variable integer
What do you want to happen when the user inputs a float? Is whatever math your program is doing something that will still work with a float, so you can just keep it a float? Or do you need to convert it to an int, and if so, what do you want to do with the part after the decimal (just truncate, always round up, round to nearest int, etc). Or do you want to make them input again until they input an int? More on reddit.com
๐ŸŒ r/learnpython
7
0
October 28, 2024
How are variables and class fields supposed to be declared in Python? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I want to clarify how variables are declared in Python. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Not sure how to check if a variable is an int
I think cases like this are when you should be using a try-except statement. The other option I can think of is to loop through the user input, check if every character is a digit, and if it is, set the while loop conditional value to False. If any of the characters are not digits, then set the while loop conditional value to True, and keep running the while loop until every character in the user input is a digit. Then at the end of the function, convert the user input to an int and print it. More on reddit.com
๐ŸŒ r/learnpython
20
5
July 27, 2022
How to declare a variable in Python?
I assume from the "Meme" flaire this is satire? More on reddit.com
๐ŸŒ r/ProgrammerHumor
11
0
May 14, 2022
๐ŸŒ
Learn Python
learnpython.org โ€บ en โ€บ Variables_and_Types
Variables and Types - Learn Python - Free Interactive Python Tutorial
These are beyond the scope of this tutorial, but are covered in the Python documentation. Simple operators can be executed on numbers and strings: one = 1 two = 2 three = one + two print(three) hello = "hello" world = "world" helloworld = hello + " " + world print(helloworld) Assignments can be done on more than one variable "simultaneously" on the same line like this ... The target of this exercise is to create a string, an integer, and a floating point number.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-variables
Python Variables - GeeksforGeeks
For example, int() converts compatible values to an integer, float() to a floating point number, and str() to a string, We can remove a variable from the namespace using the del keyword.
Published ย  2 weeks ago
๐ŸŒ
OpenGenus
iq.opengenus.org โ€บ integer-in-python
Integer (Int Variable) in Python
February 12, 2022 - Using our last example of the "hello_" variable, if we type the following line: ... So what would happen if we divide two integer variables? Let's say for instance, 5 and 2. First, we would assign the two variables, in this case "number_one" and "number_two" can be assigned 5 and 2, respectively, ...
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-int
Python int
In the following program, we have initialized variables x and y with integer values. ... To print an integer to standard console output, we can use built-in function print(). print() accepts integer as argument. In the following example, we shall initialize an integer and print it to console. ... In Python 3, there is no bound on the maximum or minimum value an integer variable can hold.
Find elsewhere
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ int
Python int() (With Examples)
The newer version of Python uses the __index__() method. class Person: age = 23 def __index__(self): return self.age # def __int__(self): # return self.age person = Person() # int() method with a non integer object person print("int(person) is:", int(person)) Output ยท int(person) is: 23 ยท In ...
๐ŸŒ
Quora
quora.com โ€บ How-do-you-declare-an-int-variable-in-Python
How to declare an int variable in Python - Quora
Answer (1 of 4): before you declare int you have to know the syntax(Rules or usage of a keyword) of int syntax : variablename = value or you can give input (if you want value from your user) variablename can be anything except a keyword or fuction name example: apple=20 Apple=20 APPLE=20 t...
๐ŸŒ
Earth Data Science
earthdatascience.org โ€บ home
Variables in Python | Earth Data Science - Earth Lab
September 23, 2020 - As described previously, you do ... you can create a int variable called boulder_precip_in, which contains the value for the average annual precipitation in inches (in) in Boulder, Colorado, rounded to the nearest integer....
๐ŸŒ
Real Python
realpython.com โ€บ python-variables
Variables in Python: Usage and Best Practices โ€“ Real Python
January 12, 2025 - Note: To dive deeper into expressions and operators, check out the Operators and Expressions in Python tutorial. The above expressions are sort of rigid. For each expression, you have to repeat the input values, which is an error-prone and repetitive task. ... >>> pi = 3.1416 >>> radius = 10 >>> 2 * pi * radius 62.912 >>> radius = 20 >>> 2 * pi * radius 125.824 ยท In this example, you first define variables to hold the input values.
๐ŸŒ
Python Course
python-course.eu โ€บ python-tutorial โ€บ data-types-and-variables.php
6. Data Types and Variables | Python Tutorial | python-course.eu
February 12, 2022 - Or, to be precise, a new object, which can be of any type, will be assigned to it. We illustrate this in our following example: i = 42 # data type is implicitly set to integer i = 42 + 0.11 # data type is changed to float i = "forty" # and now it will be a string
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-declare-a-variable-in-python
How to declare a variable in Python?
August 23, 2023 - Thus, declaring a variable in Python is very simple. ... The data type of the variable will be automatically determined from the value assigned, we need not define it explicitly. ... This is how you declare a integer variable in Python. Just name the variable and assign the required value to it.
๐ŸŒ
Tutorial Teacher
tutorialsteacher.com โ€บ python โ€บ python-variables
Python Variables: A Beginner's Guide to Declaring, Assigning, and Naming Variables in Python
Just assign a value to a variable using the = operator e.g. variable_name = value. That's it. The following creates a variable with the integer value. ... In the above example, we declared a variable named num and assigned an integer value 10 to it.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_variables.htm
Python - Variables
This example prints variables. counter = 100 # Creates an integer variable miles = 1000.0 # Creates a floating point variable name = "Zara Ali" # Creates a string variable print (counter) print (miles) print (name) Here, 100, 1000.0 and "Zara ...
๐ŸŒ
Geek University
geek-university.com โ€บ home โ€บ numeric variables
Numeric variables | Python# - Geek University
January 29, 2022 - You can convert an integer number to a floating-point value using the float (NUMBER) function, like in this example: ... To convert a floating-point value to an integer number, use the int(NUMBER) function. Now, consider what happens if we divide two integer numbers: >>> x = 5 >>> y = 2 >>> z = x / y >>> print(z) 2.5 >>> Notice how, although numbers are defined as integers (whole numbers), the result of the division operation was a floating-point number. This is a Python 3.x feature โ€“ Python 2.x would perform integer division and return the result of 2.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-use-variables-in-python-3
How To Use Variables in Python 3 | DigitalOcean
February 26, 2026 - You can think of a variable as a label tied to a value. For example, storing the integer 103204934813 in a variable lets you reuse it without retyping the number: Info: To follow along, open a Python interactive shell with the python3 command and run the examples after the >>> prompt.
๐ŸŒ
Wiingy
wiingy.com โ€บ home โ€บ learn โ€บ python โ€บ python variables (with examples)
Python Variables (With Examples) - Wiingy
January 30, 2025 - We can also perform operations on variables, such as arithmetic operations on integers and floating-point numbers. For example: 1# Adding two integer variables 2num1 = 5 3num2 = 7 4result = num1 + num2 5print(result) # Output: 12 ยท In summary, creating and initializing variables is an important aspect of programming in Python.
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-var.html
Python Variables and Assignment
This is a little confusing, since ... value has a "type" which helps determine how that value is treated by the code. For example, the value 42 is type int, which is the integer type....
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.