Python3 101 Tricks for beginners Part 1
Last updated: September 13, 2024 By Sunil Shaw
Introduction
Python is an open source popular programming language. It is mostly using in every fields. In Python3 101 tricks for beginners part 1 is the first part of Python3 Tricks for begineers Series.
Python List Small Python3 101 Tricks for beginners Part 1
I hope you knows about slicing, +
operator, indexing, for loop. Here i show you how to use you own small function for your benefits when interviwers ask to do not use any built-in
function.
Letâs Jump to +
operator, see how this operator works on list.
list1 = [1, 2]
list2 = [3, 4]
print(list1 + list2)
Output
[1, 2, 3, 4]
As you can see two list added further, think if we use +
on every element of old list to add in a new list, when iteration works we can use, pointer i
to change in list. Example :- [i]
1. How to create own len() function for Python List?
Algorithms
- create
counted_length
variable and store0
- iterate through list for size of list times
- Update
counted_length
by adding1
in them, when for loop iterate - Now return the
counted_length
def length(array):
counted_length = 0
for i in array:
counted_length += 1
return counted_length
Driver/Execution code for Testing
c = [1, 2, 3, 4, 5, 6]
print(length(c))
# checking for mixed data types
d = ['1', '2', 'a', 4.5, -5, "6", [1, 2], (1, 2), {'key':'value', 'key2': 2}, {1, 2, 3}]
print(length(d))
Output
6
10
Explanation:-
In Python lists are iterable data types so we are using for loop to iterate through every element of list item. We created a variable Counted_length
and stores 0. When every time for loop iterate we adding 1
to the existing value of Counted_length
variable. When every item iterated step by step, For Loop
stop iterating and function return stored value of Counted_length
.
2. How to create own append Function for Python List?
Algorithm :-
- First create empty list
- Iterate over an array of list, for size of no of array time.
- Add every iteration of array in previous array.
- If Size of
new_array
reached equal to array then add item. Break the loop
def appending(array, item):
lst = []
for i in range(len(array)):
lst = lst + [array[i]]
if len(lst) == len(array):
lst = lst + [item]
return lst
driver/execution code
x = [1, 2, 3, 4, 5, 6]
print(appending(x, 6))
Output
[1, 2, 3, 4, 5, 6, 6]
Explanation : –
In above code snippet function takes two argument array takes as a list and item for adding in list. We iterating through list len of array times, when len of new list named lst
will reached equal to len of array
it add item to the new list. Then for loop breaks and return new list lst
.
Another Method
def appending(array, item):
return array + [item]
driver/explanation code
h = [1, 2, 3, 4, 5, 6]
print(appending(h, 6))
Output
[1, 2, 3, 4, 5, 6, 6]
Simple and sweeter code. In above code snippet function takes two argument array takes as a list and item for adding in list. Here array is an list and item is an integer, so we have change it into list then weâll use +
to add in array. I hope you can understand.
3. How to create custom insert() function for python list?
Algorithms:-
- As usual we created an empty list.
- Now iterating through length of list of array times.
- check when pointer
i
is equal to passed parameterindex
then add item and also add item of array that present at sameindex
in list of array. - if pointer not equal to index else add index of array to new list named
lst
. - return
lst
.
def inserting(array, index, item):
lst = []
for i in range(len(array)):
if i == index:
lst = lst + [item]
lst = lst + [array[i]]
else:
lst = lst + [array[i]]
return lst
driver/execution code
y = [1, 2, 4, 5]
print(inserting(y, 2, 3))
Output
[1, 2, 3, 4, 5]
Explanation:-
Above code having named a inserting() function that takes 3 arguments, array as a list, index, and item. Passed parameter array ask for list, index is for where you want to add your argument. checking every item when pointer i
is equal to index then add item and also add item of array that present at same index in list of array if pointer not equal to index else add index of array to new list named lst
4. How to create Custom/Own remove() function for Python List?
Algorithms:-
- Iterate through list
- Add that item when pointer i in not equal to item.
def removing(array, item):
lst = []
for i in range(len(array)):
if array[i] != item:
lst = lst + [array[i]]
return lst
driver/ececution code
z = [1, 2, 3, 4]
print(removing(z, 4))
Output
[1, 2, 3]
Explanation:-
In Above code iteration takes place length of array times. Adding that item in new list that is not equal to item.
5. How to create Custom reverse() function for Python List?
Algorithms:
- iterating through list of array
- Adding one item from last
- Returning new list
def reverse(array):
lst = []
for i in range(len(array)):
lst = lst + [array[len(array)-1-i]]
return lst
driver/execution code
p = [1, 2, 3]
print(reverse(p))
Output
[3, 2, 1]
Explanation:-
len(array)-1-i
array[len(array)-1-i]
[array[len(array)-1-i]]
Above code calling for len() function that returns the length of array, we substracting them by 1
and again by pointer i
. After calculation it gives an integer result we use them as slicing or index of array to call a particular item from array and then pass them in a list. Now we can add two list further.
6. How to create Custom poping() function for Python List?
Algorithm:-
- Iterate len of array time
- Skip last iteration to pop last item
- Add every item in a new list
- return list
def poping(array):
lst = []
for i in range(len(array)-1):
lst = lst + [array[i]]
return lst
driver/execution code
x = [1, 2, 3, 4, 5, 6]
print(poping(x))
Output
[1, 2, 3, 4, 5]
Explanation:-
In above code we simply iterating for loop through (size of array)-1
. The basic idea is that when loop iterate one time lesser than original length of array, for loop canât access the last item of array.
7. How to get min Value from list without using min() built-in function?
algorithm:-
- Sort in ascending
- Access 0th item of sorted array
def min(array):
for i in range(len(array)):
for j in range(0, len(array)-1-i):
if array[j]>array[j+1]:
array[j], array[j+1] = array[j+1], array[j]
return array[0]
driver/explanation code
x = [2, -2, 8, -8, 3, 7, 9]
print(min(x))
Output
-8
Explanation :-
In above Code snippet i am using bubble sort algorithm to sort them in ascending order. Then accessing 0th item of sorted array. Example array[0]
8. How to get max Value from list without using max() built-in function?
Algorithm:-
- Sort in ascending
- Access Last item of sorted array
def max(array):
for i in range(len(array)):
for j in range(0, len(array)-1-i):
if array[j]>array[j+1]:
array[j], array[j+1] = array[j+1], array[j]
return array[-1]
driver/explanation code
x = [2, -2, 8, -8, 3, 7, 9]
print(max(x))
Output
9
Explanation:-
In above Code snippet i am using bubble sort algorithm to sort them in ascending order. Then accessing Last item of sorted array. Example array[-1]
9. How to Print String to Specific Number of Time
Algorithm:-
- We create a function
- We use asteric (*) to multiply
- Lastly return result
def Multiply_String(str, time):
res = str*time
return res
Driver/explanation code
print(Multiply_String('CodeWithRonny ', 10))
Output
CodeWithRonny CodeWithRonny CodeWithRonny CodeWithRonny CodeWithRonny CodeWithRonny CodeWithRonny CodeWithRonny CodeWithRonny CodeWithRonny
Explanation:-
We are creating function named Multiply_String
and passing 2 parameter into it str
, time
. In parameter str
denote String
and time
to print how many times. Using asterisk
operator multiply string and store into res
variable, then return res
.
About Author
I am a Web Developer, Love to write code and explain in brief. I Worked on several projects and completed in no time.
View all posts by Sunil Shaw