Skip to content

Commit 775ea6f

Browse files
committed
021: Classes and Objects
1 parent f9d25df commit 775ea6f

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

30days challange/013_List_comprehension/List_comprehension.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
print(even_numbers)
5252

5353
# Lambda function
54+
# syntax
55+
# lambda par1, par2, ...: expresion
5456
add_numbers = lambda first_number, second_number: first_number + second_number
5557

5658
print(add_numbers(10, 20)) # prints 30
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# In python, a function can return another function
3+
4+
def sum_and_difference(a, b):
5+
6+
sum = a + b
7+
difference = a - b
8+
9+
return sum, difference
10+
11+
s, d = sum_and_difference(90, 50)
12+
13+
print(s) # prints the sum of the numbers
14+
print(d) # prints the difference of the numbers
15+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
10+
</body>
11+
</html>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
# finding the squre of even numbers using list cpmrehension
3+
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
4+
5+
def square_of_even():
6+
for i in my_list:
7+
if i % 2 == 0:
8+
print(i**2)
9+
10+
square_of_even()
11+
12+
# alternatively
13+
14+
lst = [i**2 for i in my_list if i % 2 == 0]
15+
print(lst)
16+
17+
18+
19+
def total(w, x, y=10, z=20):
20+
return (w**x) + y + z
21+
22+
print(total(w=3, x=2))
23+
24+
25+
# Custom sorting
26+
27+
def main():
28+
list1 = ["Alex", "Mwangi", "Mungai", "Dorcas", "Kamau", "Mary"]
29+
30+
print(list1.sort(key=len))
31+
32+
main()
33+
34+
# lambda expressions
35+
36+
add_numbers = lambda first_number, second_number: first_number + second_number
37+
38+
print(add_numbers(10, 10))
39+
40+
41+
marks = [90, 79, 98, 87, 98, 99, 78]
42+
43+
marks.sort()
44+
45+
marks.reverse()
46+
47+
print(marks)
48+
49+
50+
scores = sorted(marks)
51+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
3+
# working with files
4+
5+
listvar = []
6+
7+
infile = open(r"C:\Users\alexm\Desktop\Python programms\Generic programming\Class work\functions.py", 'r')
8+
9+
for line in infile:
10+
listvar.append(line.rstrip())
11+
12+
print(listvar)
13+

0 commit comments

Comments
 (0)