0% found this document useful (0 votes)
2 views

data types

The document provides an overview of data types in Python, including Numeric, Boolean, String, List, Tuple, Dictionary, and Set. Each data type is defined along with its characteristics, examples, and common methods. The document serves as a comprehensive guide for understanding and using various data types in Python programming.

Uploaded by

akhilaammu2001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

data types

The document provides an overview of data types in Python, including Numeric, Boolean, String, List, Tuple, Dictionary, and Set. Each data type is defined along with its characteristics, examples, and common methods. The document serves as a comprehensive guide for understanding and using various data types in Python programming.

Uploaded by

akhilaammu2001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

DATA TYPES IN PYTHON

What are Data types

❖ Data types define the type of value a variable can hold in a programming language.

❖ They determine the operations that can be performed on the data.

❖ In python , type() function is used to check the data type of a variable or value.
1. Numeric
 It represent the data that has a numeric value.
 A numeric value can be an integer, a floating number or a complex number.
 Integers (int): Whole numbers, positive or negative, without decimals, of unlimited length
 Float (float): Floating point numbers
 Complex Numbers (complex): Numbers with real and imaginary parts

x = 1 # int
y = 2.8 # float
z = 3+4 j # complex

Print(type(x) <class ‘int’>

print(type(y)) <class ‘float’>

print(type(z)) <class ‘complex’>


2. Boolean
 Boolean has only two values that is "True" or "False"
 Boolean data type is represented as "bool"

a=True
type(a)
<class ‘bool’>
3. String
 Collection of one or more characters put in a single quote, double quote or triple quote
 String data type is represented as 'str'
a = "Hello"
b = ‘Hello’
c = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""

 Square brackets can be used to access elements of the string

a = "Hello, World!"
print(a[0])
➢ The len() function returns the length of a string
a = "Hello, World"
print(len(a)) #len=12

➢ Slicing : To get a range of characters. Specify the start index and the end index,
separated by a colon, to return a part of the string.
a = "Programming"
print(b[3:7]) #gram
print(b[-4:-1]) #min

➢ The upper() method returns the string in upper case:


a = "Hello, World!"
print(a.upper())

➢ The lower() method returns the string in lower case:


a = "Hello, World!"
print(a.lower())
➢ The strip() method removes any whitespace from the beginning or the end:
a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"

➢ The replace() method replaces a string with another string:


a = "Hello, World!"
print(a.replace("H", "J"))
➢ The split() method splits the string into substrings if it finds instances of the separator:
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']

 String Concatenation: To concatenate, or combine, two strings you can use the + operator.
a = "Hello"
b = "World"
c = a + b
print(c)
capitalize() Converts the first character to upper case
casefold() Converts string into lower case

count() Returns the number of times a specified


value occurs in a string
endswith() Returns true if the string ends with the
specified value
isalnum() Returns True if all characters in the string
are alphanumeric
isalpha() Returns True if all characters in the string
are in the alphabet
isdigit() Returns True if all characters in the string
are digits

islower() Returns True if all characters in the string


are lower case

isnumeric() Returns True if all characters in the string


are numeric
4. List
 It can hold collection of items. Defined by enclosing a comma-seperated
sequence of elements within square brackets.
 They are ordered, index based, mutable and allow duplicate values
 thislist = ["apple", "banana", "cherry"]
 print(thislist[1])
 print(thislist[-1])
 To change the value of a specific item, refer to the index number:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)
 To add an item to the end of the list, use the append() method:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
 They are ordered, index based, mutable and allow duplicate values
 thislist = ["apple", "banana", "cherry"]
 print(thislist[1])
 print(thislist[-1])
 To change the value of a specific item, refer to the index number:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)
5. Tuple
 It can hold collection of items. Defined by enclosing a comma-seperated
sequence of elements within round brackets.
 Tuple items are ordered, unchangeable, and allow duplicate values.
 thistuple = ("apple", "banana", "cherry")
print(thistuple)
 One item tuple, remember the comma:
thistuple = ("apple",)
print(type(thistuple))

#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
➢ To change tuple values
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
➢ To add items:
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.append("orange")
thistuple = tuple(y)
Or
thistuple = ("apple", "banana", "cherry")
y = ("orange",)
thistuple += y

print(thistuple)
➢ To join two or more tuples you can use the + operator:
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
➢ To multiply the content of a tuple a given number of times, you can use the * operator:
fruits = ("apple", "banana", "cherry")
mytuple = fruits * 2
print(mytuple)

count() Returns the number of times a specified


value occurs in a tuple

index() Searches the tuple for a specified value


and returns the position of where it was
found
6. Dictionary
 Dictionaries are used to store data values in key:value pairs.
 A dictionary is a collection which is ordered, changeable and do not allow
duplicates.
 Dictionaries are written with curly brackets
 Use the dict() constructor to make a dictionary
thisdict = dict(name = "John", age = 36, country = "Norway")
print(thisdict)

 thisdict = {
"name": "maya",
"age": "20",
"id": 1964
}
print(thisdict["age"])
7. Set
 Sets are used to store multiple items in a single variable.
 A set is a collection which is unordered, unchangeable, unindexed and do not allow
duplicate values.
 Sets are written with curly brackets.
thisset = {"apple", "banana", "cherry"}
print(thisset)

 Cannot access items in a set by referring to an index or a key.But can loop through the
set items using a for loop.
thisset = {“apple”, “banana”, “cherry”}
for x in thisset:
print(x)
 add() – Adds an element to the set
 myset ={“a”, “b”, “c”}
myset.add(“d”)
print(myset)
 clear() – Removes all the elements from the set
 copy() – Returns a copy of the set
 Union() - | - Return a set containing the union of sets
people = {“Jay”, “Idrish”, “Archil”}
vampires = {“Karan”, “Arjun”}
population = people.union(vampires)
OR
population = people|vampires
 Intersection() - & - Returns a set, that is the intersection of two other sets
 difference() - - - Returns a set containing the difference between two
or more sets
 remove() – Removes the specified element
THANK YOU

You might also like