Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Python functions and methods

The document provides an overview of various Python data types and their associated methods, including strings, lists, tuples, dictionaries, and sets. It outlines key operations such as adding, removing, and manipulating elements, as well as retrieving information like maximum and minimum values. Additionally, it distinguishes between functions and methods, highlighting that functions are independent while methods are tied to objects.
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)
4 views

Python functions and methods

The document provides an overview of various Python data types and their associated methods, including strings, lists, tuples, dictionaries, and sets. It outlines key operations such as adding, removing, and manipulating elements, as well as retrieving information like maximum and minimum values. Additionally, it distinguishes between functions and methods, highlighting that functions are independent while methods are tied to objects.
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/ 5

Python functions and methods

1. String Methods

s = "hello world"

• print(s.upper()) # HELLO WORLD


• print(s.lower()) # hello world
• print(s.title()) # Hello World
• print(s.capitalize()) # Hello world
• print(s.strip()) # Removes spaces from start and end
• print(s.replace("world", "Python")) # hello Python
• print(s.split()) # ['hello', 'world']
• print(s.find("o")) # 4 (Index of first 'o')
• print(s.count("o")) # 2
• print(s.startswith("he")) # True
• print(s.endswith("ld")) # True
• print(max(s)) # w (Highest ASCII)
• print(min(s)) # (Space has lowest ASCII)
• print(len(s)) # 11 (Total characters)

2. List Methods

lst = [3, 1, 4, 2]

• lst.append(6)
• print(lst) # [3, 1, 4, 2, 6]

• lst.insert(2, 10)
• print(lst) # [3, 1, 10, 4, 2, 6]
• lst.remove(3)
• print(lst) # [1, 10, 4, 2, 6]

• print(lst.pop()) # 6 (Removes last element)


• print(lst.index(10)) # 1 (Index of 10)
• print(lst.count(2)) # 1 (Occurrences of 2)

• lst.sort()
• print(lst) # [1, 2, 4, 10]

• lst.reverse()
• print(lst) # [10, 4, 2, 1]

• lst.extend([7, 8])
• print(lst) # [10, 4, 2, 1, 7, 8]

• lst.clear()
• print(lst) # []

• lst = [3, 1, 4, 2] # Reset list


• print(max(lst)) #4
• print(min(lst)) #1
• print(len(lst)) #4

3. Tuple Methods

tup = (10, 5, 8, 5, 2)

• print(tup.count(5)) # 2 (Occurrences of 5)
• print(tup.index(8)) # 2 (Index of first 8)
• print(max(tup)) # 10
• print(min(tup)) #2
• print(len(tup)) #5

4. Dictionary Methods

d = {"a": 10, "b": 20, "c": 5}

• print(d.keys()) # dict_keys(['a', 'b', 'c'])


• print(d.values()) # dict_values([10, 20, 5])
• print(d.items()) # dict_items([('a', 10), ('b', 20), ('c', 5)])
• print(d.get("a")) # 10
• print(d.pop("b")) # 20 (Removes 'b')
• print(d.popitem()) # ('c', 5) (Removes last item)
• print(len(d)) # 1 (Only 'a' left)

• d.update({"d": 40})
• print(d) # {'a': 10, 'd': 40}

• d.clear()
• print(d) # {}

• d = {"a": 10, "b": 20, "c": 5} # Reset dictionary


• print(max(d)) # c (Keys are compared alphabetically)
• print(min(d)) #a
• print(max(d.values())) # 20
• print(min(d.values())) # 5

5. Set Methods
s = {4, 2, 8, 1}

• s.add(6)
• print(s) # {1, 2, 4, 6, 8}

• s.remove(2)
• print(s) # {1, 4, 6, 8}

• s.discard(10) # No error if 10 is not in the set


• print(s) # {1, 4, 6, 8}

• print(s.pop()) # Removes and returns a random element

• s.clear()
• print(s) # set()

• s1 = {1, 2, 3}
• s2 = {3, 4, 5}

• print(s1.union(s2)) # {1, 2, 3, 4, 5}
• print(s1.intersection(s2)) # {3}
• print(s1.difference(s2)) # {1, 2}
• print(s1.symmetric_difference(s2)) # {1, 2, 4, 5}

• print(max(s1)) # 3
• print(min(s1)) # 1
• print(len(s1)) # 3

Final Notes:
• Each method is in a separate line for clarity.

• Dictionaries require .values() for max() and min().

• Tuple methods are fewer since they are immutable.

• Set methods focus on unique elements and mathematical operations.

Difference Between Function and Method


• A function is independent and can be called globally.

• A method is associated with an object and is called using object.method().

You might also like