PYTHON
LECTURE 6
Today’s Agenda
More On Data Types
• The float Type
• The complex Type
• The bool Type
• The str Type
The float Data Type
• Python also supports floating-point real values.
• Float values are specified with a decimal point
• So 2.5 , 3.14 , 6.9 etc are all examples of float
data type
• Just like double data type of other languages like
Java/C , float in Python has a precision of 16
digits
Some Examples
Some Important Points
About float
• For float , we can only assign values in decimal
number system and not in binary , octal or
hexadecimal number system.
Some Important Points
About float
• Float values can also be represented as
exponential values
• Exponential notation is a scientific notation
which is represented using e or E followed by an
integer and it means to the power of 10
The complex Data Type
• Complex numbers are written in the form, x +
yj, where x is the real part and y is the
imaginary part.
• For example: 4+3j , 12+1j etc
• The letter j is called unit imaginary number.
• It denotes the value of √-1 , i.e j² denotes -1
An Example
Some Important Points
About complex Data Type
• For representing the unit imaginary number
we are only allowed to use the letter j (both
upper and lower case are allowed).
• Any other letter if used will generate error
Some Important Points
About complex Data Type
• The letter j , should only appear in suffix , not in
prefix
Some Important Points
About complex Data Type
• The real and imaginary parts are allowed to be
integers as well as floats
Some Important Points
About complex Data Type
• The real part can be specified in any int form
i.e. decimal , binary , octal or hexadecimal
but the imaginary part should only be in
decimal form
Some Important Points
About complex Data Type
• We can display real and imaginary part
separately by using the attributes of complex
types called “real” and “imag”.
• Don’t think real and imag are functions , rather
they are attributes/properties of complex
data type
The bool Data Type
• In Python , to represent boolean values we have
bool data type.
• The bool data type can be one of two values,
either True or False.
• We use Booleans in programming to make
comparisons and to control the flow of the
program.
Some Examples
Some Important Points
About bool
• True and False are keywords , so case
sensitivity must be remembered while
assigning them otherwise Python will give error
Some Important Points
About bool
• All test conditions in Python return the result
as bool which could be either True or False
Some Important Points
About bool
• To understand the next point , try to guess the
output of the following:
a=True a=True a=False
b=False b=True b=False
c=a+b c=a+b c=a+b
print(c) print(c) print(c)
Output: Output:
Output:
2 0
1
The above outputs make it clear that internally
Python stores True and False as integers with the
value 1 and 0 respectively
The str Data Type
• Just like any other language , in Python also a
String is sequence of characters.
• Python does not have a char data type, unlike
C/C++ or Java
• We can use single quotes or double quotes
to represent strings.
• However Python recommends to use single
quotes
Some Examples
The data type used by Python internally for storing
Strings is
str
Some Important Points
About Strings
• Unlike C language , Python does not uses ASCII
number system for characters . It uses UNICODE
number system
• UNICODE is a number system which supports
much wider range of characters compared to ASCII
• As far as Python is concerned , it uses UNICODE
to support 65536 characters with their numeric
values ranging from 0 to 65535 which covers
almost every spoken language in the world like
English , Greek , Spanish , Chinese , Japanese
etc
Some Important Points
About Strings
Some Important Points
About Strings
• Whenever we display a string value directly on
Python’s shell i.e. without using the function
print(), Python’s shell automatically encloses
it in single quotes
• However this does not happen when we use
print() function to print a string value
Some Important Points
About Strings
• If a string starts with double quotes , it must
end with double quotes only .
• Similarly if it starts with single quotes , it
must end with single quotes only.
• Otherwise Python will generate error
Some Important Points
About Strings
Some Important Points
About Strings
• If the string contains single quotes in between
then it must be enclosed in double quotes and
vice versa.
• For example:
• To print Monal's Python Classes , we would write:
• msg= " Sachin's Python Classes "
• Similarly to print Capital of “Raj" is “Jaipur" ,we
would write:
• msg='Capital of “Raj" is “Jaipur" '
Some Important Points
About Strings
• How will you print Let's learn "Python" ?
A. "Let's learn "Python" "
B. 'Let's learn "Python" '
NONE!
Both will give error.
Correct way is to use either triple single quotes or triple
double quotes or escape sequence character \
msg=' ' 'Let's learn "Python" ' ' '
OR
msg='Let\'s learn "Python" '
Some Important Points
About Strings
Some Important Points
About Strings
Some Important Points
About Strings
• Another important use of triple single quotes
or triple double quotes is that if our string
extends up to more than 1 line then we need to
enclose it in triple single quotes or triple
double quotes
Some Important Points
About Strings
• We also can do the same thing by using \n , so
using triple quotes or triple double quotes is
just for improving readability
Accessing Individual Characters
In String
In Python, all Strings are stored as individual
characters in a contiguous memory location.
Each character in this memory location is
assigned an index which begins from 0 and goes
up to length -1
Accessing Individual Characters
In String
For example, suppose we write
word=“Python”
Then the internal representation of this will be
Accessing Individual Characters
In String
Now to access individual character we can provide
this index number to the subscript operator [ ].
Accessing Individual Characters
In String
However if we try to provide an index number
beyond the given limit then IndexError exception
will arise
Accessing Individual Characters
In String
Not only this , Python even allows negative
indexing which begins from the end of the string.
So -1 is the index of last character , -2 is the
index of second last character and so on.
Accessing Individual Characters
In String