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
-
10
votes4
answers5082
viewsWhat does /= mean in Python?
Can anyone tell me what //= means in python? On line 8 of this code has the use of it. n = int(input("Digite um numero menor que 10: ")) aux, reverso = n, 0 while aux != 0: dig = aux%10 reverso =…
-
10
votes2
answers586
viewsWhat does #noqa in Python mean?
Sometimes I find this comment in some Python code: #noqa. What does it mean? It’s specific to Python?
-
10
votes1
answer231
viewsHow to use IBM Cloud services with POST request in Python?
I am trying to use IBM Cloud’s "Speech To Text" service in my Python application via POST requests with the package requests. The problem is that I am confused about the URL that should be used and…
-
9
votes1
answer4345
viewsHow to sort a list of tuples by the nth element?
I have a list of fashion tuples: [(0, 1), (2, 3), (4, -5), (6, -3)] I want to sort that list by the second value of each tuple (i.e., in case we would [(4, -5), (6, -3), (0, 1), (2, 3)]. How to do…
-
9
votes7
answers7922
viewsConvert an array of floats to integer
Suppose the following: import numpy as np a = np.array ( [1.1, 2.2, 3.3] ) How to convert this array to int without having to iterate each element or using another array? Why do: b = int(a) Gives an…
-
9
votes1
answer589
viewsSSL socket dying on client and no errors on server
I have a problem with connections with Sockets ssl in python When performing a stress test on the SMTP daemon I am writing the client some sending threads die with "Connection reset by peer" ,…
-
9
votes2
answers471
viewsFactorial series from thehuxley.com site
Make a program that calculates and writes the sum of the n first series terms: seriefatorial http://www.thehuxley.com/data/images/app/problems/temp/e68085c6d699d2c7029907f994c57b80.png Input format…
-
9
votes2
answers286
viewsIs it good practice to always manage mistakes with exceptions?
I’m creating a game with Python 3.4 and Pygame, and I’m using the paradigm object-oriented. In my classes, especially in __init__, am full the type checkings, to prevent Runtime errors. For example,…
-
9
votes2
answers589
viewsIs it possible to name a parameter at the time of the function call?
Can I "access" a specific parameter of a Javasscript function, that is, give a value for a given parameter? As an example is better than text (since I don’t know how to explain myself properly),…
-
9
votes1
answer5098
viewsFuncionamento @classmethod
I would like to better understand the @classmethod annotation of Python. Briefly it works as a second alternative to constructor. But what are the benefits? When to use? @classmethod def…
-
9
votes1
answer1625
viewsHow to include a C function in Python?
I want to use a function made in C. Example: I have a function int swap(){ printf("lista"); } And I want to call you by Python...…
-
9
votes4
answers1959
viewsReceive json data in PHP
I’m having trouble receiving JSON data in PHP to save to the database, and this data is sent from a python script, have tried the json_decode using the variable $_REQUEST, but unsuccessfully, when I…
-
9
votes1
answer695
viewsWhat is a Prototyping Language?
Reading an article on the Internet, I came across the following excerpt: Python, for its more generalist proposal, is a great choice as language for prototyping of systems built in, say, more…
-
9
votes2
answers13617
viewsPython - 'int' Object is not iterable
Hi, I’m creating a character converter for your ascii code through a recursive Python function. However I have a problem "'int' Object is not iterable". This is my code: def cod(n): for i in n: i =…
-
9
votes2
answers208
viewsScript for modifying Bible texts (accessibility)
I’m not a programmer. I research accessibility features based on my low vision. I am trying to develop a workflow that locates in the Bible texts standard "John 3:16" and transforms it into "John,…
-
9
votes1
answer388
viewsThread control to avoid lock
I’m starting a Crawler and his idea is to download all the content of a given website. It already has some "usable" features. What’s killing me is, I’m doing it multithreaded, but the threads in a…
-
9
votes2
answers1356
viewsConvert a list list into a single list
I’m working on Python 2.7. I have this result: Resultado=[['A','B','C'],['D','E','F'],['G','H','I']] How can I turn this into a list? And get the following:…
-
9
votes2
answers7582
viewsHow to capture the last element of a list in Python?
In PHP, to take the last element of a array I can do it like this: $array = array(1, 2, 3); echo end($array); // 3; And in Python? How could I do that with this list: arr = [1, 2, 3]…
-
9
votes2
answers7326
viewsWhat is the correct way to call Python methods?
What is the correct way to make a method call in Python? As in the example below. def __init__(self): mtd([1, 2, 2, 3, 3, 3, 4, 4, 4, 4]) def mtd(data): for value in data: print(value)…
-
9
votes1
answer1015
viewsWhat is the purpose of __slots__?
What is this property for __slots__? Li in the documentation something about "waste of space", but ultimately I didn’t quite understand the purpose of using __slots__. What it’s for and when I…
-
9
votes2
answers1038
viewsHow to remove tags in Python text?
In PHP we have a function called strip_tagsthat remove HTML tags from a given text. Example: $text = "meu nome é <strong>Wallace</strong>"; strip_tags($text); // 'meu nome é Wallace' How…
-
9
votes4
answers10562
viewsPerformatively calculate the divisors of a Python number
The code I’m using is: import time def divisores(num): for i in range(1, int(num/2+1)): if num % i == 0: yield i yield num inicio = time.time() d = divisores(47587950) [print(i) for i in d]…
-
9
votes7
answers49622
viewsHow to add elements from a list and return the result as an integer?
I need to define the function soma_elementos This function takes a list of integer numbers and returns an integer corresponding to the sum of the elements of the received list. How do I do this? def…
-
9
votes1
answer4493
viewsHow to make a "deep copy" in Python?
Let’s say I have the following code: class Foo: pass foo_list = [Foo() for _ in range(10)] How can I proceed to create a copy of foo_list without the references of both the list and the objects…
-
9
votes1
answer597
viewsHow to calculate Shannon entropy based on HTTP header
Shannon’s entropy is given by the formula: Where Ti will be the data extracted from my network dump (dump.pcap). The end of an HTTP header on a normal connection is marked by \r\n\r\n: Example of an…
-
9
votes1
answer1000
viewsWhat does __*(any word)__ or _* mean in Python?
Reading a book once in a while he puts __init__ or __init and I don’t know what this "_" means, I tried to search the internet some explanations, but I ended up making myself more difficult, someone…
-
9
votes4
answers41980
viewsIs there any way to comment on multiple lines in Python?
To comment on a line, we use the #. I wonder if it is possible to comment multiple lines in Python 3. If so, how should I?
-
9
votes3
answers318
viewsIndentation in statement "Else"
What is the difference in the indentation of else "out of" the if. In this case it is to return the prime numbers up to the nth 'n' value. First case: for i in range(2, n): for j in range(2, i): if…
-
9
votes2
answers1588
viewsData structure representing a deck of cards
I’m developing a project in Python, where I have to play a card game. I am in doubt about which data structure to use, according to the description provided: Each card must be represented by a pair…
-
9
votes2
answers4659
viewsWhat is the flush parameter of the print function?
I realized that there is the parameter flush in function print python: print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) I know that: The parameter *objects sets the list of values to…
-
9
votes1
answer2436
viewsWhat is the purpose of the`reduce` function in Python?
In which cases is useful and how can I use the function reduce?
python python-3.x functional-programmingasked 6 years, 7 months ago Pedro von Hertwig Batista 3,434 -
9
votes4
answers2878
viewsTwo "Return" in a function
How it works if there are two (+) return in a Python function? It would return the second value, or only the first? Example: def soma(a, b): x = a + b y = 'qualquer coisa' return x return y…
-
9
votes2
answers643
viewsHow does the int function handle the n character?
I created a list: usuarios = ['123\n','123\n4'] I tried to turn the index 0 into integer using the int() int(usuarios[0]) Upshot: 123 But when I tried to do the same with index 1: int(usuarios[1])…
-
9
votes2
answers102
viewsWhy should descriptors instances in Python be class attributes?
I’m studying descritores in Python and I found that they should be implemented as atributos de classe for example: class Descriptor: def __init__(self, obj): self.obj = obj def __get__(self,…
-
9
votes3
answers566
viewsHow does Python handle and represent an array internally?
In Python any type of array is of class type list, see: array = ['Gato', 'Jake', 'Finn'] print(type(array)) Exit: <class 'list'> That is, every array is an object of list. However, there is…
python array characteristic-language python-internals cpythonasked 5 years, 9 months ago gato 22,329 -
9
votes2
answers287
viewsWhat precautions should I take when naming a Python file?
It is quite common to see problems generated because the Python file was named after a library. # requests.py import requests response = requests.get(...) This code snippet would generate the error:…
-
9
votes1
answer55
viewsWhat is the default for naming modules (fonts) in Python?
In Java we have the custom of creating fonts with the first uppercase letter for example: Carrinho.java and DetalheCompraFragment.java. But so far I have not found a standard for naming modules in…
-
8
votes3
answers15165
viewsHow to subtract two dates using Python?
How do I know the difference in days between two dates using Python? For example; How to know how many days there are between 22/11/2013 and 25/03/2014, considering a possible leap year.…
-
8
votes3
answers16338
viewsHow to create and read an XML with Python?
How to create and read an XML with the component DOM in Python? And how to read an XML with the component cElementTree python?
-
8
votes5
answers989
viewsHow to remove an Element from an XML with Python?
The case is that I have a file produced by a Garmin (GPS exercise device) and I want to remove all fields related to the heartbeat to pass the file to an athlete who did the exercise with me. The…
-
8
votes5
answers436
viewsRefactoring: When is a method "too big"?
I’m with a project that "recovers" certain information from an HTML page, makes a parse with the help of Beautiful Soup and return the values in the form of Dictionary, so that in another method I…
-
8
votes3
answers5124
viewsTreat python numbers by adding dot
I am making a simple code to convert meters into millimeters. What would be the best way to treat the result. Ex: 1000 or 1,000 Code: valor_m = int(input("Digite um valor em metros: ")) valor_mm =…
pythonasked 10 years, 8 months ago Matt Costa 345 -
8
votes2
answers2098
viewsHandle windows events using Python
I need to develop a Python application with Pop-up’s on Windows (like Avast, for example). In case, my application will be a keylogger that will pick up a string from a barcode reader. When the…
pythonasked 10 years, 6 months ago user3266489 91 -
8
votes4
answers11584
viewsHow to put a Python module in a different folder than my script?
I’m trying to run a Python script (v2.4 and v2.7) and created a module with some methods. Unfortunately this module needs to stay in a separate folder. In the python documentation and so far on…
-
8
votes2
answers1172
viewsIn Python, is there any rule or advantage regarding the use of 'Self'?
Let us consider the examples below: Example 1: > class PrintText(): > def __init__(self): > text = 'Funciona!' > self.printa(text) > > def printa(self, text): > print(text)…
-
8
votes1
answer1752
viewsVectors and Angles (Molecular Geometry)
Hello, I have a problem, more mathematical than computational, to solve, but I haven’t been able to solve it myself until now... I have a set of three atoms connected to each other forming an angle…
-
8
votes1
answer349
viewsIn Python, how do you explain the expression 'x = not x'
I already know the result, but I would like to understand what happens, why of such a result... For example: >>> a = True >>> b = not a >>> print(b) False It’s a simple…
-
8
votes2
answers2530
viewsIs a module the same as a Python class?
A module is the same thing as a class? If not what are the differences? I ask this because according to the The Zen of Python, modules should be used instead of ifs. The problem is that after…
-
8
votes4
answers5471
viewsCount occurrences in a list according to prefixes
Let’s say I have a list ['rato', 'roeu', 'rolha', 'rainha', 'rei', 'russia'] and another list with prefixes ['ro', 'ra', 'r'] how do I count how many times each prefix is within the first list?…
-
8
votes1
answer787
viewsCheck if list is equal
I have the following list: x = [([1,2], [1, 2]), ([1,2], [4, 5])] And I wanted to check if the first list of each Tuple is always the same. Note: the list x contains tuples with lists. These tuples…