(more) Python.

Slides:



Advertisements
Similar presentations
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Advertisements

I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.
Python Control of Flow.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
January 24, 2006And Now For Something Completely Different 1 “And Now For Something Completely Different” A Quick Tutorial of the Python Programming Language.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
1 Python CIS*2450 Advanced Programming Concepts Material for this lecture was developed by Dr. D. Calvert.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Built-in Data Structures in Python An Introduction.
And other languages….  Array literals/initialization a = [1,2,3] a2 = [-10..0, 0..10] a3 = [[1,2],[3,4]] a4 = [w*h, w, h] a5 = [] empty = Array.new zeros.
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Python I Some material adapted from Upenn cmpe391 slides and other sources.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Introduction to Concepts in Functional Programming CS16: Introduction to Data Structures & Algorithms Thursday, April 9,
String and Lists Dr. José M. Reyes Álamo.
Scientific Programming in Python -- Cheat Sheet
CSc 120 Introduction to Computer Programing II Adapted from slides by
When to use Tuples instead of Lists
Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.
Basic operators - strings
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Section 6: Sequences Chapter 4 and 5.
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
While Loops in Python.
Engineering Innovation Center
Introduction to Python
Arithmetic operations, decisions and looping
Introduction to Python
Introduction to Python
Introduction to Python
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
Introduction to Primitive Data types
String and Lists Dr. José M. Reyes Álamo.
CSC1018F: Intermediate Python
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
CISC124 Labs start this week in JEFF 155. Fall 2018
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Python Review
Comparing Python and Java
Class code for pythonroom.com cchsp2cs
More Basics of Python Common types of data we will work with
Introduction to Primitive Data types
Dictionary.
Introduction to Computer Science
Selamat Datang di “Programming Essentials in Python”
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

(more) Python

Where we have been

Where we have been

And where we are going

Clojure Python Only values have types Only values have types Most values are immutable Some object- orientation, but mostly functional Mark-and-sweep garbage collection Only values have types Some values are mutable Object-oriented, but with functions as well Reference-counted garbage collection

C++ Python Variables and values have types Identifiers are values Most values are mutable Object-oriented, but with functions as well Scope-based destruction Only values have types Identifiers are references Some values are mutable Object-oriented, but with functions as well Reference-counted garbage collection

Python Types Integer 42 32-bit (at least) Long integer 42L Unlimited precision

Python Types Real numbers 64-bit double-precision floating point Generally you can check with sys.float_info

Python Types Complex float values for real and imaginary 1+2j

Python Types String Either type of quote is usable (as long as you're consistent) "A string" = 'A string' 'C' is not of type char Immutable

Python Types List Similar to Clojure vector L=[1, 2, 3, 4, 5] Mutable Slicing L[0:3] == L[:3] = [1, 2, 3] L[1:3] == [2, 3] L[-2:] = [4, 5] L[-1] == 5

Python Types Tuples Added to the language to provide an immutable version of the list T = (1, 2, 3, 4, 5) Special case for initializing a tuple of length one T1 = (1,) Because (1) = 1

Python Types Dictionary A hash table Keys must be hashable and immutable Mutable D = {'key1' : 'val1', 2 : [1, 2]} D['key1'] == 'val1' D[2] == [1, 2] Adding elements to the dictionary D['new-key'] = 'new-val'

Significant whitespace Python (like a few other languages) uses significant whitespace Bodies of code inside functions, conditional expressions, etc. must have the same indentation The line before a block ends in a colon

Conditionals x = 10 y = 20 if x < y: print("first condition was true") else: print("second condition was true")

Functions def function_name(arg1, arg2): print("arg1",arg1)

Classes class C: def __init__(self,val): self.val = val def get_val(self): return self.val

For loops Python doesn't provide a C-style for loop Instead, it provides something more like the 'range-based' for loop of C++11 for x in ['a', 'b', 'c']: print(x) for i in range(10): for x in {1:'one', 2:'two'}: print(x)

Collection Queries Collections can be treated as Booleans for the purpose of if and while statements when doing so, any non-empty collection evaluates to true L = ['a', 'b', 'c'] while L: print(L[0]) del L[0]

Collection Queries Containment is checked with in L = ['a', 'b', 'c'] if 'z' in L: print "I found a 'z'!" D= {1: 'one', 2: 'two'} if 'two' in D: print "D had a 'two' in it!"

Our old friends: map and filter Python has a built-in version of the map and filter that you wrote before the midterm

Map L = [1, 2, 3, 4, 5] L2 = map(lambda x: x*x, L) -> [2, 4, 6, 8, 10]

Filter L = [1, 2, 3, 4, 5] L2 = filter(lambda x: x%2==0 , L) -> [2, 4]

But don't do that! List comprehensions The scariest thing about them is the name

List Comprehensions [ <expression> for <var> in <collection>]

List Comprehensions: Map L2 = [x*x for x in L]

List Comprehensions: Filter L2 = [x for x in L if x%2==0]

List Comprehensions L = [1, 2, 3, 4, 5] L2 = [x*x for x in L if x%2==0]

List Comprehensions You can even use multiple variables [(x,y) for x in range(5) for y in range(5) if x<y]