Variable, Statement, Expression, Functions

Cing Sian Dal
4 min readApr 16, 2020

Variables are simply places to store information and to give that information a name. As the term indicates the information stored is “variable”, meaning that it can change.

Expressions are combinations of values, variables, and operators that the Python interpreter evaluates to compute a resulting value.

Statements are units of code that have an effect, like creating a variable or displaying a value. The Python interpreter executes statements to produce the effect. (When executing a statement, the interpreter evaluates any expressions included in the statement.)

Finally, functions are named sequences of statements that perform computations. After you define a function, you can call it any number of times to have the Python interpreter execute the statements in the function.

A critical advantage of variables, expressions, statements, and functions is that they allow you to take small building blocks of functionality and compose them into larger features.

A variable is a name that refers to a value.

An assignment statement creates a new variable and gives it a value:

>>> message = 'And now for something completely different'

An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions:

>>> 42
42
>>> n
17
>>> n + 25
42

A statement is a unit of code that has an effect, like creating a variable or displaying a value.

>>> n = 17
>>> print(n)

There are two modes: script mode and interactive mode.

When an expression contains more than one operator, the order of evaluation depends on the order of operations. For mathematical operators, Python follows mathematical conventions. The acronym PEMDAS is a useful way to remember the rules:

  • Parentheses
  • Exponentiation
  • Multiplication
  • Division
  • Addition
  • Subtraction

The + operator performs string concatenation.

>>> first = 'throat'
>>> second = 'warbler'
>>> first + second

The * operator also works on strings; it performs repetition. For example, ‘Spam’*3 is

‘SpamSpamSpam’.

It is a good idea to add notes to your programs to explain in natural language what the program is doing. These notes are called comments, and they start with the # symbol:

# example of program

Syntax error — grammar rules အတိုင်း မရေးတဲ့အတွက် ဖြစ်တာ။

Runtime error — program run နေတုန်း ဖြစ်တာ။

Semantic error —error မပြဘဲနဲ့ ကိုယ်မျှော်လင့်ထားသလို့ မဖြစ်လာတာ။

function call — function name နဲ့ ခေါ်တဲ့အတွက်။

argument — function parentheses ထဲကို ထည့်လိုက်တဲ့ value ဖြစ်တယ်။

return result — function က ပြန်လာတဲ့ အဖြေကို return result ဖြစ်တယ်။

import math

module object — ဆိုတာ import နဲ့ ခေါ်ရတဲ့ နာမည်။

math.log10(ratio)

module object functions — module object ထဲက functions တွေကို dot notation နဲ့ ဆွဲခေါ်ရပါတယ်။

header — function name ကို header လို့ခေါ်တယ်

body — function အောက်မှာ ရှိတဲ့ အပိုင်းကို body လို့ခေါ်တယ်။

def foo(): # Header of the function
print('foo') # Body of the function

parameter — ဆိုတာ argument ကလာတဲ့ value သည် argument name အတိုင်း function အတွင်းမှာ variable ဖြစ်သွားတဲ့အကောင်ကို parameter လို့ ခေါ်ပါတယ်။

def foo(bar): # argument လို့ခေါ်တယ်
print(bar) # parameter ဖြစ်သွားပြီ

__main__ — topmost frame ဖြစ်တယ်

frame — ဆိုတာ function ကို ကိုယ်စားပြုပါတယ်။

traceback — ဆိုတာ function list တွေကို ဖော်ပြသွားတာဖြစ်ပါတယ်။

void function — return value မလာတဲ့ function

fruitful function — return value လာတဲ့ function

Glossary

  • variable: A name that refers to a value.
  • assignment: A statement that assigns a value to a variable.
  • state diagram: A graphical representation of a set of variables and the values they refer to.
  • keyword: A reserved word that is used to parse a program; you cannot use keywords like if, def, and while as variable names.
  • operand: One of the values on which an operator operates.
  • expression: A combination of variables, operators, and values that represents a single result.
  • evaluate: To simplify an expression by performing the operations in order to yield a single value.
  • statement: A section of code that represents a command or action. So far, the statements we have seen are assignments and print statements.
  • execute: To run a statement and do what it says.
  • interactive mode: A way of using the Python interpreter by typing code at the prompt.
  • script mode: A way of using the Python interpreter to read code from a script and run it.
  • script: A program stored in a file.
  • order of operations: Rules governing the order in which expressions involving multiple operators and operands are evaluated.
  • concatenate: To join two operands end-to-end.
  • comment: Information in a program that is meant for other programmers (or anyone reading the source code) and has no effect on the execution of the program.
  • syntax error: An error in a program that makes it impossible to parse (and therefore impossible to interpret).
  • exception: An error that is detected while the program is running.
  • semantics: The meaning of a program.
  • semantic error: An error in a program that makes it do something other than what the programmer intended.

Summary

  • Expression ဆိုတာ value နဲ့သတ်ဆိုင်တယ်။
  • Statement ဆိုတာ return effect ရှိတဲ့ code ကိုဆိုလိုတယ်။

References:

  • Think Python (Second Edition)

--

--

Cing Sian Dal

Don’t follow me. I wrote junks here. Follow me on Twitter instead.