Variables in Python

  • A variable is a container that reserves memory to hold values of a specific data type. The value in a variable may change during the life of the program-hence the name “variable”.
  • Variable names in Python are case-sensitive i.e., Num and num are two different variables.
  • There is no need to explicitly declare the variable with any particular data type in Python because Python has no specific command for declaring a variable. Python variables automatically assign datatype in runtime.
  • Python is a dynamically typed programming language, meaning that Python variables don’t have a fixed/static/pre-defined data type. In Python, the data type is determined based on the assigned value at run time. For example – 
num = 100      # Here, num is an integer variable because it assigns an integer data.
num = 76.37     # Here, num is a float variable because it assigns float data.
num = “Robert”     # Here, num is a string variable that assigns string data.
  • Multiple Variable Assignment
    • Python allows to initialization of more than one variable in a single statement. 
    • For example –
(i)  a=b=c=40
     print (a,b,c)     Output: 40 40 40
(ii) a,b,c = 10,20,30
     print (a,b,c)    Output: 10 20 30

DataTypes in Python

  • Python is a strongly typed language i.e., it doesn’t allow automatic type conversion between unrelated data types. For example, a string cannot be converted to any number type. However, an integer can be cast into a float. Other languages such as JavaScript, a weakly typed languages, where an integer is converted into a string for concatenation.
  • In Python, data types are classifications that indicate how the interpreter uses the data and what operations can be performed on it.
  • Python has a rich set of built-in data types that can be broadly categorized into several classes. These are:-

1. Numeric Types

    • These data types are used to represent numbers. Python supports integers, floating-point numbers, and complex numbers.
      • int: It represents integer values, which are whole numbers, both positive and negative. For example :- x=20
      • float: It represents floating-point numbers/values (real numbers with decimals). For example :- y=20.32
      • complex: It represents complex numbers with real and imaginary parts. For example :- z = 2 + 3j# z is a complex number (2 is the real part, 3 is the imaginary part).
2. Sequence Types
    • These data types hold collections of items, which can be accessed in an ordered fashion.
      • str (String): This data type is a sequence of characters enclosed in single, double, or triple quotes. For example :- msg= “Hello, World!”# Here msg is a string.
      • list: It is a mutable (changeable) ordered collection of items. Lists can contain elements of different data types. For example :-  num = [10, 20, 30, “ten”, 5.0]   # Here num is a list.
      • tuple: It is an immutable (unchangeable) ordered collection of data items. For example :- x= (10, 20, 30)  # Here x is tuple

3. Mapping Types

    • A mapping data type represents a collection of key-value pairs. These are the following datatypes –
      • dict (Dictionary): It is a collection of key-value pairs. Here, keys must be unique, and values can be of any data type. For example :- student = {“name”: “Rohan”, “age”: 23, “courses”: [“Math”, “Physics”]}  # Here student is a dictionary datatype.
4. Set Types
    • Sets datatype are unordered collections of unique elements. These are –
      • set: It is a mutable, unordered collection of unique items. For example :- val = {1, 2, 2, 3, 4, 4, 5}  # Here ‘val’ is a set datatype, and duplicate data will be removed automatically.
      • frozenset: It is an immutable version of a set, i.e., an unchangeable, unordered collection of unique items. For example :- val = frozenset([1, 2, 3, 4])  # Here ‘val’ is a frozenset datatype.
5. Boolean Types
    • Represents one of two values: TrueorFalse.
      • bool: A data type representing Boolean values. For example :- status = True# Here status is a boolean data type.

6. Binary Types

    • These types handle binary data such as images, files, etc. These are – 
      • bytes: This data type is an Immutable sequence of bytes. For example :- x = b “Hello India”   # ‘x’ is a bytes type of data and ‘b’ represents a bytes type of data.
      • bytearray: It is a mutable sequence of bytes. For example :- ba = bytearray([65, 66, 67])   # Here, ba is a bytearray type and can be modified.
      • memoryview: This datatype allows viewing the memory of another object without copying. For example :- mv = memoryview(bytes(5))  # Here mv is a memoryview type of data.
7. None Type
    • It represents the absence of a value or a null value.
      • None: Used to define a variable with no value or to signify an empty return. For example:- x = None    # Here, x is a NoneType.

8. Callable Types

    • Objects that can be called as functions, such as:
      • Functions: It is defined using the ‘def’ orlambda’ keyword. For example :-
def msg():
return “Hello India”
Type Casting Variables in Python
  •  A type casting refers to converting an object of one type into another. It is of two types-
    • Implicit/Automatic Type Casting:
      • When any language compiler/interpreter automatically converts an object of one type into another, it is called automatic or implicit casting.
      • An integer object in Python occupies 4 bytes of memory, while a float object needs 8 bytes because of its fractional part contents. Hence, Python interpreter doesn’t automatically convert a float to int, because it will result in loss of data. On the other hand, int can be easily converted into a float by setting its fractional part to 0. For example-
x=40         # int object
y=50.5      # float object
z=x+y
print (z)         #Output : 90.5
      • In implicit type casting, a Python object with a lesser byte size is upgraded to match the bigger byte size of other objects in the operation. For example, a Boolean object is first upgraded to int and then to float, before the addition with a floating point object. For example –
x=True
y=40.5
z=x+y

print (z)     Output : 41.5  (Here, True is equal to 1, and False is equal to 0.)   
    • Explicit Type Casting: 
      • We use Python’s built-in functions int(), float(), and str() to perform explicit conversions such as string to integer. We can specify the data type of a variable as needed with the help of a suitable datatype in the casting process.
      • For example – 
x = str(10)     # In this case, x will be ’10’ i.e., String value.
y = int(10)     # In this case, y will be 10 i.e., Integer value.
z = float(10)    # In this case, z will be 10.0 i.e., Float value.

Loading

Categories: Python Theory

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.