### Unit 1 - Data Types and Operators

CBSE Revision Notes

Class-11 Informatics Practices (New Syllabus)
Unit 1: Programming and Computational Thinking (PCT-1)

This topic is common in class 11 Computer Science and Informatics Practices.

Data Types and Operators

Knowledge of data types and operators:

We have already seen different data types in python. Now we will see what are different operators in python and how we can take input from the keyboard.

Accepting input from console: There are two ways to assign value to a variable 1. By assigning value during compile time e.g. x=10, y=99.22, z=”hello”. 2.  We can also assign value at run time e.g. we’ll ask user to insert the value during run time. This is the mostly used in programming. We assign value at compile time when the value won’t change in the program. Let’s see an example how we can take input from the keyboard. For this we use the function called input(). This function takes input from keyboard and assigns the value to the variable.

Print(‘enter any number’)
Num=input()
Print(‘Enter the name’)
Name=input()
Print(‘Name and number is: ‘, Name,Num)

In the above example we are asking user to insert a number using input function it takes number from keyboard and will assign it to variable num. likewise then string is added to variable name in last statement both values are printed.

Assignment statement: Let’s understand this with an example.
Sum=4+5
This is an assignment statement because it contains the assignment operator “=”. The variable sum appears to the left of the assignment operator, so sum will receive a value when this statement executes.

Expressions: An expression is a combination of values, variables, operators, and calls to functions. Expressions need to be evaluated. The evaluation of an expression produces a value, which is why expressions can appear on the right hand side of assignment statements. There are various expressions in python let’s discuss that one by one.

 X+Y x added to y, if x and y are numbersx concatenated to y, if x and y are strings X-Y Y is subtracted from x, if x and y are numbers X*Y x times y, if x and y are numbersx concatenated with itself y times, if x is a string and y is an integery concatenated with itself x times, if y is a string and x is an integer X/Y x divided by y, if x and y are numbers X//Y Floor of x divided by y, if x and y are numbers X%Y Remainder of x divided by y, if x and y are numbers X**Y x raised to y power, if x and y are numbers

Operators and their precedence: There are different operators in python and all the operators have different precedence. When different operators appear in the same expression, the normal rules of arithmetic apply.

Precedence means when an expression contains two different kinds of operators, which should be applied first?

To see how precedence works, consider the expression
2 + 3 * 4

Should it be interpreted as
(2 + 3) * 4

(that is, 20), or rather is
2 + (3 * 4)

(that is, 14) the correct interpretation? As in normal arithmetic, multiplication and division in Python have equal importance and are performed before addition and subtraction. We say multiplication and division have precedence over addition and subtraction. In the expression
2 + 3 * 4

the multiplication is performed before addition, since multiplication has precedence over addition. The result is 14. The multiplicative operators (*, /, //, and %) have equal precedence with each other, and the additive operators (binary + and -) have equal precedence with each other. The multiplicative operators have precedence over the additive operators.

As in standard arithmetic, in Python if the addition is to be performed first, parentheses can override the precedence rules. The expression (2 + 3) * 4 evaluates to 20. Multiple sets of parentheses can be arranged and nested in any ways that are acceptable in standard arithmetic. In case operators with same precedence appear in an expression then operators that comes first from left will be evaluated.

The operator precedence in Python is listed in the following table. It is in descending order, upper group has higher precedence than the lower ones.

 Operators Meaning () Parentheses ** Exponent +x, -x, ~x Unary plus, Unary minus, Bitwise NOT *, /, //, % Multiplication, Division, Floor division, Modulus +, - Addition, Subtraction <<, >> Bitwise shift operators & Bitwise AND ^ Bitwise XOR | Bitwise OR ==, !=, >, >=, <, <=, is, is not, in, not in Comparison, Identity, Membership operators not Logical NOT and Logical AND or Logical OR