Found 10402 Articles for Python

str() vs repr() in Python?

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

3K+ Views

Both str() and repr() methods in python are used for string representation of a string. Though they both seem to serve the same puppose, there is a little difference between them.Have you ever noticed what happens when you call a python built-in function str(x) where x is any object you want? The return value of str(x) depends on two methods: __str__ being the default choice and __repr__ as a fallback.Let’s first see what python docs says about them −>>> help(str) Help on class str in module builtins: class str(object) | str(object='') -> str | str(bytes_or_buffer[, encoding[, errors]]) -> str ... Read More

How to input multiple values from user in one line in Python?

AmitDiwan
Updated on 12-Aug-2022 12:44:02

5K+ Views

In Python, to get values from users, use the input(). This works the same as scanf() in C language. Input multiple values from a user in a single line using input() To input multiple values in one line, use the input() method − x, y, z = input(), input(), input() Let’s say the user entered 5, 10, 15. Now, you can display them one by one − print(x) print(y) print(z) Output 5 10 15 From above output, you can see, we are able to give values to three variables in one line. To avoid using multiple input() methods(depends on ... Read More

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

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

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

Command Line and Variable Arguments in Python?

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

600 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

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

434 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

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

Advertisements