Tutorials Logic, IN info@tutorialslogic.com

Python Variables Data Types

Python Variables

Python comments explain why code exists, what a tricky line means, or which decision a future reader should not miss.

Python variables are names that point to values. A variable can point to a number now, a string later, or a list after that, so clear names matter more than type declarations.

On this page, focus on writing code that another beginner can read: helpful comments, meaningful variable names, and simple assignments.

Comments

Comments are lines that Python ignores during execution. They are used to explain code, make it more readable, or temporarily disable code.

Use the # symbol. Everything after # on that line is a comment.

Python doesn't have a dedicated multi-line comment syntax. Use multiple # lines, or use triple-quoted strings (which are technically string literals, not comments).

Single-Line Comments

Single-Line Comments
# This is a single-line comment
print("Hello")  # inline comment

# Comments are ignored by Python
# print("This line won't run")

Multi-Line Comments

Multi-Line Comments
# Consecutive comments explain nearby code.
# Use them for context that is not obvious from the statement itself.

def greet(name: str) -> str:
    """Return a greeting for one person.

    Args:
        name: Person to greet.
    """
    return f"Hello, {name}!"

print(greet("Maya"))
Output
Hello, Maya!

Variables

A variable is a named container that stores a value in memory. In Python, you don't need to declare a variable's type - it's inferred automatically.

Creating Variables

Creating Variables
name = "Alice"        # str
age = 25              # int
height = 5.7          # float
is_student = True     # bool

print(name)           # Alice
print(type(name))     # <class 'str'>
print(type(age))      # <class 'int'>

# Multiple assignment
x = y = z = 0         # all three equal 0

# Tuple unpacking
a, b, c = 1, 2, 3
print(a, b, c)        # 1 2 3

# Swap variables (Python style)
a, b = b, a
print(a, b)           # 2 1

Variable Naming Rules

  • Must start with a letter or underscore (_)
  • Can contain letters, digits, and underscores
  • Cannot start with a digit
  • Cannot use Python keywords (if, for, class, etc.)
  • Case-sensitive: name and Name are different variables

Valid vs Invalid Names

Valid vs Invalid Names
# Valid variable names
my_name = "Alice"
_private = 42
camelCase = "ok but not Pythonic"
snake_case = "preferred in Python"
value2 = 100

# Invalid variable names (will cause SyntaxError)
# 2value = 10       # starts with digit
# my-name = "Bob"   # hyphen not allowed
# class = "Python"  # reserved keyword

# Python naming conventions (PEP 8)
user_name = "alice"          # variables: snake_case
MAX_SIZE = 100               # constants: UPPER_CASE
class MyClass:               # classes: PascalCase
    pass

Dynamic Typing

Python is dynamically typed - a variable can hold different types at different times. Use type() to check the current type.

Reassign One Name to Different Types

Reassign One Name to Different Types
x = 10
print(type(x))   # <class 'int'>

x = "hello"
print(type(x))   # <class 'str'>

x = 3.14
print(type(x))   # <class 'float'>

x = [1, 2, 3]
print(type(x))   # <class 'list'>

# Type hints (Python 3.5+) - optional but recommended
def add(a: int, b: int) -> int:
    return a + b
Comments and variables check

Can You Write Readable Variables?

5 checks
  • Comments are lines that Python ignores during execution.
  • They are used to explain code, make it more readable, or temporarily disable code.
  • Everything after # on that line is a comment.
  • Python doesn't have a dedicated multi-line comment syntax.
  • Use multiple # lines, or use triple-quoted strings (which are technically string literals, not comments).

Variable Decisions

0 of 2 checked

Q1. Which variable name is easiest for another learner to understand?

Q2. What should a good comment explain?

Confusing Names and Comments

  • Using names that hide meaning

    Prefer total_price, user_name, or is_active instead of x, data1, or temp when the value has a real role.
  • Writing comments that repeat the code

    Use comments to explain why a value exists or why a choice was made, not to translate obvious syntax.
  • Changing a variable type halfway through

    Keep a name tied to one kind of value, or create a new name after conversion.

Try this next

Rename and Explain Values

0 of 3 completed

  1. Take five short variable names and rewrite them so a beginner can guess their purpose.
  2. Write a comment that explains a business rule, not a Python keyword.
  3. Assign a score, update it twice, and print the value after each update.

Questions About Variable Naming

No. Comment only when the reason is not obvious from the code. Clear variable names usually remove the need for many comments.

Yes. A name can point to a string first and an integer later, but changing types too often can make code harder to read.

A good name explains the value or purpose, such as total_price, user_name, or is_active. Avoid vague names like x unless the code is very small.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.