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

values and data types

Uploaded by

durgadevi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

values and data types

Uploaded by

durgadevi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

VALUES AND DATA TYPES

VALUE:

Value can be any letter ,number or string.


Eg, Values are 2, 42.0, and 'Hello, World!'. (These values belong to different
datatypes.)
DATA TYPE:

• Every value in Python has a data type.


• It is a set of values, and the allowable operations on those values.
• Python
• has four standard data types:
NUMBERS:
• Number data type stores Numerical Values.
• This data type is immutable [i.e. values/items cannot be changed].
• Python supports integers, floating point numbers and complex numbers. They are
defined as,
SEQUENCE:
• A sequence is an ordered collection of items, indexed by positive integers.
• It is a combination of mutable (value can be changed) and immutable (values
cannot be changed) data types.
• There are three types of sequence data type available in Python, they are
1. Strings
2. Lists
3. Tuples
1. Strings
A String in Python consists of a series or sequence of characters - letters, numbers, and special
characters.
Strings are marked by quotes:
single quotes (' ') Eg, 'This a string in single quotes'
double quotes (" ") Eg, "'This a string in double quotes'“
 triple quotes(""" """) Eg, “””This is a paragraph. It is made up of multiple lines and
sentences.”””
Individual character in a string is accessed using a subscript (index).
Characters can be accessed using indexing and slicing operations
Strings are immutable i.e. the contents of the string cannot be changed after it is created.
INDEXING:

• Positive indexing helps in accessing the string from the beginning.


• Negative subscript helps in accessing the string from the end.
• Subscript 0 or –ve n(where n is length of the string) displays the first
element.
Example: A[0] or A[-5] will display “H”
• Subscript 1 or –ve (n-1) displays the second element.
Example: A[1] or A[-4] will display “E”
OPERATIONS ON STRING:

• Indexing
• Slicing
• Concatenation
• Repetitions
• Member ship
2. Lists
List is an ordered sequence of items. Values in the list are called elements / items.
It can be written as a list of comma-separated items (values) between square
brackets[ ].
Items in the lists can be of different data types.
Operations on list:
• Indexing
• Slicing
• Concatenation
• Repetitions
• Updation, Insertion, Deletion
3. Tuple:
A tuple is same as list, except that the set of elements is enclosed in
parentheses instead of square brackets.
A tuple is an immutable list. i.e. once a tuple has been created, you
can't add elements to a tuple or remove elements from the tuple.

Benefit of Tuple:
Tuples are faster than lists.
If the user wants to protect the data from accidental changes, tuple can be used.
Tuples can be used as keys in dictionaries, while lists can't.
Basic Operations:
Altering the tuple data type leads to error. Following error occurs when user tries to
do.
>>> t[0]="a"
Trace back (most recent call last):
File "<stdin>", line 1, in <module>
Type Error: 'tuple' object does not support item assignment
Mapping
• This data type is unordered and mutable.
• Dictionaries fall under Mappings.

Dictionaries:
Lists are ordered sets of objects, whereas dictionaries are unordered sets.
Dictionary is created by using curly brackets. i,e. {}
Dictionaries are accessed via keys and not via their position.
A dictionary is an associative array (also known as hashes). Any key of the
dictionary is associated (or mapped) to a value.
 The values of a dictionary can be any Python data type. So dictionaries are
unordered key-value-pairs (The association of a key and a value is called a
key-value pair )
Dictionaries don't support the sequence operation of the sequence data types
like strings, tuples and lists.
If you try to access a key which doesn't exist, you will get an error message:
>>> words = {"house" : "Haus", "cat":"Katze"}
>>> words["car"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'car'
List Tuple Set Dictionary

List is a non-homogeneous A tuple is a non-homogeneous A dictionary is another type of


The set data structure is also
data structure that stores data structure that can store a non-homogeneous data
non-homogeneous but only
elements in single rows and single row and multiple rows structure that stores key-value
stores one row.
multiple rows and columns. and columns. pairs.

The dictionary is represented


The list is represented by []. The tuple is represented by (). The set is represented by {}.
by {}.

The set is mutable, which


The dictionary is mutable.
Lists are mutable, which The tuple is immutable, which means that we can change it.
However, keys are not
means they can be changed. means it cannot be changed. However, no elements are
duplicated.
duplicated.

The dictionary has been


The list is ordered. The tuple has been ordered. The set is unordered. ordered (Python 3.7 and
above)

Lists can be nested among Tuple can make use of nested Sets can be nested among All dictionaries can use nested
themselves. among all. themselves. among themselves.

You might also like