# chinchumathews94/variables

a year ago

## Variables

Variables are containers for storing data values.In other words, a variable in a python program gives data to the computer for processing.

Every value in Python has a datatype. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc. Variables can be declared by any name or even alphabets like a, aa, abc, etc.

Let's see an example...

In [8]:
``````x = 100
y = "FSEAI"
print(x)
print(y)

``````
```100 FSEAI ```

Variables do not need to be declared with any particular type and can even change type after they have been set

### Re-declare a Variable

You can re-declare the variable even after you have declared it once.

Here we have variable initialized to A = 10

In [16]:
``````A = 10
print(A)
``````
```10 ```

Later, we re-assign the variable A to 20

In [12]:
``````A = 20
print(A)
``````
```20 ```

### Variable Names

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables:

• A variable name must start with a letter or the underscore character
• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are three different variables)

Remember that variable names are case-sensitive

### Assign Value to Multiple Variables

Python allows you to assign values to multiple variables in one line:

In [14]:
``````x, y, z = "FullStack", "Engineering", "AI"
print(x)
print(y)
print(z)
``````
```FullStack Engineering AI ```

And you can assign the same value to multiple variables in one line:

In [17]:
``````x = y = z = "FullStack Engineering AI"
print(x)
print(y)
print(z)
``````
```FullStack Engineering AI FullStack Engineering AI FullStack Engineering AI ```

### Concatenate Variables

Let's see whether you can concatenate different data types like string and number together. For example, we will concatenate "Helloo" with the number "127". Unlike Java, which concatenates number with string without declaring number as string, Python requires declaring the number as string otherwise it will show a TypeError

In [20]:
``````a="Helloo"
b = 127
print(a + str(b))``````
```Helloo127 ```

Unlike Java, which concatenates number with string without declaring number as string, Python requires declaring the number as string otherwise it will show a TypeError

### Global Variables

Variables that are created outside of a function (as in all of the examples above) are known as global variables.

Global variables can be used by everyone, both inside of functions and outside.

Let's see an example

In [22]:
``````x = "AI"

def myfunc():
print("Fullstack Engineering " + "" + x)

myfunc()
``````
```Fullstack Engineering AI ```

If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.

In [27]:
``````x = "AI"

def myfunc():
x = "Awesome"
print("FSEAI is "+ x)

myfunc()

print("Fullstack Engineering "+ x)``````
```FSEAI is Awesome Fullstack Engineering AI ```

### The global Keyword

Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function.

To create a global variable inside a function, you can use the global keyword.

In [28]:
``````def myfunc():
global x
x = "AI"

myfunc()

print("FullStack Engineering " + x)``````
```FullStack Engineering AI ```

If you use the global keyword, the variable belongs to the global scope:

Also, use the global keyword if you want to change a global variable inside a function; for example

In [34]:
``````x = "AI"

def myfunc():
global x
x = "FSEAI is fantastic"

myfunc()

print("Fullstack Engineering " + x)``````
```Fullstack Engineering FSEAI is fantastic ```
In [ ]:
`` ``
In [ ]:
`` ``