Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

Python Variables and Data Types - Complete Tutorial

Comments

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

Single-Line Comments

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

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

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).

Multi-Line Comments
# Method 1: Multiple # lines
# This is line 1
# This is line 2
# This is line 3

# Method 2: Triple-quoted string (docstring style)
"""
This is a multi-line string.
Often used as a docstring at the top of
functions, classes, or modules.
"""

def greet(name):
    """
    This function greets a person.
    Args:
        name (str): The person's name
    Returns:
        str: A greeting message
    """
    return f"Hello, {name}!"

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
name = "Alice"        # str
age = 25              # int
height = 5.7          # float
is_student = True     # bool

print(name)           # Alice
print(type(name))     # 
print(type(age))      # 

# 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 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.

Dynamic Typing
x = 10
print(type(x))   # 

x = "hello"
print(type(x))   # 

x = 3.14
print(type(x))   # 

x = [1, 2, 3]
print(type(x))   # 

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

Ready to Level Up Your Skills?

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