0% found this document useful (0 votes)
13 views

Variables, Data Types, Operators in Python

The document discusses variables and data types in Python. It covers numeric, string, boolean, and sequence data types. It also discusses operators like arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. Examples are provided to demonstrate how each operator works.

Uploaded by

Peepeh Ette
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Variables, Data Types, Operators in Python

The document discusses variables and data types in Python. It covers numeric, string, boolean, and sequence data types. It also discusses operators like arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. Examples are provided to demonstrate how each operator works.

Uploaded by

Peepeh Ette
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

VARIABLES IN

PYTHON

Datec Learning Centers


Variables
• Everything is an object in python
Object reference for variables
a=5
b=3 a 5

c=3 b 3

Datec Learning Centers


Variables
• Every object has an unique identifier
Function:
id( ) a 5
Returns unique id for the specified object
Example: b 3
a=5
c
b=3
c=3
print(id(b))
print(id(c))
Output:
10105152
Datec Learning Centers
10105152
DATA TYPES IN
PYTHON

Datec Learning Centers


Data Types
• Numeric Types : integer, float, complex
Integer (int):
Number that can be written without a fractional component
Example: 3, 76, -5
Float (float):
Numbers that contain floating decimal points
Example: 3.14, 7.683
Datec Learning Centers
Data Types
• Numeric Types : integer, float, complex
Complex:
Number that can be expressed in the form a + bj, where a and b are
real numbers, and j represents the imaginary unit
Example:
value=3+4.2j
3 → real
4.2 → imaginary Datec Learning Centers
Data Types
• Text Type : string
String(str):
Sequence of one or more characters (letters, numbers, symbols)
Example:
message =‘Welcome’
day = “Day 1”
outcome =‘‘‘Happy Learning!’’’

Datec Learning Centers


Data Types
• Boolean Type : bool
Variables are defined by the True and False keywords
Example:
a = True
b = False

Datec Learning Centers


Data Types
Sequence Types
• List
• Tuple
Both are used to group values together
Mapping Type
• Dictionary
Representation → key: value pairs
Datec Learning Centers
Finding the type of a variable
Function Used:

type( )

• returns the type of the specified object


Example:
name=“Moresby”
print(type(name))
Output:
<class 'str'>
Datec Learning Centers
OPERATORS AND
EXPRESSIONS

Datec Learning Centers


Operators
Operator is a symbol that tells the compiler or interpreter to
perform specific operation
• Arithmetic operators
• Assignment operator
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Datec Learning Centers
Arithmetic Operators
Used with numeric values to perform common mathematical
operations
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Division
** Exponentiation
// Floor
Datec Learning Centers Division
Arithmetic Operators
Example:
number1 = 3
number2 = 2
print(“Sum : ”, number1+number2)
print(“Difference : ”, number1-number2)
print(“Product : ”, number1*number2)

Output:
Sum : 5
Difference : 1
Product : 6
Datec Learning Centers
Arithmetic Operators
Example:
number1 = 8
number2 = 3
#Performing modulo division
remainder=number1%number2
print(“Remainder : ”, remainder)

Output:

Remainder : 2

Datec Learning Centers


Arithmetic Operators
Example:
number1 = 5
number2 = 2
#Performing floor division
quotient=number1//number2
print(“Quotient : ”, quotient)

Output:

Quotient : 2

Datec Learning Centers


Assignment Operator
Used to assign values to variables

Operator Used:
=
Assigning values to Variables
variablename = value
Example:
marks=93

Datec Learning Centers


Assignment Operator
Assigning same value to multiple variables in a statement

variablename1= variablename2 = value

Example:

png_rugby_player=leeds_rhinos_player= “Rhyse Martin"

Datec Learning Centers


Assignment Operator
Assigning multiple values to multiple variables in a statement

variablename1, variablename2 = value1, value2

Example:

name, age, height= “Rhyse Martin",29, 6

Datec Learning Centers


Assignment Operators
Example:
age = 15
#Adding 2 to age
age += 2
print(age)

Output:

17

Datec Learning Centers


Comparison/ Relational Operators
Used to compare two values

Operator Operation
== Equal
!= Not Equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Datec Learning Centers


Comparison/ Relational Operators
Example:
number1 = 15
number2 = 10
print(number1>number2)
print(number1==number2)

Output:

True
False

Datec Learning Centers


Logical Operators
Used to combine conditional statements

Operator Outcome
and Returns True, if both statements are true
or Returns True, if one of the statements is true
not Reverse the result, returns False if the result is true

Datec Learning Centers


Logical Operators
Example:
a = 15
b = 10
print(a>5 and a>10)
print(a<5 or b>5)
print(not(a>10))

Output:
True
True
False
Datec Learning Centers
Identity Operators
▪ Used to compare the objects
▪ To check if they are actually the same object, with the same
memory location

Operator Outcome

is Returns True, if both variables are the same object

Returns True, if both variables are not the same


is not object
Datec Learning Centers
Identity Operators
Example:
a = 15
b = 10
c = 15
d=c
print(a is c)
print(d is c)
print(a is not b)
Output:
True
True
True Datec Learning Centers
Membership Operators
▪ Used to test if a sequence is presented in an object

Operator Outcome
Returns True ,if a sequence with the specified value is
in present in the object

Returns True, if a sequence with the specified value is not


not in present in the object

▪ Used with Lists, Tuples


Datec Learning Centers
Bitwise Operators
▪ performs bit by bit operation on binary representation
of values
Operator Name Outcome
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of the two bits is 1
~ NOT 1’s complement
^ XOR Sets each bit to 1 if only one of two bits is 1
Shift left by pushing zeros in from the right and let
<< LEFT SHIT
the leftmost bits fall off
RIGHT Shift right by pushing copies of the leftmost bit in
>>
SHIF from
Datec the
Learning left, and let the rightmost bits fall off
Centers
Bitwise Operators
Example using & and |
a=2 a = 0000 0010
b = 0000 0101 &
b=5
c=a & b #bitwise and c = 0000 0000
d=a | b #bitwise or
print(c)
a = 0000 0010
print(d) b = 0000 0101 |

Output: d = 0000 0111


0
7
Datec Learning Centers
Bitwise Operators
Example using ~ (not)
a=2
c=~a
print(c)
d=4
print(~d)

Output:
-3
-5

Datec Learning Centers


Bitwise Operators
Example using ^ ( XOR)
a=5 a = 0000 0101
b = 0000 0011
b=3 ^
print(a^b) c = 0000 0110

Output:
6

Datec Learning Centers


Bitwise Operators
Example for left shift, right shift
a=3 a = 0000 0011
b=2 b=2 <<

c=a << b #left shift c = 0000 1100


d=a >> b #right shift
print(c) a = 0000 0011
print(d) b=2 >>

Output: d = 0000 0000


12
0
Datec Learning Centers

You might also like