0% found this document useful (0 votes)
7 views16 pages

Python Assignment2

The document provides an overview of collection-based data types in Python, including Tuples, Lists, Sets, and Dictionaries, detailing their characteristics and usage. It also discusses various string functions such as len(), str(), lower(), upper(), and others, explaining their purposes and syntax. The conclusion emphasizes the learning of these data types and string functions.

Uploaded by

Kabir Peswani
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)
7 views16 pages

Python Assignment2

The document provides an overview of collection-based data types in Python, including Tuples, Lists, Sets, and Dictionaries, detailing their characteristics and usage. It also discusses various string functions such as len(), str(), lower(), upper(), and others, explaining their purposes and syntax. The conclusion emphasizes the learning of these data types and string functions.

Uploaded by

Kabir Peswani
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/ 16

THADOMAL SHAHANI ENGINEERING

COLLEGE

DEPARTMENT OF INFORMATION
TECHNOLOGY

2.Collection based data types and


string function
Aim: Python program to handle collection-based data types
(i.e. List, tuple, dictionary, set) and string functions.
Theory:
1)Tuple: Tuples are used to store multiple items in a single
variable. Tuple is one of 4 built-in data types in Python used to
store collections of data, the other 3 are List, Aet and Dictionary,
all with different qualities and usage. A tuple is a collection which is
ordered and unchangeable. Tuple items are ordered,
unchangeable, and allow duplicate values. Tuple items are indexed,
the first item has index [0], the second item has index [1] etc.

2)List: Lists are used to store multiple items in a single variable.


Lists are one of 4 built-in data types in Python used to store
collections of data, the other 3 are Tuple, Set, and Dictionary, all
with different qualities and usage. Lists are created using square
brackets.List items are indexed, the first item has index [0], the
second item has index [1] etc.When we say that lists are ordered, it
means that the items have a defined order, and that order will not
change. If you add new items to a list, the new items will be placed
at the end of the list.
3)Sets: Sets are used to store multiple items in a single variable. Set
is one of 4 built-in data types in Python used to store collections of
data, the other 3 are List, Tuples and Dictionary, all with
differentqualities and usage. A set is a collection
which is unordered, unchangeable*, and unindexed. Set items
are unordered, unchangeable, and do not allow duplicate values.
4)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, and have keys and values. Dictionary items are
ordered, changeable, and do not allow duplicates. Dictionary items
are presented in key:value pairs, and can be referred to by using
the key name.
5)String functions: It is a sequence of Unicode characters that is
enclosed in quotation marks. In this article, we will discuss the in-
built string functions i.e. the functions provided by Python to
operate on strings.
• len():

Purpose: Returns the length of a string (i.e., the


number of characters in the string).
Syntax: len(string).
• str()
Purpose: Converts any data type into a string
format. Syntax: str(object).
• lower()
Purpose: Converts all characters in a string to
lowercase. Syntax: string.lower().
• upper()
Purpose: Converts all characters in a string to
uppercase. Syntax: string.upper().
• capitalize()
Purpose: Capitalizes the first character of a string, making
all other characters lowercase.
Syntax: string.capitalize().

• title()
Purpose: Converts the first letter of each word in the
string to uppercase and the rest to lowercase.
Syntax: string.title().

• find()
Purpose: Searches for a specified substring in the string
and returns the index of the first occurrence. If the
substring is not found, it returns -1.
Syntax: string.find(substring).

• count()
Purpose: Counts the number of occurrences of a
substring in the string.
Syntax: string.count(substring).

• join()
Purpose: Joins a list or tuple of strings into a single
string with a specified separator.
Syntax: separator.join(iterable).
• isdigit()
Purpose: Checks if all characters in the string are
digits. Syntax: string.isdigit().
• isalnum()

Purpose: Checks if all characters in the string are


alphanumeric (letters or digits).
Syntax: string.isalnum().
• isupper()
Purpose: Checks if all characters in the string are
uppercase. Syntax: string.isupper().
• islower()

Purpose: Checks if all characters in the string are


lowercase. Syntax: string.islower().

Program with output:


• List:
• Tuple:
• Sets:
• Dictionary:
• String functions:
Conclusion: [lo2]Learned the collection-based data types like
Lists, Tuples, Sets, Dictionary and String functions.

You might also like