Skip to content

Commit 20de97a

Browse files
authored
Merge pull request #201 from codewithwaqar/patch-1
Create dictionary_comprehension.py
2 parents b566f02 + bf57f2f commit 20de97a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Python/dictionary_comprehension.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Dictionary Comprehension
2+
# squares = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}
3+
squares = {num:num**2 for num in range(1, 11)}
4+
print(squares)
5+
6+
squares2 = {f'Square of {num} is':num**2 for num in range(1, 11)}
7+
print(squares2)
8+
9+
for key,value in squares2.items():
10+
print(f'{key} : {value}')
11+
12+
# Count characters in word using Dictionary Comprehension
13+
name = 'waqar'
14+
word_count = {char:name.count(char) for char in name}
15+
print(word_count)

0 commit comments

Comments
 (0)