# Variable

img

  • Variable looks like a box which you can container one data.

# Type of Variable

# Int (Integer)

3, 4, 5, 100, 99, 0, -5

# Float/Double

3.5, 1.5, 1/2, 31.0

# String

"HelloWorld"

"Hi"

# Boolean

True

False

  • Boolean can be only True or False.

# Naming Variable Dos and Don'ts

  • Start with a letter
  • Symbols and spaces are not allowed (-, /, #, @)
  • The underscore can be used (For example, first_name, last_name)

# Assignment Operator (=)

Very Important!

Assign value of right side to left side.

# Assignment

a = 10
print(a)
1
2
output

OUTPUT

10

# Replacement

a = 10
print(a)
a = 30
print(a)
1
2
3
4
output

OUTPUT

10 30

Click me for more
 




a = 10
print(a)
a = 30
print(a)
1
2
3
4

img


img



 


a = 10
print(a)
a = 30
print(a)
1
2
3
4

img

a = 10
print(a)
a = "Hello"
print(a)
1
2
3
4
output

OUTPUT

10 Hello

# Changing

a = 10
print(a)
a = a + 1
print(a)
1
2
3
4
output

OUTPUT

10 11

Click me for more
 




a = 10
print(a)
a = a + 1
print(a)
1
2
3
4

img



 


a = 10
print(a)
a = a + 1
print(a)
1
2
3
4

img img

# Operator

Syntax Description
+ Plus
- Minus
* Multiply
/ Divide
% Modulus (หารเอาแต่เศษ)
// Floor Divide (หารไม่เอาเศษ)
** Exponentiation (ยกกำลัง)

Example

5%2 == 1 9//2 == 4 8%3 == 2 4**2 == 16

# Let's practice together

a = 4
b = 5
print(a+b)

a = "4"
b = "5"
print(a+b)
1
2
3
4
5
6
7
output

OUTPUT

9 45

a = 4
b = "5"
print(a+b)
1
2
3
output

OUTPUT

Traceback (most recent call last): File "<string>", line 3, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'str'

# Exercise Book 02.Variable