Example
x = 10
y = 2
z = x * y
print(z)
Result being 20. My question is that though I know it can assign z = 20 but what would be a reason to do it that way shown.
CodePudding user response:
Variables are just that - variable. They can be set dynamically from user input, as arguments to functions, or just change over time
For any of these cases, you need to calculate values using these "given" values.
Take for (a very simple) example, an age calculator:
current_age = int(input('What is your current age?'))
years_into_future = int(input('How many years forward would you like to calculate?'))
# You can't know this value ahead of time!
future_age = current_age years_into_future
print(f'Your age in {years_into_future} years will be {future_age}')
Because you don't know what the user input will be, you can't directly set future_age
, nor can you print years_into_future
without having it stored somewhere.