HudaTutorials.com

Python Variables - How to Declare Variables in Python

Last updated on

Variables in Python

Python Variables

A Python Variable is a reserved memory location to store values. A variable in a Python program gives data to the computer for processing. Every value in Python has a data type.

Python is completely object oriented, and not statically typed. You do not need to declare variables before using them, or declare their type. When you assign a value to a variable in Python, it will declare variable type automatically by assigned variable value. Every variable in Python is an object.

What is Python Variable ?

A Python variable is a symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object by that name.

How to Creating Variables or Declaring Variables in Python ?

Variables are containers for storing data values. Unlike other programming languages, Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.

Variables do not need to be declared with any particular data type, and can even change type after they created.

What are the rules for Creating a Variable in Python ?

The rules and conventions for naming variables in Python programming language can be summarized as follows :

  1. A variable name can have a short name or a more descriptive name.
  2. A variable name must start with a letter or the underscore character.
  3. A variable name cannot start with a number.
  4. A variable name can only contain alpha numeric characters and underscores (A-z, 0-9, and _ )
  5. Variable names are case sensitive. For example age, Age and AGE are three different variables .

Let's start learning, how to declare variables in Python and also how to assign values to Python variables.

How to declare variables in Python ?

In Python programming language variable declaration is not necessary. In many programming languages, variables are statically typed. That means a variable is initially declared to have a specific data type, and any value assigned to it during its lifetime must always have that type.

Variables in Python are not subject to this restriction, a variable may be assigned a value of one type and then later re assigned a value of a different type in Python.

In the following example we first create variables with assigning a value to a variable. In Python, the data type is set when you assign a value to a variable.

How to create and assign a value to int data type variable in Python ?

Creating and Assigning a value to int data type variable in Python Example

# CREATING A PYTHON int DATA TYPE VARIABLE BY ASSIGNING A VALUE TO VARIABLE
my_int_variable = 123
print(my_int_variable)
                    

How to create and assign a value to float data type variable in Python ?

Creating and Assigning a value to float data type variable in Python Example

# CREATING A PYTHON float VARIABLE BY ASSIGNING A VALUE TO VARIABLE
my_float_variable = 123.55
print(my_float_variable)
                    

How to create and assign a value to complex data type variable in Python ?

Creating and Assigning a value to complex data type variable in Python Example

# CREATING A PYTHON complex VARIABLE BY ASSIGNING A VALUE TO VARIABLE
my_complex_variable = 1j
print(my_complex_variable)
                    

How to create and assign a value to bool data type variable in Python ?

Creating and Assigning a value to bool data type variable in Python Example

# CREATING A PYTHON bool VARIABLE BY ASSIGNING A VALUE TO VARIABLE
my_bool_variable = True
print(my_bool_variable)
                    

How to create and assign a value to str data type variable in Python ?

Creating and Assigning a value to str data type variable in Python Example

# CREATING A PYTHON str VARIABLE BY ASSIGNING A VALUE TO VARIABLE
my_str_variable = "Python Variables Tutorial"
print(my_str_variable)
                    

How to create and assign a value to list data type variable in Python ?

Creating and Assigning a value to list data type variable in Python Example

# CREATING A PYTHON list VARIABLE BY ASSIGNING A VALUE TO VARIABLE
my_list_variable = ["Python", "Java", "HTML5"]
print(my_list_variable)
                    

How to create and assign a value to tuple data type variable in Python ?

Creating and Assigning a value to tuple data type variable in Python Example

# CREATING A PYTHON tuple VARIABLE BY ASSIGNING A VALUE TO VARIABLE
my_tuple_variable = ("Name", "Age", "Address")
print(my_tuple_variable)
                    

How to create and assign a value to dict data type variable in Python ?

Creating and Assigning a value to dict data type variable in Python Example

# CREATING A PYTHON dict VARIABLE BY ASSIGNING A VALUE TO VARIABLE
my_dict_variable = {"Name": "HudaTutorials.com", "Age": 10}
print(my_dict_variable)
                    

How to create and assign a value to set data type variable in Python ?

Creating and Assigning a value to set data type variable in Python Example

# CREATING A PYTHON set VARIABLE BY ASSIGNING A VALUE TO VARIABLE
my_set_variable = {"Apple", "Banana", "Cherry"}
print(my_set_variable)
                    

How to create and assign a value to bytes data type variable in Python ?

Creating and Assigning a value to bytes data type variable in Python Example

# CREATING A PYTHON bytes VARIABLE BY ASSIGNING A VALUE TO VARIABLE
my_bytes_variable = b"This is Bytes Variable"
print(my_bytes_variable)
                    

How to Create Variables using Data Type in Python ?

Now we are creating Python variables with specifying particular data type. If you want to specify the data type, you should use constructor functions for each data type.

How to create int variable using int data type in Python ?

Create int variable using int data type in Python Example

# CREATE A PYTHON int VARIABLE BY USING int data type
my_int_variable = int(123)
print(my_int_variable)
                    

How to create float variable using float data type in Python ?

Create float variable using float data type in Python Example

# CREATE A PYTHON float VARIABLE BY USING float data type
my_float_variable = float(123.55)
print(my_float_variable)
                        

How to create complex variable using complex data type in Python ?

Create complex variable using complex data type in Python Example

# CREATE A PYTHON complex VARIABLE BY USING complex data type
my_complex_variable = complex(1j)
print(my_complex_variable)
                        

How to create bool variable using bool data type in Python ?

Create bool variable using bool data type in Python Example

# CREATE A PYTHON bool VARIABLE BY USING bool data type
my_bool_variable = bool(True)
print(my_bool_variable)
 
# ALSO DECLARE WITH NUMBERS 0 ZERO IS False, OTHER THAN 0 ZERO IS True
# RETURNS True
my_bool_variable = bool(7)
print(my_bool_variable)
 
# RETURNS False
my_bool_variable = bool(0)
print(my_bool_variable)
                        

How to create str variable using str data type in Python ?

Create str variable using str data type in Python Example

# CREATE A PYTHON str VARIABLE BY USING str data type
my_str_variable = str("Python Variables Tutorial")
print(my_str_variable)
                        

How to create list variable using list data type in Python ?

Create list variable using list data type in Python Example

# CREATE A PYTHON list VARIABLE BY USING list data type
my_list_variable = list(("Python", "Java", "HTML5"))
print(my_list_variable)
                        

How to create tuple variable using tuple data type in Python ?

Create tuple variable using tuple data type in Python Example

# CREATE A PYTHON tuple VARIABLE BY USING tuple data type
my_tuple_variable = tuple(("Name", "Age", "Address"))
print(my_tuple_variable)
                        

How to create dict variable using dict data type in Python ?

Create dict variable using dict data type in Python Example

# CREATE A PYTHON dict VARIABLE BY USING dict data type
my_dict_variable = dict(Name="HudaTutorials.com", Age=10)
print(my_dict_variable)
                        

How to create set variable using set data type in Python ?

Create set variable using set data type in Python Example

# CREATE A PYTHON set VARIABLE BY USING set data type
my_set_variable = set(("Apple", "Banana", "Cherry"))
print(my_set_variable)
                        

How to create bytes variable using bytes data type in Python ?

Create bytes variable using bytes data type in Python Example

# CREATE A PYTHON bytes VARIABLE BY USING bytes data type
# DECLARING bytes VARIABLE
my_bytes_variable = bytes(20)
print(my_bytes_variable)
 
# DECLARING AND ASSIGNING VALUE TO bytes VARIABLE
my_bytes_variable = bytes(b"This is Bytes Variable")
print(my_bytes_variable)
                        

How to create range variable using range data type in Python ?

Create range variable using range data type in Python Example

# CREATE A PYTHON range VARIABLE BY USING range data type
# BY DEFAULT RANGE FROM VALUE IS ZERO
my_range_variable = range(7)
print(my_range_variable)
 
# ANOTHER WAY TO CREATE A PYTHON range VARIABLE BY USING range data type
my_range_variable = range(1, 10)
print(my_range_variable)
                        

How to create frozenset variable using frozenset data type in Python ?

Create frozenset variable using frozenset data type in Python Example

# CREATE A PYTHON frozenset VARIABLE BY USING frozenset data type
my_frozenset_variable = frozenset(("Apple", "Banana", "Cherry"))
print(my_frozenset_variable)
                        

How to create bytearray variable using bytearray data type in Python ?

Create bytearray variable using bytearray data type in Python Example

# CREATE A PYTHON bytearray VARIABLE BY USING bytearray data type
my_bytearray_variable = bytearray(5)
print(my_bytearray_variable)
                        

How to create memoryview variable using memoryview data type in Python ?

Create memoryview variable using memoryview data type in Python Example

# CREATE A PYTHON memoryview VARIABLE BY USING memoryview data type
my_memoryview_variable = memoryview(bytes(5))
print(my_memoryview_variable)