Programming Using
PYTHON
DATA TYPES
3
What is Data Type?
Yeasir Arafat Ratul
A data type, in programming, is a classification that specifies which type of value a
variable has and what type of mathematical, relational or logical operations can be
applied to it without causing an error.
MILK DRINKS
Data Types of Python
Yeasir Arafat Ratul
Text Type: str
Numeric Types: int, float, complex
Boolean Type: Bool
None Type None
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Data Types of Python
Yeasir Arafat Ratul
Numbers Collection
Integers Float
binary Long Complex Mappings
octal Boolean Sequence Dictionary Collection Set
hexadecimal string List Tuple range Set Frozen Set
Numeric Types
Yeasir Arafat Ratul
int, float, complex
n=5 -> integer
f = 12.0 -> float
com = 3 + 1j -> complex
(here 3 is the real part and 1j is the imaginary part)
Integer Type
Yeasir Arafat Ratul
In Python 3, there is effectively no limit to how long an integer value can be. Of course, it is constrained by
the amount of memory your system has, as are all things, but beyond that an integer can be as long as you
need it to be.
int() function returns a floating point number from a float number or string.
n=5 -> integer
>>> a = int(‘2’)
2
>>> int(True)
1
>>> int("HELLO")
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
int("HELLO")
ValueError: invalid literal for int() with base 10: 'HELLO'
Boolean Type
Yeasir Arafat Ratul
Boolean Data Type >>> bool(2)
True
True = 1 >>> bool(1)
False = 0 True
>>> bool(0)
False
>>> bool(' ')
False
>>> bool("A")
True
>>> 1 + True
2
>>> 4 * False
0
Float Type
Yeasir Arafat Ratul
float() function returns a floating point number from a number or string.
>>> float(3)
3.0
>>> float('2')
2.0
>>> float('x')
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
float('x')
ValueError: could not convert string to float: 'x'
String
Yeasir Arafat Ratul
String(str) is a series of characters.
a = ‘R’
b = “Python”
c = “““ I love python.
I want to be a programmer”””
String Functions
Yeasir Arafat Ratul
There are a lot of functions in python used for manipulating functions in different ways.
upper() – converts the string in upper case.
lower() – converts the string in lower case.
capitalize() – capitalizes the first word.
title() - converts the first character of each word to upper case
count() - returns the number of times a specified value occurs in a string
isdigit() - returns True if all characters in the string are digits
isalhpa() - returns True if all characters in the string are in the alphabet
More: https://www.w3schools.com/python/python_ref_string.asp
String Formatting
Yeasir Arafat Ratul
Representing String In The Expected Way.
• Formatting with % Operator.
• Formatting with format() string method.
• Formatting with string literals, called f-strings.
• Formatting with String Template Class
‘d’ for integers
‘f’ for floating-point numbers
‘b’ for binary numbers
‘o’ for octal numbers
‘x’ for octal hexadecimal numbers
‘s’ for string
‘e’ for floating-point in an exponent format
String Indexing and Slicing
Yeasir Arafat Ratul
Slicing like a bread.
String Indexing
Yeasir Arafat Ratul
string = ‘CONGRATULATIONS’
string = C O N G R A T U L A T I O N S
Forward Indexing 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Backward Indexing -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
# Forward Indexing # Backward Indexing
>>> string[0] >>> string[-1]
'C’ 'S'
>>> string[-15]
>>> string[13] 'C'
‘N'
String Slicing
Yeasir Arafat Ratul
string = ‘CONGRATULATIONS’
string = C O N G R A T U L A T I O N S
Forward Indexing 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Backward Indexing -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
>>> len(string)
15
>>> string[0:15]
'CONGRATULATIONS’
>>> string[0:13]
'CONGRATULATIO'
strip(), split()
Yeasir Arafat Ratul
>>> string = ‘ HELLO WORLD '
>>> string.strip()
'HELLO WORLD'
>>> a, b = 'HELLO WORLD'.split()
>>> a
'HELLO'
>>> b
'WORLD'
‘ ’.join()
Yeasir Arafat Ratul
>>> words = ['I', 'LOVE', 'BANGLADESH']
>>> sentence = ' '.join(words)
>>> sentence
'I LOVE BANGLADESH'
>>> sentence_two = '--'.join(words)
>>> sentence_two
'I--LOVE--BANGLADESH'
rjust(), ljust(), center()
Yeasir Arafat Ratul
rjust/ljust/center(width, filcher = ‘ ’)
>>> string = "HELLO"
>>> string.rjust(10, '*')
'*****HELLO'
>>> string.ljust(10, '*')
'HELLO*****'
>>> string.center(11, '-')
'---HELLO---'
Yeasir Arafat Ratul
THANK YOU
LIKE | COMMENT | SUBSCRIBE