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
-
12
votes4
answers4581
viewsHow to multiply in Python without the multiplication operator?
I have a task and I’m having trouble completing it. What I did was this:: m= int(input('Digite o primeiro fator:')) n= int(input('Digite o segundo fator:')) def multiplica(numero): while m > 0:…
-
12
votes1
answer3091
viewsWhat is "await" for in Python?
I’d like to know what it is and what it’s for await in Python. Has something to do with threads?
-
12
votes3
answers844
viewsHow to initialize a list of empty lists?
Using Python 2.7.12 I need to create a list as follows: lista = [[],[],[],[],.........,[]] This list needs to have a very large number of lists within it (so the .....). I found around the following…
-
12
votes1
answer202
viewsWhat are WET Comments?
What are W.E.T., Python comments? Why are they bad?
-
12
votes2
answers944
viewsHow do I find a character pattern corresponding to a date in a text?
I have a text in a string, and I want to use some method, like the .find, to find a string in the format "dd.mm.yyyy". I thought I’d use .find("xx.xx.xxxx") but I don’t know what to put in place "x"…
-
12
votes1
answer161
viewsWhy does 2*i*i tend to be faster than 2*(i*i) when i is integer?
The two multiplications, 2*i*i and 2*(i*i), are equal and must generate the same result, what changes is only the order that the multiplications are made, but apparently are treated differently by…
-
12
votes4
answers242
viewsWhat’s behind the "go"?
Day I came across a question from a user who wanted to print a string, but with time interval between each character, so I suggested that it use the following code: from time import sleep frase =…
-
11
votes3
answers2755
viewsHow to assign the results of a function that returns a list of objects?
In R we can make a function return more than one object through a list. But how to assign these objects to two distinct variables? Example: f<-function(){ primeiro<-1:10 segundo<-11:21…
-
11
votes2
answers35240
viewsHow to develop programs for Android through Python?
To develop programs for Android in Python it is necessary to build a framework in such a way that the application is executable on Android phones? I am new as Android programmer, I am studying and I…
-
11
votes0
answers241
viewsReload form.custom.inpval after saving form
I am developing a system in web2py and I have the following doubt: I made a custom form that has a field that sends an image with preview. The code is like this: {{=data['form'].custom.begin}} {{dsp…
-
11
votes4
answers15696
viewsHow to extract digits from a Python string and add them together?
I need to decompose one string in Python, separating letter and numbers, and summing these numbers. For example: string = "96h11k" Of that string need to extract the numbers 9, 6, 1, 1 and add them:…
-
11
votes3
answers15464
viewsFor increment in Python
I learned that in the Python, to make a loop with for, from 1 to 10, we use the range. Sort of like this: for i in range(1, 10): print(i) Generally, in other languages, when we require a simple…
-
11
votes1
answer12600
viewsHow to remove python-installed packages
downloaded a package with wget, unzipped and downloaded the folder, used the command: python setup.py install It ran without errors, but now I no longer use this program, what is the correct way to…
-
11
votes3
answers25171
viewsHow to write multiple lines in Python?
How can I write a string with multiple lines in Python? As I come from PHP, I usually do so: $string = " uma linha outra linha2 " Or else: $string = <<<EOT uma linha outra linha EOT; When I…
-
11
votes2
answers70082
viewsHow to concatenate multiple Strings in Python?
The following method c.filtraNome(nome) carry out a consultation at the bank (sqlite3), however, I don’t know how I can group each field and return the already formatted query in the following way:…
-
11
votes1
answer199
views"[-4:]" What is this syntax?
I have the following expression: namer = name[-4:] Where name gets a name, but what does that mean [-4:]?
-
11
votes3
answers7119
viewsHow to see the methods or attributes of an object in Python?
Given a Python object, I can easily list which methods or attributes it has, directly in the interactive interpreter?
pythonasked 9 years ago Leo Ribeiro 581 -
11
votes1
answer339
viewsCreate python executable: Pyos_inputhook is not available for Interactive use of Pygtk
I just finished my first program with python and would like to create an executable for distribution. However, I’m having trouble doing this. I’ve tried using py2exe, pyinstaller and cx_freeze…
-
11
votes2
answers21117
viewsWhat does "Return" do in Python?
What "really" the return does in Python, mainly in recursive functions. Ex. factorial: def fatorial(n): if n==1: return n return fatorial(n-1) * n…
-
11
votes1
answer2490
viewsPattern recognition
I have hundreds of digital images of dogs and cats, I need to make an algorithm to recognize when the dog is and when the cat is. What steps should I take?
-
11
votes1
answer15048
viewsUnderstanding the concept of Threads in practice (in Python)
from threading import Thread import time def carrinho(velocidade,nome): distancia = 0 while distancia <= 1000: print("Carrinho :",nome,distancia) distancia += velocidade time.sleep(0.3) carrinho1…
-
11
votes7
answers52205
viewsIs there a way to print everything without breaking the line?
I have this code: print "t" print "e" print "s" print "t" print "e" He’s got a way out: t e s t e What I find very boring these "\n". There is a way to print everything without line break?…
-
11
votes2
answers5134
viewsWhat makes Join() so superior compared to other concatenation techniques?
It is common to read that the method of concatenation join() is a lot superior what other techniques in Python (such as + or +=). Starting from this point, I ask some questions: What does the join()…
-
11
votes4
answers843
viewsHow to limit the generation of pseudo-random numbers to non-zero and non-repeating numbers?
I would like the figures not to repeat themselves and be different from zero. import random Sort1 = int(60 * random.random()) Sort2 = int(60 * random.random()) Sort3 = int(60 * random.random())…
-
11
votes2
answers6367
viewsOpen, edit and save a binary file in Python3
It is possible to open a binary file and read its bits in Python3, edit and save a new binary. If possible, such as?
-
11
votes2
answers23324
viewsCount the number of occurrences of a value in a list
I have a list with the following values: numeros = [5, 3, 1, 2, 3, 4, 5, 5, 5] In Python would have some function to count how many times some value repeats? For example: I want to know how many…
-
11
votes2
answers1804
viewsFormat String "28122016" for "2016-12-28" date
I know it’s a very simple question, but I’ve done a lot of research and I’m not finding a simple solution. I need to create a function that receives a date in the format "28122016" and convert her…
-
11
votes2
answers8828
viewsAfter all, what is the function Repr in python for?
I’m a bit of a beginner in python and I wanted to know what this confusing function is really for, I looked at several sites but no one has really said what it’s for
-
11
votes1
answer2259
viewsWhat to use to format a string, % or format?
Use "%" or ".format"? I think the last one is newer, but there is some other aspect to be taken into account when deciding which one to use?
pythonasked 6 years, 11 months ago Lucas Souza 567 -
11
votes2
answers872
viewsProgram to simulate the anniversary paradox
In probability theory, the anniversary paradox states that given a group of 23 (or more) randomly chosen people, the chance that two people will have the same birthday date is more than 50%. For 57…
-
11
votes3
answers3029
viewsHow to calculate perfect numbers quickly?
I am trying to perform an exercise to show the perfect numbers present within a certain range, but I can only accomplish such a feat until the perfect fourth number. If I raise the range, it takes…
-
11
votes1
answer751
viewsHow does a Ddos attack work?
I’m curious trying to understand how a Ddos attack works, so I gave a read online and then wrote this snippet of code to attack my own router to see what happens: import socket, threading n = 0 s =…
-
11
votes1
answer374
viewsDoes multiple assignment in Python use tuples?
In the question Inverting two variables without using a temporary an answer quotes a link that comments on the multiple assignment of variables in Python: Functioning of Multiple Assignment The…
-
11
votes1
answer408
viewsWhat is a virtual subclass in Python and what are its advantages?
In Python we can implement an abstract class from the module abc and one of the forms is the class inheriting from abc.ABC: from abc import ABC class AbstractClass(ABC): @abstractmethod def…
-
11
votes1
answer215
viewsWhy use a regular expression "compiled" (re.Compile) in Python?
In a another question of this site, I noticed that although the two responses made use of regular expressions, different paths were taken: One of them used the function re.search to carry out the…
-
10
votes3
answers5279
viewsHow to check if a variable was set in Python?
Is there any way to check whether a variable was set in Python or not?
-
10
votes2
answers1835
viewsWhat is the difference between 'string' and r'string in Python?
I was taking a look at Django’s code, framework in Python, and I came across the following code in the file urls.py. urlpatterns = [ url(r'^articles/2003/$', views.special_case_2003),…
-
10
votes6
answers65891
viewsHow to convert a string variable to int?
As I correctly declare a value whole, for he is returning that my variables are strings. n1 = input("informe um número ") n2 = input("informe um número ") soma = n1 + n2 print ("O resultado da soma…
-
10
votes9
answers17879
viewsHow to identify if a string is a palindrome?
A palindrome is a word or phrase that has the property that can be read from right to left and from left to right. For example, the strings "aaaaa", "1221", "bbaabb" are palindromes, however the…
-
10
votes1
answer4477
viewsWhat does Traceback mean?
When creating some basic programs in Python, I occasionally come across errors that bring the word traceback, so I was curious to find out what traceback means.
-
10
votes1
answer1006
viewsChat Python - Errno 9 - Bad File Descriptor
I am developing (for study only) a code for a chat in python, and I came across an error during client-server communication, I would like some guidance about it, since I cannot identify a solution.…
-
10
votes5
answers5611
viewsDraw square on the canvas using wire fences (#)
Hello I’m starting to program in python and I’m having a hard time doing an exercise where I have to do a rectangle with "#". largura = int(input("Digite a largura: ")) altura = int(input("Digite a…
pythonasked 7 years, 10 months ago Angelo Spinardi 103 -
10
votes3
answers883
viewsHow to create operators in Python?
I was searching the internet about why Python doesn’t have the operator ++ and I saw someone saying that you can create this operator. How do you do this? How to create a Python operator?…
-
10
votes2
answers1005
viewsWhen finishing the script with "Ctrl+C" (Keyboardinterrupt) does not close Selenium Firefox
I am using Selenium to manipulate a page, while running the script it opens firefox, but what I wanted to do is this, if any problem in the script or if the user finishes, I want firefox to close as…
python selenium firefox selenium-webdriver geckodriverasked 7 years, 1 month ago Wictor Chaves 8,445 -
10
votes1
answer318
viewsIs there any pythonic way to make "Slice" from an array?
In Javascript there is the Array.prototype.slice: var input = [ 'laranja', 'limão', 'melancia', 'mamão' ]; var aparada = input.slice(1, -1); console.log( aparada ); And in the PHP the array_slice:…
-
10
votes2
answers21590
viewsWhat is the function of "Try" and "except"
Could someone answer me what the parameter means try: and the except? I need to do a show and I have a question about this.
-
10
votes1
answer471
viewsHow can you check if the number is in between so fast?
It is known that with the module timeit it is possible to measure, in Python, the execution time of code snippets. Curious, I was testing what is the time it takes to verify if a certain number is…
-
10
votes2
answers111
viewsWhat is the name of this structure in Python?
In the following code: first_part = 46 last_part = 57 guess = f'{first_part}{last_part}'.encode() print(guess) print(type(guess)) But I didn’t understand the code: guess =…
-
10
votes5
answers9012
viewsHow to replace more than one character in the replace method in Python 3?
I have the text and need to remove all punctuation marks using just one method replace(), without using tie as well. My initial idea would be to use texto.replace('.', ''), this for each type of…
-
10
votes2
answers320
viewsWhy can’t f-strings be used as docstring?
In accordance with the PEP 257, one has: A docstring is a literal string that occurs as the first statement in a module, Function, class, or method Definition. Such a docstring Becomes the __doc__…