Sequence of Instructions

 Program

A program is a sequence of instructions given to a computer.

Defining a Variable

A variable gets created when you assign a value to it for the first time.

Code

age = 10
PYTHON

Printing Value in a Variable

Code

age = 10
print(age)
PYTHON

Output

10

Code

age = 10
print("age")
PYTHON

Output

age

Variable name enclosed in quotes will print variable rather than the value in it. If you intend to print value, do not enclose the variable in quotes.

Order of Instructions

Python executes the code line-by-line.

Code

print(age)
age = 10
PYTHON

Output

NameError: name 'age' is not defined

Variable

age
is not created by the time we tried to print.

Spacing in Python

Having spaces at the beginning of line causes errors.

Code

a = 10 * 5
b = 5 * 0.5
b = a + b
PYTHON

Output

File "main.py", line 3
b = a + b
^
IndentationError: unexpected indent

Variable Assignment

Values in the variables can be changed.

Code

a = 1
print(a)
a = 2
print(a)
PYTHON

Output

1
2

Examples of Variable Assignment

Code

a = 2
print(a)
a = a + 1
print(a)
PYTHON

Output

2
3

Code

a = 1
b = 2
a = b + 1
print(a)
print(b)
PYTHON

Output

3
2

Expression

An expression is a valid combination of values, variables and operators.

Examples a * b a + 2 5 * 2 + 3 * 4

BODMAS The standard order of evaluating an expression

  • Brackets (B)
  • Orders (O)
  • Division (D)
  • Multiplication (M)
  • Addition (A)
  • Subtraction (S)

Step by Step Explanation

(5 * 2) + (3 * 4)
(10) + (12)
22

Code

print(10 / 2 + 3)
print(10 / (2 + 3))
PYTHON

Output

8.0
2.0

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form