Dr. Philip Cannata 1 Python Overview. Dr. Philip Cannata 2 “Bad program” developed during class # The following python statement has python variables.

Slides:



Advertisements
Similar presentations
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Advertisements

Adapted from John Zelle’s Book Slides
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.
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
James Tam Lists In this section of notes you will be introduced to new type of variable that consists of other types.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
CHAPTER 6 Functions. Function Overview We’ve used built-in functions:  Examples:  print(“ABC”, x+10, sep=“:”)  round(x * 5, 2)  pygame.draw.circle(screen,
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Python Control of Flow.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 9 Iteration: Recursion 5/02/09 Python Mini-Course: Day 3 - Lesson 9 1.
By: Chris Harvey Python Classes. Namespaces A mapping from names to objects Different namespaces have different mappings Namespaces have varying lifetimes.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
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.
If statements while loop for loop
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.
30/10/ Iteration Loops Do While (condition is true) … Loop.
Built-in Data Structures in Python An Introduction.
Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Functions Chapter 6. Function Overview We’ve used built-in functions: – Examples: print(“ABC”, x+10, sep=“:”) round(x * 5, 2) pygame.draw.circle(screen,
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Fill the screen challenge! This is a starter activity and should take 3 minutes [ slide 1 ] 1.Log in to your computer 2.Open IDLE 3.In interactive mode,
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.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python Functions.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Dr. Philip Cannata 1 Python Classes. Dr. Philip Cannata 2.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
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.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Python Let’s get started!.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Written by: Dr. JJ Shepherd
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
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.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
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"
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
CS2910 Week 6, Lab Today Dictionaries in Python SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
Dr. Philip Cannata 1 Python Classes. Dr. Philip Cannata 2 # The following python statement has python variables which are intended to represent the fact.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Computer Programming Fundamentals
Java Primer 1: Types, Classes and Operators
CS-104 Final Exam Review Victor Norman.
Python Primer 2: Functions and Control Flow
File Handling Programming Guides.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
(more) Python.
15-110: Principles of Computing
Introduction to Computer Science
Presentation transcript:

Dr. Philip Cannata 1 Python Overview

Dr. Philip Cannata 2 “Bad program” developed during class # The following python statement has python variables which are intended to represent the fact that each cartoon character has the given amount of money. tom, jerry, phineas, ferb, mickey, donald = 1000, 2000, 3000, 4000, 5000, 6000 print tom, jerry # The following function can be passed three integer arguments, it subtracts the third argument from the first and adds the third argument to the second. It returns the changed values of the first two arguments. def takeMoney(c1, c2, amt) : c1-=amt c2+=amt return c1, c2 # The following are data structures that pair appropriate cartoon characters. # This data structure is a list of lists. duo=[[tom, jerry], [phineas, ferb], [mickey, donald]] # This data structure is a tuple of lists. duo=([tom, jerry], [phineas, ferb], [mickey, donald]) # This data structure is a tuple of tuples. duo=((tom, jerry), (phineas, ferb), (mickey, donald)) # This data structure is a list of tuples. duo=[(tom, jerry), (phineas, ferb), (mickey, donald)] # The following "for loop" iterates over duo calling takeMoney for each pair in duo. cnt=0 for i in duo : if cnt == 0: tom, jerry = takeMoney(i[0], i[1], 50) elif cnt == 1 : phineas, ferb = takeMoney(i[0], i[1], 50) elif cnt == 2 : donald, mickey = takeMoney(i[0], i[1], 50) cnt+=1 print tom, jerry, phineas, ferb, mickey, donald # Returns

Dr. Philip Cannata 3 Better version of the program developed by Magda Gomez in Fall 2013 tom, jerry, phineas, ferb, mickey, donald = 1000, 2000, 3000, 4000, 5000, 6000 def takeMoney(c1, c2, amt) : return c1-amt, c2+amt duo=[("tom", "jerry"), ("phineas", "ferb"), ("mickey", "donald")] keys = globals() # See the next page for a discussion about the globals() function. print keys["tom"] print(tom, jerry, phineas, ferb, mickey, donald) for i in duo: keys[i[0]], keys[i[1]] = takeMoney(keys[i[0]], keys[i[1]], 50) print(tom, jerry, phineas, ferb, mickey, donald)

Dr. Philip Cannata 4 The globals() Function The globals() function returns the dictionary which stores all global objects. It is amazing to see that you can actually define a global variable by simply typing: globals()["newVariable"] = "tom". And yes, you may refer to newVariable directly by saying print(newVariable) right after the previous statement. According to the Python Doc, there is a dictionary that stores all names (keys) and values of global variables. And when we hit an assignment statement, say tom = 1000, what the system really does is to access the value of tom by the key "tom" (which is a string), and set the value of that entry with key "tom" to 1000 in the global dictionary. So the globals() function does not initialize a new piece of memory and copy the keys and values, rather, it returns a reference of the dictionary in which Python stores all global objects. And here is a funny experiment to prove that python relies on that dictionary to remember all of its global variables. Try the following program: x = 1 print(globals()) print(x) globals().clear() print(globals()) print(x) And the program should report an error for the second print indicating that x is NOT defined! Additionally, this is also true for the locals() function, which stores all local variable names and values in the current scope.

Dr. Philip Cannata 5 Relations (A subset of the cross product of a set of domains) Examples of Relations: > keys tom1000 jerry2000 phineas3000 ferb4000 mickey5000 donald person nameagesalary phil chris Math Data Dictionary Function Definition Function Application Relations with Inheritance (see next 2 pages also) Table Classes (p 1 p 2 … p n body) (body a 1 a 2 … a n ) e.g., (x y x+y) (x+y 2 3)

Dr. Philip Cannata 6

Dr. Philip Cannata 7

Dr. Philip Cannata 8 Python class code for cartoon character example (Yes, this is a reformulation of the problem using classes instead of variables.) class Person(object): _registry = [] # _registry starts off as an empty list. name = "" money = 0 def __init__(self, name, amount): self._registry.append(self) # Each instance of Person is added to _registry. self.name = name self.amount = amount tom, jerry, phineas, ferb = Person('tom', 1000), Person('jerry', 2000), Person('phineas', 3000), Person('ferb', 4000) for p in Person._registry: print p.name + ", " + str(p.amount), # The comma at the end of the print statement causes all to print on one line. def transfer(p1, p2, amnt) : ______________________________________ # Fill in these 2 lines for Homework 1. Note, the “transfer” function ______________________________________ # requires no return statement. transfer(tom, jerry, 50) transfer(phineas, ferb, 50) print for p in Person._registry: print p.name + ", " + str(p.amount),