Page 386 - Ai_417_V3.0_C9_Flipbook
P. 386
If an expression contains two or more operators with the same precedence, then the order of evaluation is from
left to right, except for exponential (**) which is always from right to left. For example,
• 10 – 2 + 3 – 3 will be evaluated from left to right as + and – has the same order of precedence. So, the answer
will be 10 – 2 will be 8 + 3 will be 11 – 3 will be 8.
• 2 ** 3 ** 2 will always be evaluated from right to left. It will be calculated as 2 ** (3 ** 2) and not as
(2 ** 3) ** 2. So, the answer will be 512.
Task #Problem Solving & Logical Reasoning
Solve the following expressions using the operator precedence:
• 10 + 20 / 5 – 3 * 2
• 5 * (10 + 5) + 10 – 5
Variables
A variable is a name given to a memory location to hold a specific value. It is just like a container that can hold
any type of data that can be changed later in the program.
Variable has:
• A name that follows the identifier naming conventions and is case-sensitive.
• A value that can be integer, float, string, boolean, etc.
Unlike other programming languages, variables in Python are not created before use. They are created
automatically when a value is assigned to them by using the assignment operator (=).
A variable assigned a value of one type can be re-assigned a new value of a different type, as shown below:
>>>a = 20
>>>a
20
>>>a = "hello"
>>>a
'hello'
There are different ways of assigning values to variables in Python:
• Method1: Assigning different values to different variables on different lines.
>>>a = 5
>>>b = 10.5
>>>c = "hello"
• Method2: Assigning different values to different variables on same line.
>>>a = 5; b = 10.5; c = "hello"
OR
>>>a, b, c = 5, 10.5, "hello"
• Method3: Assigning same value to different variables on same line.
>>>a = b = c = 10
384 Touchpad Artificial Intelligence (Ver. 3.0)-IX

