Python 🐍

May 8, 2023

(1y ago)

Hello There 🐼

Let's talk python 🐍.


List

Setting up of a list

main.py
my_list = list()
my_list = []

insertion

main.py
my_list.append(1)        # Insert at the end.
my_list.insert(1,"hi")   # Insert "hi" at position 1.
my_list.extend([2,3,4])  # Insert 2,3,4 at the end.

bonus

main.py
my_list = [1,2,3,4,5]
append = [6,7,8,9,10]
mylist.append(append)       #[1, 2, 3, 4, 5, [6, 7, 8, 9, 10]]
mylist.extend(append)       #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
mylist = mylist + append    #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

deletion

main.py
my_list.pop()       # remove the last element from the list.
my_list.pop(1)      # remove the element at index '1' from the list.
my_list.remove(2)   # remove element '2' from the list.

sort

main.py
my_list = [5, 4, 3, 2, 1]
my_list.sort()                  # Sort the list in-place
my_new_list = sorted(my_list)   # Sort 'my_list' and put in 'my_new_list'

slice

main.py
my_list = [1, 2, 3, 4, 5]
new_list = my_list[:]               # Copy list 'my_list'
new_list = my_list.copy()           # Copy list 'my_list'
even_list = my_list[::2]            # Even index element in 'my_list'
reverse_list = my_list[::-1]        # reverse 'my_list'
reverse_list = my_list.reverse()    # reverse 'my_list'

Working with Tuples is more memory and time efficient compared to working with Lists .


Set

Setting up of a set

main.py
my_set = set()
my_set = {1, 2, 3, 3}  # my_set = {1, 2, 3}

we can perform many operations on set like Union, intersection, and difference.

Match

Switch case version of python introduced in python-3.10

main.py
color = input("Enter a color")
 
match color:
    case "green":
       print("Earth is green")
    case "black":
       print("Black is cool")
    case "pink":
       print("Barbie is pink??")
    case "blue":
       print("sky is blue")
    case _:
        print("You should enter a color name")

Counter

Count the number of occurances of a charecter/number or anything that is itterable.

main.py
from collections import Counter
 
word = "aaaabbbccc"
count = Counter(word)
 
print(count)
# Counter({'a': 4, 'b': 3, 'c': 3})
 
print(count.most_common(1))
# [("a", 4)]
 
print(count.most_common(2))
# [("a", 4), ("b", 3)]


Good Day 🐼