Most viewed "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
-
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…
-
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.
-
4
votes2
answers94597
viewsHow to break the line?
Nome = input("Digite o nome do cliente: ") DiaVencimento = input("Digite o dia de vencimento: ") MêsVencimento = input("Digite o mês de vencimento: ") ValorFatura = input("Digite o valor da fatura:…
-
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…
-
11
votes2
answers70082
viewsHow to concatenate multiple Strings in Python?
The following method c.filtraNome(nome) carry out a consultation at the bank (sqlite3), however, I don’t know how I can group each field and return the already formatted query in the following way:…
-
7
votes3
answers69151
viewsVectors in Python
Okay, I’m coming from line C, and I’m having a hard time creating vectors in python. In C if I write vet = [5]; I would create a vector called vet with 5 indices. I wish I knew how to do this in…
-
7
votes3
answers68693
viewsHow to create a python array
I want to create an array where the user specifies the number of rows and the number of columns. Initially an empty matrix was given: matrix = [] The codes must start from there the code below was…
-
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…
-
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…
-
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?
-
3
votes2
answers53582
viewsAccess to Xmlhttprequest has been blocked by CORS policy
I have two Flask applications running locally on my machine. One is running on port 5000 and the other on port 5050. I need to make a 5000 app call to 5050. But I’m having this problem: Access to…
-
1
votes2
answers52286
viewsWhich operator is equivalent to different in Python?
I know some other operators: Greater than: > Less than: < Equality: == But the "different" operator, as it is in Python?…
-
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?…
-
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…
-
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:…
-
16
votes3
answers44552
viewsHow to connect Python to Mysql database?
I wonder how I can make a simple connection to Mysql through a Python script. To do this I need to install some library? Or is there something that comes by default in Python (like in PHP)?…
-
18
votes3
answers44347
viewsHow to create a python " *.exe" executable?
I need to turn " *.py" files into executables " *.exe" to run on any Windows desktop. Currently I use 3.3
pythonasked 10 years, 11 months ago paulosdiasabreu 199 -
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
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…
-
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?
-
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()…
-
6
votes2
answers40971
viewsProblem using Switch Case in Python 3
print ("ESCOLHA A CERVEJA PELO NUMERO") print ("1-ANTARTICA R$6.00;2-SKOL R$6.50;3-BRAHMA R$8.20;4-SOL R$8.25;") cerveja = input ("5-NORTENHA R$16.80;6-PROIBIDA R$4.80;7-DEVASSA R$5.90;8-HEINEKEN…
-
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…
-
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…
-
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…
-
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…
-
6
votes3
answers37709
viewsRemove element from a list by key (key) and by value (value)
I have the following list my_list = [1,2,3,4,5,6,7,8,9] Let’s say I want to remove num 7 by value and/or key and keep: my_list = [1,2,3,4,5,6,8,9] How do I do?…
pythonasked 8 years, 9 months ago Alberto Pimenta 169 -
-1
votes1
answer36794
viewsHow do I place items on a list in ascending or descending order?
I am trying to put the numbers of a list in ascending order but the Sort() method is putting the 10 and its multiples (20, 30) as being similar to itself divided by 10. For example, in the following…
-
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.
-
11
votes2
answers35240
viewsHow to develop programs for Android through Python?
To develop programs for Android in Python it is necessary to build a framework in such a way that the application is executable on Android phones? I am new as Android programmer, I am studying and I…
-
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á"…
-
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, 4 months ago Leo Ribeiro 581 -
2
votes3
answers31234
viewsHow to print skipping lines (each variable in a row)?
How to print skipping lines (each variable in a row)? nome ='Paulo' profissao = 'estudante' escola = 'estadual dourado' idade = 18 print 'Nome: '+nome + 'Trabalho: '+profissao + 'Escola: ' +escola+…
-
2
votes2
answers30328
viewsHow to run a . py program by python IDLE on Windows?
I’m learning to code in python and I’ve made a small program, but I can’t run it through the python terminal (IDLE or sheel) program name:salario_real.py error…
-
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);…
-
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…
-
31
votes1
answer29273
viewsWhat’s the difference between break, pass and continue in Python?
The Python documentation in Portuguese is a work in progress, and reasonably confusing as you can see. I have difficulties in English and I found that website that I can’t read. So how can I use…
-
2
votes2
answers29234
viewsCreate . txt file in PYTHON if it does not exist
The code: arquivo = open(input('Nome do arquivo a ser editado:'), 'r') texto = arquivo.readlines() texto.append(input('Insira o texto:')) arquivo = open(input('Nome do arquivo a ser editado:'), 'w')…
-
7
votes3
answers27933
viewsPython str.replace() doubt
I have the following text:: 'box 1 is blue, box 10 is green, box 100 is empty' I want to replace 'box 1' with 'package 2', so I do: >>> texto = 'caixa 1 é azul, caixa 10 está verde, caixa…
-
5
votes3
answers26954
viewsPython maximum and minimum
What is the function I use to find the maximum and minimum values between 2 values in Python? Thank you
-
2
votes2
answers26379
viewsReturning only the highest value of a Python list
I have the list ['4', '07', '08', '2017', '364', '355673087875675'] and would like to re a formula to return the higher value, which in this case would be the '355673087875675', tried to use the…
-
-1
votes4
answers26369
viewsFind certain text in a string
Hello I’m having a doubt. nome=str(input('Qual o seu nome completo?')) print('Seu nome tem Enzo?{}'.format('Enzo' in nome.lower())) I have this code that it checks without having a certain string…
-
-2
votes1
answer25847
viewsCommand to finish the program
Is there a command to finish the program in Python or can only finish using ways like this: print()
-
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…
-
19
votes1
answer25589
viewsWhen should I use __init__ in functions within classes?
For the book I am studying, in some moments the author uses __init__ as a first function of a class. This function (and others) always has self as one of the variables (something I haven’t yet…
-
11
votes3
answers25171
viewsHow to write multiple lines in Python?
How can I write a string with multiple lines in Python? As I come from PHP, I usually do so: $string = " uma linha outra linha2 " Or else: $string = <<<EOT uma linha outra linha EOT; When I…
-
1
votes4
answers25024
viewsDifference between If and Elif
What is the difference between the two code snippets if variavel < 10: variavel = 1 if variavel >= 10 and <15: variavel = 2 . . . else variavel = 0 if variavel < 10: variavel = 1 elif…
pythonasked 8 years, 4 months ago Guilherme Lima 3,129 -
1
votes1
answer24100
viewsInput numbers in the same line in python
I need to read three values in a single variable being the third with comma. Input: two data lines. In each row there will be 3 values, respectively two integers and a value with 2 decimal places.…
-
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'…
-
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