CSC1018F: Intermediate Python

Slides:



Advertisements
Similar presentations
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Introduction to Python
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
The switch Statement, DecimalFormat, and Introduction to Looping
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Python Control of Flow.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Python quick start guide
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Programming for Linguists An Introduction to Python 24/11/2011.
CSC1018F: Object Orientation, Exceptions and File Handling Diving into Python Ch. 5&6 Think Like a Comp. Scientist Ch
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Python Control Flow statements There are three control flow statements in Python - if, for and while.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
CSC1018F: Introduction to Python (Tutorial) James Gain Donald Cook
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Python Functions.
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
Python Conditionals chapter 5
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
Python Review 1.
Topics Introduction to Functions Defining and Calling a Void Function
CS1022 Computer Programming & Principles
Introduction to Python
The switch Statement, and Introduction to Looping
Python: Control Structures
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
PH2150 Scientific Computing Skills
CHAPTER FOUR Functions.
Arithmetic operations, decisions and looping
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Primer 2: Functions and Control Flow
And now for something completely different . . .
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
CS190/295 Programming in Python for Life Sciences: Lecture 6
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
CISC124 Labs start this week in JEFF 155. Fall 2018
Types, Truth, and Expressions (Part 2)
(more) Python.
CMPE212 – Reminders The other four assignments are now posted.
CISC101 Reminders All assignments are now posted.
CSC1018F: Intermediate Python (Tutorial)
COMPUTER PROGRAMMING SKILLS
CISC101 Reminders Assignment 3 due today.
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
Functions John R. Woodward.
Types, Truth, and Expressions (Part 2)
Presentation transcript:

CSC1018F: Intermediate Python Diving into Python Ch. 4 Think Like a Comp. Scientist Ch. 1-6

Lecture Outline Recap of Intro to Python [week 1] Basics: Conditionals Iteration Introspection: Optional and Named Arguments Built-in Functions Filtering Lists and & or Lambda Functions

Recap of Python Intro Defining and documenting functions Block structure by indenting Native Datatypes: Dictionary List Tuple String Formatting List Comprehension (Mapping)

Recap Exercise: Functions Design a python function Dictize that will take a tuple and return a dictionary with: Values corresponding to the tuple elements and Keys that take the form “key0”, “key1”, 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

Bits and Pieces Strings: Input: Pseudo-mathematical operators + (concatenation) and * (repetition) are valid E.g., >>> "spam" * 3 => "spamspamspam" Input: There are built in functions to get keyboard input raw_input(prompt) Accepts user input terminated by pressing return Returns the typed input as a string Prompt is a string displayed before accepting input

Conditionals Conditionals: Chained Conditionals: Comparison operators (==, >, <, >=, <=, !=) return True (1) or False (0). Logical operators (and, or, not) can combine boolean expressions Conditionals: if condition: block1 With optional else clause Chained Conditionals: if condition1: elif condition2: block2 else: block3

Iteration The while statement: The for loop: while condition: block Repeats block until condition is False The for loop: for value in string: for char in fruit: print char Traverses every element of a list, tuple or string A compact equivalent of certain forms of the while loop Java-style ‘for’ loops can still be done

Optional and Named Arguments Function arguments can: Have default values Be called in any order Syntax def info(object, spacing = 10, collapse = 1) Parameters called out of order must be named (e.g., object = newtuple) Usage info(li) # spacing = 10, collapse = 1 info(li, 12) # spacing = 12, collapse = 1 info(li, collapse = 0) # spacing = 10 info(spacing = 15, object = li) # collapse = 1

Built-in Functions Python has built-in functions that are part of the core language (don’t need to be imported) Full list available in the Python Library Reference type Returns the datatype of an arbitrary object >>> type(obdchelper) <type 'module'> str Very general conversion of any object of any type into a string >>> str(1) '1' >>> str(obdchelper) "module 'obdchelper' from …" Other conversion functions also available = int, float Include dir, getattr, callable, id

Filtering Lists An extension of list comprehension Apply the mapping function to selected elements Syntax: [mapping_expression for element in source_list if filter_expression] Example: [elem for elem in li if elem != 'b'] [elem for elem in li if li.count(elem) == 1]

The weirdness of and & or The logical operators can be applied to more than just boolean expressions They return one of the values being compared 0, '', [], (), {}, None are considered False, everything else is True and Evaluated left to right. Returns the first False value, or the last value >>> 'a' and 'b' => 'b' >>> '' and 'b' => '' or Evaluated left to right. Returns the first True value, or the last value >>> 'a' or 'b' => 'a' >>> '' or 'b' => 'b'

The and-or trick Statements of the form: Can be converted into: if x: val = a else val = b Can be converted into: val = x and a or b >>> 1 and "first" or "second" => "first" >>> 0 and "first" or "second" => "second" But watch out for ‘a’ being false >>> 1 and "" or "second" => "second" Solution: (1 and [a] or [b])[0]

Lambda Functions One-line mini functions Function of the form: Become: Body of function can only be a single expression Drop parentheses around arguments Return keyword is implied Function of the form: def f(x): return x*2 Become: >>> g = lambda x: x*2 >>> g(3) => 6