Ask Your Question

# Substitute and evaluate

Hello, I would like to ask a really easy question:

How can I substitute a variable into an equation and have it evaluated simultaneously? If I have $f = a + b$ and would it to return $5$ if $a=2$ and $b=3$, how shall I proceed?

I've tried the following

a, b = var('a, b');
f = a + b;
a = 2; b = 3; f


But it returned a+b, which is not what I had in mind. Thanks for the help in advance!

edit retag close merge delete

## 1 Answer

Sort by ยป oldest newest most voted

You can use:

f (x,y)=x+y

a = 2; b = 3

f(a,b)

In this way you obtain 5, the desired result. Actually you don't need x and y, you could use only a and b, but I put different names just to underline the mechanism. The first line defines the function f with generic variables. The last line calculates the value of f when you actually pass the values of the variables. It can be a bit confusing at first, I hope my example can help.

more

## Comments

Thanks Antlab!

( 2014-04-17 00:04:40 +0200 )edit

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2014-04-16 06:47:55 +0200

Seen: 305 times

Last updated: Apr 16 '14