1.3 Basic Datatypes and Operators in Python-1
1.3 Basic Datatypes and Operators in Python-1
Basics of Python
Contents
• Introduction
• Features
• Input/output Functions
• Decorators
• Iterators and
• Generators
VASAVI. A
Datatypes in Python
A variable can hold different types of values. For example, a person's name
must be stored as a string whereas its id must be stored as an integer. Every value
in Python has a datatype.
Data can be of many types e.g., character, integer, real, string etc.,
Python provides various standard data types that define the storage method
on each of them. The data types defined in Python are given below.
1. Numbers
2. String
3. List
4. Tuple
5. Dictionary
1. Numbers:
2. Strings:
A String datatype lets you hold string data, i.e., any number of valid
characters into a set of quotation marks.
It can hold any type of known characters i.e., letters, numbers, and
special characters.
VASAVI. A
S= “This is a string”
S=’’’a multiline’’’
3. List:
a = [ 1, 2.2, ‘python’]
b= [1,2,3,4,5]
4. Tuple:
p= (1,2,3,4,5)
q= (2,4,6,8)
r= (‘a’,’e’,’i’,’o’,’u’)
s= (7,8,9,’A’,’B’,’C’)
VASAVI. A
5. Dictionary:
Within a dictionary, no two keys can be the same i.e., there are
unique keys within a dictionary.
>>>> vowels[‘a’]
>>>> 1
>>>> vowels[‘u’]
>>>> 5
VASAVI. A
Operators in Python
Operators:
1. Arithmetic operators:
• To do arithmetic, Python uses arithmetic operators.
• Each of these operators is a binary operator i.e., it require two
values (operands) to calculate final value (answer).
• Python arithmetic operators are Addition (+), Subtraction (-),
Multiplication (*), Division (/), Modulus (%), Exponent (**),
Floor division (//) ( Returns only integer value).
VASAVI. A
Example:
X=15
Y=4
print(‘X+Y=’, X+Y) // 19
print(‘X-Y=’, X-Y) // 11
print(‘X*Y=’, X*Y) // 60
print(‘X//Y=’, X//Y) // 3
Z=X+Y
print(“Addition is:”, Z)
Z=X-Y
Print(“Subtraction is:”, Z)
Output:
VASAVI. A
2. Comparison or Relational Operators:
Relational or Comparison operators are used for comparing
two expressions and results in either true or false.
The six relational operators are <, >, <=, >=, ==, !=
Example:
A=1, B=2
print(A==B) False
print(A!=B) True
print(A<B) True
print(A>B) False
print(A<=B) True
print(A>=B) False
3. Assignment Operators:
VASAVI. A
>>> a
>>> 15
>>> a*=3 a=a*3
= 15*3
= 45
>>> a
>>> 45
>>> a/=2 a=a/2
=45/2
= 22.5
>>> a
>>> 22.5
>>> a-=3.5 a=a-3.5
= 22.5-3.5
= 19.0
>>> a
>>>19
The OR Operator:
VASAVI. A
X Y X OR Y
0 0 0
0 1 1
1 0 1
1 1 1
Example:
(4==4) or (5==8) // True
T F
(5>8) or (5<2) // False
F F
X= True; Y=False
Print(X or Y) // True
X Y X AND Y
0 0 0
0 1 0
1 0 0
1 1 1
Example:
(4==4) and (5==8) // False
T F
VASAVI. A
(5>8) and (5<2) // False
F F
(5>8) and (2<5) // False
F T
X= True; Y=False
Print(X and Y) // False
X NOT X
T F
F T
Example:
not True // False
not False // True
5. Membership Operators:
Membership operators are used to test whether a value or variable is
found in sequence (string, list, set and dictionary).
NOTE:
In a dictionary, we can only test for presence of key, not the value.
VASAVI. A
(a) In operator:
Returns True if value/variable is found in the sequence.
(b) Not in operator:
Example:
pystr=”PYTHON”
pystr=”Bigdata”
X=’Hello World’
Print(‘h’ in X) // False
Print(1 in Y) // True
Print(‘a’ in Y) // False
print('e' in X) // True
VASAVI. A
6. Identity operators:
(a) is
(b) is not
(a) is operator:
Evaluates to true if the variables on either side of the operator point
to the same object and false otherwise.
>>> x=’hello’
>>> y=’hello’
>>> x is y
True
Example:
x=5
y=5
print(x is y)
id(x) // Memory location
id(y) // Memory location
(a) is not operator:
Evaluates to false if the variables on either side of the operator
point to the same object and true otherwise.
>>> x=’hello’
>>> y=’hello’
>>> x is not y
False
VASAVI. A
Example:
x=5
7. Bitwise operators:
In Python, bitwise operators are used to performing bitwise
calculations on integers. The integers are first converted into binary and
then operations are performed on bit by bit, hence the name bitwise
operators. Then the result is returned in decimal format.
Note:
Python bitwise operators work only on integers.
Python bitwise operators are:
(a) Bitwise AND &
(b) Bitwise OR |
(c) Bitwise NOT ~
(d) Bitwise XOR ^
VASAVI. A
(e) Bitwise right shift >>
(f) Bitwise left shift <<
(a) Bitwise AND &:
Returns 1 if both the bits are 1 else 0.
Example:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a & b = 1010
&
0100
= 0000
= 0 (Decimal)
>>> a=10
>>> b=4
>>> a&b
0
(b) Bitwise OR |:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a | b = 1010
|
0100
= 1110
= 14 (Decimal)
VASAVI. A
>>> 10|20
>>> 10 1010
>>> 20 10100
01010
10100
11110
>>> 30
a = 10 = 1010 (Binary)
~a = ~1010
= -(1010 + 1)
= -(1011)
= -11 (Decimal)
>>> X= 43
>>> print("~x =", ~x)
>>> -44
>>> x=-10
>>> print(~x)
>>> 9
VASAVI. A
(d) Bitwise XOR ^:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a ^ b = 1010
^
0100
= 1110
= 14 (Decimal)
The individual bits of the number are shifted to the right, and
the gaps on the left are filled with 0 (or 1 if the number is negative).
The result is similar to dividing a value by a power of 2.
Example:
10>>2 // 10/2^2
// 10/4
// 2
20>>3 // 20/2^3
// 20/8
// 2
VASAVI. A
(f) Bitwise Left shift <<:
The individual bits of the integer to the left are filled with 0. The
result is similar to multiplying a value by a power of 2.
Example:
10<<2 // 10*2^2=10*4=40
15<<4 // 15*2^4=15*16=240
8. Ternary operators:
Syntax:
a, b = 10, 20
# Copy value of a in min if a < b else copy b
min = a if a < b else b
print(min)
Output:
10
VASAVI. A
Operator Precedence in Python
The following table lists all operators from highest precedence to lowest
Operator Description
~+- Complement, unary plus and minus (method names for the last two are
+@ and -@)
VASAVI. A
Example:
x=7+3*2
Here, x is assigned 13, not 20 because operator * has higher precedence than +
Example:
a = 20
b = 10
c = 15
d=5
e=0
e = (a + b) * c / d #( 30 * 15 ) / 5
e = ((a + b) * c) / d # (30 * 15 ) / 5
e = a + (b * c) / d; # 20 + (150/5)
Output:
Value of (a + b) * c / d is 90.0
Value of ((a + b) * c) / d is 90.0
Value of (a + b) * (c / d) is 90.0
Value of a + (b * c) / d is 50.0
VASAVI. A