0% found this document useful (0 votes)
28 views6 pages

Data Handling GR 11

The document discusses multiple assignment of variables in Python, built-in core data types like numbers, sequences, mappings, operators and expressions. It explains assigning same or different values to multiple variables, numeric data types like integers and floats, sequence types like strings, lists and tuples, mapping type like dictionary. It also covers arithmetic, relational, identity, logical and augmented assignment operators and precedence of operators.

Uploaded by

sianasimon2007
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)
28 views6 pages

Data Handling GR 11

The document discusses multiple assignment of variables in Python, built-in core data types like numbers, sequences, mappings, operators and expressions. It explains assigning same or different values to multiple variables, numeric data types like integers and floats, sequence types like strings, lists and tuples, mapping type like dictionary. It also covers arithmetic, relational, identity, logical and augmented assignment operators and precedence of operators.

Uploaded by

sianasimon2007
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/ 6

Continuation of the lesson Getting Started with Python

Multiple Assignments in Variables


1. Assigning same value to multiple variables
We can assign same value to multiple variables in a single statement in python.
Eg: >>>a=b=c=10
In the above statement, the value 10 is assigned to all three variables a, b and c.

2. Assigning multiple values to multiple variables


We can assign multiple values to multiple variables in a single statement in
python. >>>x, y, z=10, 20, 30
The above statement will assign the values order wise. i.e. First variable is given
first value, second variable the second value and so on. i.e. The above statement
will assign values 10 to x , 20 to y and 30 to z.
Note:
In Python when we want to swap two values, we need to write the code as
>>> x, y=y, x
The above statement will swap (exchange) the values of x and y.
Eg: >>>x=10
>>>y=5
>>>x,y=y,x
After executing the above statement, the values of x and y are swapped ie,
>>>x=5
>>>y=10

***************************************************************

4. DATA HANDLING

Python provides a predefined set of data types for handling the data it uses.
Data can be stored in any of the data types.
Data type
It is the way to identify the type of data stored in a variable. Data can be of
many types. Python offers the following built-in core data types:
1. Numbers

Number data types are used to store numeric values in Python. The Numbers in
Python have following core data types:- i) Integers
Integers are whole numbers that have no fractional parts. It can be positive or
negative.
There are two types of integers in Python:
a) Integers (signed):- It is the normal integer representation of whole
numbers. Python 3.x provides a single data type called int to store any integer;
whether big or small.
b) Boolean:- These represent the truth values False and True. The
Boolean type is a subtype of integers as the Boolean values (True or False)
represents 1 or 0 respectively. It is represented by the data type bool.

ii) Floating point Numbers


A number having fractional part is a floating point number. For eg: 3.14159 is a
floating point number. The data type used to represent a floating number is
float.
The two advantages of floating point numbers over integers
are:
• They can represent values between the integers.
• They can represent a much greater range of values.
Disadvantage over integers:-
• Floating point numbers are usually slower than integer operations.

iii) Complex Numbers


A complex number is of the form a+ib where i is an imaginary number. Python
represents complex numbers in the form of a+bj. ie to represent imaginary
number python uses ‘j’ in place of traditional ‘i’.
For eg: >>>x=0+3.1j
>>>y=1.5+2j
2. Sequence Data types
i) Strings
A string is a character or a group of characters enclosed with in single, double
or triple quotes. In Python 3.x, each character stored in a string is a Unicode
character. Unicode is a system designed to represent every character from every
language.
ii) Lists
A list in Python represents a list of comma separated values of any data type
enclosed between square brackets.
Eg: a=[1,2,3,4]
B=[‘a’,’e’,’i’,’o’,’u’]
C=[1,100.45,’hai’,’r’]
iii) Tuples
Tuples are represented as group of comma-separated values of any data type
with in simple parentheses.
Eg: t=(1,2,3,4)
A=(1,’a’,’e’,100.5)
3. Mappings
i) Dictionary
The dictionary is an unordered set of comma separated key:value pairs with in
curly-brackets { }.
Eg: d={‘a’:1,’e’:2,’i’:3,’o’:4,’u’:5}.

OPERATORS
The symbols that trigger an operation/action on data are called Operators. The
operations are represented by operators and objects of the operands are referred
to as Operands.
Python provides the following operators:-
1. Arithmetic Operators
Python provides two types of arithmetic operators. They are
a) Unary Operators
The operators that act only on one operand is referred to as Unary operator.
Unary + and Unary – are the two operators.
Eg: if a=5 then +a means 5 (Unary +)
If a=-5 then +a means -5 (Unary -)
b) Binary Operators
Operators that act upon two operands are referred to as binary operators. The
binary operators in Python are:
i) Addition Operator (+)
It adds the values of its operands and the result is the sum of the values of its
two operands. Eg: 5+10 results 15
ii) Subtraction Operator(-)
It subtracts the second operand from the first.
Eg: 5-10 results -5
iii) Multiplication Operator(*)
It multiplies the value of its operands.
Eg: 5 * 10 results 50

iv) Division Operator (/)


It divides its first operand by the second operand and always returns the result
as a float value Eg: 4/2 results 2.0
5/2 results 2.5

v) Floor Division Operator (//)


It performs the floor division that is the division in which only the whole part of
the result is given in the output and fractional part is truncated.
Eg: 5//2 results 2
6.5 // 2 results 3.0
4//2 results 2

vi) Modulus operator (%)


It finds the remainder of its first operand relative to the second. That is it
produces the remainder of dividing the first operand by the second operand.
Eg: 10 % 2 results 0
19 % 6 results 1
5 % 3 results 2

vii) Exponentiation Operator (**)


It performs exponentiation (power) calculation. That is it returns the result of a
number raised to a power.
Eg: 5 ** 2 results 25
3 ** 3 results 9
10 ** 3 results 1000

2. Relational Operators
Relational operators determine the relation among different operands. Python
provides six relational operators for comparing values (thus also called
comparison operators). If the comparison is true, the relational expression results
into the Boolean value True. If the comparison is false, the relational expression
results into the Boolean value False.
The six relational operators are:
< (less than) > (greater than) <=(less than or equal to)
>= (greater than or equal to) = =(equal to) != (not equal to)
For example,

3. Identity Operators
There are two identity operators in Python. They are
i) is ii) is not
They are used to check if both of its operands points to the same memory
location or not and also returns True or False accordingly.

4. Logical Operators
Python provides three logical operators to combine existing expressions. These
are or, and & not.
i) The or Operator
The or operator combines two expressions, which makes its operands. The or
operator evaluates to True if either of its operands (expressions) evaluates to
True; False if both operands evaluates to False.
E.g. >>>(5>10) or (5%2==1) results True
>>>(6>10) or (10<6) results False ii)
The and Operator
The and operator combines two expressions, which makes its operands. The and
operator evaluates to True if both of its operands evaluates to True; False if
either or both operands evaluates to False.
E.g. >>> (5>10) and (5%2==1) results False
>>> (6<10) and (10>6) results True.
iii) The not operator
The Logical not operator works on a single operand. i.e it is a unary operator. The
logical not operator negates the truth value of an expression. That is if the
expression is true, the not expression results a False value and vice versa. e.g.
>>> not 5 results a False value because 5 is non-zero.
>>> not 0 results a True value
>>>not(5>10) results True
5. Augmented Assignment Operator
Python has an assignment operator = which assigns the value specified on the
RHS to the variable on the LHS of ‘=’.
Python also offers augmented assignment arithmetic operators, which combine
the impact of arithmetic operator with an assignment operator.
If you want to add value of b to a and assign the result to a, then instead of
writing a=a+b, we can write a+=b.

For eg: In python a=a+10 is equivalent to a+=10


b=b*100 is equivalent to b*=100
Expression
An expression in Python is any valid combination of operators, literals and
variables. The expressions in python can be of 5 types:-
1. Arithmetic Expression a+b-c
2. Relational expression a<=b
3. Logical expression a and b not c
4. String ‘hello’+’welcome’
5. Compound a+b>c or a*b<c*d
Evaluation of an expression
While evaluating an expression in Python, we have to consider the precedence of
operators in the given expression.

PRECEDENCE OF OPERATORS
Operator Description
** Exponentiation (raise to the power)
* / % // Multiply, divide, modulo and floor division
+- Addition and subtraction
<= < > >= Comparison operators
== , != Equality operators
is, is not Identity operators
in, not in Membership operators
not, and, or Logical operators

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy