Most anwered "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
-
7
votes11
answers37801
viewsCompute sum of digits of a number
Type a program that receives an integer number in the input, calculate and print the sum of the digits of this number in the output. Example: >>> Digite um número inteiro: 123 6 Tip: To…
-
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…
-
10
votes9
answers17879
viewsHow to identify if a string is a palindrome?
A palindrome is a word or phrase that has the property that can be read from right to left and from left to right. For example, the strings "aaaaa", "1221", "bbaabb" are palindromes, however the…
-
8
votes9
answers23719
viewsInverting sequence (PYTHON 3)
I have to implement the following: Write a program that takes a sequence of integer numbers ending with 0 and prints all values in reverse order. **Note that 0 (ZERO) should not be part of the…
pythonasked 8 years ago Reginaldo Barros da Cunha 137 -
41
votes8
answers3166
viewsFewer moves from a horse to a given house in Chess
On a chessboard in any house I have a Horse (represented in red) and I will have only one other piece (represented in green) that the horse must go to it: I must use the simplest path and take into…
-
47
votes8
answers4052
viewsHow to generate 200,000 primes as fast as possible in Python?
Heed: I’m not looking for ready-made pieces of code. I just want someone to help me think about in some way to generate these 200,000 primes as efficiently as possible, in Python. I’m working on it…
-
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…
-
11
votes7
answers52205
viewsIs there a way to print everything without breaking the line?
I have this code: print "t" print "e" print "s" print "t" print "e" He’s got a way out: t e s t e What I find very boring these "\n". There is a way to print everything without line break?…
-
4
votes7
answers15010
viewsPrint "n" natural odd numbers
I’m solving an exercise that asks me to enter a value for example: 5 and print the first 5 natural odd numbers in the case: entree: Enter the value of n: 5 1 3 5 7 9 The code I wrote works but it…
-
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…
-
0
votes7
answers1969
viewsHow to remove repeated numbers from a python list
Hello guys I’m doing a course in python and currently I Completed this exercise. Write the remove_repeated function that takes as a parameter a list of integer numbers, checks whether such a list…
-
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)…
-
10
votes6
answers65891
viewsHow to convert a string variable to int?
As I correctly declare a value whole, for he is returning that my variables are strings. n1 = input("informe um número ") n2 = input("informe um número ") soma = n1 + n2 print ("O resultado da soma…
-
6
votes6
answers37862
viewsHow to remove characters from a string?
The program must read two strings and remove from the first string all the letters that occur in the second string. Example: Be the strings "chocolate" and "hollow", then the program should print…
-
4
votes6
answers41177
viewsHow to find the position of an item on a list?
I am writing a program that takes a specific amount of values, and returns the lowest value of the list and its position. It is as follows: quantidade = int(raw_input()) numeros = raw_input()…
-
24
votes6
answers40933
viewsDoes ternary surgery exist in Python?
I often see several programming languages where a ternary operation is almost always identical. (x % 2 == 0) ? "par" : "impar" However, when I went to try to do this in Python, it was wrong: (x % 2…
-
4
votes6
answers42162
viewsRemoving duplicate elements in a python list
I have to do a function and I’m not being able to develop the logic. Here’s the thing: Write the remove_repeated function that takes as a parameter a list of integer numbers, checks whether such a…
-
-3
votes6
answers6752
viewsGet the list of prime numbers smaller than N
I have an exercise in which I need to enter a number (N) and calculate with Python which are the numbers below N that are primes. That’s the code I have now. num == int(input("Insira um número: "))…
pythonasked 7 years, 10 months ago Rodrigo Costa 13 -
0
votes6
answers14517
viewsHow to define the largest prime number within a given number?
For example, you give the number 100 and the highest prime number within 100 is 97. How can I program this? "Write the maior_prime function that takes an integer number greater than or equal to 2 as…
-
7
votes6
answers49406
viewshow to search for an element in a list that is inside another list?
I have the following list composed of lists. [['julian', '0', '5'], ['ana', '10', '4']] and I need to use a function that tells me the position of any element within that list, I tried to use:…
-
0
votes6
answers7088
viewsIntersection between two sets (element that is in set A and B at the same time)
I’m trying to make a function that returns the intersection between two sets: def intersecao(conjuntoA, conjuntoB): inter = [x for x in range(1,12) if x in conjuntoA and conjuntoB] return inter a =…
-
1
votes6
answers2884
viewsmusic does not play
I have the following python code #*-coding:utf-8;-* import pygame pygame.init() pygame.mixer.music.load('ex1.mp3') pygame.mixer.music.play() pygame.event.wait() In this case, it should play a song…
-
0
votes6
answers4819
viewsFibonacci-Python
Construct a function, that is, an algorithm that receive the maximum number of the sequence of Fibonacci. Returning all numbers from zero to zero Fibonacci number less than or equal to the number…
-
4
votes6
answers491
viewsOptimize code in Python
I made the code below but I have the feeling that it can be improved and that maybe I’m turning it too much. It is a code in which I have a text input and if it is not written in all uppercase…
-
0
votes6
answers342
viewsGet a list of the first multiples of a number
I need to do a program that determines the first 5 multiples of 3, and I wanted to put the values of the first 5 numbers into a variable, but I don’t know how to do that. I tried to make: for index…
-
7
votes6
answers19845
viewsHow to change the name of the pandas dataframe column?
I am using pandas to process a CSV file in the following structure: nome;idade Fulano;28 Joao da Silva;27 Maria;29 The reading of the file is done as follows: import pandas as pd df =…
-
0
votes6
answers444
viewsCounting multiples of an integer is giving very different values than expected
When receiving two integer numbers, I have to show how many multiples the number n1 (typed 3) has until you reach the n2 (typed 44). The right result should be: The number 3 has 14 multiples smaller…
-
21
votes5
answers8633
viewsWhat is the most complete way to install python on Windows?
I know this question can be interpreted as an argument, so I did it with the word "complete" instead of "better". I’m a python user on GNU/Linux and here it comes more or less pre-installed. Still,…
-
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…
-
25
votes5
answers80372
viewsHow to "round" a float in Python?
I have this sum: total = 0.1 + 0.2 print total #retorna 0.30000000000000004 How would I "round" the decimal places and return only the number of places summed? Example: totalA = 0.1 + 0.2…
-
2
votes5
answers11991
viewsHow to validate and calculate the control digit of a CPF
How does the algorithm that calculates the digit of a CPF (Cadastro da Pessoa Física Brasileiro) work? And how is this calculation used to validate the CPF? If possible, I would like examples in…
-
25
votes5
answers63261
viewsHow to clean the console in Python?
I am an Operating System user Ubuntu and when I want to clean the terminal, I use the command clear. >>> clear However, in the Python, how could I do to clear the terminal, when I use it on…
-
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.
-
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)); //…
-
4
votes5
answers322
viewsPython how to print output
How can I print a list this way, example: l=[4,26,32] I want to print the output as follows: 4 26 32 No comma and a blank space on the same line. Grateful from now on.…
-
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?…
-
20
votes5
answers140931
viewsHow to limit decimal numbers in Python?
How do I format the number of decimals of a decimal number in Python? For example, I want to display only two decimal places of the following number: numero_decimal = 3.141592653589793 How could I…
-
8
votes5
answers23915
viewsHow do I know if the variable is an integer in Python?
How can I know if the variable is an integer in Python? I know how to do it in PHP: is_int($variable); // bool(true) But how can I do in Python? Example: numero = 1 #é numero nao_eh_numero = 'texto'…
-
5
votes5
answers1463
viewsBeginner in Python, Else and Elif
idade = int(input("Insira sua idade:")) if(idade<=0): print("Sua idade nao pode ser 0 ou menos de zero") elif(idade>150): print("sua idade nao pode ser maior de 150 anos") elif(idade<18):…
pythonasked 8 years, 2 months ago Eduardo Rodrigues 51 -
10
votes5
answers5611
viewsDraw square on the canvas using wire fences (#)
Hello I’m starting to program in python and I’m having a hard time doing an exercise where I have to do a rectangle with "#". largura = int(input("Digite a largura: ")) altura = int(input("Digite a…
pythonasked 8 years, 2 months ago Angelo Spinardi 103 -
1
votes5
answers789
viewsList comprehension with conditional
My goal is to count how many elements of a given list correspond to a condition. To do so, I made the following lines of code: cont = 0 seq = [] max = 10 for x in seq if x == max: cont = cont+1…
-
5
votes5
answers29639
viewsHow to convert an int number to string in python?
I need to do an exercise where I input an integer variable of 3 digits and the python has to show it reversed, but up to where I know an integer variable n can do this, but a string can. What can I…
-
8
votes5
answers10407
viewsIdentify if there is an uppercase letter in the string
I have declared two certain variables, one of which is uppercase and the other only lowercase. See the example below: actor = "Jon Snow" pet = "wolf" How can I identify if it exists at least one…
-
5
votes5
answers13183
viewsPython - NIM game
Enunciation You must write a program in the Python language, version 3, which will allow a "victim" to play the NIM against the computer. The computer, of course, must follow the winning strategy…
pythonasked 8 years, 1 month ago Mário Rodeghiero 389 -
1
votes5
answers12449
viewsHow to separate a digit from an integer?
For example, for the program to tell the number of the hundreds of a number from: número_int = int(input("Digite um número inteiro: "))
-
6
votes5
answers21141
viewsWhat is the functionality of n?
I’m learning to program in Python and some basic things still confuse me, for example this question I asked. What is the use of n?
-
0
votes5
answers9375
viewsProgram to find MMC in Python
I was doing a basic exercise for the function While, he asked to create a program that found the MMC between 2 numbers... I managed to do, but the program continues to print the answer non-stop on…
-
3
votes5
answers7921
viewsHow to properly format CPF in Python?
I have the following code: test = input ("CPF: ") When the typed CPF gets: 12345678900 But when I return this input he comes back: 123.456.789-00 How do I do it in Python?…
-
5
votes5
answers11566
viewsHow to find the type of a variable given by the user?
I would like to ask the user to type something and find out what kind of what he wrote. So far I only know the input(), but the input() only returns the type String. If I put int(input()) or…