Most voted "python" questions
Python is a dynamic and heavily typed programming language whose design philosophy emphasizes usability. Two similar but incompatible versions of Python are in use (2 and 3). Please mention the version you are using when asking a question about Python.
Learn more…8,642 questions
Sort by count of
-
81
votes4
answers4098
viewsWhat is pythonic code?
In Python programming it is common to hear the term pythonic (or pythonic), see: Sopt: "Understanding the pythonic way of dealing with properties" Sopt: "Pass pythonic form parameters" Wikipedia:…
-
48
votes3
answers22473
viewsWhat is "with" in Python for?
I’m doing a study on Python and I came across this guy with. I know him from Javascript. However, I don’t know how he works in Python. Most of the examples I’ve seen show it being used in reading…
-
47
votes8
answers4052
viewsHow to generate 200,000 primes as fast as possible in Python?
Heed: I’m not looking for ready-made pieces of code. I just want someone to help me think about in some way to generate these 200,000 primes as efficiently as possible, in Python. I’m working on it…
-
41
votes8
answers3166
viewsFewer moves from a horse to a given house in Chess
On a chessboard in any house I have a Horse (represented in red) and I will have only one other piece (represented in green) that the horse must go to it: I must use the simplest path and take into…
-
38
votes3
answers1568
viewsHow does the licensing of Open-Source programs work?
I’ve searched a lot on Google but never found a good explanation for how to make open-source programs. I like programming a lot in Java and Python and I wanted to develop some open program with…
-
33
votes1
answer2909
viewsHow do Python decorators work?
The @Elizeu Santos asked in the group Python in facebook Portuguese the following: "Talk guys, I’m studying python and something I don’t understand are the decorators. I faced them making the flask…
-
31
votes1
answer29273
viewsWhat’s the difference between break, pass and continue in Python?
The Python documentation in Portuguese is a work in progress, and reasonably confusing as you can see. I have difficulties in English and I found that website that I can’t read. So how can I use…
-
29
votes3
answers15296
viewsWhat is Yield for?
I’ve been writing some for some time scripts with Python, in some tutorials I am sometimes presented with the yield, that normally arises in repeat structures commonly while. What is it for? And how…
-
25
votes5
answers80372
viewsHow to "round" a float in Python?
I have this sum: total = 0.1 + 0.2 print total #retorna 0.30000000000000004 How would I "round" the decimal places and return only the number of places summed? Example: totalA = 0.1 + 0.2…
-
25
votes5
answers63261
viewsHow to clean the console in Python?
I am an Operating System user Ubuntu and when I want to clean the terminal, I use the command clear. >>> clear However, in the Python, how could I do to clear the terminal, when I use it on…
-
25
votes4
answers4174
viewsWhat is the meaning of the operator ( * ) asterisk?
In C this operator is used in pointer type variables. However, in Python I don’t know which way, and why to use it. Therefore, what is the meaning of the operator (*) in the Python language?
-
24
votes1
answer1187
viewsHow to use virtualenv to manage dependencies on a Python application?
I need to manage the dependencies of an application Python that I am developing, so that it is easy for other team developers to work on the project using the same versions of the packages that I am…
-
24
votes2
answers517
viewsFielderror: Relation Fields do not support nested lookups
I’m trying to make a query in Django with several joins, and I came across this mistake that I don’t know what it means: Tag.objects.filter(dset__descendant__entities__entity=e) Fielderror: Relation…
-
24
votes6
answers40933
viewsDoes ternary surgery exist in Python?
I often see several programming languages where a ternary operation is almost always identical. (x % 2 == 0) ? "par" : "impar" However, when I went to try to do this in Python, it was wrong: (x % 2…
-
23
votes1
answer15547
viewsHow is 'super' used in Python classes and what is it used for?
How to use and for what purpose super in classes Python?
-
23
votes4
answers3572
viewsHow can I distribute the . py program without the user having to install all libraries?
I’m learning Python and I need to distribute a program, I read about cx_Freeze and py2exe to generate one .exe. However, I don’t mind distributing the program code together, so I don’t see the need…
-
22
votes2
answers8927
viewsIn Python, what is the difference between == and the is command?
I’m confused in the use of two commands, the is and the == that as far as I understood they perform the same thing that is to compare if two objects are equal. Is there any more performatic?…
-
22
votes1
answer4503
viewsWhat’s the difference between global and nonlocal in Python?
In Python, there are two statements that are used to reference variables outside the local scope: global and nonlocal. What is the difference between the two statements? When to use each?…
-
21
votes5
answers8633
viewsWhat is the most complete way to install python on Windows?
I know this question can be interpreted as an argument, so I did it with the word "complete" instead of "better". I’m a python user on GNU/Linux and here it comes more or less pre-installed. Still,…
-
21
votes4
answers6715
viewsHow to call an external function without sending the 'self'?
I’m using a class attribute to save the View that I want to test on a Django application, like this: # TESTS.PY class OrderTests(TestCase, ShopTest): _VIEW = views.order def…
-
21
votes3
answers553
viewsIn Python, what are the consequences of using is instead of '=='
The two forms below return True, but what are the consequences of this in a more complex code? >>> a = '2' >>> a is '2' >>> True >>> a == '2' >>> True…
-
20
votes4
answers3838
viewsWhat is the expression 'if __name__ == "__main__"' for?
I notice some scripts on Python, at the very end of the code, have the following expression: if __name__ == "__main__": #faça alguma coisa aqui What’s the point of this?…
pythonasked 9 years, 2 months ago Wallace Maxters 102,340 -
20
votes1
answer3361
viewsWhat is the __future__module for?
I’ve seen it in some codes written in Python the following statement: from __future__ import print_function What is the purpose of this module __future__? When do I need to import it into a script…
pythonasked 8 years, 7 months ago Wallace Maxters 102,340 -
20
votes1
answer8743
viewsPython, difference between assert and raise?
I came across a question with the structures raise and assert python. In the code below shows the implementation of structures forcing an error if the number passed is negative. def…
-
20
votes5
answers140931
viewsHow to limit decimal numbers in Python?
How do I format the number of decimals of a decimal number in Python? For example, I want to display only two decimal places of the following number: numero_decimal = 3.141592653589793 How could I…
-
20
votes3
answers17530
viewsWhy do we have to use the attribute self as an argument in the methods?
I watched a video class where it is said that every method is required to pass the parameter self in the method definition, as in the example below: class Complex(object): def __init__(self, real,…
-
20
votes3
answers14315
viewsPythonic way of defining setters and getters
Searching the internet, I see that it is highly recommended not to use Setter and Getters in Python classes. However, I cannot determine a way to access private values without using them. There are…
-
20
votes3
answers719
viewsWhy static methods can be called through the "instance" of the Python 3 class?
Let’s say I have the following class: class Person: @staticmethod def hello(): print('Hello!) When performing the operation Person().hello() the method is executed normally. But the method hello…
-
20
votes2
answers2035
viewsWhat is the Python semicolon function?
I found some codes that had variables inside classes, finished in semicolons. I don’t understand why, and I haven’t found anything on the Internet about it either. class exemplo():…
-
19
votes1
answer2421
viewsHow does this code that generates a maze work?
I don’t know Python very well and I can’t read that code right. Would someone kindly post comments to facilitate reading? Preferably explaining what each line does. from random import shuffle,…
-
19
votes1
answer25589
viewsWhen should I use __init__ in functions within classes?
For the book I am studying, in some moments the author uses __init__ as a first function of a class. This function (and others) always has self as one of the variables (something I haven’t yet…
-
18
votes3
answers1094
viewsAssign value to a dictionary variable that is optional
I have a function where one of the parameters is a dictionary, but it’s optional. I need to assign a value in this dictionary, but take into account the case where this parameter is not filled in.…
-
18
votes3
answers44347
viewsHow to create a python " *.exe" executable?
I need to turn " *.py" files into executables " *.exe" to run on any Windows desktop. Currently I use 3.3
pythonasked 10 years, 7 months ago paulosdiasabreu 199 -
18
votes1
answer2595
viewsExplain the SVR algorithm
I wonder if someone could explain the algorithm Support Vector Regression? (used this of the scikit) I’ve already looked through some sites but I’m still not getting it right.…
-
18
votes1
answer2673
viewsWhat makes Python a multiparadigma programming language?
Here on this Wikipedia page cites some examples of multi-language such as C++, Groovy, Oz, Ruby, Scala, Swift, Groovy and even Python. Particularly I did not find so objective the explanation. What…
-
18
votes1
answer281
viewsWhat is Python Assignment Expressions 3.8?
As assignment Expressions are defined in the PEP 572 which has been approved to be implemented in version 3.8 of Python. But what are the assignment Expressions and when they should be used? What…
-
18
votes1
answer155
viewsWhat justified adding the syntax for positional-only parameters to Python version 3.8?
As stated in What’s New In Python 3.8 to PEP 570, that defines the syntax for only positional parameters, has been implemented. According to PEP 570 it will be possible to use the bar in defining…
-
17
votes3
answers3083
viewsHow to do a search ignoring Python accent?
Suppose I have a list of words, in Python (if necessary, already ordered according to the rules of collation): palavras = [ u"acentuacao", u"divagacão", u"programaçao", u"taxação", ] Notice I didn’t…
-
17
votes1
answer1876
viewsWhat is Global Interpreter Lock (GIL)?
One of the first things you read when you start studying about threads in Python (Cpython) is about the Global Interpreter Lock (GIL). What exactly is GIL? What are its practical implications for an…
-
17
votes2
answers653
viewsHow does Python treat the "Yield" command internally?
Was reading on command yield from Python, and it seems to me that this command creates a Generator which would be a kind of data list in which the return on value occurs over demand, as if the last…
-
16
votes1
answer8086
viewsWhat’s the set for in Python?
What is and what is the set in Python? test = set([1, 2, 3]);
pythonasked 9 years, 4 months ago Wallace Maxters 102,340 -
16
votes3
answers44552
viewsHow to connect Python to Mysql database?
I wonder how I can make a simple connection to Mysql through a Python script. To do this I need to install some library? Or is there something that comes by default in Python (like in PHP)?…
-
16
votes1
answer1890
viewsWhat is __all__ in Python for?
I’ve seen some code written in Python whose file __init__.py had a variable __all__ with a list assigned. Example: __all__ = ['foo', 'bar'] I’ve noticed that in Python, when things start with…
pythonasked 7 years, 11 months ago Wallace Maxters 102,340 -
16
votes11
answers38715
viewsHow to check if the variable string value is number?
I am developing a program where the user type a data, then I have to check if the value is number to proceed with the operation, but if he type a non-numeric value he is alerted about the invalid…
-
16
votes4
answers634
viewsWhat is the difference between i += 2 and i = i + 2?
I was seeing, there are people who speak that is different and there are those who speak that is the same thing. I am confused. They are different or do the same thing?
-
16
votes1
answer3610
viewsWhat exactly is the backslash ( ) for in Python?
I sent an application to Github written in Python that was updated by someone who claimed to have formatted the code properly. After starting to see the changes, I got scared thinking that it had…
-
15
votes3
answers1932
viewsIs there a more efficient way to create an array from another array dynamically, filtering the contents of the first one?
I have an array of values that can include several numpy.Nan: import numpy as np a = np.array ( [1, 2, np.nan, 4] ) And I want to iterate over your items to create a new array without np.Nan. The…
-
15
votes4
answers1418
viewsHow do I programmatically respond to a command on the Linux terminal?
I have a Python script that runs a certain command on the system. This command expects a password to be typed right away, the only way it works is this, it is not possible to pass the password via…
-
15
votes1
answer6546
viewsNaming conventions for variables and functions in Python?
In the R, there is a lot of freedom and variety in function names between packages. Dot names (get.this), names in camel (getThis), names in snake_case (get_this). This has its pros and cons. Why,…
python python-2.7 python-3.x nomenclature conventionsasked 10 years, 9 months ago Carlos Cinelli 16,826 -
15
votes3
answers10708
viewsWhat is the main difference between a Tuple and a List?
What are the differences between a Tuple and a List in the Python? Example: >a = [1, 2, 3] # [1, 2, 3] >b = (1, 2, 3) # (1, 2, 3) The Tuple, grotesquely speaking, it is a constant that accepts…