Skip to content

Commit 2855029

Browse files
committed
sequence
1 parent 487a16b commit 2855029

7 files changed

+151
-0
lines changed

dictionaries.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#dictionary
2+
student = {'Name' : 'Laiba',
3+
'Age' : 19}
4+
5+
#adding key-value pair
6+
student['Gender'] = 'Female'
7+
8+
#deleting key-value pair
9+
#student.pop('Age')
10+
11+
#clearing dictionary
12+
#student.clear()
13+
14+
#updating values
15+
student['Name'] = 'Roma'

evenoddlambda.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from functools import reduce
2+
3+
#learning lambda (anonymous functions) , filter
4+
#even and odd filter
5+
6+
nums = [2, 5, 2, 1, 6, 7, 5, 20, 99, 102]
7+
even_nums = list(filter(lambda n : n % 2 == 0, nums))
8+
odd_nums = list(filter(lambda n : n % 2 != 0, nums))
9+
print('Even numbers :', even_nums)
10+
print('Odd numbers :', odd_nums)
11+
12+
#doubling odds and evens
13+
#using map
14+
#map replaces the values
15+
16+
even_nums_double = list(map(lambda n : n * 2, even_nums))
17+
odd_nums_double = list(map(lambda n : n * 2, odd_nums))
18+
print('Double even numbers :', even_nums_double)
19+
print('Double odd numbers :', odd_nums_double)
20+
21+
#import reduce from functools to use reduce
22+
23+
sum = reduce(lambda a, b : a + b, even_nums)
24+
product = reduce(lambda a, b : a * b, odd_nums)
25+
print('Sum of even numbers sequence :', sum)
26+
print('Product of odd numbers sequence :', product)

list.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
colors = ['blue', 'orange', 'green', 'grey', 'red']
2+
cars = ['bmw', 'mercedees']
3+
4+
#lists
5+
#slicing
6+
colors_choice = colors[1:4] #(upto 4 , 4 not included)
7+
8+
#append
9+
colors.append('yellow')
10+
11+
#insert (needs index and element)
12+
colors.insert(2, 'cyan')
13+
14+
#extend
15+
colors.extend(colors_choice)
16+
17+
#remove
18+
colors.remove('yellow')
19+
20+
#reverse
21+
colors.reverse()
22+
23+
#concatenation
24+
concatenate = colors + cars
25+
26+
#repetition
27+
repeat = cars * 3 #['bmw', 'mercedees', 'bmw', 'mercedees', 'bmw', 'mercedees']

sets.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#sets
2+
integers = {1, 2, 3, 4, 5, 6, 6, 7} #repetitive elements will be considered once for eg 6 here
3+
real = {7, 8, 9}
4+
#sequence of unique elements
5+
6+
#discard
7+
integers.discard(4)
8+
9+
#union
10+
print(integers|real)
11+
12+
#intersection
13+
print(integers & real)
14+
15+
#difference (uncommon elements)
16+
print(integers - real)
17+
print(real - integers)
18+
19+
#symmetric difference (union of uncommon diefference) (xor operator , caret symbol ^)
20+
print(integers ^ real)
21+
22+
#converting list into set
23+
list_integers = [1, 1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 9]
24+
set_integers = set(list_integers)
25+
print(set_integers)
26+
tuple_integers = tuple(list_integers)

strings.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
canada = 'Keep the red flag flying high'
2+
3+
#length
4+
size = len(canada)
5+
6+
#indexing
7+
index_of_f = canada.index('f')
8+
9+
#count
10+
count_of_f = canada.count('f')
11+
12+
#slicing
13+
slice_keep = canada[:4]
14+
15+
#upper case
16+
upper_canada = canada.upper()
17+
18+
#repetition
19+
repeat_canada = canada * 2
20+
21+
#concatenation
22+
concatenate_canada = 'Canada' + canada
23+
24+
#membership testing
25+
#if 'z' not in canada:
26+
# print('Its not an element')

tuple inside a list.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
sports = [('football', 'cricket', 'badminton', 'hockey'),
2+
('chess', 'ludo', 'table tennis')]
3+
4+
chess = sports[1][0]
5+
6+
points = [(1, 3), (2, 5), (3, 7)]
7+
points.append((4, 9))
8+
points.remove((1, 3))

tuples.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#tuples
2+
outdoor = ('football', 'cricket', 'badminton', 'hockey')
3+
indoor = ('chess', 'ludo', 'table tennis')
4+
5+
#indexing
6+
fav_outdoor = outdoor[0]
7+
fav_indoor = indoor[2]
8+
9+
#slicing
10+
difficult_outdoor = outdoor[2:4]
11+
difficult_indoor = indoor[1:3]
12+
13+
#concatenation
14+
sports = outdoor + indoor
15+
16+
#repetition
17+
outdoor_more = outdoor * 3
18+
19+
#count
20+
number = outdoor.count('football')
21+
22+
#length
23+
size = len(outdoor)

0 commit comments

Comments
 (0)