python-backend
/

Python Variables & Data Types – Storing Information

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

python-backend

Python Variables & Data Types – Storing Information

What is a Variable?

A variable is a reserved memory location to store values. In Python, you don't need to declare the type of a variable explicitly; it is created the moment you assign a value to it. This is known as Dynamic Typing.

Core Data Types

Python has several built-in data types categorized into Numbers, Sequences, and Mappings.

  • int: Whole numbers, positive or negative, without decimals (e.g., 10, -5).
  • float: Floating point numbers, containing one or more decimals (e.g., 10.5, -0.1).
  • str: Textual data enclosed in single or double quotes (e.g., 'Hello', "Python").
  • bool: Represents logical values: True or False.

Naming Conventions

Python follows the snake_case convention for variable names. Names must start with a letter or underscore and are case-sensitive.

PythonRead-only
1
# Valid naming examples
user_age = 28
_is_active = True
profile_name = "Kishore"

# ❌ Invalid: 1st_user = "Error"
# ❌ Invalid: user-name = "Error"

Dynamic vs. Static Typing

Unlike Dart or Java, Python allows a variable to change its type during execution.

FeaturePython (Dynamic)Dart/C++ (Static)
Declarationx = 5int x = 5;
Type Changex = 'Hi' (Valid)x = 'Hi' (Error)
FlexibilityHighLow
SafetyLow (Runtime errors)High (Compile errors)

Checking Types

You can use the built-in type() function to find out the data type of any variable.

PythonRead-only
1
x = 100
print(type(x)) # Output: <class 'int'>

x = "Kishore"
print(type(x)) # Output: <class 'str'>

Try it yourself

# Create variables
name = "Kishore"
experience = 5.5
is_hired = False

# Change type dynamically
status = "Active"
status = 1 # Now it's an integer!

print(f"Name: {name}")
print(f"Type of experience: {type(experience)}")
print(f"Status: {status}")

Test Your Knowledge

Q1
of 3

Which of the following is a valid variable name in Python?

A
2nd_user
B
user-name
C
user_name
D
user name
Q2
of 3

What happens if you assign a string to a variable that previously held an integer?

A
It throws a TypeError
B
Python crashes
C
The variable's type changes to string (Dynamic Typing)
D
The integer is converted to its ASCII value
Q3
of 3

Which function is used to get the data type of a variable?

A
typeof()
B
get_type()
C
type()
D
class()

Frequently Asked Questions

Does Python have Constants?

Python doesn't have true constants. By convention, developers use ALL_CAPS names to indicate a variable should not be changed, but it's not enforced by the language.

Can I declare multiple variables at once?

Yes! You can use: x, y, z = 1, 2, 3 or x = y = z = 'Same Value'.

What is Type Casting?

Converting one type to another using functions like int(), float(), or str(). Example: str(10) converts the number 10 to the string '10'.

Previous

python syntax

Next

python control flow

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.