/
Using Variables in Python

Using Variables in Python

1. Declaring Variables

In Python, you simply assign a value to a variable using the equals ( = ) sign.

a = 5            # Integer b = 3.14         # Float c = 'Hello'      # String d = True         # Boolean

2. Naming Conventions

  • Variable names should start with a letter (a-z, A-Z) or an underscore ( _ ).

  • Subsequent characters can include letters, digits (0-9), and underscores.

  • Variable names are case-sensitive (age and Age are different variables).

    my_variable = 10 _myVariable = 20 variable3 = 30

3. Using Variables

You can use variables in various operations and expressions.

# Arithmetic operations x = 10 y = 20 sum = x + y        # Addition difference = x - y # Subtraction product = x * y    # Multiplication quotient = x / y   # Division # Using variables in strings name = "Alice" greeting = "Hello, " + name + "!" print(greeting)

4. Updating Variables

Variables can be updated by reassigning them new values.

counter = 1 counter = counter + 1  # Incrementing counter += 1           # Equivalent to counter = counter + 1

5. Data Types and Type Conversion

Python variables can hold different types of data, and you can convert between types.

# Integer to string age = 25 age_str = str(age) # String to integer age_str = "25" age = int(age_str) # Float to integer pi = 3.14 pi_int = int(pi) # Boolean to integer is_active = True is_active_int = int(is_active)

6. Working with Lists and Dictionaries

Variables can also store collections like lists and dictionaries.

# Lists fruits = ['apple', 'banana', 'cherry'] print(fruits[0])  # Accessing the first element # Dictionaries person = {'name': 'John', 'age': 30} print(person['name'])  # Accessing the value associated with the key 'name'

7. Variable Scope

Variables have different scopes depending on where they are declared. There are local and global variables.

# Global variable x = 5 def example_function():     # Local variable     y = 10     print(x)  # Accessing global variable     print(y)  # Accessing local variable example_function() print(x)  # Global variable can be accessed outside the function # print(y)  # This would raise an error as y is local to the function

8. Best Practices

  • Use meaningful variable names for better code readability.

  • Follow the Python style guide (PEP 8) for naming conventions (e.g., snake_case for variable names).

    # Good practice total_price = 150 # Avoid using single letters unless in short blocks or loops t = 150  # Not recommended

Example Script

Here’s a simple example script that uses variables:

# Example Python script using variables # Declare variables name = "Alice" age = 25 height = 5.6 # Print variables print("Name:", name) print("Age:", age) print("Height:", height) # Update variables age += 1 height += 0.1 # Print updated variables print("Updated Age:", age) print("Updated Height:", height) # Using variables in expressions years_until_30 = 30 - age print(name, "will be 30 in", years_until_30, "years.")



Related content