Download presentation
Presentation is loading. Please wait.
1
Introduction To Python
Lecture 17 Introduction To Python Note: these lectures are written for students in an upper-division CS class. This is not a good starting point for novice programmers.
2
Python Basics Open-source, first released 1991
Developed by Guido van Rossum, who now works for Dropbox. Much of the development was done while van Rossum was employed by Google. Van Rossum is still the main developer. High level language intended to be easy to use Hello World in Python: print ("Hello World") Supports plain imperative programming as well as botyh OOP and functional programming Named after comedy group Monty Python; usage examples often contain Monty Python references. I have added links to several representative samples of Monty Python's work to the web page.
3
Scripting Languages “Scripting language” is a loosely defined concept; the term has been used to describe languages used to schedule batch jobs, to test applications, or generally to write small-sized applications. However, as we will see, Python is scalable enough to use for full featured web apps. In contemporary usage, the term usually refers to interpreted languages with dynamic typing. These are higher-level languages intended to be easy to use, so they typically have automatic memory management and garbage collection.
4
Scripting Language Python is not a purely interpreted language, however. It is translated to machine language in a two- step process somewhat like the one used by Java or the Microsoft .net languages. Step 1: Python source (.py files) is compiled to bytecode. If the compiler has write access to the directory where the code is saved, the bytecode will be stored with the .pyc file name extension. If you run the program again, and the appropriate .pyc files have later timestamps than the .py files, the compiler does not recompile. CPython, the standard or "reference implementation" of Python, compiles to a bytecode format that runs on a Python virtual machine. The Jython implementation compiles to Java bytecode, which runs in a JVM for interoperability with Java. IronPython compiles to .net bytecode. Step 2: Bytecode is run on a virtual machine. The primitive operations map to functions implemented in the VM, which is usually written in C. Follow these links to read some very interesting but not-necessary-to-this-course information on the primitive operations:
5
Scripting Language Bytecode consists of bytes that map to primitive operations and operands. This example examines Cpython bytecode using the utility dis, which shows an Assembly-like format. >>> def foo(a): x = 3 return x + a ... >>> import dis >>> dis.dis(foo.func_code) LOAD_CONST (3) 3 STORE_FAST (x) LOAD_FAST (x) 9 LOAD_FAST (a) 12 BINARY_ADD 13 RETURN_VALUE
6
Uses of Python Python is often used to customize applications written in other languages. Off-the-shelf software packages sometimes provide Python APIs that customers can use to customize or extend to applications. Python is suitable for this because a) it is comparatively easy to learn and can readily be used for small functionality and b) there is no need to recompile anything. Django web framework Widely used for prototyping. Develop ideas with an easy to use language; reimplement in a more efficient language after the main design decisions are made. Heavily used in machine learning. Increasingly used in place of shell scripts for automation of OS tasks, and in place of Perl for text processing.
7
Use Python 3 There are two incompatible versions of Python in common use: Python 2.x and Python 3.x In preparing these lectures, I ran into many code examples in various sources that do not work in Python 3. Command to run Python 3 in Fedora is python3, not just python, which is Python 2. BE CAREFUL ABOUT THIS! May have to run sudo dnf install python3 Definitely need to run sudo dnf install python3-tools This will give you a shell; use it to try out statements Idle3 will also give you a shell, but with menus for such items as creating a new code file Python developers sometimes use the word Python to mean a release or implementation of Python, as in "this code does not work in older Pythons," or "this syntax is correct in the Python I am using but not in the one you are using"
8
Python 3 Installing Python 3 on my Windows tablet from download at was straightforward. I have not tried it on a Mac. You may want to try out QPython3 for Android, too. If you do, be sure to go into app settings to deny the very intrusive access permissions it requests. As with the C++ part of the course, test your work in Fedora on Digital Ocean before you turn it in This unit will culminate in building a web site with Django on Digital Ocean, so you have to get comfortable with using Python in Fedora.
9
Idle Idle is a lightweight IDE used with Python, and is part of Python installations Named for one of the members of Monty Python Desktop GUI, won't work on Digital Ocean Make sure to run idle for Python 3. For example, in desktop Linux, you need to run idle3; if you just run idle, you will get idle for Python 2
10
Pip Pip is a package manager used to install Python software, including add-ons for Python environments May need to sudo dnf install python-pip.noarch Google instructions to install for Windows or OSX Packages are located in the Python Package Index (PyPI), also called "The Cheese Shop" after a Monty Python sketch Try it out by installing the numPy math library: sudo pip install numpy Pip is the best way to install python packages in Windows and probably in OSX. In Fedora, I have found (sudo) dnf install much more useful.
11
Python Syntax Unlike C-family languages, Python uses whitespace as part of its syntax Incorrect indentation or line breaks are syntax errors and prevent correct interpretation of the code Newline ends a statement; no need for semicolons Blocks are delimited with indentation (four spaces, not a tab) or colons, not curly braces Comments begin with #. Docstrings are long comments intended to provide explanations of classes, functions, etc. They should be at the beginning of the block they explain and are delimited by three double quotes at each end. Single quotes and double quotes are interchangeable. Just be consistent. No need to declare data types for variables or return types for functions (more on this later) Python has automatic memory management and garbage collection
12
Python Syntax References
The definitive documentation for Python syntax is at If you think there is probably a way to do something (say, trim leading whitespace from a string), look there for reference info Stackoverflow.com usually has the best examples, but for Python they are very often outdated. Specifically, many examples you will find there are in Python 2, but Python 3 is not always backward-compatible. Python 2 did not require parentheses in print statements, but Python 3 does. This will often serve as a reminder that the example you are viewing was written for Python 2.
13
Naming Conventions joined_lower for functions, methods, attributes
ALL_CAPS for constants CamelCaseWithInitialCaps for classes
14
Python Shell Python platforms provide a shell that is analogous to an operating system’s command shell. Think of UNIX-style bash (Linux command shell or Macintosh Terminal) or Windows cmd commands as programming languages and you will understand the analogy. You can get the shell in Linux by typing python3. You can also get to it from within Idle if you are working with a GUI. Type a value (like 42 or ‘John’) into the shell and it will output the value immediately Type the assignment x = 42, and then type x. The shell will output the value of x. The shell will remember the value, so if you then type x += 1 x The shell will output 43. Try this: s = 'Spam' a = ' and ' e = ' eggs' s + a + e
15
Python Datatyping Python is dynamically typed: it determines data types automatically, without type declaration. However, it is also strong typed: it enforces the data types after it figures them out. So, for example, you can’t just append an integer to a string. You must first convert the integer to a string itself. x = 'the answer is ' # Decides x is string. y = # Decides y is integer. print x + y TypeError: Can't convert 'int' object to str implicitly Instead, need to do this: print(x + str(y)) From a slide by Dr. Mary-Angela Papalaskari and Dr. Paula Matuszek, Villanova Univ.
16
Python Shell Python platforms use a shell that is analogous to an operating system’s command shell. Think of UNIX-style bash (Linux command shell or Macintosh Terminal) or Windows cmd commands as programming languages and you will understand the analogy. You can get the shell in Linux by typing python3. You can also get to it from within Idle if you are working with a GUI. Type a value (like 42 or ‘John’) into the shell and it will output the value immediately Type the assignment x = 42, and then type x. The shell will output the value of x. The shell will remember the value, so if you then type x += 1 x The shell will output 43. Try this: s = 'Spam' a = ' and ' e = ' eggs' s + a + e
17
Define a function in the shell
Type this in the shell (no need to save a file yet.) At the end of the two-line function definition, but before the function call, hit enter to get back to the command line def mul (x, y): return x * y mul(5, 3)
18
More on Data Types In Python, every value is an object, but the definition of "object" is looser than in, say, Java. An object possesses the following three characteristics: 1) an identity, which is a base 10 number that is a unique identifier and can be retrieved by the function id(); 2) a type, which can be retrieved by the function type(); and 3) a value. >>> x = 'the cheesemakers' >>> id(x) >>> type(x) <class 'str'> >>> x 'the cheesemakers' Wesley Chun, Core Python
19
More on Data Types In Python, data types are bound to objects, but not to variables. To put it another way, objects have data types (else how could we save them in memory?), but variables do not have data types. We can, however, can variables to get the types of the values to which they refer. >>> x = "John" >>> x 'John' >>> type(x) <class 'str'> >>> x = 42 <class 'int'> >>> x = <class 'float'> >>> x = [1, 2, 3] # this is a list, not an array <class 'list'>
20
More on Data Types Get help in more depth: help(str)
Show what you can do with a particular object based on its type: >>> a = 'John' >>> dir(a) # or dir(str) ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] Get help in more depth: help(str) This produces a lot of output, which is broken into pages, UNIX-style. Hit the space bar to move forward one page or q to quit
21
More on Data Types In Python, function and method lookup is done at runtime. There is no type checking for function arguments until runtime. >>> def add(x, y): return x + y A call to this function will work if the + operator is defined for the data types actually received when the function is run. This is called duck typing, a reference to the proverb "if it looks like a duck, quacks like a duck, and swims like a duck, it's a duck." The data type of the return value is the type of the result of the + operation. >>> add(3, 4) 7 >>> add( , 1) >>> add ('Spam and ', 'eggs') 'Spam and eggs' >>> add("Dead parrot", 3) # runtime error, not syntax error
22
Python Lists Python uses lists heavily. CPython implements lists internally using arrays of pointers, so access by index is O(1) NumPy contains an array class that typically uses contiguous memory, like a C or Java array. It is mostly used for very computation-intensive code. A list can be initialized like this: >>> a = [1,2,3] >>> a = ['blessed', 'are', 'the', 'cheesemakers'] Items in a list may have different data types (!): >>> a = ['this', 'is', 1, 'dead', 'parrot'] Lists may be nested, and scalar items can be mixed with inner lists: >>> a = a = [['this is a dead parrot', 'no, he\'s resting'], ['blessed', 'are', 'the', 'cheesemakers'], 'Ni!']
23
Python Lists Iterating through a list:
>>> b = ['this', 'is', 'a', 'dead', 'parrot'] >>> for item in b: # enhanced for loop; "item" is just a variable name print(item) ... this is a dead parrot >>> a = 1 >>> b = >>> c = 1/3 >>> d = 0 >>> l = [a, b, c] >>> for item in l: d += item >>> d
24
Python Lists For a nested list, the items are the inner lists:
>>>a = [['this is a dead parrot', 'no, he\'s resting'], ['blessed', 'are', 'the', 'cheesemakers'], 'Ni!'] >>> for item in a: print(item) ... ['this is a dead parrot', "no, he's resting"] ['blessed', 'are', 'the', 'cheesemakers'] Ni!
25
More List Operations >>> l = ['What is your name?', 'What is your quest?'] >>> l.append('What is your favorite color?') >>> l.insert(0, 'Does this helmet make me look fat?') >>> l ['Does this helmet make me look fat?', 'What is your name?', 'What is your quest?', 'What is your favorite color?'] >>> del l[0] ['What is your name?', 'What is your quest?', 'What is your favorite color?'] >>> l.pop() 'What is your favorite color?' ['What is your name?', 'What is your quest?'] >>> len(l) 2 >>> l1 = ['a', 'b', 'c'] >>> l2 = [1, 2, 3] >>> l1 += l2 >>> l1 ['a', 'b', 'c', 1, 2, 3]
26
More List Operations >>> l = ['I\'m Spartacus!'] * 10
["I'm Spartacus!", "I'm Spartacus!", "I'm Spartacus!", "I'm Spartacus!", "I'm Spartacus!", "I'm Spartacus!", "I'm Spartacus!", "I'm Spartacus!", "I'm Spartacus!", "I'm Spartacus!"] >>> l = [[1,2,3],[4,5,6]] [[1, 2, 3], [4, 5, 6]]
27
Range range() can create lists of data automatically.
>>> my_list = range(5) >>> for item in my_list: item ... 1 2 3 4 Can set both lower and upper bounds: >>> my_list = range(5,10) print(item) 5 6 7 8 9 Can step by integer increments other than 1: >>> my_list = range(5,17,2) 11 13 15
28
List Slicing List Slicing >>> for item in my_list[2:]:
print(item) ... 2 3 4 >>> for item in my_list[1:3]: 1 >>> for item in my_list[:3]:
29
List Slicing List Slicing doesn't only work with range():
>>> my_list = ['blessed', 'are', 'the', 'cheesemakers'] >>> for item in my_list[1:3]: print(item) ... are the >>> my_list[1:] ['are', 'the', 'cheesemakers'] >>> my_list[:2] ['blessed', 'are']
30
Tuples A tuple is an immutable sequence, similar to an immutable list
(1, 2) >>> type(t1) <class 'tuple'> >>> tt = ((1,2),(3,4),(5,6,7)) >>> def print_tuples(nt): for t in nt : print(t) ... >>> print_tuples(tt) (3, 4) (5, 6, 7)
31
Pass by Object Reference
All variables are references (remember, variables don't have types at all): >>> l1 ['a', 'b', 'c', 1, 2, 3] >>> l2 = l1 >>> l2 >>> l2[0] = 42 [42, 'b', 'c', 1, 2, 3] >>> def change(vals): vals[0] = 500 ... >>> change(l1) [500, 'b', 'c', 1, 2, 3]
32
Pass by Object Reference
However, note that a parameter variable in a called function is in scope only in the called function, so this function has different effects than the append() function above: >>> def reassign(list): list = [0,1] ... >>> list = [0] >>> reassign(list) >>> print(list) [0]
33
Python Modules A Python code file is called a script or module. The file name extension should be .py Code that is not part of a function definition will run (functions will run, too, if you call them) However, it is conventional to write a main() for an application. Call it like this: if __name__ == "__main__": sys.exit(main()) This means "if this module has been run at the top level, run main() now, then exit." If the module has been imported to some other module, we probably don't want main() to run.
34
Command Line Input input(prompt)
returns a string, of course, so need to cast it if you need some other type
35
Future Value Calculator
import sys def main(): principal = float(input('Enter the principal amount: ')) rate = float(input('Enter the interest rate in percent per year: '))/100 time = float(input('Enter the number of years you will leave the money on deposit: ')) print(fv(principal, rate, time)) def fv(principal, rate, time): curr = principal * ((1 + rate) ** time) return curr if __name__ == "__main__": sys.exit(main())
36
Strings Strings are immutable, as in Java, but the += operator for strings is optimized Python 3 strings use Unicode-8
37
string.split() sentence = 'This is a dead parrot'
>>> words = sentence.split() >>> for word in words: print(word) This is a dead parrot
38
string.format() >>> 'aaa {1} bbb {0} ccc {1} ddd'.format('xx', 'yy', ) 'aaa yy bbb xx ccc yy ddd' >>> x = >>> '{:.2f}'.format(x) '3.14' >>> '{:.3f}'.format(x) '3.142' >>> '{0}, {1}, {2}'.format('a', 'b', 'c') 'a, b, c' >>>'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude=' W') 'Coordinates: 37.24N, W' >>> coord = ['37.24N', ' W'] >>> 'Coordinates: {}, {}'.format(*coord) #can be also 'Coordinates: {0}, {1}'.format(*coord)
39
Try...Except input validation is typically done with exception handling: import sys def main(): principal = getFloat('principal amount', 0, ) rate = getFloat('annual interest rate', 0, 100)/100 time = getFloat('number of years', 0, 100) print('{:.2f}'.format(fv(principal, rate, time))) def fv(principal, rate, time): curr = principal * ((1 + rate) ** time) return curr def getFloat(desc, min, max): ret = None while ret == None: try: curr = float(input('Enter the ' + desc +': ')) if (curr >= min) & (curr <= max): ret = curr else: raise ValueError() except ValueError: print('Invalid input. Please try again.') return ret if __name__ == "__main__": sys.exit(main())
40
Recursive Maze Solver import sys def main(): maze = setup()
start_found = False finish_found = False for row_num in range(len(maze)): for col_num in range (len(maze[row_num])): if maze[row_num][col_num] == 's': start_found = True; finish_found = search(maze, row_num, col_num) if not start_found: print("No start!") if not finish_found: print("No exit!") def setup(): # backslash continues a statement on the next line maze = [['#', '#', '#', '#', '#', '#', '#'], \ ['#', ' ', ' ', ' ', ' ', ' ', '#'], \ ['#', ' ', '#', ' ', ' ', ' ', '#'], \ ['#', ' ', '#', '#', '#', ' ', '#'], \ ['#', ' ', '#', ' ', '#', ' ', '#'], \ ['#', 's', '#', ' ', ' ', ' ', '#'], \ ['#', '#', '#', 'f', '#', '#', '#']] return maze def search(maze, row_num, col_num): for row in maze: print(row) print() if maze[row_num][col_num] != 'f': maze[row_num][col_num] = 'X' neighbors = [(row_num-1, col_num), (row_num+1, col_num), (row_num, col_num-1), (row_num, col_num+1)] while neighbors: space = neighbors[0] neighbors = neighbors[1:] if (maze[space[0]][space[1]] != '#') & (maze[space[0]][space[1]] != 'b') & (maze[space[0]][space[1]] != 'X'): # why didn't if (maze[space[0]][space[1]] == ' '): work? if search (maze, space[0], space[1]): return True maze[row_num][col_num] = 'b' # note: we will not necessarily backtrack over *every* space that is not on the solution path else: print('found exit at ' + str(row_num) + ',' + str(col_num) +'!') return True; if __name__ == "__main__": sys.exit(main())
41
Default Arguments >>> def my_function(inp = 42):
print(inp) ... >>> my_function(100) 100 >>> my_function() 42
42
Dictionaries A dictionary is a collection of name-value pairs, equivalent to a Java or C++ Map. The order of elements in a dictionary is undefined. But, we can iterate over (1) the keys, (2) the values, and (3) the items (key-value pairs) in a dictionary. We can set the value of a key and we can get the value associated with a key. Keys must be immutable objects, such as ints, strings, or tuples.
43
Dictionaries Create a dictionary with a literal:
>>> answers = {'name': 'Lancelot', 'quest': 'I seek the Holy Grail', 'color': 'mauve'} List keys: >>> answers.keys() dict_keys(['name', 'quest', 'color']) Look up values by key: >>> answers['name'] 'Lancelot' >>> answers['quest'] 'I seek the Holy Grail' >>> answers['color'] 'mauve' Iterate through entries: >>> for key,value in answers.items(): print (key +': ' + value) name: Lancelot quest: I seek the Holy Grail color: mauve Add an entry: >>> answers['age'] = 42
44
Dictionaries http://www.davekuhlman.org/python_101.html#dictionaries
More ways to create dictionaries: dict({'one': 2, 'two': 3}) dict(zip(('one', 'two'), (2, 3))) dict([['two', 3], ['one', 2]]) dict(one=2, two=3) Check whether a key is present in a dictionary: d = {'tomato': 101, 'cucumber': 102} >>> k = 'tomato' >>> k in d True Get value or a sentinel: >>> d.get('tomato', -1) 101 >>> d.get('chard', -1) -1 >>>if d.get('chard') is None: print('sorry, no chard!') sorry, no chard!
45
Sets Sets are implemented using hash maps and are usually O(1) for both insertion and lookup >>> food = set() >>> food.add('Cheddar'); >>> food.add('Mozzarella'); >>> food.add('Stilton') >>> food {'Cheddar', 'Stilton', 'Mozzarella'} >>> 'Stilton' in food True >>> 'Gorgonzola' in food False >>> len(food) 3 >>> other_food = set() >>> other_food.add('jalapeno') >>> other_food.add('habanero') >>> other_food {'jalapeno', 'habanero'} >>> all_food = food.union(other_food) >>> food.issubset(all_food) >>> food.issuperset(all_food) >>> all_food.issuperset(other_food) >>> third_food = {'Stilton', 'Roquefort'} >>> food.intersection(third_food) {'Stilton'}
46
Python Philosophy Like Java, but more so, Python has a well-developed set of conventions rooted in a set of principles. Code that is good idiomatic Python is said to be pythonic. If you write Python that looks as if it was thought out in Java and then translated, Python programmers will view it as unpythonic. Python puts a high emphasis on code readability Python discourages “clever” approaches that are tightly coupled to details of particular problems in favor of conventions about how to approach common types of problems To read the Zen of Python, a list of high-level principles, type import this in a Python shell
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.