0% found this document useful (0 votes)
12 views17 pages

Python Week2 Notes

Python CSE

Uploaded by

KDP King
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)
12 views17 pages

Python Week2 Notes

Python CSE

Uploaded by

KDP King
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/ 17

Loyola Institute of Technology

and Management

Python Training Program

Dr.Vajrala Venkata Reddy


Training & Placement Officer

PYTHON TRAINING MANUAL, WEEK-2 1


Data Representation in Python

Index
-------------------------------------------------------------------------------------------
 What is Data
 Purpose of Data
 Types of Literals OR Values OR Data
 Importance of Identifiers OR Variables
 Rules for Using Identifiers OR Variables in Python Program
====================================================================

What is Data and Purpose of Data


=====================================================================
 Processed information is called Data
 The Purpose of Collecting Data is that "To Take Effective Decisions"
=====================================================================

Types of Literals OR Values OR Data


=====================================================================
 In Programming Languages, In General we have 5 Types of Literals. They are

1. Integer Literals
2. Float Literals
3. String Literals
4. Boolean Literals
5. Collections Literals
=====================================================================

Importance of Identifiers OR Variables


=====================================================================
PYTHON TRAINING MANUAL, WEEK-2 2
==========================
 In Any Programming Language, Literals Must be Stored in Main Memory by allocating
Sufficient Amount of Memory space with the Help of Data Types.
 We know that All types of Literals are stored in main memory by having memory space.
 To Process values which are present in memory space, Programmers must give
DISTINCT NAMES to created memory spaces. These DISTINCT NAMES makes us to
identify the values present in memory space and hence they are called IDENTIFIERS.
 The IDENTIFIER Values are Changing/ Varying during Program Execution and hence
IDENTIFIERS are also called VARIABLES.
 Hence All types of LITERALS are stored in the form VARIABLES and all Variables are
called OBJECTS.
------------------------------------
Definition of Variable:
 A Variable is an Identifier, whose Value can be Changed OR Varying During the
Program Execution.
------------------------------------
====================================================================

Rules for Using Identifiers OR Variables in Python Program


=====================================================================
==========================
To Use Variables in PYTHON Program, we follow the Following Rules
------------
Rule-1: The Variable Name is a Combination of Alphabets, Digits and Special Symbol Under
Score ( _).

Rule-2: The First Letter of Variable Name Must strats with either Alphabet OR Under Score ( _ )
Only
Examples:
----------------
sal=56----Valid

PYTHON TRAINING MANUAL, WEEK-2 3


_sal=67---valid
-sal=67---Invalid
sal$=67---Invalid
__sal=56--Valid
__=56--valid
123=56--Invalid

Rule-3: Within the Variable Name, No special symbols are allowed except Under score ( _ )

Examples
---------------- emp sal=45-----Invalid
emp-sal=56----Invalid
emp_sal=56--valid
emp$sal=56--Invalid
emp1sal=56--Valid

Rule-4: No Keywords to be used as Variable Names (Because Keywords are the Reserved
Words, and they give special meaning to the compilers)
Examples
---------------
while=56--Invalid
while1=56--valid
_while=67--valid
for=56--Invalid
for2=67---Valid
False=67---Invalid
false=67--valid

Rule-5 : All the Variable Name are Case Sensitive

Examples:
PYTHON TRAINING MANUAL, WEEK-2 4
------------------
age=99
AGE=98
Age=97
aGe=96
print(age, AGE,Age,aGe)----99 98 97 96

I. Fundamental Category Data Types


=========================================================
 The purpose of Fundamental Category Data Types is "To Store Single Value".
 We have 4 Data Types in Fundamental Category. They are

1. int
2. float
3. bool
4. complex

1. int
=====================================================
 'int' is one of the Pre-Defined Class Name
 The purpose of int data type is that "To Store Whole Numbers OR Integral Values OR
Integer Values (Numbers without Decimal Places) such as stno,acno,empno...etc.
--------------------------------------------------------------------------------------------------------------------
Examples
--------------------------------------------------------------------------------------------------------------------
Python Instructions Outputs
----------------------------------- -----------------------
>>> a=10
>>> print(a)-----------------------------------10
>>> print(id(a))-------------------------------140713121888984
>>> print(type(a))----------------------------<class 'int'>

PYTHON TRAINING MANUAL, WEEK-2 5


----------------
>>> b=456
>>> print(b)------------------------------------456
>>> print(id(b))--------------------------------2050807135184
>>> print(type(b))----------------------------<class 'int'>
-----------------------------------
>>> a=100
>>> b=200
>>> c=a+b
>>> print(a,type(a))--------------------------100 <class 'int'>
>>> print(b,type(b))--------------------------200 <class 'int'>
>>> print(c,type(c))--------------------------300 <class 'int'>
--------------------------------------------------------------------------------------------------------------------
 int Data Type not only storing Integral Values but also Stores Various Number System
Values.
 In any Programming Language, we have 4 Types of Number Systems. They are

1. Decimal Number System


2. Binary Number System
3. Octal Number System
4. Hexa Decimal Number System
--------------------------------------------------------------------------------------------------------------------
1. Decimal Number System
--------------------------------------------------------------------------------------------------------------------
 Decimal Number System is the default Number system used by all Human Beings for
their Day-to-day Operations
 This Number System Contains the following
Digits: 0 1 2 3 4 5 6 7 8 9--------Total Digits=10
Base: 10
 Base 10 Literals are called Decimal Number System Values
--------------------------------------------------------------------------------------------------------------------
PYTHON TRAINING MANUAL, WEEK-2 6
2. Binary Number System
--------------------------------------------------------------------------------------------------------------------
 Binary Number System is Understandable by OS and Processor
 This Number System Contains the following
 Digits: 0 1 --------Total Digits=2
 Base: 2
 Base 2 Literals are called Binary Number System Values
 In Python Programming, to store Binary Values, Binary Values must precede with a letter
'0b' OR '0B'.
Syntax: varname=0b Binary Value
o (OR)
 varname=0B Binary Value
 Even though we store Binary Data, Internally Python Execution Environment displays the
Binary Data in the form of Decimal Number System.
------------------------------------
Examples
------------------------------------
>>> a=0b1111
>>> print(a,type(a))-------------------------------15 <class 'int'>
---------------------------
>>> a=0B10000
>>> print(a,type(a))-------------------------------16 <class 'int'>
>>> a=0B101
>>> print(a,type(a))-------------------------------5 <class 'int'>
>>> a=0b1101011111
>>> print(a,type(a))-------------------------------863 <class 'int'>
---------------
>>> a=0b1111
>>> b=0b101
>>> c=a+b

PYTHON TRAINING MANUAL, WEEK-2 7


>>> print(c,type(c))----------------------------20 <class 'int'>
>>> a=0b10102---------------------------------Syntax Error: invalid digit '2' in binary literal

3. Octal Number System


--------------------------------------------------------------------------------------------------------------------
 Octal Number System is Understandable Microprocessor Kits in Assembly Lang
Programming (8086).
 This Number System Contains the following
 Digits: 0 1 2 3 4 5 6 7 --------Total Digits=8
 Base: 8
 Base 8 Literals are called Octal Number System Values
 In Python Programming, to store Octal Values, Octal Values must precede with a letter
'0o' OR '0O'.
o Syntax: varname=0o Octal Value
o (OR)
 varname=0O Octal Value

 Even though we store Octal Data, Internally Python Execution Environment displays the
PYTHON TRAINING MANUAL, WEEK-2 8
Octal Data in the form of Decimal Number System.
------------------------
Examples
------------------------
>>> a=0o17
>>> print(a,type(a))----------------------------15 <class 'int'>
>>> a=0O123
>>> print(a,type(a))----------------------------83 <class 'int'>
>>> a=0o18--------------------------------------Syntax Error: invalid digit '8' in octal literal

4. Hexa Decimal Number System


-------------------------------------------------------------------------------------------------------------------
 Hexa Decimal Number System used in Development of Operating Systems
 This Number System Contains the following
 Digits: 0 1 2 3 4 5 6 7 8 9
o A(10) , B(11), C(12), D(13), E(14), F(15)
 Base: 16
PYTHON TRAINING MANUAL, WEEK-2 9
 Base 16 Literals are called Hexa Decimal Number System Values
 In Python Programming, to store Hexa Decimal Values, Hexa Decimal Values must
precede with a letter '0x' OR '0X'.
o Syntax: varname=0x Hexa DecimalValue
o (OR)
 varname=0X Hexa Decimal Value

 Even though we store Hexa Decimal Data, Internally Python Execution Environment
displays the Hexa Decimal Data in the form of Decimal Number System.
------------------------------------------
Examples
------------------------------------------
>>> a=0xAC
>>> print(a,type(a))-------------------172 <class 'int'>
>>> a=0XBEE
>>> print(a,type(a))-------------------3054 <class 'int'>
>>> a=0xFaCe
>>> print(a,type(a))------------------64206 <class 'int'>
-------------------
>>> a=0xFaCer-----------------------Syntax Error: invalid hexadecimal literal
>>> a=0XBEER----------------------Syntax Error: invalid hexadecimal literal
>>> a=0X12
>>> print(a,type(a))----------------18 <class 'int'>
>>> a=0xC
>>> print(a,type(a))---------------12 <class 'int'>
>>> a=0X15
>>> print(a,type(a))------------21 <class 'int'>
--------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------
Note:

PYTHON TRAINING MANUAL, WEEK-2 10


------------
>>> a=0123--------------Syntax Error: leading zeros in decimal integer literals are not permitted

2. float

=========================================================
 "float" is one of the Pre-Defined Class Name
 The purpose of float data type is that "To Store Real Constant Values OR Floating-Point
Values" such as percentage, Interest rate given by bank to the customer, percentile...etc
 All Types of int values are automatically Considered as float values but all types of float
values can't be considered automatically is int value.
 we can also use float data type store Scientific Notation Values (Mantissa e Exponent)
and whose equivalent floating-point value is Mantissa x 10 to the power Exponent
 The advantage of Scientific Notation is that to store Big Floating-Point Values can be
stored in Less Memory space.
 float data type never allows the programmer to store Binary, Octal and Hexa Decimal
Values.

PYTHON TRAINING MANUAL, WEEK-2 11


--------------------------------------------------------------------------------------------------------------------
------------------------------------------------
Examples
--------------------------------------------------------------------------------------------------------------------
------------------------------------------------
>>> a=2.3
>>> print(a,type(a))-----------------------------2.3 <class 'float'>
>>> a=2.3
>>> b=4.5
>>> c=a+b
>>> print(a,type(a))----------------------------2.3 <class 'float'>
>>> print(b,type(b))----------------------------4.5 <class 'float'>
>>> print(c,type(c))-----------------------------6.8 <class 'float'>
>>> a=10
>>> b=1.2
>>> c=a+b
>>> print(a,type(a))----------------------------10 <class 'int'>
>>> print(b,type(b))----------------------------1.2 <class 'float'>
>>> print(c,type(c))----------------------------11.2 <class 'float'>
--------------------------------------------------------------------------------------------------------------------
---------------------------------
>>> a=3e2
>>> print(a,type(a))----------300.0 <class 'float'>
>>>
>>> b=4e-2
>>> print(b,type(b))----------0.04 <class 'float'>
>>> c=0.000000000000000000000000000000000000000000000000005
>>> print(c,type(c))-----------5e-51 <class 'float'>
>>> a=0.00000100000007
>>> print(a,type(a))-------1.00000007e-06 <class 'float'>
--------------------------------------------------------------------------------------------------------------------
PYTHON TRAINING MANUAL, WEEK-2 12
------------------------------------------------
>>> a=0b1111.0b1010------------SyntaxError: invalid decimal literal
>>> a=0o12.0o34------------------SyntaxError: invalid decimal literal
>>> a=0xbee.0x34----------------SyntaxError: invalid decimal literal

3. bool
==========================================================
 "bool" is one of the Pre-Defined Class Name.
 The purpose of bool data type is " To Store Logical OR Boolean values such as True and
False".
 Here True and False are the Key words used as values for bool data type.
 Internally, The True is Considered as 1 and False is Considered as 0
 On Boolean Values we can Perform Arithmetic Operations.
--------------------------------------------------------------------------------------------------------------------
Examples
--------------------------------------------------------------------------------------------------------------------
>>> a=True
>>> print(a,type(a))-------------------True <class 'bool'>
>>> a=False
>>> print(a,type(a))------------------False <class 'bool'>
>>> a=false--------------------Name Error: name 'false' is not defined.
>>> a=true--------------------Name Error: name 'true' is not defined.
-----------------------------------------------------
>>> a=True
>>> b=False
>>> c=a+b
>>> print(c,type(c))--------------1 <class 'int'>
>>> print(False+False)----------0
>>> print(True-True)-------------0
>>> print(True-False)------------1

PYTHON TRAINING MANUAL, WEEK-2 13


>>> print(True+2+False)--------3
>>> print(True+2*3)--------------7
>>> print(True-2+False)---------1
>>> print(0b1111+True)----------16
>>> print(0xF-True)----------------14
>>> print(False/True)---------------0.0
>>> print(False//True)--------------0
>>> print(False/False)Zero Division Error: division by zero
>>> print(True/False)----Zero Division Error: division by zero
>>> print(2*True+True-False)----------

4. complex
============================================================
 "complex" is one of the pre-defined class
 The purpose of complex data type is "To store Complex Values OR Imaginary Values".
 The general Notation of Complex Number OR Value is shown Bellow.
o a+bj OR a-bj
 Here 'a' is called Real Part
 here 'b' is called Imaginary Part
 Here letter 'j' Represents sqrt(-1) OR sqr(j) = -1
 Internally, The Value of Real Part and Imaginary are Taken float data type.
 In order to get Real Part and Imaginary parts from complex object, we have 2 Types of
Pre-Defined Attributes in complex. They are
 Real
 imag
 Syntax: complexobj.real ====>Gives Real Part of Complex Object
o complexobj.imag====>Gives Imaginary Part of Complex
Object

PYTHON TRAINING MANUAL, WEEK-2 14


 On Complex Values, we can perform Arithmetic Operations such as Addition,
Subtraction, multiplication...etc
 An object of complex belongs to IMMUTABLE bcoz It never allows us to modify Its
Content at SAME MEMORY Address(Value Modified and whose value at Different
Memory Address )
--------------------------------------------------------------------------------------------------------------------
-------------------------------------------------
Examples
--------------------------------------------------------------------------------------------------------------------
-------------------------------------------------
>>> a=2+3j
>>> print(a,type(a))---------------------------------(2+3j) <class 'complex'>
>>> b=3-4j
>>> print(b,type(b))---------------------------------(3-4j) <class 'complex'>
>>> c=-3+4j
>>> print(c,type(c))----------------------------------(-3+4j) <class 'complex'>
-----------------------
>>> a=2.3+4.5j
>>> print(a,type(a))--------------------------------(2.3+4.5j) <class 'complex'>
>>> b=-2.3-4.5j
>>> print(b,type(b))--------------------------------(-2.3-4.5j) <class 'complex'>
>>> c=-4.5+5j
>>> print(c,type(c))--------------------------------(-4.5+5j) <class 'complex'>
>>> d=4+5.6j
>>> print(d,type(d))--------------------------------(4+5.6j) <class 'complex'>
--------------------------------------
>>> a=2+3h---------------SyntaxError: invalid decimal literal
>>> b=2+3i---------------SyntaxError: invalid decimal literal
----------------------------------------
>>> a=4j
>>> print(a,type(a))---------------4j <class 'complex'>
PYTHON TRAINING MANUAL, WEEK-2 15
>>> a=0+4j
>>> print(a,type(a))---------------4j <class 'complex'>
>>> c=3.5j
>>> print(c,type(c))---------------3.5j <class 'complex'>
-----------------------
>>> a=-3j
>>> print(a,type(a))-----------> (-0-3j) <class 'complex'>
>>> b=-3.5j
>>> print(b,type(b))-----------> (-0-3.5j) <class 'complex'>
--------------------------------------------------------------------------------
>>> a=2+3j
>>> print(a,type(a))-------------------(2+3j) <class 'complex'>
>>> print(a.real)-----------------------2.0
>>> print(a.imag)---------------------3.0
>>> b=2.3+4.5j
>>> print(b,type(b))-----------------(2.3+4.5j) <class 'complex'>
>>> print(b.real)----------------------2.3
>>> print(b.imag)--------------------4.5
-----------------------
>>> c=-2.3+4j
>>> print(c,type(c))----------------(-2.3+4j) <class 'complex'>
>>> print(c.real)-------------------- -2.3
>>> print(c.imag)------------------- 4.0
>>> a=4j
>>> print(a,type(a))----------------- 4j <class 'complex'>
>>> print(a.real)--------------------- 0.0
>>> print(a.imag)---------------------- 4.0
>>> b=-4.5j
>>> print(b,type(b))--------------------(-0-4.5j) <class 'complex'>
>>> print(b.real)------------------------> -0.0
>>> print(b.imag)-----------------------> -4.5
PYTHON TRAINING MANUAL, WEEK-2 16
>>> print(b.imagaginary)-------------AttributeError: 'complex' object has no attribute
'imagaginary'
-------------------------------------------------------------------
>>> a=2+3j
>>> b=3+4j
>>> c=a+b
>>> print(c,type(c))-------------(5+7j) <class 'complex'>
>>> print(4+50j+6+70j)---------(10+120j)
------------------------------------------------------------------------
>>> a=2+3j
>>> b=3+4j
>>> print(a,type(a))----------(2+3j) <class 'complex'>
>>> print(b,type(b))----------(3+4j) <class 'complex'>
------------------
>>> c=a*b
>>> print(c,type(c))----------(-6+17j) <class 'complex'>
>>> print(-4j*3j)---------------(12-0j)
>>> print(2+0j+2.3+0j)------(4.3+0j)

Task:
Practice all the example programs mentioned under each topic

Interview Questions:
1) What are the built-in data types in Python?
2) What is the purpose of the type() function in Python?
3) Differentiate between int, float, and complex numeric data types in Python
4) What is the purpose of the bool data type in Python?

PYTHON TRAINING MANUAL, WEEK-2 17

You might also like