Questions tagged as 'python-3.x'

2
answers

Format String "28122016" for date "2016-12-28"

I know it's a 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 "28122016" format and converts it to "2016-12-28" . How can I do this?     
asked by 24.01.2017 / 12:49
3
answers

How to calculate perfect numbers quickly?

I'm trying to perform an exercise to show the perfect numbers present within a given range, but I can only do this to the perfect fourth number. If I increase the range, it takes a long time and does not respond: def perfeito(n_max): lista...
asked by 24.09.2018 / 19:56
3
answers

What is the purpose of declaring a function within a function?

In Python it is possible to declare a function inside another function, as shown in the following code. def foo(palavra=None): print(palavra) def bar(outra_palavra=None): print(outra_palavra) if __name__ == '__main__': fo...
asked by 05.11.2017 / 07:54
2
answers

Differences between Python versions?

Hello! I recently decided to start studying the Python language, however, when researching the language, I noticed that there are differences between version 2 and 3. Are these differences really significant?     
asked by 11.12.2015 / 09:57
1
answer

How to calculate the shannon entropy based on the HTTP header

The Shannon entropy is given by the formula: WhereTiwillbethedataextractedfrommynetworkdump(dump.pcap).TheendofanHTTPheaderonaregularconnectionismarkedby\r\n\r\n: ExampleofanincompleteHTTPheader(couldbeadenialofserviceattack): Mygoalistocalc...
asked by 01.09.2017 / 20:51
2
answers

What 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 did not understand the code snippet: guess = f'{first_part}{last_part}'.encode() I need an explana...
asked by 27.09.2018 / 15:20
1
answer

What is the difference of global and nonlocal in Python?

In Python, there are two statements that are used to reference variables external to the local scope: global and nonlocal . What's the difference between the two statements? When to use each?
asked by 27.10.2017 / 23:10
1
answer

Why 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 should generate the same result, which only changes is the order that the multiplications are made, but apparently they are treated differently by the interpreter. > In th...
asked by 03.01.2019 / 12:19
4
answers

How to generate random numbers in Python?

I'd like to know how to generate random numbers in Python. I'm with version 3.4.     
asked by 24.07.2015 / 02:02
4
answers

Two "return" in a function

How does it work if there are two (+) return in a Python function? Would you like to return the second value, or just the first one? Example: def soma(a, b): x = a + b y = 'qualquer coisa' return x return y     
asked by 21.09.2018 / 02:36