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
» Learn 1.2 Variable admin 2022.11.03 82
23 Learn 1.1 Getting into Python admin 2022.11.01 105
22 Learn πŸ‡°πŸ‡· 8.1 μ˜ˆμ™Έμ²˜λ¦¬(try, except) admin 2022.10.27 41
21 Learn πŸ‡°πŸ‡· 7.1. 클래슀(class)와 μΈμŠ€ν„΄μŠ€ admin 2022.10.27 69
20 Learn πŸ‡°πŸ‡· 6.1 ν…μŠ€νŠΈ 파일 admin 2022.10.21 58
19 Learn πŸ‡°πŸ‡· 5.3 μ—¬λŸ¬ 가지 λͺ¨λ“ˆ admin 2022.10.15 56
18 Learn πŸ‡°πŸ‡· 5.2 λͺ¨λ“ˆ κ°€μ Έμ˜€κΈ°(import) admin 2022.10.07 55
17 Learn πŸ‡°πŸ‡· 5.1 λͺ¨λ“ˆμ΄λž€ admin 2022.10.03 58
16 Learn πŸ‡°πŸ‡· 4.5 μ„ΈνŠΈ(set) admin 2022.10.02 58
15 Learn πŸ‡°πŸ‡· 4.4 λ”•μ…”λ„ˆλ¦¬(dict) admin 2022.09.30 61
14 Learn πŸ‡°πŸ‡· 4.3 νŠœν”Œ(tuple) admin 2022.09.21 61
13 Learn πŸ‡°πŸ‡· 4.2 λ¬Έμžμ—΄κ³Ό 리슀트 admin 2022.09.16 58
12 Learn πŸ‡°πŸ‡· 4.1 μžλ£Œν˜• admin 2022.09.16 60
11 Learn πŸ‡°πŸ‡· 3.4 λžŒλ‹€(lambda) admin 2022.09.16 60
10 Learn πŸ‡°πŸ‡· 3.3 μ§€μ—­λ³€μˆ˜, μ „μ—­λ³€μˆ˜ admin 2022.09.15 63
9 Learn πŸ‡°πŸ‡· 3.2 λ°˜ν™˜(return)λ¬Έ admin 2022.09.10 61
8 Learn πŸ‡°πŸ‡· 3.1 ν•¨μˆ˜ admin 2022.09.01 71
7 Learn πŸ‡°πŸ‡· 2.3 forλ₯Ό μ‚¬μš©ν•˜λŠ” 반볡문 admin 2022.08.24 55
6 Learn πŸ‡°πŸ‡· 2.2 쑰건문 (if-elif-else) admin 2022.08.23 63
5 Learn πŸ‡°πŸ‡· 2.1 while을 μ‚¬μš©ν•˜λŠ” 반볡문 admin 2022.08.23 57
Board Pagination Prev 1 2 3 Next
/ 3