CS156
Chris Pollett
Sep 7, 2022
>>>2 / 3 + 7.9outputs 8.566666666666666 (notice converts to a float).
#!/usr/bin/env python
# The above would mean don't have to type python when run under *nix
# Comments begin with #
print("Hello World")
If we didn't have the first line or on Windows we would need to type "python3 hello.py" to run the program.
x = 7 x += 1 #note there are no ++ and -- operators x *= 3 y = 5 z = x - y print(z)which would print 19
print("%3d %0.2f" % (10, .9799))
prints 10 with a leading space followed by 0.98
a = 5
if a > 4:
print("a is bigger than 4")
b = 99
if b < 50:
print("b is too little")
else:
print("b is big enough")
if a > 4 and b < 50:
print("1")
elif not a == 6:
print("2")
else:
print("3")
f = open("hello.py")
line = f.readline()
while line:
print (line, end='') #print without newline
#in Python 2, would write print line,
line = f.readline()
f.close()
for line in open("hello.py"):
print(line, end='')
to achieve the same affect
f = open("somefile.txt", "w")
print("%0.2f" % 0.7999, file=f) #we are redirecting output to file f here
#In Python 2, for above, could have done print >>f, "%02f" % 0.7999
f.write("hello") #normal way of doing a write
f.close()
import sys
print("\n".join(sys.argv)) # print the command line arguments, each on their own line
sys.stdout.write("Type something\n");
name = sys.stdin.readline()
print("You wrote:" +name)
import glob path = './*' files = glob.glob(path) for name in files: print(name)
a = "Hello" b = 'Good "bye"' c = """ Triple quotes one can go over multiple lines """ d = ''' this one works as well '''
a = "Hello World" b = a[4] # b is 'o' c = a[:5] # c is 'Hello' d = a[6:] # d is 'World' e = a[3:8] # e is "lo Wo"
my_list = ['YO', 1.2, 7, "HI", ["scary nested list", "watch out!"]] my_empty_list = [] # or list()
b = my_list[0] # b is now: 'YO' c = my_list[4][1] # c is now: 'watch out!' d = my_list[1:3] # d is [1.2, 7] e = my_list[2:] # e is [7, 'HI', ['scary nested list', 'watch out!']]
my_list.append("an end") #my_list now
#['YO', 1.2, 7, 'HI', ['scary nested list', 'watch out!'], 'an end']
my_list.insert(2, 3) # my_list now
#['YO', 1.2, 3, 7, 'HI', ['scary nested list', 'watch out!'], 'an end']
a = [1, 2, 3] + [4, 5] #a is [1, 2, 3, 4, 5]
import sys
if(len(sys.argv)) != 2:
#notice sys.argv is a list of command-line args and we found its length
print("Please supply a filename")
raise(SystemExit(1)) # throw an error and exit
f = open(sys.argv[1]) #program name is argv[0]
lines = f.readlines() # reads all lines into list one go
f.close()
#convert inputs to list of ints
ivalues = [int(line) for line in lines]
# print min and max
print("The min is ", min(ivalues))
print("The max is", max(ivalues))
a = ( 1, "hello", 3)
b = ( some, where)
c = "6 scared of 7", "as 7 8 9" #notice can omit paren's
d = () # 0-tuple
e = 'yo', #one tuple
f = ('yo',) #same one tuple
g = (d,) # g is ((),)
c = (4, 5) a, b = c
my_set = set([3, 9, 2, 6])
another_set = set("goodness") # set of unique chars
print (another_set) # set(['e', 'd', 'g', 'o', 'n', 's'])
if 'e' in another_set:
print("it's in there")
a = my_set; b = another_set;
c = a | b # union of sets
c = a & b # intersection of sets
c = a - b # difference of sets
c = a ^ b # symmetric difference of sets
another_set.add('y') # adds a single element to set
another_set.update([6,7,8]) # add multiple elements
my_set.remove(3) # removes the number 3 from my_set