for
Loops with Setsmin
And max
with SetsI have posted homework 5 here.
It is due this coming Sunday at 11:59 PM.
Let's look at the answers to Quiz 3
len
function gives the size of a set
>>> set_1 = {1, 2, 3} >>> len(set_1) 3 >>> set_2 = {3, 2, 1} >>> len(set_2) 3 >>> set_3 = {'one', 'two', 'three', 'four'} >>> len(set_3) 4
>>> set_1 = {1, 2, 3} >>> set_2 = {3, 2, 1} >>> set_1 == set_2 True
>>> tuple_set = {(1,2), (3,4), (5,6)} >>> tuple_set {(5, 6), (1, 2), (3, 4)}
>>> list_set = {[1,2], [3,4], [5,6]}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
for
Loops with Setsfor
loopfor
loop looks like this
for LOOP_VARIABLE in ITERABLE_OBJECT:
STATEMENT
...
for
loop you will get each element in the
set
>>> set_1 = {1, 2, 3, 4, 5} >>> for number in set_1: ... print(number) ... 1 2 3 4 5 >>> set_2 = {'one', 'two', 'three', 'four', 'five'} >>> for number in set_2: ... print(number) ... five two three one four
in
operator
>>> set_1 {1, 2, 3, 4, 5} >>> 7 in set_1 False >>> 8 in set_1 False >>> 3 in set_1 True
not in
operator
>>> 8 not in set_1 True >>> 3 not in set_1 False
>>> A = {1, 4, 8, 12} >>> B = {1, 2, 6, 8} >>> A.union(B) {1, 2, 4, 6, 8, 12}
A ∪ B
B ∪ A
>>> A | B {1, 2, 4, 6, 8, 12}
>>> A {8, 1, 12, 4s} >>> B {8, 1, 2, 6} >>> A.intersection(B) {8, 1}
A ∩ B = B ∩ A
>>> B.intersection(A) {8, 1}
>>> A & B {8, 1}
A
that are
not in B
A - B
>>> A {8, 1, 12, 4} >>> B {8, 1, 2, 6} A.difference(B) {12, 4}
A - B ≠ B - A
>>> B.difference(A) {2, 6}
>>> A - B {12, 4}
A Δ B
>>> A {8, 1, 12, 4} >>> B {8, 1, 2, 6} >>> A.symmetric_difference(B) {2, 4, 6, 12}
A Δ B = B Δ A
A ^ B {2, 4, 6, 12}
>>> A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} >>> B = {1, 3, 5, 7, 9}
>>> A.issubset(B) False >>> B.issubset(A) True
>>> A <= B False >>> B <= A True
>>> A.issuperset(B) True >>> B.issuperset(A) False
>>> A >= B True >>> B >= A False
>>> odds = {1, 3, 5, 7, 9} >>> evens = {2, 4, 6, 8, 10} >>> odds.isdisjoint(evens) True
>>> evens.isdisjoint(odds) True
>>> D = {1, 2, 3, 4, 5} >>> D {1, 2, 3, 4, 5} >>> D.clear() >>> D set()
min
And max
with Setsmax
built-in function
>>> B = {1, 3, 5, 7, 9} >>> max(B) 9
min
function
>>> min(B) 1
>>> import os
>>> os.getcwd() '/home/ghoffmn'
>>> course_dir = os.listdir('/courses/it117/s14/ghoffmn')
>>> for entry in course_dir :
... print(entry)
...
GROUP
MAIL
cmanuel1
jpinto
fortinsy
ebeazer
...
>>>os.listdir() ['News', 'mail', 'it114', '.ssh', '.bash_history', '.bashrc', ...
>>> os.listdir('/home/ghoffmn/assignments_submitted') ['homework_submitted', 'code_entry_submitted']
>>> os.listdir('assignments_submitted') ['homework_submitted', 'code_entry_submitted']
>>> os.chdir('/home/ghoffman') >>> os.getcwd() '/home/ghoffman'
>>> os.chdir('..') >>> os.getcwd() '/home'
>>> os.chdir('/home/ghoffmn/tmp')
>>> os.listdir('.')
['test.txt', 'dir1']
>>> os.rename('test.txt', 'file.txt')
>>> os.listdir('.')
['dir1', 'file.txt']
>>> os.rename('dir1', 'test_dir')
>>> os.listdir('.')
['test_dir', 'file.txt']
>>> os.remove('file.txt') >>> os.listdir('.') ['test_dir']
>>> os.remove('test_dir')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 21] Is a directory: 'test_dir'
>>> os.rmdir('test_dir') >>> os.listdir('.') []
>>> os.mkdir('dir1') >>> os.listdir('.') ['dir1']
>>> result = os.system('touch foo.txt') >>> result 0 >>> os.listdir('.') ['dir1', 'foo.txt']
>>> os.environ['HOME'] '/home/ghoffmn' >>> os.environ['SHELL'] '/bin/bash' >>> os.environ['PATH'] '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games'
>>> os.path.isfile('foo.txt') True >>> os.path.isfile('dir1') False
>>> os.path.isdir('dir1') True >>> os.path.isdir('foo.txt') False
>>> os.getcwd() '/home/ghoffmn/tmp' >>> os.path.basename(os.getcwd()) 'tmp'
>>> import sys
$ cat print_args.py #! /usr/bin/python3 import sys print("The command line arguments:") for index in range(len(sys.argv)) : print("Argument ", index, ":", sys.argv[index]) $ ./print_args.py foo bar bletch The command line arguments: Argument 0 : ./print_args.py Argument 1 : foo Argument 2 : bar Argument 3 : bletch
$ cat print_file.py
#! /usr/bin/python3
import sys
file_name = input("Please enter the name of a file: ")
try :
file = open(file_name, "r")
except :
print("Could not open file", file_name)
sys.exit()
for line in file :
print(line.strip())
$ ./print_file.py
Please enter the name of a file: xxxxxxxxxxxxxxx
Could not open file xxxxxxxxxxxxxxx
input
Usage: SCRIPT_NAME ARGUMENT_1 ARGUMENT_2 ...
$ ./list_dir.py
Usage: list_dir.py DIRECTORY_NAME
if len(sys.argv) < 2: print("Usage:", os.path.basename(sys.argv[0]), "DIRECTORY_NAME") sys.exit()
sys.argv[0]
$ ./list_dir.py
Usage: ./list_dir.py DIRECTORY_NAME
>>> import platform >>> print(platform.node()) Neptune.local
>>> import platform >>> print(platform.node()) pe15
>>> print(platform.platform()) macOS-10.15.7-x86_64-i386-64bit
>>> print(platform.platform()) Linux-5.4.0-65-generic-x86_64-with-glibc2.29
>>> print(platform.system()) Darwin
print(platform.system()) Linux