What is a Variable in Python?
A Python variable is a reserved memory location to store values. In other words, a
variable in a python program gives data to the computer for processing.
Python Variable Types
Every value in Python has a datatype. Different data types in Python are Numbers,
List, Tuple, Strings, Dictionary, etc. Variables in Python can be declared by any
name or even alphabets like a, aa, abc, etc.
How to Declare and use a Variable
Let see an example. We will define variable in Python and declare it as “a” and
print it.
a=100
print (a)
1.How to Declare and use a Variable
To declare a variable we use “=” to assign the value to a variable.
Example:
We will declare the following variable and print it.
Number= 25
Name = “Kiran”
B= 3.5
print (Number)
print(Name)
print (B)
2.Re-declare a Variable
You can re-declare Python variables even after you have declared once.
# Declare a variable and initialize it
f=0
print f
# re-declaring the variable works
f = 'food'
print f
3. Multiple Assignment
In Python, we can assign the same value to multiple variables at once.
Example:
x = y = z = "SoftwareTestingHelp"
print (x)
print (y)
print (z)
We can also assign multiple values to multiple variables.
Example:
a, b, c = 5, 3.2, "Hello"
print (a)
print (b)
print (c)
4. Print function
print("Hello World")
Example :
If you want to print the name of five countries, you can write:
print("USA")
print("Canada")
print("Germany")
print("France")
print("Japan")
5. How to print blank lines
Sometimes you need to print one blank line in your Python program. Following is
an example to perform this task using Python print format.
Example:
Let us print 8 blank lines. You can type:
print (8 * "\n")
or
print ("\n\n\n\n\n\n\n\n\n")
Ex2 check the following
print ("Welcome to python “)
print (8 * "\n")
print ("Welcome to python")
6.Print end command
By default, print function in Python ends with a newline. This function comes with
a parameter called ‘end.’
print("Python" , end = '@')
Output:
Python@
print ("Welcome to", end = ' ')
print ("FOOD Science", end = '!')
Output:
Welcome to FOOD Science !
7.Python String Concatenation and Variable
Let’s see whether you can concatenate different data types like string and number
together. For example, we will concatenate “Guru” with the number “99”.
a="Guru"
b = 99
print a+b
Once the integer is declared as string, it can concatenate both “Guru” + str(“99”)=
“Guru99” in the output.
a="Guru"
b = 99
print(a+str(b))
8. Python Variable Types: Local & Global
There are two types of variables in Python, Global variable and Local variable.
When you want to use the same variable for rest of your program or module you
declare it as a global variable, while if you want to use the variable in a specific
function or method, you use a local variable while Python variable declaration.
Let’s understand this Python variable types with the difference between local and
global variables in the below program.
1. Let us define variable in Python where the variable “f” is global in scope
and is assigned value 101 which is printed in output
2. Variable f is again declared in function and assumes local scope. It is
assigned value “I am learning Python.” which is printed out as an output.
This Python declare variable is different from the global variable “f” defined
earlier
3. Once the function call is over, the local variable f is destroyed. At line 12,
when we again, print the value of “f” is it displays the value of global
variable f=101
# Declare a variable and initialize it
f = 101
print(f)
# Global vs. local variables in functions
def someFunction():
# global f
f = 'I am learning Python'
print(f)
someFunction()
print(f)
Delete a variable
You can also delete Python variables using the command del “variable name”.
In the below example of Python delete variable, we deleted variable f, and when
we proceed to print it, we get error “variable name is not defined” which means
you have deleted the variable.
Example of Python delete variable or Python clear variable :
f = 11;
print(f)
del f
print(f)
10. Properties of variables
It can be only one word.
It can use only letters, numbers, and the underscore (_) character.
It can’t begin with a number.
Variable name starting with an underscore (_) are considered as “unuseful”.
11. How to include the comment about program
# This is a comment
Multiline comments
# This is a
# multiline comment
Code with a comment:
a = 1 # initialization
12. The print() Function
The print() function writes the value of the argument(s) it is given. […] it handles
multiple arguments, floating point-quantities, and strings. Strings are printed
without quotes, and a space is inserted between items, so you can format things
nicely:
>>> print('Hello world!')
# Hello world!
13. The input() Function
This function takes the input from the user and converts it into a string:
>>> print('What is your name?') # ask for their name
>>> my_name = input()
>>> print('Hi, {}'.format(my_name))
14. The str(), int(), and float() Functions
These functions allow you to change the type of variable. For example, you can
transform from an integer or float to a string: