Open In App

Python | Set 4 (Dictionary, Keywords in Python)

Last Updated : 11 Sep, 2023
Comments
Improve
Suggest changes
62 Likes
Like
Report

In the previous two articles (Set 2 and Set 3), we discussed the basics of python. In this article, we will learn more about python and feel the power of python.

Dictionary in Python 

In python, the dictionary is similar to hash or maps in other languages. It consists of key-value pairs. The value can be accessed by a unique key in the dictionary. (Before python 3.7 dictionary used to be unordered meant key-value pairs are not stored in the order they are inputted into the dictionary but from python 3.7 they are stored in order like the first element will be stored in the first position and the last at the last position.)

Output: 

{'xyz': 123, 'abc': 345}
['xyz', 'abc']
[123, 345]
xyz 123
abc 345
0 xyz 123
1 abc 345
True
False

Time Complexity: O(1)
Auxiliary Space: O(n)

break, continue, pass in Python 

  • break: takes you out of the current loop.
  • continue:  ends the current iteration in the loop and moves to the next iteration.
  • pass: The pass statement does nothing. It can be used when a statement is required. syntactically but the program requires no action.

It is commonly used for creating minimal classes.

# Function to illustrate break in loop
def breakTest(arr):
    for i in arr:
        if i == 5:
            break
        print (i)
    # For new line
    print("") 

# Function to illustrate continue in loop
def continueTest(arr):
    for i in arr:
        if i == 5:
            continue
        print (i)

    # For new line
    print("") 

# Function to illustrate pass 
def passTest(arr):
        pass

    
# Driver program to test above functions

# Array to be used for above functions:
arr = [1, 3 , 4, 5, 6 , 7]

# Illustrate break
print ("Break method output")
breakTest(arr)

# Illustrate continue 
print ("Continue method output")
continueTest(arr)

# Illustrate pass- Does nothing
passTest(arr)

Output: 

Break method output
1 3 4
Continue method output
1 3 4 6 7

Time complexity: O(n), where n is the length of the array arr.
Auxiliary space: O(1), since the space used is constant and does not depend on the size of the input.

map, filter, lambda

  • map: The map() function applies a function to every member of iterable and returns the result. If there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables.
  • filter:  It takes a function returning True or False and applies it to a sequence, returning a list of only those members of the sequence for which the function returned True.
  • lambda: Python provides the ability to create a simple (no statements allowed internally) anonymous inline function called lambda function. Using lambda and map you can have two for loops in one line.

Output:

[1, 8, 27, 64, 125]
25
12
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4]

For more clarity about map, filter, and lambda, you can have a look at the below example: 

Output: 

[15, 25]

The same operation can be performed in two lines using map, filter, and lambda as : 

Output:

[15, 25]


Next Article
Practice Tags :

Similar Reads