Python Week2 Notes
Python Week2 Notes
and Management
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
====================================================================
1. Integer Literals
2. Float Literals
3. String Literals
4. Boolean Literals
5. Collections Literals
=====================================================================
Rule-2: The First Letter of Variable Name Must strats with either Alphabet OR Under Score ( _ )
Only
Examples:
----------------
sal=56----Valid
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
Examples:
PYTHON TRAINING MANUAL, WEEK-2 4
------------------
age=99
AGE=98
Age=97
aGe=96
print(age, AGE,Age,aGe)----99 98 97 96
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'>
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
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:
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.
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
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
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?