This document discusses values, data types, and the five standard data types in Python. It defines that values are the fundamental things like numbers and strings that programs manipulate. Data type refers to the type and size of data that variables can hold. The five main data types in Python are numbers, strings, lists, tuples, and dictionaries. Numbers include integers, floating point values, and complex numbers. Lists and tuples are ordered sequences that can hold heterogeneous data, but lists are mutable while tuples are immutable. Strings are ordered sequences of characters. Dictionaries are unordered collections of key-value pairs.
1. Values and Data types
in Python
Prepared By,
P.Jothi Thilaga, AP/CSE
Ramco Institute of Technology
Rajapalayam
2. Value
one of the fundamental things, like a number, a
character or a string that program manipulates.
Example: a = 25, b= “Hi”
25, Hi represents values
Data type
refers to the type and size of data. Variables can
hold values of different data types. Python is a
purely object oriented language. It refers to
everything as an object, including numbers and
strings.
3. The five standard data types supported by python
includes:
Number
String
List
Tuple
Dictionary
4. Numbers
Number refers to a numeric value.
It includes
integers
long integers
floating point
complex numbers
5. Integers are whole numbers with no fractional parts.
They can be either positive, negative or zero value.
Long integers in python is displayed with an
uppercase L (56788333354533L is a long integer). -
supported only in python 2 version not in python 3.
Floating point numbers are numbers with fractions
or decimal points. Floating point values can be
expressed in scientific notation using the letter ‘e’ or
‘E’.
Complex number is a number that can be expressed
in the form a + bi, where a and b are real numbers,
and i is the imaginary unit.
6. Boolean is another data type in Python.
A variable of Boolean type can have one of
the two values - True or False.
The type() function can be used to know which data
type a variable or a value belongs to.
The isinstance() function to check if an object
belongs to a particular class.
7. Examples:
a = 5
print(a, "is of type", type(a))
a = 2.0
print(a, "is of type", type(a))
a = 1+2j
print(a, "is complex no.?", isinstance(1+2j,complex))
Output:
5 is of type <class 'int'>
2.0 is of type <class 'float'>
(1+2j) is complex no.? True
8. List
List is an ordered sequence of items. It is one
of the most used data type in Python and is
very flexible.
All the items in a list do not need to be of the
same type.
Lists are mutable. The elements in the list can
be modified.
To declare a list in python, separate the items
using commas and enclose them within square
brackets [ ].
9. >>> a = [1, 2.2, 'python'] The slicing operator [ ] is used
to extract an item or a range of items from a list. Index
starts from 0 in Python.
Examples:
>>>a = [5,10,15,20,25,30,35,40]
>>>a[2]
15
>>>print("a[0:3] = ", a[0:3])
a[0:3] = [5, 10, 15]
10. Tuple
Tuple is an ordered sequence of items same as
list. The only difference is that tuples are
immutable. Tuples once created cannot be
modified.
It is defined within parentheses ( ) where items
are separated by commas.
12. Strings
A String in python can be a series or a
sequence of alphabets, numerals and special
characters.
Single quotes or double quotes are used to
represent strings.
>>> s = "This is a string"
Strings are immutable.
13. Examples:
s = 'Hello world!'
print("s[4] = ", s[4])
print("s[6:11] = ", s[6:11])
Output:
s[4] = o
s[6:11] = world
14. Dictionary
A python dictionary is an unordered collection
of key-value pairs.
Keys and values can be of any type in a
dictionary.
Items in dictionary are separated by comma
and enclosed with the curly-braces { }.
Example:
>>>dict={1:“hi”,2: “hello”}
15. Exercises
1. --------- are the reserved memory locations that stores
values.
2. What data type is used to store values in terms of key
and value?
(a) list
(b) tuple
(c) class
(d) dictionary
3. What data type is the object below ?
L = [1.0, 15, ‘hi’, 27]
(a) list
(b) dictionary
(c) array
(d) tuple