What is Ordered Dictionary and Default Dictionary in Python?

What is Ordered Dictionary and Default Dictionary in Python?

Dictionary in Python Dictionary is an unordered collection of mutable values indexed by unique keys. Values in a dictionary can be of any datatype, mutable and can be duplicated, whereas keys can’t be repeated and are immutable. Dictionaries are just like map(), storing, and retrieving elements by referencing a key. As dictionaries are referenced by … Read more

Some Basic Python Tuple Programs-2

Some Basic Python Tuple Programs-2

Python Program to check if a tuple and a list are identical tuple = (9, 8, 7, 6, 5) list = [9, 8, 7, 6, 5] #using loop flag1 = True for i in range(0, len(tuple)): if tuple[i] != list[i]: flag1 = False break if flag1: print(f”{tuple} and {list} are identical”) else: print(f”{tuple} and {list} … Read more

Some Basic Python List Programs-3

Some Basic Python List Programs-3

Python program to remove duplicates from a list list1 = [2, 6, 9, 8, 4, 8, 6, 6, 5, 7, 2, 4] #using list comprehension list2 = [] [list2.append(ele) for ele in list1 if ele not in list2] print(list2) #Output: [2, 6, 9, 8, 4, 5, 7] #using list comprehension and enumerate() list3 = [ele … Read more

Some Basic Python String Programs-3

Some Basic Python String Programs-3

Python program to move Word to Rear-end str1 = “Beta Python: Python for U ” word = “Python” #using slicing and find() str2 = str1[:str1.find(word)] + str1[str1.find(word) + len(word):] + word print(str2) #Output: Beta : Python for U Python #using replace() str3 = str1.replace(word,””) + word print(str3) #Output: Beta : for U Python Slicing extracts … Read more

Some Basic Python Tuple Programs-3

Some Basic Python Tuple Programs-3

Python program to find the minimum and maximum value in a list of tuples tuple_list = [(6, 5), (3, 8), (1, 6), (4, 7), (3, 1)] ele1 = tuple(map(max, zip(*tuple_list))) ele2 = tuple(map(min, zip(*tuple_list))) print(ele1, el2) #Output: (6, 8) (1, 1) The built-in max() function returns the largest element in an iterable. The built-in min() … Read more

Some Basic Python String Programs-4

Some Basic Python String Programs-4

Python program to check if the string is ASCII string = “Beta Python: Python for U” #using all() and ord() if all(ord(ch) < 128 for ch in string): print(f”{string} is ASCII string”) else: print(f”{string} is not an ASCII string”) #Output: Beta Python: Python for U is ASCII string #using lambda and encode() if lambda ch: … Read more

Some Basic Python Dictionary Programs-1

Some Basic Python Dictionary Programs-1

Dictionary Dictionary is an unordered collection of mutable values indexed by unique keys. Values in a dictionary can be of any datatype, mutable and can be duplicated, whereas keys can’t be repeated and are immutable. Python program to extract values of Particular Key in Nested Values dict = {“Uppercase” : {1 : “A”, 2 : … Read more

Some Basic Python String Programs-2

Some Basic Python String Programs

Python Program to split a string into groups of n consecutive characters string = “Beta Python: Python for U” n = 5 split_string = [(string[ch:ch+n]) for ch in range(0, len(string), n)] #list comprehension print(split_string) #Output: [‘Beta ‘, ‘Pytho’, ‘n: Py’, ‘thon ‘, ‘for U’] List comprehension is a compact method that defines a list and … Read more

Some Basic Python List Programs-2

Some Basic List Programs

Python program to copy a list list1 = [9, 4, 10, 5] list2 = list(list1) print(“Original list: “,list1) print(“Copied list: “,list2) #Output: Original list: [9, 4, 10, 5] Copied list: [9, 4, 10, 5] The list() built-in method takes an iterable and creates a new list of its elements. Python program to concatenate multiple lists … Read more

What is indexing, iterating and enumerating in python?

What is indexing, iterating and enumerating in python?

Indexing “Indexing” means addressing an element of a sequential object by its position within the sequential object.  A sequential object is in general, an ordered collection. In python, every element of an ordered collection is accessed based on their position in the collection. An index is a numerical form of position. Python uses square bracket … Read more

%d bloggers like this: