Chapter 3
Variable for Numbers
A string isn't the lone thing you can allocate to a variable. You can likewise allot a number.
weight = 150
Having to code the
articulation above, at whatever point you compose weight
in your code, Python realizes you mean 150. You can utilize this variable in
numerical counts.
In the event that you request that Python adds 25 to weight...
weight + 25
...Python, maintenance
that weight states to 150, will concoct the
aggregate 175.
In contrast to a
string, a number isn't encased in statements. That is the way Python realizes
it's a number that it can do the math on and not a content string, similar to a ZIP
code, that it grasps as text.
Yet, at that point,
since it's not encased in statements, how does Python know it is anything but a
variable? Indeed, in light of the fact that a number can't be utilized as a
variable name. In the event that it's a number, Python disposes of it as a
variable. So, it should be a number.
On the off chance that
you encase a number in concentrate denotes, it's a string. Python can't do an expansion on it. It can do expansion just on numbers not encased in statements.
Presently see this code.
1 original num = 23
2 new num = original num + 7
In the second
proclamation in the code above, Python substitutes the number 23 when it
experiences the variable original num. It
adds 7 to 23. it relegates the outcome, 30, to the variable new num.
Python can likewise do a computation comprised of only factors. For instance.
1 original num = 23
2 num to be added = 7
3 new num = original num + num to
be added
The variable new num now has an estimation of 30.
A variable can be utilized in figuring its own new worth.
1 original num = 90
2 original num = original num +
10
The original num now
has an estimation of 100.
On the chance that you encase a number in quotes and add 7...
1 original num = "23"
2 new num = original num + 7
...it will not work,
since Python can't total a string and a number.
Note that an adjustable name can be the name of a number adjustable or a thread adjustable. From Python's perspective, there's nothing in a name that indicates some sort of factor. a variable can begin as one kind of factor, at that point become another sort of factor. You could compose…
Your age = "21"
…and the variable your age alludes to a string. You can't do the math on it. Yet, at that point on the off chance that you compose…
Your age = 21
the variable your age no longer alludes to a string. It alludes
to a number. You can do the math on it.
I've disclosed to you that a variable name can't be a number. In any case, you can remember numbers for a variable name — as long as you don't start the name with a number.
1st_prime_number = 2
...is illicit, because
of that underlying 1 in the variable name.
Be that as it may, this the variable name, where the 1 comes later in the name,
prime_number_that_comes1st = 2
In this current part's
models, the numbers I relegated to factors were whole numbers — entire numbers
like 2, 47, 0, and - 5. You can likewise allocate buoys to factors—numbers like
1.7, - .005, and 1.00009.
No comments:
Post a Comment