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

Python Functions for datatype

The document provides a comprehensive list of methods and their syntax for various Python data types including strings, lists, dictionaries, tuples, sets, ranges, bytes, and frozensets. Each method is accompanied by a brief description of its functionality and an example of its usage. This serves as a quick reference guide for Python programming.

Uploaded by

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

Python Functions for datatype

The document provides a comprehensive list of methods and their syntax for various Python data types including strings, lists, dictionaries, tuples, sets, ranges, bytes, and frozensets. Each method is accompanied by a brief description of its functionality and an example of its usage. This serves as a quick reference guide for Python programming.

Uploaded by

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

Sl String (str) Syntax Meaning Example

No

1 str.upper() Converts all characters "hello".upper() → 'HELLO'


to uppercase.

2 str.lower() Converts all characters "HELLO".lower() → 'hello'


to lowercase.

3 str.capitalize() Capitalizes the first letter "hello".capitalize() → 'Hello'


of the string.

4 str.title() Converts the first letter of "hello world".title() → 'Hello


each word to uppercase.
World'

5 str.strip() Removes leading and " hello ".strip() → 'hello'


trailing spaces.

6 str.replace(old, Replaces a substring "hello".replace("e", "a") → 'hallo'


with another.
new)

7 str.split() Splits the string into a list "hello world".split() → ['hello',


of words.
'world']

8 str.find(sub) Finds the first occurrence "hello world".find("world") → 6


of a substring.

9 str.count(sub) Counts the occurrences "hello world".count("o") → 2


of a substring.

10 str.join(iterable) Joins elements of an " ".join(['hello', 'world']) →


iterable into a string.
'hello world'

11 str.isalpha() Checks if all characters "hello".isalpha() → True


are alphabetic.

12 str.isdigit() Checks if all characters "12345".isdigit() → True


are digits.

13 str.islower() Checks if all characters "hello".islower() → True


are lowercase.
14 str.isupper() Checks if all characters "HELLO".isupper() → True
are uppercase.

15 str.startswith(pref Checks if the string starts "hello".startswith("he") → True


with a prefix.
ix)

16 str.endswith(suffix Checks if the string ends "hello".endswith("lo") → True


with a suffix.
)

Sl List Syntax Meaning Example


No

1 list.append(item) Adds an item to the end of the list. [1, 2, 3].append(4) → [1, 2,
3, 4]

2 list.extend(iterabl Adds all elements of an iterable to [1, 2].extend([3, 4]) → [1,


the list.
e) 2, 3, 4]

3 list.insert(index, Inserts an item at a specified [1, 3].insert(1, 2) → [1, 2,


position.
item) 3]

4 list.remove(item) Removes the first occurrence of an [1, 2, 3].remove(2) → [1, 3]


item.

5 list.pop(index) Removes and returns the item at [1, 2, 3].pop(1) → 2


the specified position.

6 list.clear() Removes all items from the list. [1, 2, 3].clear() → []

7 list.index(item) Returns the index of the first [1, 2, 3].index(2) → 1


occurrence of an item.

8 list.count(item) Returns the count of the [1, 2, 2, 3].count(2) → 2


occurrences of an item.

9 list.sort() Sorts the list in ascending order. [3, 1, 2].sort() → [1, 2, 3]


10 list.reverse() Reverses the order of the list. [1, 2, 3].reverse() → [3, 2,
1]

11 list.copy() Returns a shallow copy of the list. [1, 2, 3].copy() → [1, 2, 3]

Sl Dictionary (dict) Meaning Example


No Syntax

1 dict.get(key) Returns the value for the specified key. {'a': 1}.get('a') → 1

2 dict.keys() Returns a view object of all the keys. {'a': 1}.keys() →


dict_keys(['a'])

3 dict.values() Returns a view object of all the values. {'a': 1}.values() →


dict_values([1])

4 dict.items() Returns a view object of all key-value {'a': 1}.items() →


pairs.
dict_items([('a', 1)])

5 dict.update(other Updates the dictionary with key-value {'a': 1}.update({'b': 2}) →


pairs from another dictionary.
_dict) {'a': 1, 'b': 2}

6 dict.pop(key) Removes and returns the value for the {'a': 1}.pop('a') → 1
specified key.

7 dict.popitem() Removes and returns the last {'a': 1, 'b': 2}.popitem()


key-value pair.
→ ('b', 2)

8 dict.clear() Removes all key-value pairs from the {'a': 1}.clear() → {}


dictionary.

9 dict.copy() Returns a shallow copy of the {'a': 1}.copy() → {'a': 1}


dictionary.
Sl No Tuple Syntax Meaning Example

1 tuple.count(item) Counts the occurrences of an item in the (1, 2, 2, 3).count(2) → 2


tuple.

2 tuple.index(item) Returns the index of the first occurrence (1, 2, 3).index(2) → 1


of an item.

Sl SetSyntax Meaning Example


No

1 set.add(item) Adds an item to the set. {1, 2}.add(3) → {1, 2, 3}

2 set.update(iterable Adds all items from an iterable to the {1, 2}.update([3, 4]) →
set.
) {1, 2, 3, 4}

3 set.remove(item) Removes an item from the set. Raises {1, 2}.remove(2) → {1}
KeyError if item not found.

4 set.discard(item) Removes an item from the set if present, {1, 2}.discard(2) → {1}
does nothing if not found.

5 set.pop() Removes and returns an arbitrary item {1, 2, 3}.pop() → 1


from the set.

6 set.clear() Removes all items from the set. {1, 2, 3}.clear() → set()

7 set.copy() Returns a shallow copy of the set. {1, 2}.copy() → {1, 2}

Sl Range Syntax Meaning Example


No

1 range.start Returns the starting value of the range. range(1, 5).start → 1

2 range.stop Returns the stopping value of the range. range(1, 5).stop → 5

3 range.step Returns the step value of the range. range(1, 10, 2).step → 2
Additional Methods for String (str) Data Type

Sl Syntax Meaning Example


No

17 str.isdigit() Checks if all characters in the string are "12345".isdigit() → True


digits.

18 str.isspace() Checks if all characters are whitespace. " ".isspace() → True

19 str.isnumeric() Checks if all characters in the string are "12345".isnumeric() → True


numeric.

20 str.isdecimal() Checks if all characters are decimal "12345".isdecimal() → True


characters.

21 str.isalnum() Checks if all characters are "hello123".isalnum() → True


alphanumeric.

22 str.isidentifie Checks if the string is a valid Python "variable".isidentifier() →


identifier.
r() True

23 str.islower() Checks if all characters in the string are "hello".islower() → True


lowercase.

24 str.isupper() Checks if all characters are uppercase. "HELLO".isupper() → True

25 str.zfill(width Pads the string to a specified width with "42".zfill(5) → '00042'


leading zeros.
)

26 str.swapcase() Swaps the case of all characters. "Hello World".swapcase() →


'hELLO wORLD'

27 str.lstrip() Removes leading whitespaces. " hello".lstrip() → 'hello'

28 str.rstrip() Removes trailing whitespaces. "hello ".rstrip() → 'hello'

2. Additional Methods for List Data Type

Sl Syntax Meaning Example


No
12 list.sort(reverse=True) Sorts the list in [3, 1, 2].sort(reverse=True)
descending order.
→ [3, 2, 1]

13 list.reverse() Reverses the elements in [1, 2, 3].reverse() → [3, 2,


the list.
1]

14 list.copy() Creates a shallow copy of [1, 2, 3].copy() → [1, 2, 3]


the list.

15 list.remove(item) Removes the first [1, 2, 3, 4].remove(2) → [1,


occurrence of an item in
the list.
3, 4]

16 list.insert(index, item) Inserts an item at the [1, 2, 3].insert(1, 10) → [1,


specified position in the
list.
10, 2, 3]

3. Additional Methods for Dictionary (dict) Data Type

Sl Syntax Meaning Example


No

10 dict.setdefault(key, Returns the value of the d = {'a': 1};


specified key. If key doesn't
default) exist, inserts the key with
d.setdefault('b', 2) → 2
the default value.

11 dict.pop(key, default) Removes a key-value pair d = {'a': 1}; d.pop('b',


and returns the value, or
returns the default if key
'not found') → 'not found'
doesn't exist.

12 dict.fromkeys(iterable, Creates a new dictionary dict.fromkeys([1, 2], 'a')


with keys from the iterable
value) and values set to the
→ {1: 'a', 2: 'a'}
specified value.
13 dict.update(other_dict) Merges the other dictionary d1 = {'a': 1}; d2 = {'b':
into the current dictionary.
2}; d1.update(d2) → {'a': 1,
'b': 2}

4. Additional Methods for Set Data Type

Sl Syntax Meaning Example


No

8 set.union(*others) Returns a new set with all {1, 2}.union({3, 4}) → {1, 2,
elements from the set and all
other sets.
3, 4}

9 set.intersection(*ot Returns a new set with the {1, 2}.intersection({2, 3})


common elements from the set
hers) and other sets.
→ {2}

10 set.difference(*othe Returns a new set with all {1, 2}.difference({2, 3}) →


elements in the set that are not in
rs) the other sets.
{1}

11 set.symmetric_differ Returns a new set with elements {1,


in either the set or the other sets,
ence(*others) but not both.
2}.symmetric_difference({2,
3}) → {1, 3}

12 set.issubset(other) Checks if the set is a subset of {1, 2}.issubset({1, 2, 3}) →


another set.
True

13 set.issuperset(other Checks if the set is a superset of {1, 2, 3}.issuperset({2, 3})


another set.
) → True

14 set.isdisjoint(other Checks if the set has no common {1, 2}.isdisjoint({3, 4}) →


elements with another set.
) True

5. Additional Methods for Tuple Data Type


Sl Syntax Meaning Example
No

3 tuple.index(item, Returns the index of the first (1, 2, 3).index(2, 0, 3)


occurrence of an item in the tuple
start, end) within the specified range.
→1

6. Additional Methods for Range Data Type

Sl Syntax Meaning Example


No

4 range.__contains__(item) Checks if the specified item is in 3 in range(1, 5) →


the range.
True

7. Methods for Bytes Data Type

Sl Syntax Meaning Example


N
o

1 bytes.decode(encodi Decodes a byte object to a string b'hello'.decode('utf-8') →


using the specified encoding.
ng) 'hello'

2 bytes.find(sub) Finds the first occurrence of a byte b'hello'.find(b'o') → 4


sequence.

3 bytes.replace(old, Replaces a byte sequence with b'hello'.replace(b'l',


another.
new) b'r') → b'herro'

4 bytes.split(sep) Splits the byte object at the b'hello world'.split(b' ')


specified separator.
→ [b'hello', b'world']
8. Additional Methods for frozenset Data Type

Sl Syntax Meaning Example


No

1 frozenset.add(item) Adds an item to the frozenset. (Does not frozenset([1,


work if item is already present)
2]).add(3) →
frozenset([1, 2, 3])

2 frozenset.remove(it Removes an item from the frozenset. frozenset([1,


em) 2]).remove(1) →
frozenset([2])

3 frozenset.copy() Returns a shallow copy of the frozenset. frozenset([1,


2]).copy() →
frozenset([1, 2])

4 frozenset.differenc Returns a new frozenset with the frozenset([1,


difference of items between frozenset
e(*others) and others.
2]).difference([2,
3]) → frozenset([1])

You might also like