Variable and Data Type


    
    
        

Data Type

There are several data types for Python:

Data Type Categories
Text Type str
Numeric Type int, float, complex
Sequence Type list, tuple, range
Mapping Type dict
Set Type set, frozenset
Boolean Type bool
Binary Type bytes, bytearray, memoryview
None Type NoneType


        

To get a data type in Python, we use the type() function. Example:

x=6 print(type(x)) #the output will be int

Variable Name

The general rules for naming variables are:

  • Names can contain letters, digits, and underscores
  • Names must begin with a letter or an underscore (_)
  • Names are case-sensitive (lowercase and uppercase letters have different meanings)
  • Names cannot contain whitespaces or special characters ($, #, %, etc.)
  • Names cannot contain reserved words, such as int, float, etc.

Creating Variable

Creating variables also known as declaring variables is an action of making a variable to store data.

Example: x = 5

Assign Value to Variable

Assigning value means storing a value in a variable. You can assign a value when declaring a variable.

Syntax: variable_name = value;

Example:

x = 5 y = 'me'

        

To change a variable value, we just need to assign a new value to an existing variable. The new assigned value will overwrite the previous value.

Example:

x = 20 #the value of x is 20 x = 100 #the value of x is now 100

        

We can also copy the value of a variable to another variable.

Syntax: variable_name1 = variable_name2;

Example:

x = 10 y = x #the value of y becomes 10


In Python, the data type is set automatically when we assign a value to a variable.

Example Data Type
x = 'Hello' str
x = 20 int
x = 10.9 float
x = 3j complex
x = [1,2,3,5] list
x = (1,2,3) tuple
x = range(9) range
x = {"name" : "John", "age" : 19} dict
x = {1,2,3} set
x = frozenset({1,2,3}) frozenset
x = bytearray(5) bytearray
x = True bool
x = memoryview(bytes(5)) memoryview
x = None NoneType

Casting

Casting, also known as type conversion is an action of converting a value of one data type to another data type. For example, the conversion of int to float or vice versa. There are two types of conversion in Python:

  1. Implicit Conversion
  2. This conversion occurs automatically when we assign a value to a variable.

    Example:

                  
    x = 8 #x is automatically set to int
    
    
                
  3. Explicit Conversion
  4. This conversion is done manually by using constructor functions. There are three well-known constructor functions:

    • int() - constructs an integer number
    • float() - constructs a float number
    • str() - constructs a string

    Example:

    x = int(9.12) #x will be 9 y = float(8) #y will be 8.0 z = str(100) #z will be '100'


Variable Scope

Scope is the area in which a variable can be used or can be accessed. For Python variables, there are 2 types of scopes, local and global. Local variable exists and can only be accessed in a particular function, while global variable can be accessed by all functions in the entire program.

Example:

x = "Hello!" #this is a global variable def myFunc(){ y = "Hi!" #this is a local variable }

We can also use the global keyword to make a local variable become global variable.

Example:

def myFunc(){ global y y = "Hi!" #y is now a global variable }