0% found this document useful (0 votes)
16 views

Python Variable

Uploaded by

Atharv Potdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Python Variable

Uploaded by

Atharv Potdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

**Understanding Python Variables**

Variables in Python are symbolic names that serve as references or placeholders for values. They are
fundamental to storing, accessing, and manipulating data in a program. Python simplifies the use of
variables by dynamically inferring their data types based on the values assigned to them.

---

**Key Characteristics of Python Variables**

1. **Dynamic Typing**:

Python is dynamically typed, meaning you don’t need to declare the type of a variable. For
example:

```python

age = 25 Automatically considered as an integer

name = "Alice" Automatically considered as a string

```

2. **Case Sensitivity**:

Variable names in Python are case-sensitive. For instance:

```python

Score = 10 Different from score

```

3. **Reassignment**:

Variables can be reassigned to values of different types during execution:

```python

value = 42

value = "Hello, World!" Type changes from int to string

```
---

**Rules for Naming Variables**

1. **Start with a Letter or Underscore**:

Variable names must begin with a letter (a–z, A–Z) or an underscore (_).

```python

_counter = 0

```

2. **Contain Letters, Numbers, or Underscores**:

Variable names can include letters, numbers, or underscores but cannot have spaces or special
characters.

```python

user_name = "John Doe"

```

3. **Avoid Keywords**:

Python keywords like `if`, `while`, and `return` cannot be used as variable names.

4. **Be Descriptive**:

Use meaningful names that reflect the purpose of the variable. For example:

```python

radius = 7 Good name

r=7 Less descriptive

```

---

**Types of Variables in Python**


Python variables can hold various types of data, including:

1. **Numeric Types**:

- `int`: Integer values, e.g., `42`

- `float`: Decimal values, e.g., `3.14`

- `complex`: Complex numbers, e.g., `3 + 4j`

2. **Text Type**:

- `str`: String of characters, e.g., `"Hello"`

3. **Boolean Type**:

- `bool`: Values are `True` or `False`

4. **Collections**:

- `list`: Ordered collection, e.g., `[1, 2, 3]`

- `tuple`: Immutable collection, e.g., `(1, 2, 3)`

- `set`: Unordered collection of unique items, e.g., `{1, 2, 3}`

- `dict`: Key-value pairs, e.g., `{"name": "Alice", "age": 25}`

---

**Best Practices for Variables**

1. **Use Descriptive Names**:

Avoid generic names like `x` or `data`. Instead, use meaningful names like `user_age` or
`total_score`.

2. **Follow Naming Conventions**:

Use snake_case for variable names, and UPPER_CASE for constants:

```python
MAX_SPEED = 120

```

3. **Avoid Global Variables**:

Limit the use of global variables to avoid unintended modifications.

---

**Variable Scope**

1. **Local Scope**: Variables declared inside a function are local and cannot be accessed outside it:

```python

def example():

local_var = 10 Only accessible within this function

```

2. **Global Scope**: Variables declared outside all functions are global and accessible anywhere in
the file:

```python

global_var = "I'm global"

```

3. **Nonlocal Scope**: Variables in nested functions that use the `nonlocal` keyword to modify their
enclosing function's variables.

---

**Constants in Python**

While Python does not have built-in support for constants, conventionally, variable names in
uppercase are treated as constants:

```python
PI = 3.14159

GRAVITY = 9.8

```

By adhering to best practices and understanding the nuances of Python variables, developers can
write clean, maintainable, and effective code.

You might also like