• Discover
  • Collections
  • Board
  • Create
  • Profile
  • Settings
Paths

Lesson 2.1: Variables and Naming Conventions

Lesson 2.1: Variables and Naming Conventions

3 min read

In programming, a variable is a named storage location that holds a value. Think of it as a label or a box where you can store different kinds of information. Python variables are dynamic; you don't need to declare their type before using them. Python automatically infers the type based on the value assigned.

Declaring and Assigning Variables:

To create a variable, you simply give it a name and assign a value using the assignment operator (=):

python
# Assigning an integer to a variable age = 30 # Assigning a string to a variable name = "Alice" # Assigning a boolean to a variable is_student = True # You can also assign multiple variables in one line (though use sparingly for readability) x, y, z = 10, 20, 30 # Or assign the same value to multiple variables a = b = c = "Python" print(age) print(name) print(is_student) print(x, y, z) print(a, b, c)

Variable Naming Conventions (PEP 8):

Python has specific rules and widely accepted conventions for naming variables to ensure code readability and consistency (outlined in PEP 8, Python Enhancement Proposal 8).

Rules:

  1. Must start with a letter or an underscore (_). They cannot start with a number.

    python
    # Valid: my_variable = 10 _internal_var = 20 # Invalid (will raise a SyntaxError): # 1st_variable = 30
  2. Can only contain alphanumeric characters (A-Z, a-z, 0-9) and underscores. Special characters like $, #, @ are not allowed.

    python
    # Valid: user_name = "Bob" product_id_123 = 456 # Invalid: # user-name = "Bob" # Hyphens are used for subtraction # total$ = 100
  3. Are case-sensitive: age, Age, and AGE are three different variables.

  4. Cannot be Python keywords: Words that have special meaning in Python (e.g., if, else, while, for, print, True, False, None).

    python
    # Invalid (will raise a SyntaxError or be confusing): # print = "hello" # Don't redefine built-in functions or keywords # class = 5

Conventions (PEP 8 Recommendations):

  • snake_case for variables and functions: All lowercase letters, with words separated by underscores. This is the most common convention.
    python
    first_name = "John" total_price = 150.75 calculate_area() # For functions
  • CamelCase for class names: (You'll learn about classes later).
    python
    class MyClass: pass
  • UPPER_SNAKE_CASE for constants: Variables whose values are not expected to change throughout the program.
    python
    MAX_SIZE = 1000 PI = 3.14159
  • Descriptive names: Choose names that clearly indicate the purpose of the variable. Avoid single-letter variable names unless their context is absolutely clear (e.g., x, y for coordinates in a mathematical context).
    python
    # Good: customer_age = 25 student_grade = 'A' # Less descriptive: c = 25 s = 'A'

Adhering to these conventions makes your code more readable, maintainable, and understandable to others (and your future self).

Key Takeaways:

  • Variables are named containers for storing data.
  • Assign values using the = operator.
  • Follow naming rules (start with letter/underscore, alphanumeric/underscore content, case-sensitive, no keywords).
  • Adhere to PEP 8 conventions, especially snake_case for variable names.
End of lesson
👏Well done!
Previous Lesson
Quiz: Module 1: Getting Started with Python
Next Lesson
Lesson 2.2: Numeric Data Types (Integers, Floats, Complex) and Type Conversion

Course Content

0% Complete0/22 Lessons

Lesson 1.1: What is Python? History, Features, and Applications

Lesson 1.2: Setting Up Your Python Environment

Lesson 1.3: Your First Python Program: "Hello, World!" and Basic Syntax

Quiz

Lesson 2.1: Variables and Naming Conventions

Lesson 2.2: Numeric Data Types (Integers, Floats, Complex) and Type Conversion

Lesson 2.3: String Data Type and Basic Operations

Lesson 2.4: Operators: Arithmetic, Comparison, Logical, Assignment

Quiz

Course Content

0% Complete0/22 Lessons

Lesson 1.1: What is Python? History, Features, and Applications

Lesson 1.2: Setting Up Your Python Environment

Lesson 1.3: Your First Python Program: "Hello, World!" and Basic Syntax

Quiz

Lesson 2.1: Variables and Naming Conventions

Lesson 2.2: Numeric Data Types (Integers, Floats, Complex) and Type Conversion

Lesson 2.3: String Data Type and Basic Operations

Lesson 2.4: Operators: Arithmetic, Comparison, Logical, Assignment

Quiz