Unit 3
Unit 3
Converted String:
itm college
Converted String:
Itm College
Converted String:
ITM cOLLEGE
Converted String:
Itm college
Original String
itm College
Here is the list of in-built Python string methods, that you can use to perform
actions on string:
rsplit() Split the string from the right by the specified separator
List of numbers:
[10, 20, 14]
List Items:
Geeks
Geeks
Python lets you use negative indexing as well. The negative indices are counted
from the right. The index -1 represents the final element on the List's right side,
followed by the index -2 for the next member on the left, and so on, until the last
element on the left is reached.
Example-
List = [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']
print("Accessing element using negative indexing")
# print the last element of list
print(List[-1])
# print the third last element of list
print(List[-3])
Output-
Accessing element using negative indexing
Geeks
For
Elements can be added to the List by using the built-in append () function. Only
one element at a time can be added to the list by using the append () method.
append() method only works for the addition of elements at the end of the List,
for the addition of elements at the desired position, insert() method is used.
Unlike append () which takes only one argument, the insert() method requires two
arguments(position, value).
Example-
List = []
print("Initial blank List: ")
print(List)
# Addition of Elements in the List
List.append(1)
List.append(2)
List.append(3)
List.append(4)
print("\nList after Addition of Three elements: ")
print(List)
List.insert(3, 12)
List.insert(0, 'Geeks')
print("\nList after performing Insert Operation: ")
print(List)
Output-
Initial blank List:
[]
Here is a list of in-built dictionary functions with their description. You can use
these functions to operate on a dictionary.
Method Description
Example-
List = ['Mathematics', 'chemistry', 1997, 2000]
List.append(20544)
print(List)
List.insert(2, 10087)
print(List)
List1 = [1, 2, 3]
List2 = [2, 3, 4, 5]
# Add List2 to List1
List1.extend(List2)
print(List1)
# Add List1 to List2 now
List2.extend(List1)
print(List2)
List = [1, 2, 3, 4, 5]
print(sum(List))
List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.count(1))
print(List.index(3))
numbers = [5, 2, 8, 1, 9]
print(min(numbers))
print(max(numbers))
List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
#Reverse flag is set True
List.sort(reverse=True)
print(List)
print(List.pop())
print(List.pop(0))
del List[0]
print(List)
List.remove(3)
print(List)
Output-
['Mathematics', 'chemistry', 1997, 2000, 20544]
['Mathematics', 'chemistry', 10087, 1997, 2000, 20544]
[1, 2, 3, 2, 3, 4, 5]
[2, 3, 4, 5, 1, 2, 3, 2, 3, 4, 5]
15
4
2
1
9
[5.33, 4.445, 3, 2.5, 2.3, 1.054]
1.054
5.33
[3, 2.5, 2.3]
[2.5, 2.3]
Dictionary manipulation methods:
Python Dictionary is like a map that is used to store data in the form of a key:
value pair. Python provides various built-in functions to deal with dictionaries.
Here’s a list of some important Python dictionary methods:
items() Return the list with all dictionary keys with values
pop() Returns and removes the element with the given key
Example-
my_dict = {'1': 'Geeks', '2': 'For', '3': 'Geeks'}
my_dict.clear()
print(my_dict)
d = {'Name': 'Ram', 'Age': '19', 'Country': 'India'}
print(d.get('Name'))
print(d.get('Gender'))
print(list(d.items())[1][0])
print(list(d.items())[1][1])
print(list(d.keys()))
print(list(d.values()))
d.pop('Age')
print(d)
d1 = {'Name': 'Ram', 'Age': '19', 'Country': 'India'}
d2 = {'Name': 'Neha', 'Age': '22'}
d1.update(d2)
print(d1)
Output-
{}
Ram
None
Age
19
['Name', 'Age', 'Country']
['Ram', '19', 'India']
{'Name': 'Ram', 'Country': 'India'}
{'Name': 'Neha', 'Age': '22', 'Country': 'India'}
Python Functions:
Python Functions is a block of statements that return the specific task. The idea
is to put some commonly or repeatedly done tasks together and make a function
so that instead of writing the same code again and again for different inputs, we
can do the function calls to reuse code contained in it over and over again.
Some Benefits of Using Functions-
Increase Code Readability
Increase Code Reusability
Types of Functions in Python:
Below are the different types of functions in Python:
Built-in library function: These are Standard functions in Python that are
available to use.
User-defined function: We can create our own functions based on our
requirements.
Python's standard library includes number of built-in functions. Some of Python's
built-in functions are print(), int(), len(), sum(), etc. In addition to the built-in
functions ,we can also create our own functions. These functions are called user-
defined functions.
Syntax-
# Driver code
greet("John")
num1, num2 = 5, 15
ans = add(num1, num2)
print(f"The addition of {num1} and {num2} results {ans}.")
Output-
Hello John
The addition of 5 and 15 results 20.
The following are the types of arguments that we can use to call a function:
1. Default arguments
2. Keyword arguments
3. Required arguments
4. Variable-length arguments
Output-
Even
Odd