0% found this document useful (0 votes)
3 views5 pages

3.data Handling

Uploaded by

zohafazal0705
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

3.data Handling

Uploaded by

zohafazal0705
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DATA HANDLING

Data types in Python


1. Numbers: Number data types store numeric values.
There are three numeric types in Python:

➢ int
➢ float
➢ complex
Example:
w=1 # int
y = 2.8 # float
z = 1j # complex

2. Sequences

String: Sequence of characters represented in the quotation marks.


▪ Python allows for either pairs of single or double quotes. Example: 'hello' is the same
as "hello" .
>>> str1 = 'Hello Friend'
>>> str2 = "452"
List :List is a sequence of items separated by commas and the items are enclosed in
square brackets [ ].
Example
#To create a list
>>> list1 = [5, 3.4, "New Delhi", "20C", 45]
#print the elements of the list list1
>>> print(list1)
[5, 3.4, 'New Delhi', '20C', 45]

Basic Operators in Python:


i. Arithmetic Operators
ii. Relational Operator
iii. Logical Operators
iv. Assignment Operators
v. Other Special Operators
o Identity Operators
Page 1
i. Arithmetic Operators: To perform mathematical operations.

RESULT
OPERATOR NAME SYNTAX
(X=14, Y=4)

+ Addition x+y 18

_ Subtraction x–y 10

* Multiplication x*y 56

/ Division (float) x/y 3.5

// Division (floor) x // y 3

% Modulus x%y 2

** Exponent x**y 38416

+ >>> str1 = "Hello" # + is used to concatenate Strings


>>> str2 = "India"
>>> str1 + str2
'HelloIndia'

* >>> str1 = 'India' # * in string


>>> str1 * 2
'IndiaIndia'

Page 2
ii. Relational Operators: Relational operators compare the values. It either
returns True or False according to the condition.

RESULT
OPERATOR NAME SYNTAX
(IF X=16, Y=42)

False
> Greater than x>y
True
< Less than x<y
False
== Equal to x == y
True
!= Not equal to x != y
False
>= Greater than or equal to x >= y
True
<= Less than or equal to x <= y

iii. Logical operators: Logical operators perform Logical AND, Logical OR and Logical
NOT operations.

OPERATOR DESCRIPTION SYNTAX


and Logical AND: True if both the operands are true x and y
or Logical OR: True if either of the operands is true x or y
not Logical NOT: True if operand is false not x

Truth Tables

X Y X and Y
False False False
False True False
True False False
True True True

Page 3
X Y X or Y
X not X
False False False
False True
False True True
True False
True False True
True True True

Examples of Logical Operator:

>>> 10>6 and 4<5


True
>>> 10>6 and 4>5
False
>>> 10>6 or 4>5
True
>>> not(10>6 or 4>5)
False
iv. Assignment operators: Assignment operators are used to assign values to the variables.

DESCRIPTION SYNTAX
OPERA
TOR
Assign value of right side of expression to left side operand x=y
=
+z
Add AND: Add right side operand with left side operand and a+=b
+=
then assign to left operand a=a+b
Subtract AND: Subtract right operand from left operand and a-=b
-=
then assign to left operand a=a- b
Multiply AND: Multiply right operand with left operand and a*=b
*=
then assign to left operand a=a*b
Divide AND: Divide left operand with right operand and then a/=b
/=
assign to left operand a=a/b
Modulus AND: Takes modulus using left and right operands and a%=b
%=
assign result to left operand a=a%
b
Divide(floor) AND: Divide left operand with right operand and a//=b
//=
then assign the value(floor) to left operand a=a//b

Page 4
Exponent AND: Calculate exponent(raise power) value using a**=b
**=
operands and assign value to left operand a=a**b

Operator Precedence and Associativity:


Operator Precedence: It describes the order in which operations are performed when an
expression is evaluated. Operators with higher precedence perform the operation first.

Note:For operators with equal precedence, the expression is evaluated from left to right.
** Exponent -Right to Left
Order of Operator Description
Precedence
1 ( ), { } Parentheses (grouping)
2 ** Exponent
3 +x, -x Positive, negative
4 *, /, % Product, division, remainder
5 +, – Addition, subtraction
6 <=, <, >, >= Comparisons
7 =, %=, /=, += Assignment
8 not Boolean NOT
9 and Boolean AND
10 or Boolean OR

Expressions
An expression is defined as a combination of constants, variables, and operators.
An expression always evaluates to a value.. Some examples of valid expressions
are given below.
(i) 3.0 + 3.14 (ii) 23//3 -5 * 7 +14 -2 (iii) "Global" + "Citizen"
Evaluate the following expressions:
1. 12+3*4-6/2 = 12+ 12- 3.0= 21.0
2. (12+3)*4- 6/2
3. 12+3*(4-6)/2
4. 12*3%5+2*6//4
5. 12+ (3*4-6)/3
6. (2+3)**2 - 6/2

Page 5

You might also like