Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

VARIABLES, BASIC OPERATIONS

AND OPERATORS

Dr.V.V.Sumanth Kumar
Principal Scientist
ICAR- NAARM
Hyderabad
Variables
• Reserved name memory locations which helps to store values

• Creating variable results in allocation of space in memory

• Based on datatype variable, interpreter allocates the space in memory and decides
what can be stored in reserved memory

• It can store characters, integers and decimals

• Memory space gets allocated automatically, when assigning value to variable

• Operand on the left is, ‘name of the variable’

• Operand on the right is, ‘value stored in the variable’


cont..
• It starts with alphabets and followed by number, but can not be a number followed by alphabets

a1=77 is valid 1a=77 invalid

Eg: a=17 then a is variable and 17 is value

• Print(a) gives value of variable


• But print(‘a’) gives variable name only, not the value.
Cont..
• value of two variables is same, then memory allocation is assigned with values
Eg: a=17
b=17
print(a,b)
Print(id(a),id(b))

• a b=77 invalid
• a_b=11 valid
• Special characters are not valid
• _a can be valid.
Basic Input Operations
User need to enter required information in the input() function of line code

Syntax: input()

Eg: name=input(‘enter the name’)


Python examples:
x= input(‘name of the program’)
y=int(input(“enter the number”))
Output Operations
Syntax : print()

• Entering the string(characters), should enter in quotations

Eg: print(“hello world”)

• variable or number can be written directly, no quotations marks are required

Eg: print(122)
Basic Operators
• Perform operations between variables or values

• Values which used in operation of programs are called operands.

• Operands can be any type of variables or data types in python

Operand 1 Operator Operand 2 Result


Types of operators

Types of operators

Arithmetic Assignment Comparison Logical

Membership Identity Bitwise


Arithmetic operators
Operator Name Example

+ Addition x+y

- subtraction x-y

* multiplication x*y

/ Division x/y

% Modulus x%y

** Exponents x**y

// Floor division x//y


Example: Arithmetic operators
Input Output
Assignment operators
operator example same as

= x=5 x=5

+= x+=7 x=x+7

-= x-=5 x=x-5
Example: Assignment operators
Input Output
Comparison operators
Operator Name Example

== Equal x==y

!= Not equal x!=y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x>=y

<= Less than or equal to x<=y


Example: Comparison operators
Input Output
Logical operators
Operator Description Example

AND Return true when both x<6 and x<12


statements are true

OR Return true if any one of the x<12 or x< 18


statement is true

NOT Returns false if the result is Not(x<6 and x<12)


true,
Return true if the result is
false
Example: Logical operators

Input Output
Membership operators
Operator Description Example

In Returns true if sequence with X in Y


the specified value is present in
the object

Not in Returns true if sequence with X not in Y


specified value is not present in
the object
Example: Membership operators

Input Output
Identity operators
Operator Description Example

is Returns true if both variable are X is Y


same objects

is not returns true if both variables X is not Y


are not the same objects
Example: Identity operators

Input Output
Bitwise operators
operator Name Description

& AND X&Y

| OR X |Y

^ XOR X^Y
Truth Table
A B A&B A|B A^B

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0

You might also like