Login

Search

Learn
2022.11.03 22:26

1.2 Variable

Views 82 Votes 0 Comment 0

In this lesson, we're going to learn about variables. What is a variable? A name or space that stores a value or data is called a variable because it can change at any time.

 

 

Variables point to integer values

Okay, on my desk I have a clock, a lighter, a toothbrush, a pen, and the paper cup I've been using to drink water for a week. How much is my watch worth? Five dollars? No, let's just say that my watch is worth 100,000 dollars.

Now let's say someone wants to buy my watch. How much is my watch worth? It's called value. If I ask for a million dollars, it's a million dollar watch.

Let's programmatically express the value of the watch

>>> watch = 100000

 

 

This is how I write that my watch is worth 100,000 won.

Now, let's express the lighter the same way

>>> lighter = 300

 

 

The lighter costs 300 won.

 

 

Using variables to calculate numbers

So, how much would you have to give me to buy a watch and a lighter from me? If you want to buy a watch, you'll have to give me a million won instead of 100,000 won, so you'll have to increase the value of the watch.

>>> watch = 1000000

 

 

The watch that cost 100,000 won is now worth 1 million won, so what's the total?

Some of you do the math by eye, right? Let's ask the computer to do it for us.

>>> watch + lighter
1000300

 

 

Wow, the smart computer did the math in a flash. You can put a value in a variable multiple times.

 

 

Calculating the value of a variable and assigning it to the same variable again

You want to sell your watch to buy a gaming console. What is the 15% discount on a watch that costs 1 million won?

>>> 1000000 * 0.85
850000.0

 

 

(Of course, you could also calculate it as 1000000 - 1000000 * 0.15or 1000000 * (1 - 0.15), but we'll stick with the above).

Similarly, if you wanted to discount the value pointed to by some variable by 15%, you could probably represent it with code like this

>>> price = 5000
>>> price = price * 0.85
>>> price
4250.0

 

 

The above sentence in the form variable = variable * value can also be abbreviated to variable *= value

>>> price = 5000
>>> price *= 0.85
>>> price
4250.0

 

 

Variables that point to strings

Variables can contain letters as well as numbers.

>>> a = 'pig'

 

 

Thistells the variable a to contain the string 'pig'(multiple letters), so aequals 'pig'. Notice the quotation marks around 'pig'. Without the quotationmarks, it would be mistaken for the variable pig. The quotationmarks areused to say “pig is a string”. Now, let's put the string 'dad' in a variable called b.

>>> b = 'dad'

 

 

That's easy, right? Now let's try concatenating strings together

>>> a + b
pigdad

 

 

We used the same method as when we added the watch and lighter values, and the strings were joined together. Isn't that fun? Not fun? Well, let's try something a little more fun.

>>> a + ' ' + b
pig dad

 

 

Here, we've created astring called 'pig dad'by concatenating avariable called awith ' '(one space character) and a variable called b.Does that make sense?


List of Articles
No. Subject Author Date Views
Notice Learn What is Python? admin 2024.10.19 81
44 Learn 8.1 Exception handling (try, except) admin 2024.11.17 43
43 Learn 7.1 Class and Instance admin 2024.11.17 40
42 Learn 6.1 Text file admin 2024.11.17 37
41 Learn 5.3 Different modules admin 2024.11.17 37
40 Learn 5.2 Import modules admin 2024.11.17 46
39 Learn 5.1 What is module? admin 2024.11.17 35
38 Learn 4.5 Set admin 2024.11.17 37
37 Learn 4.3 Tuple admin 2024.11.17 35
36 Learn 4.2 String and List admin 2024.11.17 31
35 Learn 4.1 Data types admin 2024.11.17 33
34 Learn 3.4 Lambda admin 2024.11.17 39
33 Learn 3.3 Global and local variables admin 2024.11.17 38
32 Learn 3.2 return statement admin 2022.11.29 32
31 Learn 3.1 Function admin 2022.11.27 33
30 Learn 2.3 Iterative statements using for loop admin 2022.11.26 40
29 Learn 2.2 if-elif-else statements admin 2022.11.22 34
28 Learn 2.1 Iterative statements using while admin 2022.11.19 32
27 Learn 1.4 Interpreter and Compiler admin 2022.11.05 37
26 Learn 1.3 LIst admin 2022.11.03 33
» Learn 1.2 Variable admin 2022.11.03 82
Board Pagination Prev 1 2 3 Next
/ 3