top of page

Python Data Types: Float, Complex, Boolean, Strings

Float (float)

It is used to represent floating (decimal) point values.

X=10.15

Float
Python52_edited.jpg

Note: Binary, octal, hexa decimal conventions are not applicable for float.

x=0b10.15    (not applicable)

x=0o10.15  (not applicable)

x=0x10.15    (not applicable)

Python53.png

Complex Numbers (complex)

Data type 'complex' is used to represent complex numbers in the form of a+bj, which is is used to perform scientific calculations. To represent imaginary part, only 'j' is allowed in Python; if any other symbol is used, it will give syntax error.

The format is always a+bj, you can not use a+jb.

a = real part, b=imaginary part and j^2=-1

Python 'complex' data type has inbuilt attributes to retrieve real and imaginary parts.

for example x=10+20j

complex
Python55_edited.jpg

if you want to develop mathematical or scientific applications Python is the best choice because it contains Complex data type.

Python56_edited.jpg

a and b can be either int values or float values.

Python57.png

Note: in real part if you are using 'int' value you can specify in binary, octal and hexa decimal also. But imaginary part should always be specified in decimal only.

Python58_edited_edited.png
Python59_edited.jpg

Operations on complex numbers:

Python60_edited.jpg

Boolean Values (bool)

It is used to represent Boolean or logical values. Only 2 values are allowed True and False.

Note: T and F must be in capital letter.

bool
Python61_edited.jpg

Internally,  true is treated as 1 and false is treated as 0.

True+True=1+1=2

True+False=1+0=1

True*False=1*0=0

True/False=1/0= ZeroDivisionError

Python62_edited.jpg
Python63_edited.jpg

Strings (str)

string is a sequence of characters.

To represent single line string literal, use '' (single quotes) or "" (double quotes).

str
Python65.png

To represent multi line string literal, use ''' (triple single quotes) or """ (triple double quotes).

Python66_edited.jpg
Python67_edited.jpg

Slice

Slice mean a piece or substring. In Python, 2 types of indexing are possible means we can access a string from both side: left to right and right to left.

1. +ve index (from left to right)

2. -ve index (from right to left)

Python68_edited.jpg

1. +ve index(from left to right)

Python69.png

2. -ve index(from right to left)

Python70.png

If we try to access out of range Index.

Python71_edited.jpg

Slice Operator:

Slice Operator is used to divide string into smaller substrings.

S[begin:end]: returns substring from (begin) to (end-1) index.

Ex: s=’knowcubs’

s[1:7]==>nowcub

s[2:7]==>owcub

Python72.png

Note 1: end index is optional; if you are not specifying end, default end value is end of the string.

s[1:]==>nowcubs

Python73_edited.jpg

Note 2: begin index is also not mandatory, it is optional; if you are not specifying begin by default begin value is start of the string.

s[:7]==>knowcub

Python74_edited.jpg

Note 3: Even you have an option of not specifying either beginning or end index. In this case output will be full string.

Python75_edited.jpg

Note 4: S[begin:end:step]

By default value of step is step 1.

Ex:   s=’knowcubs’

s[1:8:2]==>nwus

starting from n and ends at s. Here, since step value = 2; so every second character is skipped.

Python76.png

Note 5: string repetition operator *

Ex: s=’knowcubs’

s*2=knowscubs knowscubs

Python77.png

Note 6: len() is a built-in function in Python. You can use the len() to get the length of the given string, array, list, tuple, dictionary etc.

Ex: s=’knowcubs’

Python77.png
Python78_edited.jpg

For any Query/Suggestion, do let us know in the comment section.

bottom of page