0% found this document useful (0 votes)
4 views13 pages

Module-4 Data Types in Python

Uploaded by

suyogfromballu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
4 views13 pages

Module-4 Data Types in Python

Uploaded by

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

Module-4

Data Types in Python

Introduction to Data Structures: Importance of data structures


in programming, Built-in data structures in Python, List:
Creating and accessing lists, List methods (e.g., `append`,
`remove`, `sort`), List slicing and indexing, Tuples: Creating
and accessing tuples, Use cases for tuples over lists,
Dictionaries: Introduction to key-value pairs, Creating and
accessing dictionaries, Dictionary methods (e.g., `get`, `keys`,
`values`, `items`), Sets: Introduction to sets and their
properties, Creating and modifying sets, Set operations (union,
intersection, difference), Strings: Strings as Data Structures,
String methods and operations, String formatting and f-strings,
Slicing and indexing strings
List
• Lists are used to store multiple items in a single variable.
• List is one of the built-in data types in Python.
• Creating and accessing lists
• my_list = [1, 2, 3]
• empty_list = []
• Lists contain regular Python objects, separated by commas and
surrounded by brackets.
• The elements in a list can have any data type and can be
mixed.
• list1 = ["Rohan", "Physics", 21, 69.75]
• list2 = [1, 2, 3, 4, 5]
• list3 = ["a", "b", "c", "d"]
• list4 = [25.50, True, -55, 1+2j]
• Each item in a list has a unique position index, starting from 0.
Accessing Values in Lists
• To access values in lists, use the square brackets for slicing
along with the index or indices to obtain value available at
that index
#WAPS TO ACCESS THE ELEMENTS
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print ("list1[0]: ", list1[0])
print ("list2[1:5]: ", list2[1:5])

output
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
LIST-INDEX
languages = ["Python", "Swift", "C++"]

# access item at index 0


print(languages[0]) # Python

# access item at index 2


print(languages[2]) # C++
Negative Indexing in Python

• Python allows negative indexing for its sequences. The index of -


1 refers to the last item, -2 to the second last item and so
on.

languages = ["Python", "Swift", "C++"]

# access item at index 0


print(languages[-1]) # C++

# access item at index 2


print(languages[-3]) # Python
List methods (e.g., `append`, `remove`, `sort`),
• Add Elements to a List
• Lists are mutable (changeable). Meaning we can add and
remove elements from a list.
• 1. Using append()
• The append() method adds an item at the end of the list. For
example,
numbers = [21, 34, 54, 12]
print("Before Append:", numbers)

# using append method


numbers.append(32)

print("After Append:", numbers)


Remove an Item From a List
• 1. Using del Statement
In Python we can use the del statement to remove one or
more items from a list. For example,
languages = ['Python', 'Swift', 'C++', 'C', 'Java', 'Rust', 'R']

# deleting the second item


del languages[1]
print(languages) # ['Python', 'C++', 'C', 'Java', 'Rust', 'R']

# deleting the last item


del languages[-1]
print(languages) # ['Python', 'C++', 'C', 'Java', 'Rust']

# delete the first two items


del languages[0 : 2] # ['C', 'Java', 'Rust']
print(languages)
Remove an Item From a List
• 2. Using remove()
• We can also use the remove() method to delete a list
item. For example,

languages = ['Python', 'Swift', 'C++', 'C', 'Java',


'Rust', 'R']

# remove 'Python' from the list


languages.remove('Python')

print(languages) # ['Swift', 'C++', 'C', 'Java',


'Rust', 'R']
Python List sort()
• The sort() method sorts the items of a list in ascending or
descending order.

#WAPS TO SORT THE LIST IN ASCENDING


ORDER
prime_numbers = [11, 3, 7, 5, 2]
# sorting the list in ascending order
prime_numbers.sort()

print(prime_numbers)

# Output: [2, 3, 5, 7, 11]


Sort in Descending order
• The sort() method accepts a reverse parameter as an
optional argument.
• Setting reverse = True sorts the list in the descending
order

• list.sort(reverse=True)
# WAPS Sort the list in Descending order
# vowels list
vowels = ['e', 'a', 'u', 'o', 'i']

# sort the vowels Output


vowels.sort(reverse=True)
Sorted list (in Descending): ['u', 'o',
# print vowels 'i', 'e', 'a']
print('Sorted list (in Descending):', vowels)
List slicing and indexing
• Sometimes you need to get parts of a list.
• Python has a powerful syntax to do so, called slicing,
and it makes working with lists much easier than other
programming languages.
• Slicing works on Python lists and all other sequence
types, like strings, tuples, and ranges.
• The slicing syntax is as follows:
• my_list[start:stop:step]
>>> my_list = [1, 2, 3, 4, 5, 6, 7, 8]
>>> my_list[0:3] # get the first three elements of a
list
[1, 2, 3]
>>> my_list[:3] # start is 0 by default
[1, 2, 3]
>>> my_list[4:] # skip the first 4 elements
[5, 6, 7, 8]
The step value

The step value in a slice is one by default.

If you increase the step size, you can step over elements. E.g., a step size
of two means on each step, one element is skipped over. Let’s try
this:
>>> my_list = [1, 2, 3, 4, 5, 6, 7, 8]
>>> my_list[::2] # skip one each time
[1, 3, 5, 7]
Tuples:

You might also like