Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Mohd Mohtashim
Page 3 of 19
Destroying Objects (Garbage Collection) in Python
Python deletes unneeded objects (built-in types or class instances) automatically to free the memory space. The process by which Python periodically reclaims blocks of memory that no longer are in use is termed Garbage Collection. Python's garbage collector runs during program execution and is triggered when an object's reference count reaches zero. An object's reference count changes as the number of aliases that point to it changes. How Reference Counting Works An object's reference count increases when it is assigned a new name or placed in a container (list, tuple, or dictionary). The object's reference count decreases ...
Read MoreCreating Instance Objects in Python
To create instances of a class, you call the class using class name and pass in whatever arguments its __init__ method accepts. Creating Instance Objects When you create an object, Python calls the class constructor (__init__ method) to initialize the new instance ? # This would create first object of Employee class emp1 = Employee("Zara", 2000) # This would create second object of Employee class emp2 = Employee("Manni", 5000) Accessing Object Attributes You access the object's attributes using the dot (.) operator with object. Class variables can be accessed using class name ? ...
Read MoreCreating Classes in Python
The class statement creates a new class definition. The name of the class immediately follows the keyword class followed by a colon as follows ? class ClassName: 'Optional class documentation string' class_suite The class has a documentation string, which can be accessed via ClassName.__doc__. The class_suite consists of all the component statements defining class members, data attributes and functions. Basic Class Structure A Python class contains attributes (data) and methods (functions). Here's the basic syntax ? ...
Read MoreOOP Terminology in Python
Object-Oriented Programming (OOP) in Python uses specific terminology to describe its concepts. Understanding these terms is essential for working with classes, objects, and inheritance in Python. Core OOP Terms Class A user-defined blueprint for creating objects that defines attributes and methods. Think of it as a template that describes what data and behaviors objects will have ? class Car: wheels = 4 # Class variable def __init__(self, brand): self.brand = brand # Instance ...
Read MoreArgument of an Exception in Python
An exception can have an argument, which is a value that provides additional information about the problem. The contents of the argument vary by exception. You capture an exception's argument by supplying a variable in the except clause as follows − Syntax try: # Your operations here pass except ExceptionType as argument: # You can access the exception argument here print(argument) If you write the code to handle a single exception, you can have a variable follow the name ...
Read MoreVariable-length arguments in Python
You may need to process a function for more arguments than you specified while defining the function. These arguments are called variable-length arguments and are not named in the function definition, unlike required and default arguments. Syntax Syntax for a function with non-keyword variable arguments is this − def functionname([formal_args, ] *var_args_tuple): "function_docstring" function_suite return [expression] An asterisk (*) is placed before the variable name that holds the values of all nonkeyword variable arguments. This tuple remains empty if no additional arguments ...
Read MoreRequired arguments in Python
Required arguments are the arguments passed to a function in correct positional order. Here, the number of arguments in the function call should match exactly with the function definition. To call the function printme(), you definitely need to pass one argument, otherwise it gives a syntax error as follows ? Example with Missing Required Argument # Function definition is here def printme(str): "This prints a passed string into this function" print(str) return # Now you can call printme function printme() When ...
Read MoreCalling a Function in Python
Defining a function only gives it a name, specifies the parameters that are to be included in the function and structures the blocks of code. Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt. Following is the example to call printme() function ? Example # Function definition is here def printme(str): """This prints a passed string into this function""" print(str) return # Now you can call printme ...
Read MoreBuilt-in Dictionary Functions & Methods in Python
Python dictionaries come with built-in functions and methods that make working with key-value pairs efficient and straightforward. These functions help you manipulate, query, and transform dictionary data. Built-in Dictionary Functions Python provides several built-in functions that work with dictionaries ? len() Function student = {'name': 'Alice', 'age': 20, 'grade': 'A'} print(len(student)) 3 str() Function student = {'name': 'Alice', 'age': 20} print(str(student)) print(type(str(student))) {'name': 'Alice', 'age': 20} type() Function student = {'name': 'Alice', 'age': 20} print(type(student)) ...
Read MoreProperties of Dictionary Keys in Python
Dictionary values have no restrictions. They can be any arbitrary Python object, either standard objects or user-defined objects. However, the same is not true for dictionary keys. There are two important points to remember about dictionary keys: No Duplicate Keys Allowed More than one entry per key is not allowed. When duplicate keys are encountered during assignment, the last assignment wins ? # Duplicate keys - last value overwrites previous ones student = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'} print("student['Name']:", student['Name']) print("Full dictionary:", student) student['Name']: Manni Full dictionary: {'Name': 'Manni', 'Age': 7} ...
Read More