Basic calculator in Python I am a beginner in Python, I want to know if this code can be improved. Is it possible to make it work without the if/else?

number1=int(input('Enter first number: '))
number2=int(input('Enter Second number: '))
operation=input("Enter operation\n[+,-,*,/]")
if operation == '+':
    sum=number1+number2
elif operation == '-':
    sum =number1-number2
elif operation == '*':
    sum =number1*number2
else:
    sum =number1//number2
print(sum)
If you don't want any if statements then you can do the same with the operator library and a dictionary. This is by using the dict.get method that allows you to have a default value (operator.floordiv) if the value isn't in the dictionary. After getting a valid function from the dictionary, you then just have to call the function.

import operator

operators = {
    '+': operator.add,
    '-': operator.sub,
    '*': operator.mul
}

number1 = int(input('Enter first number: '))
number2 = int(input('Enter Second number: '))
operation = input("Enter operation\n[+,-,*,/]")
print(operators.get(operation, operator.floordiv)(number1, number2))


I would however not assume division if the user enters random data. And so I'd add an if and else. In this case I'd check that the operator is in the operators dictionary, and perform the operation if it is. If it isn't, then I'd tell the user that they entered an invalid operator:

operators = {
    '+': operator.add,
    '-': operator.sub,
    '*': operator.mul,
    '/': operator.floordiv
}

if operation in operators:
    print(operators[operation](number1, number2))
else:
    print('Invalid operator')


You can also make the operator list in the input automatically created from the dictionary too. To create the string '+,-,*,/' you can use ','.join(['+', '-', '*', '/']), to get the array you can then use operators.keys(). Finally you'd have to insert it into the rest of the string, and so I'd use str.format.

operation = input("Enter operation\n[{}]".format(','.join(operators.keys())))


And so tying this all together you could use:

import operator

operators = {
    '+': operator.add,
    '-': operator.sub,
    '*': operator.mul,
    '/': operator.floordiv
}

number1 = int(input('Enter first number: '))
number2 = int(input('Enter Second number: '))
operation = input("Enter operation\n[{}]".format(','.join(operators.keys())))
if operation in operators:
    print(operators[operation](number1, number2))
else:
    print('Invalid operator')