Most voted "python-3.x" questions
Python 3 is the latest version of the Python programming language and was formally released on December 3, 2008.
Learn more…3,274 questions
Sort by count of
-
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
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
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
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():…
-
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…
-
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
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,…
-
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
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…
-
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…
-
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
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
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…
-
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
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
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
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
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
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
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
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
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…
-
10
votes1
answer4201
views"__" or "Dunder" methods in Python, which are the most used?
In Python there are "Magic" methods, such as __len__, that allows the use of Len(Object), in addition to the one in specific, which are the most used that can facilitate the use of the structure ?…
python-3.xasked 8 years, 2 months ago Arthur Silva 186 -
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
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__…
-
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
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
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
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
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
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
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
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…