Problem Solving and Python Programming: UNIT II: Data Types, Expressions, Statements

Variables, Expressions and Statements

Engineering Python Programming

• Definition : A variable is nothing but a reserved memory location to store values.

Variables, Expressions and Statements  AU : Jan.-18, May-19, Dec.-19, Marks 16

 

1. Variables

• Definition : A variable is nothing but a reserved memory location to store values.

• Variable is an entity to which the programmer can assign some values. Programmer choose the variable name which is meaningful. For example:

• In the following example we have declared the variable count to which value 10 is assigned with. This value is displayed by passing the variable name to print statement.


We can re-declare the variables for assigning different values. For example


Rules for variable names

• Variable names must be meaningful. The variable name normally should denote its purpose. For example - If the variable name is even_count, then clearly total number of even elements is stored in this variable.

• The variable names can be arbitrarily long. They contain both letters and digits but they cannot start with a number.

Normally variable name should start with lower case letter.

• The underscore character is allowed in the variable name. For example – int total_elements.

 

Example 2.8.1 Evaluate the following expressions in Python

24/16%3

float(4+int(2.39)%2)

2**2**3

AU : Dec.-19, Marks 6

Solution :

i) 1

ii) 4.0

iii) 256

 

2. Assignment Statements

• The assignment statement creates new variables and then corresponding value can be assigned to it.

• We can use assignment operator = to assign values to variables. Any type of value can be assigned to valid variable.

• For example :


• It is possible to have multiple assignments to different variables. For example : >>> a,b,c=100, 11.11, "Energy"

>>> print(a)

100

>>> print(b)

11.11 >>>

 print(c)

Energy

 

3. Keywords

The keywords are special words reserved for some purpose. The Python3 has following list of keywords


The names of keywords can be not be used as variable name. For instance a variable name can not be from because it is a keyword.

 

4. Expressions and Statements

• Expression is a combination of values, variables and operators.

• Value is considered as an expression.

• For example : Following are expressions

>>> a=10

>>> a+20

30

>>> 2+3*4

14

The first statement in above code is assignment statement. The second statement is an expression. The interpreter evaluates the expression and displays the result.

a. Order of Execution

• When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence.

• Python follows the same precedence rules for its mathematical operators that mathematics does.

• The acronym PEMDAS is a useful way to remember the order of operations.

1. P : Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since expressions in parentheses are evaluated first, 1 * (10-5) is 5

2. E: Exponentiation has the next highest precedence, so 2**3 is 8.

3. MDAS : Multiplication and Division have the same precedence, which is higher than Addition and Subtraction, which also have the same precedence. So 2+3*4 yields 14 rather than 20.

4. Operators with the same precedence are evaluated from left to right. So in the expression 3-2+1 will result 2. As subtraction is performed first and then addition will be performed.

b. Types of Expression

There are three types of expressions -


Infix Expression : It is a type of expression in which, expression is written in the form as

“Operand1 operator operand2

For example a+b is an infix expression.

Prefix Expression : It is a type of expression in which, expression is written in the form as

“Operator Operand1 Operand2"

For example: +ab is an prefix expression.

Postfix Expression :

Review Questions

1. What is numeric literal ? Give example.

AU : Jan.-18, Marks 4

2. Outline the operator precedence of arithmetic operators in Python.

AU : Jan.-18, Marks 6

3. Mention the list of keywords available in Python. Compare it with variable name.

AU : May-19, Marks 8

4. What are the statements ? How are they constructed from variable and expressions in Python ?

AU : May-19, Marks 8

5. What are keywords ? Give examples.

AU : Dec.-19, Marks 5

6. Describe about the concept of precedence and associativity of operators with example.

AU : May-19, Marks 16

 

Problem Solving and Python Programming: UNIT II: Data Types, Expressions, Statements : Tag: Engineering Python : Engineering Python Programming - Variables, Expressions and Statements