Learn Python - 3 [Variables]

in #python6 years ago

Photo 11.03.2018 4 45 02 PM.jpg
One of the building blocks of programming is associating a name to a value. The associated name is usually called a variable. A variable is nothing more than a name for something so you can use the name rather than the something as you code. Main reason assign a value to a name is to carry that value through your code and make your code readable like English.

Naming variables is important. When you look your codes they will help you to understand what you tried to achieve. Or someone else will not have hard time to read and understand your code.

Lets assign a value to a name:

>>> code = 121
>>> code
121

Its enough simple to define a variable: first find a meaningful name and then use = operator and last value you want to assign.

>>> x = 4
>>> x * x
16
>>> initial_value = 100
>>> current_value = 120
>>> total = initial_value + current_value
>>> total
220

Notes

  • x=4, x = 4 both are the same but first one is bad form and so add space around operators like this so that it’s easier to read.
  • Use _ underscore if your variable name is contains two word Its a good form and making all character lower alsp good form current_coupon = 3.5

Last if you try to get a variable value but not defined then you will get an error:

>>> foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined

Coin Marketplace

STEEM 0.28
TRX 0.13
JST 0.032
BTC 60793.50
ETH 2910.51
USDT 1.00
SBD 3.59