Python Variables


Variables

Variables are data storage containers.


Creating Variables

Python has no dynamic declaration command.

Variables are created when you first give value to it.


Example
x = 5
y = "John"
print(x)
print(y)

Variables do not need to be declared in any particular format, and can change even genre after setup.


Example
x = 4       # x is of type int
x = "Sally" # x is now of type str
print(x)


Casting

If you want to specify a variant data type, this can be done by streaming.


Example
x = str(3)    # x will be '3'
y = int(3)    # y will be 3
z = float(3# z will be 3.0


Get the Type

You can get the type of data variable with the type() function.


Example
x = 5
y = "John"
print(type(x))
print(type(y))


Single or Double Quotes?

Cable variables can be announced using one or two quotes:


Example
x = "John"
# is the same as
x = 'John'


Case-Sensitive

Flexible words are very sensitive.


Example
a = 4
A = "Sally"
#A will not overwrite a




Python - Variable Names




Variable Names

Variables can have a shorter word (such as x and y) or a more descriptive word (age, carname, total_ volume). Python flexibility rules:

  • The variable word should start with a letter or underscore character
  • A different name cannot start with a number
  • A different name may contain only alpha-numeric characters and underscore (A-z, 0-9, and _)
  • The most sensitive variables are (age, age and AGE are three different variables)

Example

Legal variable names:

myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"


Example

Illegal variable names:

2myvar = "John"
my-var = "John"
my var = "John"


Remember that flexible words are very sensitive



Multi Words Variable Names

Flexible words with more than one word can be difficult to read.

There are several strategies you can use to make it more readable:

Camel Case

Each word, except the first one, begins with a capital letter:


myVariableName = "John"


Pascal Case

Each word begins with a capital letter:


MyVariableName = "John"


Snake Case

Each word is separated by an underscore character:


my_variable_name = "John"




Python Variables - Assign Multiple Values



Many Values to Multiple Variables

Python lets you assign multiple values ​​to a single line:


Example
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)

Note: Make sure the value of the variables is the same as the value of the values, otherwise you will get an error.



One Value to Multiple Variables

You can also assign the same number of multiple variables in a single line:


Example
x = y = z = "Orange"
print(x)
print(y)
print(z)


Unpack a Collection

If you have a set of values ​​in the list, tuple etc. Python lets you extract values ​​into variables. This is called disassembly.


Example

Unpack a list:

fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)




Python - Output Variables



Output Variables

Python print statement is often used for flexible output.

Combining both text and flexibility, Python uses the character +:


Example
x = "awesome"
print("Python is " + x)

You can also use the character + to add variables to other variables:


Example
x = "Python is "
y = "awesome"
z =  x + y
print(z)

In numbers, the character + acts as a mathematical operator:


Example
x = 5
y = 10
print(x + y)

If you try to combine a unit of letters with a number, Python will give you an error:


Example
x = 5
y = "John"
print(x + y)




Python - Global Variables



Global Variables

The variables created outside of work (as in all the examples above) are known as global variables.

Global variables can be used by everyone, both internally and externally.


Example

Create a variable outside of a function, and use it inside the function

x = "awesome"

def myfunc():
  print("Python is " + x)

myfunc()

If you create variables with the same name within a function, these variations will be local, and can only be used within the function. Global variation of the same name will remain as it was, global and real value.


Example

Create a variable inside a function, with the same name as the global variable

x = "awesome"

def myfunc():
  x = "fantastic"
  print("Python is " + x)

myfunc()

print("Python is " + x)


The global Keyword

Usually, when you create a variable within a function, that variation is local, and can only be used within that function

To create a global dynamic within a function, you can use a global keyword.


Example

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

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

myfunc()

print("Python is " + x)

Also, use a global keyword if you want to change the global variable within a function.


Example

To change the value of a global variable inside a function, refer to the variable by using the global keyword:

x = "awesome"

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

myfunc()

print("Python is " + x)