Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC1018F: Introduction to Python (Tutorial) James Gain Donald Cook

Similar presentations


Presentation on theme: "CSC1018F: Introduction to Python (Tutorial) James Gain Donald Cook"— Presentation transcript:

1 CSC1018F: Introduction to Python (Tutorial) James Gain jgain@cs.uct.ac.za Donald Cook dc@cs.uct.ac.za

2 Mini-Test (1) The Python statement x = "CSC" + 1018 + "F" will be rejected by the interpreter because of: a.Static typing b.Dynamic typing c.Strong typing d.Weak typing In Python a function without a return statement, will return: a.Nothing b.The value None c.The value False d.The value Null

3 Mini-Test (2) Is the following a valid Python program? a.yes b.no Which of these statements do NOT apply to the Dictionary datatype? a.Keys are case sensitive b.Values can be of any datatype c.Duplicate keys are permissible d.Keys can be of any datatype e.Dictionary elements have no ordering def test(n): if n > 10: return True return False

4 Mini-Test (3) The square bracket notation (“[ ]”) is used in Python to: a.Define a Dictionary b.Define a List c.Define a Tuple d.Index a Dictionary Key e.Index a List element f.Index a Tuple element Two lists can be combined with: a.The ‘+’ operator b.Extend c.Append d.Insert

5 Mini-Test (4) Given the following Python statements: The output will be: a.[1, 2, 3] b.[2, 2, 3] c.[1, 3, 3] d.undefined >>> a = [1, 2, 3] >>> b = a >>> a[1] = a[1] + 1 >>> b

6 Tut Q1: Lists You are given two lists (North and South) representing generals of the American Civil War North = [“Grant”, “Sheridan”, “McClellan”, “Hood”] South = [“Lee”, “Jackson”] Write out the contents of each list after the following operations: South.insert(len(South), North.pop()) South.append(South[2:]) South = South[:1] + South[2:] North = South + North

7 Tut Q2: Functions Design a python function Dictize that will take a tuple and return a dictionary with: Values correponding to the tuple elements and Keys that take the form “key1”, “key2”, etc. (where the number matches the tuple index) Create a doc string for your function and use the tuple (“Cleese”, “Gilliam”, “Chapman”, “Jones”, “Idle”, “Palin”) as a test within the module Dictize

8 Tut Q3: Dictionaries and Mapping You have a dictionary defined as: >>> translate = {"one":["un", "eins"], "two":["deux", "zwei"], "three":["trois", "drei"]} Write Python statements to: Add a new element "four":["quatre", "vier"] Through list comprehension print out the dictionary in the format "one is un (in French) or eins (in German) and two is …” Convert the dictionary to a list of sublists each of the form ["one", "un", "eins"]


Download ppt "CSC1018F: Introduction to Python (Tutorial) James Gain Donald Cook"

Similar presentations


Ads by Google