Found 27759 Articles for Server Side Programming

How do I check if a Python variable exists?

Alekhya Nagulavancha
Updated on 22-Feb-2023 17:43:21

14K+ 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

How to print the Python Exception/Error Hierarchy?

Alekhya Nagulavancha
Updated on 24-Feb-2023 11:18:30

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. Common Exceptions IOError (Input Output Error) − ... Read More

How to raise Python exception from a C extension?

Rajendra Dharmkar
Updated on 27-Sep-2019 07:58:58

120 Views

For the above module, we need to prepare following setup.py script −from distutils.core import setup, Extension setup(name='helloworld', version='1.0', \ ext_modules=[Extension('helloworld', ['hello.c'])])Now, we use the following command,$ python setup.py installOnce we install the extension, we would be able to import and call that extension in our Python script test.py and catch the exception in it as follows −#test.py import helloworld try: print helloworld.helloworld() except Exception as e: print str(e)This would produce the following result −bad format char passed to Py_BuildValue

How to call a function with argument list in Python?

Alekhya Nagulavancha
Updated on 22-Feb-2023 17:47:07

3K+ 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'll 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 it. This ... Read More

How to pass keyword parameters to a function in Python?

Alekhya Nagulavancha
Updated on 24-Feb-2023 11:14:02

5K+ 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 languages. Whereas, keyword arguments are passed as key-value pairs so that the arguments are mapped to a key using the “=” operator which allows the Python interpreter to ignore the positions. In this article, we will be discussing about keyword parameters/arguments and how to pass ... Read More

How to use variable-length arguments in a function in Python?

Pranathi M
Updated on 16-Sep-2022 07:28:09

15K+ 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. A tuple is used to store the values passed to ... Read More

How to set default parameter values to a function in Python?

Pranathi M
Updated on 16-Sep-2022 07:38:48

13K+ Views

Python functions are used to implement logic that you wish to run repeatedly at various locations across your code. These functions accept function arguments as input parameters. You can specify default argument values in Python functions in addition to supplying parameters to them through function calls. If you don't explicitly specify a parameter value to the given argument, these default values are applied to function arguments. The actual values supplied to function arguments are called parameters. The syntax representation and default values for function parameters are different in Python. If no argument value is given during the function call, default ... Read More

How to pass optional parameters to a function in Python?

Pranathi M
Updated on 23-Aug-2023 21:54:29

48K+ Views

A type of argument with a default value is a Python optional parameter. A function definition's assignment operator or the Python **kwargs statement can be used to assign an optional argument. Positional and optional arguments are the two sorts of arguments that a Python function can take. Values that are not required to be given in order to call a function are known as optional parameters. Function in Python A function is a section of code that only executes when called. You can supply parameters that is data to a function. As a result, a function may or may not ... Read More

How to pass arguments by value in Python function?

Manogna
Updated on 27-Sep-2019 07:52:12

566 Views

For given code the output is as followsb = 30 a = ['10']In this case, "a" seems to be passed by value, because the value is  unchanged even after the call to the function. So it is clear that the arguments have been passed by value in the python function.

Can we explicitly define datatype in a Python Function?

Manogna
Updated on 13-Feb-2020 06:27:43

462 Views

In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally. In Java, C++, and other statically-typed languages, you must specify the datatype of the function return value and each function argument.If we explicitly define datatype in a python function, it still works like a normal program where data type is not declared explicitly.ExampleWe get the following output for the given codeC:/Users/TutorialsPoint1/~.py The required Sum is:  13.0Consider this functiondef addSum(x, y):        return x+y print addSum(2.2, 5.6) print addSum(float(2.2), float(5.6))Output7.8 7.8So the declaration of the datatype of a ... Read More

Advertisements