
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10400 Articles for Python

23K+ Views
Variables are defined as the containers used to store some data. They represent a memory location. Any type of data or value can be stored in a variable in Python, including integers, strings, float, Boolean etc. In Python, a variable's data type does not need to be specified when it is defined in a program. However, before any function or application can use variables, they must be defined first. It can be done by simply assigning a value to a name as shown below - x = 5 Here, x is the name of a variable. Also, since x ... Read More

3K+ Views
We will learn about what an exception is before learning how to print Python exceptions. An exception occurs when a program fails to execute in the direction it was intended to be. Python throws exceptions when unexpected errors or events occur. It is common for exceptions to be both valid and invalid. Errors and exceptional conditions can be managed in a program through exceptions in a variety of ways. The exception handling technique can be used when you suspect your code may create an error. This can prevent the software from crashing. Following are some common Exceptions in Python - IOError ... Read More

198 Views
When writing a C extension for Python, you might need to raise an exception if something goes wrong in your C code. Python provides a special C API that helps you to raise errors in a way that works just like regular Python exceptions.This is helpful because even if your code is written in C for better performance, users can still handle errors using Python's normal try...except blocks. It makes your extension feel just like any other Python module.Using Python C API to Raise ExceptionsIn C extensions for Python, you can raise exceptions using the Python C API functions like ... Read More

4K+ Views
The purpose of a function is to perform a specific task using code blocks. In programming, functions save time by eliminating unnecessary and excessive copying and pasting of code. Hence, a function will be of great use if there is a common action that needs to be performed in different places and often. The only thing you will need to do is update that specific function, if you want to make a change. As a result, you no longer have to copy and paste the same piece of code that is scattered throughout your program in order to find ... Read More

6K+ Views
In Python, functions are usually called by passing none, one or more arguments to it. There are two types of arguments that can be used - Positional arguments Keyword arguments Positional arguments depend on the order they are passed to a function. They are the more common type of arguments used in programming. Keyword arguments, on the other hand, are passed as ... Read More

20K+ Views
As the name implies, an argument with a variable length can take on a variety of values. You define a variable argument using a '*', for example *args, to show that the function can take a variable number of arguments. Observations on Python's variable-length arguments are as follows - The designation "*args" for variable length arguments is not required. The only thing needed is *; the variable name can be anything, like *names or *numbers. You can send zero or more arguments to a function using a variable length argument. ... Read More

14K+ Views
Python functions are used to implement logic that you may want to reuse in multiple places in your code. These functions accept input parameters called arguments. You can also assign default values to these parameters. If you do not pass any value for a parameter during a function call, the default value will be used automatically. Default values are assigned using the assignment operator (=) in the format param=value. The syntax representation and default values for function parameters are different in Python. If no argument value is given during the function call, the default values mean that the function parameter ... Read More

52K+ Views
In Python, you can pass optional parameters to functions, allowing you to call a function with fewer arguments than specified. Optional parameters are useful for providing default values when certain arguments are not provided. Python provides different ways to define optional parameters, including default values, variable-length argument lists, and keyword arguments. Using Default Arguments One common way to pass optional parameters is by setting default values for the parameters in the function definition. If no argument is passed for those parameters during the function call, the default value is used. Example In the following example, the greet() function has an ... Read More

697 Views
In Python, when you pass arguments to a function, they are passed by object reference. This means the function gets a reference (or pointer) to the actual object, not a copy of it. However, how this reference affects the object depends on whether the object is mutable or immutable. Mutable objects (like lists, dictionaries, and sets) can be changed inside the function. If you modify a mutable object inside the function, the changes will affect the original object outside the function as well. Immutable objects (like numbers, strings, and tuples) cannot be changed. If you try to modify ... Read More

654 Views
Yes, in Python, you can explicitly define the datatype of function parameters and return values using type hints or type annotations. Even if you specify the data types using type hints, Python will still run the code even when the wrong types are passed. The execution of the program will not be interrupted, and an error will not be raised.To find these type-related mistakes, you can use tools like mypy or pyright that check your code before it runs. Using Type Hints in Python Functions To add type hints to a parameter of a function, you need to specify the ... Read More