Found 10805 Articles for Python

Differences between Python 2.x and Python 3.x?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

1K+ Views

There is always a debate in the coding community on which python version was the best one to learn: Python 2.x or Python 3.x.Below are key differences between pyton 2.x and python 3.x1. The print functionIn python 2.x, “print” is treated as a statement and python 3.x explicitly treats “print” as a function. This means we need to pass the items inside your print to the function parentheses in the standard way otherwise you will get a syntax error.#Python 2.7 print 'Python', python_version() print 'Hello, World!' print('Hello, World!') print "text", ; print 'some more text here'OutputPython 2.7.6 Hello, World! ... Read More

Packing and Unpacking Arguments in Python?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

826 Views

If you have done little-bit of programming in python, you have seen the word “**args” and “**kwargs” in python functions. But what exactly they are?The * and ** operators did different operations which are complementary to each other depending on where we are using them.So when we are using them in method definition, like -def __init__(self, *args, **kwargs): passAbove operation is called ‘packing” as it pack all the arguments into one single variable that this method call receives into a tuple called args. We can use name other than args, but args is the most common and pythonic way of ... Read More

Mathematical Functions in Python?

George John
Updated on 30-Jul-2019 22:30:26

1K+ Views

From doing simple to complex mathematical operations(like trigonometric, logarithmic operations etc) in python we may need to use the math() module.The python math module is used to access mathematical functions. All methods of math() function are used for integer or real type objects but not for complex numbers.To use this function, we need to import it in our codeimport mathConstantsWe use these constants for calculation in python -ConstantsDescriptionsPiReturn the value of pi: 3.141592EReturn the value of natural base e. e is 0.718282tauReturns the value of tau. tau = 6.283185infReturns the infinitenanNot a number typeNumbers and Numeric RepresentationPython provides different functions ... Read More

Complex Numbers in Python?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

16K+ Views

A complex number is created from real numbers. Python complex number can be created either using direct assignment statement or by using complex () function.Complex numbers which are mostly used where we are using two real numbers. For instance, an electric circuit which is defined by voltage(V) and current(C) are used in geometry, scientific calculations and calculus.Syntaxcomplex([real[, imag]])Creating a simple complex number in python>>> c = 3 +6j >>> print(type(c)) >>> print(c) (3+6j) >>> >>> c1 = complex(3, 6) >>> print(type(c1)) >>> print(c1) (3+6j)From above results, we can see python complex numbers are of type complex. Each complex ... Read More

Command Line and Variable Arguments in Python?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

453 Views

Command Line ArgumentsCommand line arguments are input parameters which allow user to enable programs to act in a certain way like- to output additional information, or to read data from a specified source or to interpret the data in a desired format.Python Command Line ArgumentsPython provides many options to read command line arguments. The most common ways are -Python sys.argvPython getopt modulePython argparse modulePython sys moduleThe sys module stores the command line arguments (CLA) into a list and to retrieve it, we use sys.argv. It’s a simple way to read command line arguments as string.import sys print(type(sys.argv)) print('The command ... Read More

How to write an empty function in Python?

AmitDiwan
Updated on 11-Aug-2022 12:05:44

2K+ Views

In this article, we will see how we can create an empty functions in Python. A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ). Here, we will see examples of Empty Functions. Empty Function Use the pass statement to write an empty function in Python −Example # Empty function in Python def demo(): pass Above, ... Read More

Reloading modules in Python?

AmitDiwan
Updated on 16-Aug-2022 12:12:47

17K+ Views

The reload() is used to reload a previously imported module or loaded module. This comes handy in a situation where you repeatedly run a test script during an interactive session, it always uses the first version of the modules we are developing, even we have made changes to the code. In that scenario we need to make sure that modules are reloaded. The argument passed to the reload() must be a module object which is successfully imported before. Few points to understand, when reload() is executed − Python module’s code is recompiled and the module-level code re-executed, defining a ... Read More

Inplace vs Standard Operators in Python

Chandu yadav
Updated on 30-Jul-2019 22:30:25

308 Views

Inplace Operator in PythonInplace operation is an operation which directly changes the content of a given linear algebra or vector or metrices without making a copy. Now the operators, which helps to do this kind of operation is called in-place operator.Let’s understand it with an a simple example -a=9 a += 2 print(a)output11Above the += tie input operator. Here first, a add 2 with that a value is updated the previous value.Above principle applies to other operators also. Common in place operators are -+=-=*=/=%=Above principle applies to other types apart from numbers, for example -language = "Python" language +="3" print(language)OutputPython3Above ... Read More

Global and Local Variables in Python?

Arjun Thakur
Updated on 31-Aug-2023 02:52:46

11K+ Views

There are two types of variables: global variables and local variables. The scope of global variables is the entire program whereas the scope of local variable is limited to the function where it is defined.Example def func(): x = "Python" s = "test" print(x) print(s) s = "Tutorialspoint" print(s) func() print(x) OutputIn above program- x is a local variable whereas s is a global variable, we can access the local variable only within the function it is defined (func() above) and ... Read More

Counters in Python?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

3K+ Views

A Counters is a container which keeps track to how many times equivalent values are added. Python counter class is a part of collections module and is a subclass of dictionary.Python CounterWe may think of counter as an unordered collection of items where items are stored as dictionary keys and their count as dictionary value.Counter items count can be positive, zero or negative integers. Though there is no restrict on its keys and values but generally values are intended to be numbers but we can store other object types too.InitializingCounter supports three forms of initialization. Its constructor can be called ... Read More

Advertisements