Calculator in one line of code in python

preview_player
Показать описание
Create a simple Calculator in one line of code in python using the eval() function that evaluates any python expression , it is highly recommended that you dont use eval() function in your python projects , using eval is a bad practice. Just to name a few reasons:
1) There is almost always a better way to do it
2) Very dangerous and insecure as eval can run any python expressions , this can be used against your application by hackers.
3) Makes debugging difficult
Slow

#python #pythontricks #pythontips #learnpython #pythonforbeginners
#coding #programming #pythonnetworking #requests
Рекомендации по теме
Комментарии
Автор

Just type python in cmd and enter
We can do any calculation..
No need of code..😅😅

naveenkumargembali
Автор

print(eval(input("Enter your Opretion: ")))
Isko run karne ke baad terminal me apna Operation perform type kre example 1+2
answer 3 aaye
Uske just baat mujhe phir se code run na karna pare code automatically run ho jaye or Enter your Opretion: Show hone lage or mai apna dusra Opretion perform kar saku.

Kaise hoga?

RohanKumar-whfc
Автор

this example is pretty risky because eval() executes not only arithmetic expressions, but any python code you give it. so you can easily hack it.

InsaneParrot
Автор

You literally started doing calculations in Powershell after the Python interpreter exited 💀

amongusisdeadstopjokingabo
Автор

Bro your second calculation was done by Powershell

laszlotorhosi
Автор

im very very beginner to coding but you could just do
print (input)
right?

icyi
Автор

that's not good considering if someone enters python code instead of an expression and the program runs it. (not suppose to happen in a calculator)

a better code would be -
no = list(map(int, input("Enter 2 numbers separated by space : ").split()))

print({"add" : str(no[0]) + " + " + str(no[1]) + " = " + str(no[0]+no[1]), "subtract":str(no[0]) + " - " + str(no[1]) + " = " + str(no[0]-no[1]), "multiply":str(no[0]) + " x " + str(no[1]) + " = "+str(no[0]*no[1]), "divide":str(no[0]) + " ÷ " + str(no[1]) + " = " + str(no[0]/no[1])}[input("What would you like to do with the numbers?(add, subtract, multiply, divide) : ")])


This is 2 lines of code, but isn't vulnerable to injection attacks.

arpitashrivas