Jovian
⭐️
Sign In

Variables

Variables are simply boxes or containers for storing data values.

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

Assign Values 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

concatenation

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

In Python we have to declaring the number as string otherwise it will show a TypeError

Global Variables

In [22]:
x = "AI"

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

myfunc()
Fullstack Engineering AI
In [27]:
x = "AI"    # Global variable

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

myfunc()

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

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