Download presentation
Presentation is loading. Please wait.
Published bySara Payne Modified over 8 years ago
1
Tutorial 2 Exam Revision
2
Notices Assignment + project Recap string formatting Revision
3
Quick recap: String formatting print (“this is the {} example”.format(‘first’)) print (“this is the {0} and {1} example”.format(‘first’, ‘second’)) print (“this is the {one} and {two} example”.format(one=‘first’, two=‘second’)) print (“this is a %s example” % ‘new’) print (“this is a %s with %s example” % (‘new’, ‘brackets’) print (“this is a %s with %d %s example” % (‘new’, 2, ‘brackets’)
4
Python A great language! Easy and powerful scripting -- try to follow PEP8ish style Dynamically typed Procedural and object oriented The exam will test your knowledge on: Python fundamentals Reading and writing python code -- some parts will be easy, some parts hard. Exam etiquette: Answer everything. I don’t care if you don’t think you know it, just put something down. There are 0 marks for blank answers.
5
Revision: The basics Know basic types: Strings/bools/ints etc. Restrictions on valid variable and function names Key words What they do An example of using them in, pass, with, is, as, elif, __main__
6
Revision: Structures The difference between: Lists, Sets, Dicts,Tuples When they are used + their tradeoffs What is: ‘==’ vs ‘is’ Examples of using them Make sure you can identify which is which, e.g.: (‘hi’) (‘hi’,){‘hi’}[‘hi’] {‘hi’:1}
7
Revision: Structures Make sure you can understand code relating to structures: Such as functions: len(), sort(), append(), update(), sort(reverse=True), slicing them with “:” E.g: mylist = [1,2,’hi’,’bob’] print (mylist[1]) print (mylist[1:]) print (mylist[-1]) mydict = {‘hi’:’hello’,’name’:’bob’,’age’:29} print (mydict[‘age’]) print (mydict.values())print (mydict.keys()) mydict.update({‘a’:’b’})print (mydict)
8
Revision: Structures Those functions are important: mylist = [1,2,4,3] print (len(mylist)) x = mylist.sort() mylist.append(100) print (x)print (mylist) mylist += [1,2,3] print (mylist)
9
Revision: Iterating While (condition == True): For i in range(5): # 0,1,2,3,4 For i in range(len(mylist)) For i in range(0,10,2) # 0,2,4,6,8 For i in range(10,0,-2) For i, val in enumerate(mylist): For k, v in mydict.items()
10
Revision: Working with strings You need to know basic string functions: join(), split(), + concatenates, \n: str1 = “this is a string”; str2 = “so,is,this” print (str1.split()[0])print (len(str2.split(‘,’))) print (str1 + ” “ + str2) str3 = str1.split().sort() print (“\n”.join(str3))
11
Revision: Functions and modules Modules -- Remember the important ones: os, sys, etc. Import with ‘import’. Dot notation to use variables/functions of theirs. Functions: def myfunc(): print(‘do stuff’) return ‘hello’ # or any number of values as a tuple # and call it res = myfunc()
12
Working with OS module Getting parameters from the command line using sys.argv: $ ‘python3 myfile.py file.txt 3’ print (len(sys.argv))print(sys.argv[1]) print(sys.argv[2]) Relative vs absolute paths os.path.exists(‘myfile’) os.getcwd()
13
Revision: Working with files and the web Files and the web are pretty similar to one another: You open it, read it, then close it. Or use the ‘with’ keyword. with open(fname) as f: # could do: content = f.read() or readlines() for line in f: print (line)
14
Revision: Exceptions try: fin = open('bad_file') for line in fin: print (line) fin.close() except Exception as e: print ("Something went wrong.") print (e)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.