Skip to main content

About Variables in python

In Python, variables are used to store data or values. Unlike some other programming languages, Python is dynamically typed, which means you don't need to explicitly declare the type of a variable before assigning a value to it. The type of the variable is automatically determined based on the value it holds.

Rules for naming variables in Python:

1. Variable names can contain letters (a-z, A-Z), digits (0-9), and underscores (_). However, they cannot start with a digit.

2. Variable names are case-sensitive. For example, "age" and "Age" would be treated as different variables.

3. Python keywords (reserved words) cannot be used as variable names. For example, you cannot use words like `if`, `else`, `while`, `def`, etc., as variable names.

Examples of valid variable names:

```python
name = "John"
age = 30
is_student = True
x = 10
```

Assigning values to variables:

In Python, you can assign a value to a variable using the assignment operator (`=`).

```python
x = 10
y = "Hello"
is_valid = True
```

Dynamic typing:

Python allows you to reassign variables with different types of data at any point in the code. For example:

```python
x = 10
print(x)  # Output: 10

x = "Hello"
print(x)  # Output: Hello
```

Multiple assignment:

Python also allows multiple assignments in a single line:

```python
a, b, c = 1, 2, 3
```

This assigns the values 1, 2, and 3 to variables `a`, `b`, and `c`, respectively.

Variable scope:

The scope of a variable determines where it can be accessed within the code. Variables declared inside a function have local scope and are only accessible within that function. Variables declared outside any function have global scope and can be accessed throughout the code.

```python
def my_function():
    x = 10  # This is a local variable and can only be accessed inside my_function()
    print(x)

x = 5  # This is a global variable
my_function()  # Output: 10
print(x)  # Output: 5
```

It's important to note that if a variable is modified inside a function that has the same name as a global variable, Python will create a new local variable with the same name, leaving the global variable unchanged.

```python
x = 5

def my_function():
    x = 10  # This creates a new local variable 'x', different from the global 'x'
    print(x)  # Output: 10

my_function()
print(x)  # Output: 5 (global variable is not affected)
```

Understanding variables and their scope is essential for writing clean and efficient code in Python.

Comments

Popular posts from this blog

Gujarati Keyboard layout (terafont-varun), Computer Short cut key, Tally short cut key

Word , Excel , Power Point Shortcut Key in Gujarati

Terafont-Varun (Gujarati Typing) Keyboard Layout by "Sama Soyab"

  For Gujarati Typing : Required : Terafont-Varun Font  After Successfully Installed Terafont Varun Open Any Text Editor or any program. Select Font Terafont-Varun -> Ok For more detail please watch below video. Search Topics : Learn terafont varun, Learn terafont chandan, Learn terafont gujarati to english translation, Learn terafont varun keyboard, Learn terafont converter, Learn terafont varun zip, Learn terafont keyboard, Learn terafont kinnari, Learn terafont akash, Learn terafont aakash, Learn terafont akash ttf, Learn terafont aakash gujarati download, Learn terafont akash keyboard, Learn terafont akash download for windows 10, Learn terafont akash font download, Learn terafont arun, Learn terafont border, Learn terafont chandan keyboard, Learn terafont-chandan font, Learn tera font chandana, Learn convert terafont to shruti, Learn convert terafont varun to shruti, Learn terafont varun chart, Learn terafont download, Learn terafont download for windows 10, Learn tera...