Python Data Types.pdf

3,330 views 56 slides May 26, 2022
Slide 1
Slide 1 of 56
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56

About This Presentation

Datatype python


Slide Content

Python Data Types

Number, String, List Data Types

Python Data Types

Variables can hold values, and every value has a data-type.
Python is a dynamically typed language; hence we do not need to define
the type of the variable while declaring it.
The interpreter implicitly binds the value with its type.

Python Data Types

a = 5
The variable a holds integer value five and we did not define its type.
Python interpreter will automatically interpret variables a as an integer type.
Python enables us to check the type of the variable used in the program.
Python provides us the type() function, which returns the type of the variable
passed.

NUMERIC DATA TYPE

Numeric Data Type

●Number stores numeric values.
●The integer, float, and complex values belong to a Python Numbers
data-type.
●Python provides the type() function to know the data-type of the variable.
●Similarly, the isinstance() function is used to check an object belongs to
a particular class.

Numbers

Python supports three types of numeric data.
1.Int - Integer value can be any length such as integers 10, 2, 29, -20, -150
etc. Python has no restriction on the length of an integer. Its value
belongs to int
2.Float - Float is used to store floating-point numbers like 1.9, 9.902, 15.2,
etc. It is accurate upto 15 decimal points.
3.complex - A complex number contains an ordered pair, i.e., x + iy where
x and y denote the real and imaginary parts, respectively. The complex
numbers like 2.14j, 2.0 + 2.3j, etc.

Numbers

a = 5
print("The type of a", type(a))
b = 40.5
print("The type of b", type(b))
c = 1+3j
print("The type of c", type(c))
print(" c is a complex number", isinstance(1+3j,complex))

Output

The type of a <class 'int'>
The type of b <class 'float'>
The type of c <class 'complex'>
c is complex number: True

Number Data Type : Examples
Complex:
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))

Float
x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))
Float
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))

Type Conversion

Output
……………………

1.0
2
(1+0j)
<class 'float'>
<class 'int'>
<class 'complex'>

String Data Type

String
Python string is the collection of the characters surrounded by single quotes, double quotes,
or triple quotes.
Syntax:
str = "Hi Python !"
Here, if we check the type of the variable str using a Python script
print(type(str)) #then it will print a string (str).
In Python, strings are treated as the sequence of characters, which means that Python doesn't
support the character data-type;
instead, a single character written as 'p' is treated as the string of length 1.

String

#Using single quotes
str1 = 'Hello Python'
print(str1)
#Using double quotes
str2 = "Hello Python"
print(str2)

#Using triple quotes
str3 = '''''Triple quotes are
generally used for represent the
multiline or docstring'''
print(str3)

Strings indexing and splitting

Strings indexing and splitting

the slice operator [] is used to access
the individual characters of the
string.

However, we can use the : (colon)
operator in Python to access the
substring from the given string.

Strings indexing and splitting

We can do the negative slicing in the string; it starts from the rightmost character,
which is indicated as -1.
The second rightmost index indicates -2,
and so on.

Reassigning Strings


Updating the content of the strings is as easy as assigning it to a new string.
The string object doesn't support item assignment i.e.,
A string can only be replaced with new string since its content cannot be
partially replaced.
Strings are immutable in Python.

String Operators
Operator Description

+ It is known as concatenation operator used to join the strings
given either side of the operator.
* It is known as repetition operator. It concatenates the
multiple copies of the same string.
in It is known as membership operator. It returns if a particular
sub-string is present in the specified string.
not in It is also a membership operator and does the exact reverse
of in. It returns true if a particular substring is not present in
the specified string.

String Operators

str = "Hello"
str1 = " world"
print(str*3) # prints HelloHelloHello
print(str+str1) # prints Hello world
print(str[4]) # prints o
print(str[2:4]); # prints ll
print('w' in str) # prints false as w is not present in str
print('wo' not in str1) # prints false as wo is present in str1.

String
The format() method is the most flexible and useful method in formatting strings.
The curly braces {} are used as the placeholder in the string and replaced by the
format() method argument.

# Using Curly braces
print("{} and {} both are the best friend".format("Arun","Abhishek"))
#Positional Argument
print("{1} and {0} best players ".format("Virat","Rohit"))
#Keyword Argument
print("{a},{b},{c}".format(a = "James", b = "Peter", c = "Ricky"))

String Concatenation

a = "Hello"
b = "World"
c = a + b
print(c)
To add a space between them, add a " ":
a = "Hello"
b = "World"
c = a + " " + b
print(c)

LIST

Python List

●A list in Python is used to store the sequence of various types of data.
●Python lists are mutable type its mean we can modify its element after it created.
●A list can be defined as a collection of values or items of different types.
●The items in the list are separated with the comma (,) and enclosed with the square
brackets [].
A list can be defined as below
1.L1 = ["John", 102, "USA"]
2.L2 = [1, 2, 3, 4, 5, 6]
List is a collection which is ordered and changeable.
Allows duplicate members.

List indexing and splitting

List indexing and splitting

Unlike other languages, Python provides the flexibility to use the negative indexing also.
The negative indices are counted from the right.

Updating List values

●Lists are the most versatile data structures in Python since they are
mutable, and their values can be updated by using the slice and
assignment operator.

●Python also provides append() and insert() methods, which can be used to
add values to the list.

Append Items in List
To add an item to the end of the list, use the append() method:
Example
Using the append() method to append an item:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)

Insert Items in List

To insert a list item at a specified index, use the insert() method.
The insert() method inserts an item at the specified index:
Example
Insert an item as the second position:
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)

Remove List Items

The remove() method removes the specified item.
Example
Remove "banana":
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)

Remove Specific Index Element

The pop() method removes the specified index.
Example
Remove the second item:
thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
print(thislist)

Remove Specific Index Element

If you do not specify the index, the pop() method removes the last item.
Example
Remove the last item:
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)

Remove Specific Index Element

The del keyword also removes the specified index:
Example
Remove the first item:
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)

Delete the list
The del keyword can also delete the list completely.
Example
Delete the entire list:
thislist = ["apple", "banana", "cherry"]
del thislist

Clear the List
The clear() method empties the list.
The list still remains, but it has no content.
Example
Clear the list content:
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)

Loop Through a List

You can loop through the list items by using a for loop:
Example
Print all items in the list, one by one:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)

Sort List Alphanumerically

List objects have a sort() method that will sort the list alphanumerically, ascending, by
default:
Example
Sort the list alphabetically:
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)

Sort the list numerically

thislist = [100, 50, 65, 82, 23]
thislist.sort()
print(thislist)

Sort Descending

To sort descending, use the keyword argument reverse = True:
Example
Sort the list descending:
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort(reverse = True)
print(thislist)

Sort Descending

Sort the list descending:
thislist = [100, 50, 65, 82, 23]
thislist.sort(reverse = True)
print(thislist)

Copy a List

We cannot copy a list simply by typing list2 = list1, because: list2 will only be a
reference to list1, and changes made in list1 will automatically also be made in list2.
There are ways to make a copy, one way is to use the built-in List method copy().
Example
Make a copy of a list with the copy() method:
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)

Copy a List

Another way to make a copy is to use the built-in method list().
Example
Make a copy of a list with the list() method:
thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)

Join Two Lists
There are several ways to join, or concatenate, two or more lists in Python.
One of the easiest ways are by using the + operator.
Example
Join two list:
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)

Join Two Lists

Use the extend() method to add list2 at the end of list1:
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)

LIST OPERATIONS

LIST OPERATIONS

LIST FUNCTIONS

Tuple

●Tuples are used to store multiple items in a single variable.
●A tuple is a collection which is ordered and unchangeable.
●Tuples are written with round brackets.

Create a Tuple:
thistuple = ("apple", "banana", "cherry")
print(thistuple)

Tuple


●Tuple items are ordered, unchangeable, and allow duplicate values.
●Tuple items are indexed, the first item has index [0], the second item has
index [1] etc.
●When we say that tuples are ordered, it means that the items have a defined
order, and that order will not change.
●Tuples are unchangeable, meaning that we cannot change, add or remove
items after the tuple has been created. (Immutable)
●Allow Duplicates : they can have items with the same value:

Tuple

T1 = (101, "Peter", 22)
T2 = ("Apple", "Banana", "Orange")
T3 = 10,20,30,40,50
T4 = (10,)
T5 = ()

Tuple indexing and slicing
The indexing and slicing in the tuple are similar to lists.
The indexing in the tuple starts from 0 and goes to length(tuple) - 1.
The items in the tuple can be accessed by using the index [] operator.
Python also allows us to use the colon operator to access multiple items in the
tuple.

Tuple indexing and slicing

Tuple indexing
Negative Indexing

The tuple element can also access by using negative indexing.
The index of -1 denotes the rightmost element and -2 to the second last item
and so on.

Basic Tuple operations

Basic Tuple operations

Python Tuple inbuilt functions