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 –
- Multiple Variable Assignment
- Python allows to initialization of more than one variable in a single statement.
- For example –
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).
- These data types are used to represent numbers. Python supports integers, floating-point numbers, and complex numbers.
-
- 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
- These data types hold collections of items, which can be accessed in an ordered fashion.
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.
- A mapping data type represents a collection of key-value pairs. These are the following datatypes –
-
- 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.
- Sets datatype are unordered collections of unique elements. These are –
-
- 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.
- These types handle binary data such as images, files, etc. These are –
-
- 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.
- None: Used to define a variable with no value or to signify an empty return. For example:- x = None # Here, x is a NoneType.
- It represents the absence of a value or a null value.
8. Callable Types
-
- Objects that can be called as functions, such as:
-
Functions: It is defined using the ‘def’ or ‘lambda’ keyword. For example :-
-
- Objects that can be called as functions, such as:
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-
- Implicit/Automatic Type Casting:
-
-
- 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 –
-
-
- 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 –
- Explicit Type Casting:
0 Comments