Skip to content

Commit 391df21

Browse files
committed
Lab 5 - Bitwise operators
1 parent 0f53601 commit 391df21

File tree

15 files changed

+172
-0
lines changed

15 files changed

+172
-0
lines changed

L5-1.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# python lab 5 10-6-20
2+
3+
# l5_1 31&14 (and)
4+
5+
def print_padded_byte(b):
6+
print(bin(b)[2:].rjust(8, '0'))
7+
8+
9+
print_padded_byte(31)
10+
print_padded_byte(14)
11+
12+
print(31 & 14)

L5-10.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# python lab 5 10-6-20
2+
3+
# l5_10
4+
5+
the_list = ['Apples', 'Pears', 'Oranges', 'Mangoes', 'Tomatoes']
6+
7+
# print elements 2 and 3 of the list
8+
print(the_list[2:4])

L5-11.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# python lab 5 10-6-20
2+
3+
# l5_11
4+
5+
the_list = ['Apples', 'Pears', 'Oranges', 'Mangoes', 'Tomatoes']
6+
7+
# print substring [1:4] of element [3] of the list
8+
print(the_list[3][1:4])

L5-12.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# python lab 5 10-6-20
2+
3+
# l5_12
4+
5+
the_list = ['Apples', 'Pears', 'Oranges', 'Mangoes', 'Tomatoes']
6+
7+
# print substring [1:4] of element [3] of the list
8+
print(the_list[2][3:5])

L5-13.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# python lab 5 10-6-20
2+
3+
# l5_13 shallow copy of a list, the id() are the same indicating
4+
# that they are both pointers to the same list object
5+
6+
the_list = ['Apples', 'Pears', 'Oranges', 'Mangoes', 'Tomatoes']
7+
8+
9+
print('the_list: ', the_list)
10+
the_new_list = the_list
11+
12+
print('the_new_list: ', the_new_list)
13+
14+
print('the_list', id(the_list))
15+
print('the__new_list', id(the_new_list))

L5-14.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# python lab 5 10-6-20
2+
3+
# l5_13 deepcopy of a list, the id() are different indicating
4+
# that they do not point to the same list object
5+
6+
from copy import deepcopy
7+
8+
the_list = ['Apples', 'Pears', 'Oranges', 'Mangoes', 'Tomatoes']
9+
10+
11+
print('the_list: ', the_list)
12+
the_new_list = deepcopy(the_list)
13+
14+
print('the_new_list: ', the_new_list)
15+
16+
print('the_list', id(the_list))
17+
print('the__new_list', id(the_new_list))

L5-15.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# python lab 5 10-6-20
2+
3+
# l5_15
4+
5+
from random import randint
6+
7+
the_list = [[randint(1, 20) for each in range(4)]for item in range(3)]
8+
9+
print('the_list: ', the_list)
10+
11+
# tricksy because indexes start at *zero*
12+
print('Second element of the second list is', the_list[1][1])
13+
14+
# challenge 5_1
15+
16+
for i in range(3):
17+
for j in range(4):
18+
print(the_list[i][j], '\t', end='')
19+
print()

L5-2.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# python lab 5 10-6-20
2+
3+
# l5_2 60|14 (or)
4+
5+
def print_padded_byte(b):
6+
print(bin(b)[2:].rjust(8, '0'))
7+
8+
9+
print_padded_byte(60)
10+
print_padded_byte(14)
11+
12+
print(60 | 14)

L5-3.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# python lab 5 10-6-20
2+
3+
# l5_3 60 ^ 14 (exclusive or)
4+
5+
def print_padded_byte(b):
6+
print(bin(b)[2:].rjust(8, '0'))
7+
8+
9+
print_padded_byte(60)
10+
print_padded_byte(14)
11+
12+
print(60 ^ 14)

L5-4.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# python lab 5 10-6-20
2+
3+
# l5_4 125 << 2 (shift left two bits, same as *4)
4+
5+
def print_padded_byte(b):
6+
print(bin(b)[2:].rjust(8, '0'))
7+
8+
9+
print_padded_byte(125)
10+
11+
12+
print(125 << 2)

0 commit comments

Comments
 (0)