Python Tutorials
Python File Handling
Python Modules
Variables are data storage containers.
Python has no dynamic declaration command.
Variables are created when you first give value to it.
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.
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
If you want to specify a variant data type, this can be done by streaming.
x =
str(3) # x will be '3'
y = int(3) # y
will be 3
z = float(3) # z will be 3.0
You can get the type of data variable with the type()
function.
x = 5
y = "John"
print(type(x))
print(type(y))
Cable variables can be announced using one or two quotes:
x = "John"
# is the same as
x =
'John'
Flexible words are very sensitive.
a = 4
A =
"Sally"
#A will not overwrite a
Variables can have a shorter word (such as x and y) or a more descriptive word (age, carname, total_ volume). Python flexibility rules:
Legal variable names:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
Illegal variable names:
2myvar = "John"
my-var = "John"
my var = "John"
Remember that flexible words are very sensitive
Flexible words with more than one word can be difficult to read.
There are several strategies you can use to make it more readable:
Each word, except the first one, begins with a capital letter:
myVariableName = "John"
Each word begins with a capital letter:
Each word is separated by an underscore character:
my_variable_name = "John"
Python lets you assign multiple values to a single line:
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.
You can also assign the same number of multiple variables in a single line:
x = y = z = "Orange"
print(x)
print(y)
print(z)
If you have a set of values in the list, tuple etc. Python lets you extract values into variables. This is called disassembly.
Unpack a list:
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)
Python print
statement is often used for flexible output.
Combining both text and flexibility, Python uses the character +
:
x = "awesome"
print("Python is " + x)
You can also use the character +
to add variables to other variables:
x = "Python is "
y = "awesome"
z = x + y
print(z)
In numbers, the character +
acts as a mathematical operator:
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:
x = 5
y = "John"
print(x + y)
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.
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.
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)
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.
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.
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)