0% found this document useful (0 votes)
3 views10 pages

DAP Week 1 Implement Different Types of Data and Data Structures Use Cases Using Python

The document provides an overview of various data types and data structures in Python, including Numeric, Sequence Types (String, List, Tuple), Boolean, Set, and Dictionary. Each data type is explained with its characteristics, creation methods, and how to access elements. It emphasizes that Python treats everything as an object, and data types are essentially classes with variables as their instances.

Uploaded by

manoharmanukoji
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views10 pages

DAP Week 1 Implement Different Types of Data and Data Structures Use Cases Using Python

The document provides an overview of various data types and data structures in Python, including Numeric, Sequence Types (String, List, Tuple), Boolean, Set, and Dictionary. Each data type is explained with its characteristics, creation methods, and how to access elements. It emphasizes that Python treats everything as an object, and data types are essentially classes with variables as their instances.

Uploaded by

manoharmanukoji
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

1. Implement Different types of data and data structures use cases?

Using Python

 Data types are the classification or categorization of data items. It represents the kind of
value that tells what operations can be performed on a particular data. Since everything is
an object in Python programming, data types are actually classes and variables are instance
(object) of these classes.
 Following are the standard or built-in data type of Python:
1. Numeric
2. Sequence Type
3. Boolean
4. Set
5. Dictionary
Numeric
 In Python, numeric data type represent the data which has numeric value. Numeric value
can be integer, floating number or even complex numbers. These values are defined as int,
float and complex class in Python.
 Integers – This value is represented by int class. It contains positive or negative whole
numbers (without fraction or decimal). In Python there is no limit to how long an integer
value can be.
 Float – This value is represented by float class. It is a real number with floating point
representation. It is specified by a decimal point. Optionally, the character e or E followed
by a positive or negative integer may be appended to specify scientific notation.
 Complex Numbers – Complex number is represented by complex class. It is specified as
(real part) + (imaginary part)j. For example – 2+3j
Note – type() function is used to determine the type of data type.
Sequence Type
 In Python, sequence is the ordered collection of similar or different data types. Sequences
allows to store multiple values in an organized and efficient fashion. There are several
sequence types in Python –
String
List
Tuple
1) String
 In Python, Strings are arrays of bytes representing Unicode characters. A string is a
collection of one or more characters put in a single quote, double-quote or triple quote.
 In python there is no character data type, a character is a string of length one. It is
represented by str class.
Creating String
 Strings in Python can be created using single quotes or double quotes or even triple quotes.
Accessing elements of String
 In Python, individual characters of a String can be accessed by using the method of
Indexing. Indexing allows negative address references to access characters from the back
of the String, e.g. -1 refers to the last character, -2 refers to the second last character and so
on.
2) List
 Lists are just like the arrays, declared in other languages which is a ordered collection of
data. It is very flexible as the items in a list do not need to be of the same type.
Creating List
 Lists in Python can be created by just placing the sequence inside the square brackets[].
Accessing elements of List
 In order to access the list items refer to the index number. Use the index operator [ ] to
access an item in a list. In Python, negative sequence indexes represent positions from the
end of the array.
 Instead of having to compute the offset as in List[len(List)-3], it is enough to just write
List[-3]. Negative indexing means beginning from the end, -1 refers to the last item, -2
refers to the second-last item, etc.
3) Tuple
 Just like list, tuple is also an ordered collection of Python objects. The only difference
between tuple and list is that tuples are immutable i.e. tuples cannot be modified after it is
created. It is represented by tuple class.
Creating Tuple
 In Python, tuples are created by placing a sequence of values separated by ‘comma’ with or
without the use of parentheses for grouping of the data sequence. Tuples can contain any
number of elements and of any datatype (like strings, integers, list, etc.).
Note: Tuples can also be created with a single element, but it is a bit tricky. Having one
element in the parentheses is not sufficient, there must be a trailing ‘comma’ to make it a
tuple.
Note – Creation of Python tuple without the use of parentheses is known as Tuple
Packing.
Accessing elements of Tuple
 In order to access the tuple items refer to the index number. Use the index operator [ ] to
access an item in a tuple. The index must be an integer. Nested tuples are accessed using
nested indexing.
Boolean
 Data type with one of the two built-in values, True or False. Boolean objects that are equal
to True are truthy (true), and those equal to False are falsy (false). But non-Boolean objects
can be evaluated in Boolean context as well and determined to be true or false. It is denoted
by the class bool.
Note – True and False with capital ‘T’ and ‘F’ are valid booleans otherwise python will throw
an error.
Set
 In Python, Set is an unordered collection of data type that is iterable, mutable and has no
duplicate elements. The order of elements in a set is undefined though it may consist of
various elements.
Creating Sets
 Sets can be created by using the built-in set() function with an iterable object or a sequence
by placing the sequence inside curly braces, separated by ‘comma’. Type of elements in a
set need not be the same, various mixed-up data type values can also be passed to the set.
Accessing elements of Sets
 Set items cannot be accessed by referring to an index, since sets are unordered the items
has no index. But you can loop through the set items using a for loop, or ask if a specified
value is present in a set, by using the in keyword.
Dictionary
 Dictionary in Python is an unordered collection of data values, used to store data values
like a map, which unlike other Data Types that hold only single value as an element,
Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more
optimized. Each key-value pair in a Dictionary is separated by a colon :, whereas each key
is separated by a ‘comma’.
Creating Dictionary
 In Python, a Dictionary can be created by placing a sequence of elements within curly {}
braces, separated by ‘comma’. Values in a dictionary can be of any datatype and can be
duplicated, whereas keys can’t be repeated and must be immutable. Dictionary can also be
created by the built-in function dict(). An empty dictionary can be created by just placing it
to curly braces{}.
Note – Dictionary keys are case sensitive, same name but different cases of Key will be
treated distinctly.
Accessing elements of Dictionary
 In order to access the items of a dictionary refer to its key name. Key can be used inside
square brackets. There is also a method called get() that will also help in accessing the
element from a dictionary.

You might also like