Tasks
Tasks
C
1 reate a list of squares of all even numbers from 1 to 20.
"Python is
2. Create a list of the lengths of each word in a given sentence
awesome and fun"
.
[2, 3, 5, 7, 10,
3. Extract all numbers greater than 5 from a given list
, 4, 8]
1 .
. Create a list containing only the first character of each word in a given list
4
["apple", "banana", "cherry", "date"]
.
5. Create a list of tuples with the number and its square for numbers from 1
to 10.
6. Create a list of strings that are uppercase and have more than 3 letters
['abc', 'DEF', 'ghi', 'JKL', 'mnop']
from the list .
7. F latten a 2D list[[1, 2], [3, 4], [5, 6]]into asingle list.
Out_put : [1,2,3,4,5,6]
8. Given a list of numbers [1, 2, 3, 4, 5] , generatea new list where each
element is the result of multiplying the original number by 3, but only if
the number is odd.
9. Using a nested ternary operator, create a list that replaces numbers greater
'Large'and others with
than 10 with the string 'Small'from the list
[5,
12, 9, 19, 7, 3]
.
Dictionary Comprehension Questions
1. C reate a dictionary where keys are numbers from 1 to 5 and values are their
squares.
2. Given a list of names ['Alice', 'Bob', 'Charlie'] ,create a dictionary
where each name is a key, and the value is the length of the name.
3. Create a dictionary where the keys are the numbers from 1 to 10 and the
'Even'or
values are 'Odd'based on whether the numberis even or odd.
4. G [(1, 'a'), (2, 'b'), (3, 'c')]
iven a list of tuples ,create a
dictionary where the first element of each tuple is the key and the second
element is the value.
5. Create a dictionary that maps the first letter of each word to the
["apple", "banana",
corresponding word from the list "cherry",
"date"]
.
6. C ['apple', 'banana',
reate a dictionary from a list 'cherry', 'date']
where the key is the word and the value is its length, but exclude words with
less than 5 letters.
{'a': 1,
7. Using a dictionary comprehension, invert the following dictionary
'b': 2, 'c': 3}
, so the values become keys and keysbecome values.
8. C reate a dictionary that maps each word in the sentence"This is Python"
to its index in the sentence.
['apple',
9. Create a dictionary where keys are the elements of a list
'banana', 'cherry', 'date']and values are
'Yes'ifthe word contains
'No'otherwise.
more than 5 letters,
. C
1 reate a set containing all even numbers between 1 and 20
"Python Programming"
2. Create a set of the unique characters in the string .
3. F [1, 2, 2, 3, 4, 5, 5, 6]
rom a list of numbers , createa set of unique
numbers.
4. Create a set that contains only the vowels from the string "The quick brown
ox jumps over the lazy dog"
f .
. Create a set of squares for all numbers from 1 to 10, but only include the
5
square if it’s an odd number.
27/12/2024
1. String methods without using string methods using functions and
return s statements.