0% found this document useful (0 votes)
10 views2 pages

PS Assignment 2

The document outlines the various types of variables in Python, including numeric types (int, float, complex), sequence types (str, list, tuple), mapping type (dict), set types (set, frozenset), and boolean type (bool). It also explains tuples as ordered, immutable collections, detailing methods such as count, index, len, max, min, sum, and sorted, along with examples for each method. The key takeaway is that tuples cannot be modified after creation, and the methods return new values or lists without altering the original tuple.

Uploaded by

rajpinbsjd
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)
10 views2 pages

PS Assignment 2

The document outlines the various types of variables in Python, including numeric types (int, float, complex), sequence types (str, list, tuple), mapping type (dict), set types (set, frozenset), and boolean type (bool). It also explains tuples as ordered, immutable collections, detailing methods such as count, index, len, max, min, sum, and sorted, along with examples for each method. The key takeaway is that tuples cannot be modified after creation, and the methods return new values or lists without altering the original tuple.

Uploaded by

rajpinbsjd
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/ 2

1. Define Types of Variable in Python.

In Python, variables are used to store data. They can hold different types of data, each with its
own characteristics and usage. Here are the main types of variables in Python:
a. Numeric Types:
● int: Represents whole numbers (e.g., 10, -5, 0).
● float: Represents numbers with decimal points (e.g., 3.14, -2.5, 0.0).
● complex: Represents complex numbers (e.g., 2+3j).
b. Sequence Types:
● str: Represents a sequence of characters (e.g., "Hello", 'Python').
● list: An ordered, mutable collection of elements (e.g., [1, 2, 3], ["apple", "banana"]).
● tuple: An ordered, immutable collection of elements (e.g., (1, 2, 3), ("apple", "banana")).
c. Mapping Type:
● dict: An unordered collection of key-value pairs (e.g., {"name": "Alice", "age": 30}).
d. Set Types:
● set: An unordered collection of unique elements (e.g., {1, 2, 3}).
● frozenset: An immutable set (e.g., frozenset({1, 2, 3})).
e. Boolean Type:
● bool: Represents either True or False.
2. What is tuple in Python? Explain various methods with example.
In Python, a tuple is an ordered, immutable collection of elements. It's similar to a list, but once
created, its elements cannot be changed or added to. Tuples are often used to represent groups
of related data that should not be modified.
Methods of Tuples:
● count(value): Returns the number of occurrences of value in the tuple.
my_tuple = (1, 2, 3, 2, 1)
count_of_2 = my_tuple.count(2) # count_of_2 will be 2

● index(value, start=0, end=len(tuple)): Returns the index of the first occurrence of value
within the specified range.
my_tuple = (1, 2, 3, 2, 1)
index_of_2 = my_tuple.index(2) # index_of_2 will be 1

● len(tuple): Returns the number of elements in the tuple.


my_tuple = (1, 2, 3)
length = len(my_tuple) # length will be 3

● max(tuple): Returns the largest element in the tuple.


my_tuple = (1, 5, 3, 2)
max_value = max(my_tuple) # max_value will be 5

● min(tuple): Returns the smallest element in the tuple.


my_tuple = (1, 5, 3, 2)
min_value = min(my_tuple) # min_value will be 1

● sum(tuple): Returns the sum of all elements in the tuple.


my_tuple = (1, 2, 3)
total = sum(my_tuple) # total will be 6
● sorted(tuple): Returns a new sorted list from the elements of the tuple.
my_tuple = (3, 1, 2)
sorted_list = sorted(my_tuple) # sorted_list will be [1, 2, 3]

Remember that tuples are immutable, so these methods don't modify the original tuple but
rather return new values or lists.

You might also like