Catapult Python Programming Wednesday Morning session

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

2/7/2008. >>> Overview * boolean * while * random * tuples.
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Types and Arithmetic Operators
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.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Python November 14, Unit 7. Python Hello world, in class.
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.
1 Python Chapter 2 © Samuel Marateck, After you install the compiler, an icon labeled IDLE (Python GUI) will appear on the screen. If you click.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Introduction to Computing Using Python Python  Python is an interactive language.  Java or C++: compile, run  Also, a main function or method  Python:
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Built-in Data Structures in Python An Introduction.
Functions Chapter 4 Python for Informatics: Exploring Information
Variables, Expressions, and Statements
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Python Conditionals chapter 5
Python – Part 3 Functions 1. Getting help Start the Python interpreter and type help() to start the online help utility. Or you can type help(print) to.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
A Tutorial on the Python Programming Language. Overview Running Python and Output Data Types Input and File I/O Control Flow Functions.
Variables, Types, Expressions Intro2CS – week 1b 1.
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Functions Chapter 4 Python for Informatics: Exploring Information Slightly modified by Recep Kaya Göktaş on March 2015.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Introduction to Python Developed by Dutch programmer Guido van Rossum Named after Monty Python Open source development project Simple, readable language.
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.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
String and Lists Dr. José M. Reyes Álamo.
Lesson 03: Variables and Types
Python Review 1.
CSc 120 Introduction to Computer Programing II Adapted from slides by
Introduction to Python
Data Types and Conversions, Input from the Keyboard
Computer Programming Fundamentals
Making Choices with if Statements
Catapult Python Programming Thursday Session
CS 100: Roadmap to Computing
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
The CS 5 Black Post Penguins Invade Dormitory
Python Data Types Expressions, Variables, and Assignments Strings
Examples of Primitive Values
Exception Handling.
Data types Numeric types Sequence types float int bool list str
CS 100: Roadmap to Computing
String and Lists Dr. José M. Reyes Álamo.
Margaret Derrington KCL Easter 2014
Lesson 03: Variables and Types
Recap Week 2 and 3.
Python I Some material adapted from Upenn cmpe391 slides and other sources.
Python for Informatics: Exploring Information
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
CHAPTER 3: String And Numeric Data In Python
Chapter 2 Programming Basics.
CS1110 Today: collections.
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
CS 100: Roadmap to Computing
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
CSCE 206 Lab Structured Programming in C
CS 100: Roadmap to Computing
Class code for pythonroom.com cchsp2cs
Presentation transcript:

Catapult Python Programming Wednesday Morning session

Ideas from yesterday Assignment function definition and use x = 5 x x = x + 3 x function definition and use def f(x): return 2*x + 3 print(f(4), f(6)) prints 11 15 5 8

Do you have questions on … numbers and arithmetic importing the math module range simple loops function definition function use Simple graphics

Getting input from the user The input function prints a prompt, then returns a string containing whatever the user types: Fix this:

Some Python data types Numeric types int: whole numbers float Arbitrarily large whole numbers. Examples 6, 44389654908498902 float Real numbers (there can be roundoff errors) Examples 4.72 1.7e4 1.0/3.0 complex : the imaginary number i is represented by 1j.

Operations on numeric types x + y x – y x * y x / y x // y forces integer division x % y remainder (modulo) x**y same as pow(x,y) -x abs(x) x == y True if x equals y, False otherwise. x != y not equal x < y x <= y x > y x >= y cmp(x,y) -1 if x < y 0 if x==y 1 if x > y Other numeric operations, such as sin, cos, tan, sqrt, log, can be imported from the math module.

More Python variable types Boolean values: True False >>> 3 < 5 True String – an immutable sequence of characters. Examples: ″abc ″ ′abc ′ ′a\nc′ ′a\\c′ print abc vs print ′abc′ String concatenation: >>> s = "abc" >>> s + "def" 'abcdef' >>> 'abc' 'def' 'abcdef' >>> s 'def' SyntaxError: invalid syntax

String Operations (do live) >>> alpha = 'abcdefghjklm' >>> alpha[1] 'b' >>> alpha [3:5] 'de' >>> alpha[6:] 'ghjklm' >>> len(alpha) 12 >>> alpha[0:12:2] 'acegjl‘ >>> alpha[1] = 'X‘ # are strings immutable Traceback (most recent call last): File "<pyshell#32>", line 1, in <module> alpha[1] = 'X' TypeError: 'str' object does not support item assignment >>> 'def' in alpha True >>> 'df' in alpha False >>> alpha.find('def') 3 >>> alpha.find('deg') -1 >>> >>> alpha.upper() 'ABCDEFGHJKLM' >>> alpha.islower() True >>> '***' + alpha.center(20) + '***' '*** abcdefghjklm ***' >>> '***' + alpha.rjust(20) + '***' '*** abcdefghjklm***‘ >>> '***' + alpha.ljust(20) + '***' '***abcdefghjklm ***‘ >>> sent = "This sentence has 5 words" >>> sent.split() ['This', 'sentence', 'has', ‘5', 'words'] >>> sent.split('e') ['This s', 'nt', 'nc', ' has 5 words'] >>> sent.count('en') 2

Tuples and Lists Tuples (immutable) lists (mutable) >>> tup1 = ('coke', 'sprite', 'dr pepper') >>> tup1[1] 'sprite' >>> tup1[1:] ('sprite', 'dr pepper') >>> for drink in tup1: print 'I like ' + drink I like coke I like sprite I like dr pepper >>> tup1[2] = 'pepsi' Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> tup1[2] = 'pepsi' TypeError: 'tuple' object does not support item assignment lists (mutable) >>> list2 = ['coke', 'sprite', 'dr pepper'] # all one line >>> list2[1:] ['sprite', 'dr pepper'] >>> list2[2] = 'pepsi' >>> list2 ['coke', 'sprite', 'pepsi'] >>> numList = [2, 3, 6, 7, 3, 4, 7, 5, 3, 4, 2, 1, 8, 3]; >>> numList.count(3) 4 >>> numList.remove(4) >>> numList [2, 3, 6, 7, 3, 7, 5, 3, 4, 2, 1, 8, 3] >>> numList.reverse() [3, 8, 1, 2, 4, 3, 5, 7, 3, 7, 6, 3, 2] >>> numList.sort() [1, 2, 2, 3, 3, 3, 3, 4, 5, 6, 7, 7, 8] >>> len(numList) 13 Notice the difference in the form of the use of the sort method and the len function. A list is an Object. Objects know things and do things.

Getting help How could we find the names of all list operations? Try dir(list) help(list) help(list.append) – what is that self argument.

Recap: Defining a function General form: def functionName(arguments): statements return expression Example: def slope(x1, y1, x2, y2): return (y2-y1)/(x2-x1) Using this function: slope(4.5, 5.3, 8.0, 12.9) It is possible to define a function with optional arguments. More on that later.

Review/practice Do this yourself (with help from neighbors, Chris, Jeremy, and me) Define and test a function that takes the radius of a circle and returns its area. (Don't forget to import math) Write a loop that calls this function repeatedly, so that it prints the areas of circles whose radii are 1, 2, 3, 4, …, 9, 10. Then a function that asks the user how they are doing. If good, it says “That’s nice!”. If “bad”, it says “That’s too bad!”.

Together: More functions Parallel Lines, Concentric circles Tri2, tri3, … , triN clickInsideCircle (after a brief while loop discussion) ConnectTheDots (putting it all together) Each click draws a point there (small circle) Connect the dots as we go along. If click is inside the first dot, fill the polygon We'll do some of these this afternoon.