Python Introduction | Basics of Values and Calculations | Variables and Operations

Variables

Values are rarely used only once. Usually, they are stored in variables.

A variable represents a memory area prepared to store a value. For now, it is enough to think of a variable as a container that can hold different values. A program stores values in variables, calculates with them, and stores the results again.

Use an equals sign (=) to assign a value to a variable.

variable_name = value

The value on the right is assigned to the variable on the left. In Python, assigning a value creates the variable immediately. You do not need to declare it in advance.

For example, a = 10 creates a variable named a. You can then use that variable like a value.

a = 10
print(a)
b = 'Hello'
print(b)

Calculations

Variables are useful not only for storing values, but also for calculating and storing results.

Numeric Operations

Use the familiar arithmetic operators for numbers: addition (+), subtraction (-), multiplication (*), division (/), and remainder (%). The remainder operator calculates what is left after division.

a = 10
b = 20
c = a + b
print(c)

Python also has an exponentiation operator: **. For example, write 10**2 for 10 squared.

Text Operations

Text values support addition and multiplication.

  • The + operator concatenates the text on the left and right.
  • The * operator repeats the text on the left the specified number of times.

For example, 'A' + 'B' produces "AB", and 'A' * 3 produces "AAA".

a = 'A'
b = 'B'
c = a + b
print(c)

print('A' * 3)

Output:

>>> a = 'A'
>>> b = 'B'
>>> c = a + b
>>> print(c)
AB
>>>
>>> print('A' * 3)
AAA
>>>