• Slider Banner

    The Programming World

    Learn about top programming languages, tools and frameworks.

    Read more
  • Slider Banner

    Certification MCQ

    Practice for certification from 1000+ multiple choice questions.

    Read more
  • Slider Banner

    Prepare for Technical Interview

    Over 1 Million+ interview questions for different technical profiles.

    Read more

Is There a Way to Get a List of All the Keys in a Dictionary? If So, How Would You Do It?

To obtain a list of all the keys in a dictionary, we have to use function keys(). Check below example:

my_dict={'k1':10,'k2':5,'k3':20,'k4':25}
keys_list = []

for key in my_dict.keys():
    keys_list.append(key)

print(keys_list)

Output: ['k1', 'k2', 'k3', 'k4']

In a List and in a Dictionary, What Are the Typical Characteristics of Elements?

Elements in lists maintain their ordering unless they are explicitly commanded to be re-ordered. They can be of any data type, they can all be the same, or they can be mixed. Elements in lists are always accessed through numeric, zero-based indices.

In a dictionary, each entry will have a key and a value, but the order will not be guaranteed. Elements in the dictionary can be accessed by using their key.

Lists can be used whenever you have a collection of items in an order. A dictionary can be used whenever you have a set of unique keys that map to values.

Write a Regular Expression using Python that confirms an Email Id is correct?

You can use Python Reg Expression Module "Re"

import re

print(re.search(r"[0-9a-zA-Z.]+@[a-zA-Z]+\.(com|co\.in)$","ram.sharma@programsbuzz.com"))
print(re.search(r"[0-9a-zA-Z.]+@[a-zA-Z]+\.(com|co\.in)$","ram.sharmaprogramsbuzz.com"))
print(re.search(r"[0-9a-zA-Z.]+@[a-zA-Z]+\.(com|co\.in)$","ram.sharma@programsbuzzcom"))
print(re.search(r"[0-9a-zA-Z.]+@[a-zA-Z]+\.(com|co\.in)$","ram.sharma@programsbuzz.co.in"))

Output

What are negative indexes and why are they used?

To access an element from ordered sequences, we simply use the index of the element, which is the number of the position of that element. The index usually starts from 0, meaning that the first element has the index 0 and the second has 1, and so on.

When we use the index to access elements from the end of a list, it’s called reverse indexing. In reverse indexing, the indexing of elements start from the last element with the index number being ‘−1’. The second last element has the index ‘−2’, and so on. These indexes used in reverse indexing are called negative indexes.

What are functions in Python?

A function is a block of code which is executed only when it is called. To define a Python function, the def keyword is used. You can pass data, known as parameters, into a function. A function can return data as a result.

Example

def my_function(name):
    print("Hi, Welcome to",name)

# Calling the function
my_function("ProgramsBuzz");

Output: Hi, Welcome to ProgramsBuzz.

What is docstring in Python?

The docstrings are declared using """triple double quotes""" just below the class, method or function declaration. All functions should have a docstring.

The docstrings can be accessed using the __doc__ method of the object or using the help function.

Example

def my_function(): 
    """This is my function documentation."""
    
print("Output Using __doc__:")
print(my_function.__doc__)
  
print("Using help:")
help(my_function) 

Output

Subscribe to