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
-
15
votes5
answers106673
viewsHow to generate random numbers in Python?
I would like to know how to generate random numbers in Python. I’m with version 3.4.
-
15
votes2
answers22340
viewsWhat is assert in Python for?
The @Ciganomorrisonmendez gave me a reply in a previous question on Python. And as I’m a beginner in Python still, I didn’t know what the assert that he indicated in the reply. The excerpt from the…
pythonasked 9 years, 2 months ago Wallace Maxters 102,340 -
15
votes1
answer2699
viewsHow does list assignment work using range?
I was reading a documentation on the internet when I came across the following code: sys.path[:0] = new_sys_path I was curious, because I had never seen intervals used on the left side of an…
-
15
votes3
answers7080
viewsIn a list, what is the difference between append and extend?
Num list in Python, there are the methods append and extend. What’s the difference between them and when to use each?
-
15
votes2
answers2129
viewsWhat does the "|=" operator mean in Python?
I was analyzing a code and came across the operator |=, would like to know what this means, and what is its practical application. Example: x |= y…
-
15
votes1
answer137
viewsHow can the search for an element in a set be O(1)?
In accordance with the official Python page as to the complexity of time algorithms, sequences list and set have the following characteristics:: List Set Highlight for the operator in, which…
-
15
votes2
answers399
viewsHow does Python structural matching (match declaration) work?
Recently, the Pattern matching structural version was introduced 3.10 (still in alpha) Python. For this, the keywords match and case. Were, in the notes of release, included several examples, among…
python python-3.x characteristic-language pattern-matchingasked 3 years, 8 months ago Luiz Felipe 32,886 -
14
votes2
answers2852
viewsPlot differences in matplotlib for different parameter accuracies
For a course work, I built the following code in Python using the Matplotlib and scikit-image packages: #!/usr/bin/env python # -*- coding: utf-8 -*- import numpy as np from scipy import interpolate…
-
14
votes1
answer13158
viewsDeveloping Android apps using Python
Is it possible to create Android apps with the Python language? If yes, how does it work?
-
14
votes2
answers3077
viewsWhy in class statements in Python should we extend Object?
In the Python, when we declare a class, we extend object. class StackExchange(object): def __init__(self): pass I do not know if I am mistaken, but I had the impression that in some versions this is…
-
14
votes2
answers1293
viewsXpath with Python
I have the following XML (simplified): <produto refid="cat01" idprod="tv01"> <marca>xxx</marca> <modelo>xxxx</modelo> <genero>xxx</genero> </produto>…
-
14
votes4
answers36247
viewsList files from a Python folder
I’d like to know how I use the library os to list files from a given directory.
-
14
votes2
answers8854
viewsDifferences between Python versions?
Hello ! I recently decided to start studying the Python language, however when researching the language I realized that there are differences between version 2 and 3. These differences are really…
-
14
votes5
answers42959
viewsFunction equivalent to Trim (function to remove extra spaces at the beginning and end) in Python?
In PHP and Javascript, we have the function trim that serves to trim a string, removing extra spaces at the beginning and end. Thus: $str = ' meu nome é wallace '; var_dump(trim($str)); //…
-
14
votes4
answers1231
viewsVery interesting switch/case on Swift - What other languages support this?
The Swift language presents a very interesting and very intuitive way to work with intervals using switch-case, with "partial matching" techniques, "Pattern-matching" etc, look at these examples:…
-
14
votes2
answers8743
viewsWhat is the difference between __str__ and __repr__?
What is the difference between the methods __str__ and __repr__? They both do the same thing?
-
14
votes1
answer1251
viewsDemonstrating a slowloris attack on apache server using Python
I have the network dump (file in PCAP format captured with tcpdump) of a "chat" between the attacked server (Apache web server: 192.168.1.2) and the malicious clients: The attack was a simulation in…
-
14
votes2
answers2682
viewsIs it possible to declare Unknowns in Python to calculate constants of a Mathematical function?
I need to assemble a quadratic function Y = Ax² + bx + c from values of X and Y acquaintances. In this case, I’m trying to H(Q) = aq² + Bq + c (Gauge height as a function of flow rate). Where: H1 =…
-
13
votes3
answers2326
viewsProblem accessing an accented file (matching character)
I’m trying to list a folder (files, subfolders) in Python [2.7 on Windows XP], and I’m having problems with sharp files. I know the method os.listdir behaves differently if the argument is a single…
-
13
votes3
answers15049
viewsHow to check if a file exists using Python
How to check if a file exists, in Python, without using a block try:?
-
13
votes4
answers262
viewsHow to implement journaling in Python?
I need to do a series of operations involving binary files (you can’t use BD), and I need to ensure that they end successfully even in the event of failure in the middle of the operation. For this,…
-
13
votes3
answers8700
viewsTypeerror: not all Arguments converted During string formatting (Python 3.4)
I’m new to programming and I’m trying to do a simple code for an exercise to calculate a phone bill: t = float(input('Digite a quantidade de minutos gasta: ')) if t < 200: p = t * 0,2 if t >=…
-
13
votes2
answers54909
viewsEncoding utf-8 allows accents?
If we do # encoding: utf-8 in the first line of a Python program, we can make accents in the whole code?
-
13
votes3
answers29788
viewsMeasuring the run time of a function
How can I measure the running time of a Python function? In C#, I can use the class Stopwatch in that way var sw = new Stopwatch(); sw.Start(); AlgumaFuncao(); sw.Stop(); WriteLine(sw.ElapsedTicks);…
-
13
votes1
answer1093
views -
13
votes1
answer2693
viewsDifferences between Python versions 3.x and 2.7
I will start a project in college related to search engines and want to take the opportunity to also learn Python. I don’t know much about the language and my biggest doubt is about which version,…
-
13
votes3
answers1488
viewsUsing Lower() in a list of lists in Python
If I have such a list: Lista = [['DE','DO','OU'],['AE','YHH','OO'],['OW','LA','FOR']] and I want to leave her like this: Lista = [['de','do','ou'],['ae','yhh','oo'],['ow','la','for']] How do I do…
-
13
votes2
answers265
viewsWhen is a default argument evaluated in Python?
Let us consider the following class: class Foo: def __init__(self, values = []): self.values = values Note that we are using an instance attribute and not a class attribute, but see the following…
-
13
votes2
answers262
viewsCheck "in" return change the Python result
It is known that to check whether a particular item does not belong to a list it is sufficient to use the operator in: values = [1, 2, 3, 4, 5] if 9 not in values: print("9 não pertence à lista.")…
-
12
votes3
answers278
viewsHow to prevent Django from finding Ids in the template?
I’m using Django 1.4 with path (L10N) enabled, which causes the numeric values in the template to be formatted: 1.234,56. The problem is that every time I put one ID in the template, for example:…
-
12
votes2
answers377
viewsOverride the Python mock Decorator
I have a class TestCase where all tests, except one, need to patch the same object. I am using the Python Mock, and I did the following: @mock.patch('metodo_alvo', mock.Mock(return_value=1)) class…
-
12
votes3
answers611
viewsHow to ensure that a service is always running on Centos?
I created a startup script for my application in Centos, so I can use the commands below to initialize/stop the application: service django_app start service django_app stop How do I ensure this…
-
12
votes1
answer253
viewsIs it wise to implement functions that only call for other functions?
I learned that functions should be small and concise. This rule would also apply to functions like this? def run_game(self): process_input() update_state() render() What I mean is: run_game appears…
-
12
votes2
answers1657
viewsHow to generate noise in an image using python?
How to generate a periodic noise of the type Moiré in an image at gray levels using Python?
-
12
votes2
answers2077
viewsWhat Class Does in Python
I’m new to programming but there’s one thing I don’t understand. What’s the point of class in Python? Because it seems to me that with function I can do everything without using class. I’ve already…
-
12
votes6
answers16210
viewsHow do I return a value in the Brazilian currency format in the Django view?
How to return the value 1768 in BRL currency format 1.768,00 in the Django view? def moeda(request): valor = 1768 # formata o valor return HttpResponse('Valor: %s' % valor)…
-
12
votes2
answers1195
viewsDifference from #! in the first line of a Python script
For a Python script to be executable on a Linux/Unix-based operating system, it must start with the designated shebang (#!): #! /usr/bin/env python But followed by the same I have seen used two…
-
12
votes4
answers17968
viewsRemove elements from a Python List
I have a problem with lists in Python. I have a list of 2 different variables with the same value, but if I remove an element from any of the lists, the list that was meant to be intact also has its…
-
12
votes4
answers33522
viewsHow to read a CSV file in Python?
I need to read a very large CSV file (+ 300,000 lines). What is the best way to read a CSV file in python?
pythonasked 9 years ago Leo Ribeiro 581 -
12
votes3
answers8338
viewsWhat does %=mean?
I have a code that contains the situation below: id %= 1000 But I don’t know the function of that operator.
-
12
votes4
answers3402
viewsHow to detect the operating system with Python?
How can I do in Python to identify which operating system is being used?
-
12
votes5
answers15940
viewsHow to create a directory in Python?
How can I use Python to create a certain directory? For example: app/ main.py How could I create a directory called temp inside app through Python main.py?…
-
12
votes1
answer148
viewsWhen it comes to Python’s "list", is it wrong to call it an "array"?
In an answer given to a question I asked here on the site, I fixed a user who wrote "array" when referring to a Python "list". I did this because it is a common nomenclature to be used in Python,…
-
12
votes1
answer1016
viewsWhat is Python Egg?
Reading a book about Python, the author shows a command in which it is possible to create a Egg and make upload of your package on Pypi (python official package repository). Basically using the…
-
12
votes4
answers11601
viewsSet constant in Python
How can I declare a constant in Python the same way I do in C with #Define PI 3.1415 or in java public final double PI = 3.1415…
-
12
votes1
answer25845
viewsIn Python we have the switch function?
In Python we have the function switch? wanted to create a program but didn’t want to use many if, and that’s why the function switch help, but I’ve seen several videos Python lessons and no talk of…
-
12
votes2
answers3744
viewsHow does the "for" inline command work?
I made a question about a Python algorithm, but reply of user Anderson Carlos Woss he used a kind of for inline that I had never seen and that left me confused. Follow the code section corresponding…
-
12
votes3
answers5645
viewsWhat is the purpose of declaring a function within a function?
In Python it is possible to declare a function within another function, as shown in the following code. def foo(palavra=None): print(palavra) def bar(outra_palavra=None): print(outra_palavra) if…
-
12
votes1
answer4268
viewsHow to get any kind of cookie using Selenium?
Summary To summarize everything I said here below, I need to open Whatsapp Web read the QR Code and save cookies so that at the next Selenium startup I do not need to read the QR Code again. I found…
-
12
votes2
answers1007
viewsWhat is Ellipsis in Python?
In the list of native constants from Python, you can find Ellipsis. print(Ellipsis, type(Ellipsis)) #-> (Ellipsis, <type 'ellipsis'>) In Python 3, there is still syntactic sugar ... that…