# 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 ยป
๐ŸŒ
Learn Python
learnpython.org โ€บ en โ€บ Variables_and_Types
Variables and Types - Learn Python - Free Interactive Python Tutorial
This tutorial will go over a few basic types of variables. Python supports two types of numbers - integers(whole numbers) and floating point numbers(decimals).
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-int
Python int
In this tutorial, we shall learn how to initialize an integer, what range of values an integer can hold, what arithmetic operations we can perform on integer operands, etc. To initialize a variable with integer value, use assign operator and assign the integer value to the variable.
๐ŸŒ
OpenGenus
iq.opengenus.org โ€บ integer-in-python
Integer (Int Variable) in Python
February 12, 2022 - In order to manually state int variables in python, we can utilize the int() method.
๐ŸŒ
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 the above example, the class Person is not of the integer type. But we can still return the age variable (which is an integer) using the int() method.
Find elsewhere
๐ŸŒ
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 - In Python, variables can be created without explicitly defining the type of data that it will hold (e.g. integer, text string).
๐ŸŒ
Tutorial Teacher
tutorialsteacher.com โ€บ python โ€บ python-number-type
Python Numbers: int, float, complex (With Examples)
In Python, integers are zero, positive or negative whole numbers without a fractional part and having unlimited precision, e.g. 0, 100, -10. The followings are valid integer literals in Python.
๐ŸŒ
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 - Another remarkable aspect of Python: Not only the value of a variable may change during program execution, but the type as well. You can assign an integer value to a variable, use it as an integer for a while and then assign a string to the same variable.
๐ŸŒ
Data Science Discovery
discovery.cs.illinois.edu โ€บ guides โ€บ Python-Fundamentals โ€บ Python-data-types
Python Data Types - Data Science Discovery
Python is a dynamically typed language, meaning you don't need to declare the type of a variable explicitly. Here's a summary of some commonly used data types in Python: Integer (int): Whole numbers without any decimal point, e.g., 5, -10, 100.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-variables
Python Variables - GeeksforGeeks
A variable is essentially a name that is assigned to a value. Unlike Java and many other languages, Python variables do not require explicit declaration of type.
Published ย  2 weeks ago
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.3 documentation
February 25, 2026 - Also referred to as integer division. For operands of type int, the result has type int. For operands of type float, the result has type float. In general, the result is a whole integer, though the resultโ€™s type is not necessarily int.
๐ŸŒ
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.
Top answer
1 of 16
1433

If you need to do this, do

isinstance(<var>, int)

unless you are in Python 2.x in which case you want

isinstance(<var>, (int, long))

Do not use type. It is almost never the right answer in Python, since it blocks all the flexibility of polymorphism. For instance, if you subclass int, your new class should register as an int, which type will not do:

class Spam(int): pass
x = Spam(0)
type(x) == int # False
isinstance(x, int) # True

This adheres to Python's strong polymorphism: you should allow any object that behaves like an int, instead of mandating that it be one.

BUT

The classical Python mentality, though, is that it's easier to ask forgiveness than permission. In other words, don't check whether x is an integer; assume that it is and catch the exception results if it isn't:

try:
    x += 1
except TypeError:
    ...

This mentality is slowly being overtaken by the use of abstract base classes, which let you register exactly what properties your object should have (adding? multiplying? doubling?) by making it inherit from a specially-constructed class. That would be the best solution, since it will permit exactly those objects with the necessary and sufficient attributes, but you will have to read the docs on how to use it.

2 of 16
204

Here's a summary of the different methods mentioned here:

  • int(x) == x
  • try x = operator.index(x)
  • isinstance(x, int)
  • isinstance(x, numbers.Integral)

and here's how they apply to a variety of numerical types that have integer value:

You can see they aren't 100% consistent. Fraction and Rational are conceptually the same, but one supplies a .index() method and the other doesn't. Complex types don't like to convert to int even if the real part is integral and imaginary part is 0.

(np.int8|16|32|64(5) means that np.int8(5), np.int32(5), etc. all behave identically)

๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ functions.html
Built-in Functions โ€” Python 3.14.3 documentation
February 27, 2026 - Return a Boolean value, i.e. one of True or False. The argument is converted using the standard truth testing procedure. If the argument is false or omitted, this returns False; otherwise, it returns True. The bool class is a subclass of int (see Numeric Types โ€” int, float, complex).