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
-
6
votes3
answers2675
viewsIs it possible to include more than one variable per input in Python?
In C I can do it: printf("Informe 3 numeros'); scanf("%d%d%d", &a,&b,&c); Can I do a similar process in Python? I did some searches and found that I can do so: a,b,c = input('informe 3…
-
6
votes2
answers409
viewsPython algorithm complexity with two loops
This algorithm is used to return the element that repeats most in a list: def algorithm_x(a): x = 0 y = 0 for i in range(0, len(a)): k = 1 for j in range(i+1, len(a)): if a[i] == a[j]: k = k + 1 if…
pythonasked 4 years, 11 months ago Pirategull 659 -
6
votes2
answers583
viewsA recursive function can replace while and for?
If so, how could I, for example, create a recursive function equivalent to the code below? lista = list(range(1000)) for i in lista: print(i)
-
6
votes3
answers264
viewsReceive a string and put keys in it
I have a script that receives a string. I need to receive this string and add it to its key ends. I have a problem when placing: variavel = input('informe um valor') print(f'{variavel} aqui vem um…
-
6
votes1
answer347
viewsWhat are dataclasses and when to use them?
In version 3.7* calls were added dataclasses which were designed from the PEP 557 and consists of using a decorator, dataclass, in "normal classes". What are these dataclasses? What are the benefits…
-
6
votes2
answers208
viewsWhat is the actual implementation of "in" in Python?
This week I found myself wondering what the actual implementation of "in" in Python is. To try to answer, I went to look the official documentation about his details, and only say that: For…
-
6
votes1
answer200
viewsIs a Python dictionary as efficient as a tree or a hash?
I use a lot of Python dictionaries: dic = dict() #ou dic = {} dic['113654'] = {'nome': 'João', 'idade': 21} dic['853872'] = {'nome': 'Maria', 'idade': 27} dic['427845'] = {'nome': 'Fernando',…
-
6
votes1
answer157
viewsHelp with first neural network
I am beginner in dealing with Nns, and need help and explanation in certain things with my first project. Basicamento, to making an NN for checkers, my input are the black parts of the board (8x4)…
-
6
votes2
answers153
viewsHow to apply decorators that are classes in other class instance methods?
I’m trying to implement a class that’s a method Decorator python. The idea is to apply it to the methods of other classes. However, it is getting lost in the reference self of the annotated class…
-
6
votes1
answer144
viewsWhat is the Join() function for in the threading module?
I’ve seen the function join() for the module threading, but I didn’t quite understand what she does.
-
6
votes2
answers229
viewsIf Python strings are immutable, how can we change them with the replace() method?
If Python strings are immutable, how can we change them with the method replace(), for example? Ex: s = "banana" s = s.replace("b", "z") print(s) # zanana Is this a change of the original string or…
-
6
votes2
answers112
viewsWhen is an Slice data copied?
When I do so: a = [ 1, 2, 3] b = a[1:] b use the same list of a or it creates another list and copies the data that is pertinent? If you have many items it will slow down? Happens even if I don’t…
-
6
votes1
answer177
viewsHow to reduce the number of loops to calculate the smallest difference between all the numbers in a list?
The program below returns the smallest possible difference between all the elements of a list: def calculo(A): r = float("inf") n = len(A) for i in range(0, n-1): for j in range(1, n): if abs(A[i] -…
-
6
votes5
answers427
viewsHow to run faster a code that calculates the number of factorial digits of a number?
The code must print how many digits have the factorial of N. I am trying to perform a simple factor challenge on the URI site and when I submit the code the answer is always "Time limit exceeded",…
-
6
votes1
answer669
viewsWhy do formatting options not work with lists, dictionaries, and other objects?
When I want to print a number or string, I can use f-strings (in Python >= 3.6) or str.format, and I can only pass the variable between keys, or use the formatting options. Ex: numero, texto =…
python python-3.x characteristic-language python-internals cpythonasked 4 years, 9 months ago hkotsubo 55,826 -
6
votes3
answers146
viewsHow to use double quotes in a python application by running a subprocess
I’m trying to add a new wifi network via command line in a Raspberry Pi, however when I pass the string containing the data of the Wifi network the quotes that should be between the login name and…
-
6
votes5
answers1031
viewsCheck that all items in the list are equal
I have a list of times containing the values of times of several cars and I would like to know how do I know if all these values are equal I thought to compare each value with its later, ie compare…
-
6
votes1
answer124
viewsWhy is the "in" method for checking if an element belongs to a collection so slow?
I was having a lot of difficulty executing code efficiently and found that the problem was on a line that used the operator in. Then I created a new function that searches for a match in the list of…
-
5
votes1
answer4590
viewsHow to resize graphics in Ipython Notebook without loss of quality?
In Ipython Notebook I produce graphics with (for example): x = [1,2,3,4] y = [5,6,7,8] plot(x, y) When I place the cursor over the generated graph, the tool appears at the bottom right corner to…
-
5
votes1
answer2226
viewsSave multiple figures in a loop - Python
I’m using python for experimental data analysis. I have in a folder several data files that, in the script, are grouped according to some criteria. I use a loop to read all the files in the folder…
-
5
votes2
answers306
viewsHow to do data Mining in a txt file with re.finditer
This code can tell me the location of the words batman and sei throughout the file txt: import re f = open('C:/pah.txt','r+') text = f.read() words = ['batman','sei'] for x in words: for m in…
pythonasked 10 years, 10 months ago João Pontes 73 -
5
votes1
answer1073
viewsIt is good practice to import modules into __init__.py
I noticed that in Django framework modules are imported into __init__.py for convenience. """ Django validation and HTML form handling. """ from django.forms.forms import * # NOQA That way it: from…
-
5
votes3
answers34998
viewsTimer in Python
I’m trying to make a timer that, after 10 seconds, prints a string. I tried to use the time.sleep() and it didn’t work. Example: def tempo(): #passados 10 segundos print "olá"…
-
5
votes1
answer1266
viewsHelp Tables python27
Good Morning, I’m new with Python/Tkinter and I have some projects in mind, but all of them have use of tables along with them, could anyone tell me if there is a library I could use to create these…
-
5
votes2
answers1037
viewsUse "ping" on a Python site
In Windows CMD it is possible to use the command ping to take the IP of certain websites, for example: ping www.facebook.com.br Is there any way to do something similar using Python 3.4?…
-
5
votes1
answer2843
viewsSublime problem to run python code
In giving CRTL+B in sublime 2, to run my code in python, appears this error message, someone knows what is? Decode error - output not utf-8] [cmd: [u'python', u'-u',…
python python-3.x sublime-text sublime-text-2 sublimereplasked 10 years, 7 months ago Leonardo Villela 53 -
5
votes2
answers404
viewscarry out independent process
I’m having trouble loading processes from a python application. Before I was using the subprocess. Popen, but it creates subprocesses of the main application, and in my case, I need to create…
-
5
votes2
answers453
viewsMake list changes to be local in Python?
In Python as I do for the function below change the feather list locally, that is, only in the scope of the function f without changing x set outside of f? x = [1, 2] def f(x): x[1] = "novo valor"…
pythonasked 10 years, 4 months ago Carlos Cinelli 16,826 -
5
votes1
answer334
viewsHow to create immutable objects in Python?
Based on this question, Is it possible to create your own immutable objects/classes in Python, in the sense that by default, this object of mine will be copied when passed as a function argument? If…
pythonasked 10 years, 4 months ago Carlos Cinelli 16,826 -
5
votes1
answer723
viewsFind all instances of a pattern in a text
I need to write a program that identifies a lowercase letter surrounded by three uppercase letters on each side. For example: "AEIoSDE" == "o" I wrote the code as follows: # coding: utf-8 letra =…
-
5
votes1
answer174
viewsScraping with python
How to capture one or more phrases from a website with Python and/or regular expressions? I want everything that starts with <p>“ e acabe com ”</p> Example:…
-
5
votes3
answers10914
viewsHow to remove the first element from a list in python?
I have the following code: On the command line > teste.py primeiro segundo In the script teste.py: import sys print(sys.argv) And I have the following return: ['c:\\teste.py', 'primeiro',…
-
5
votes1
answer1327
viewsHow to set date formats in Django 1.7 for the whole system
Is there any way to change the display settings and/or date formatting at system level?
-
5
votes1
answer376
viewsIn Django why does the is_authenticated method always return True?
I saw that the documentation indicates the method is_authenticated as being responsible for telling templates if there is a logged-in user. I also saw in the method code that it contains only the…
-
5
votes1
answer729
viewsWhat is string.maketrans for?
I was looking at some Python challenges and found one that involved rotating the characters of a string. Of type 'a' turns 'c', 'b' turns’d', and at the end 'y' turns 'a' and 'z' turns 'b'. While…
-
5
votes1
answer236
viewsMonitoring REST API status
I’m learning and trying to solve a POST case request where a dispatch of a body well wide to be processed by API and the return is a job ID to access the result. It turns out that the time it takes…
-
5
votes1
answer1502
viewsWait to finish the thread before ending the program
I have a loop while that ends only when I press the key q, then the program is closed. Within that loop, at a given time or condition, I initiate a thread that takes about a few 10 seconds to run.…
-
5
votes1
answer181
viewsCreation and communication with daemon
I have a Python application where I need to keep it online all the time. The application has its own console, which is where I do the communication and step parameters. I understood that I can…
-
5
votes3
answers4053
viewsHow to add list values in tuples?
I have for example a list with tuples, and inside these tuples I have 2 lists, where only the second list of each tuple varies: x= [(y, [1, 2]), (y, [3,4]), (y, [5, 6])] How do I add the first…
-
5
votes2
answers2743
viewsHow do I set a private property in Python? And is there a protected property?
I come from PHP. In it, when we want to define a property of a class as private we add the keyword private in his statement. Example: class StackOverflow extends StackExchange { private $language =…
-
5
votes2
answers707
viewsMysql to Python 3 connector
I’m doing a project with Django and Python 3 but I can’t find the appropriate Mysql connector. The django.db.backends.mysql works, but only for Python 2, and I wanted to avoid using this version. On…
-
5
votes2
answers4646
viewsHow to print a txt file in Python
How to print a txt file in Python? I am not referring to print but to print on the printer. Thanks (:
-
5
votes2
answers240
viewsrender specific part of a page
I am using the following code to render a web page: import dryscrape # set up a web scraping session sess = dryscrape.Session(base_url = 'http://www.google.com') # we don't need images…
-
5
votes4
answers7094
viewsSwap the first character of a string with the last python character
I have the following function: def troca(s): letra1 = s[0] letra2 = s[-1] s = s.replace(s[0], letra2) s = s.replace(s[-1], letra1) return s troca ('bar') As you can see, I provide as an argument for…
-
5
votes3
answers392
viewsWhy in Python can we define an index of a Dict as a Tuple?
I noticed that in Python, we can add values like tuple as an index of a dict (dictionary). Example: test = {} test[(1, 2, 3)] = ("um", "dois", "tres") print test // {(1, 2, 3): ('um', 'dois',…
-
5
votes1
answer245
viewsError [Forbidden] when sending event in google calendar, googleCL API
For those who do not know, the googleCl(Command line tools for the Google Data Apis) follow the link https://code.google.com/p/googlecl/ I use only the command that sends an event to my google…
-
5
votes1
answer437
viewsUrllib2, exception handling
I’m a beginner in the art of programming. I’m learning to code in Python through a book, Learning to Program: The art of teaching the computer (Cesar Brod - Novatec Editora) In one of the exercises,…
-
5
votes1
answer3608
viewsRun Python file with arguments
In my program, I do the following operation: import sys qtd_ponts = int(sys.argv[1]) I would like to know a practical way to run this file .py passing arguments without needing an IDE, using files…
-
5
votes2
answers7137
viewsWhat does KWARGS in Python mean?
I know that, in Python, we can define in a function something similar to what they call "named Parameters", ie, "named parameters". def my_func(**kwargs): pass my_func(nome="Stack Overflow",…
-
5
votes1
answer233
viewsSet class for a table
I’m starting now with Flask and I came across the following question: how do I define a table class using the flask_table? Follow my example code and the result: from flask_table import Table, Col…