270 likes | 395 Views
How about the Python Stuff?. Python Review. What have we learned to this point? Quite a bit, actually. Let’s go over it before we have a test, shall we? Questions for you: What is Python? What is Java? What is a computer program?. Python Review - Operators.
E N D
Python Review • What have we learned to this point? Quite a bit, actually. Let’s go over it before we have a test, shall we? • Questions for you: • What is Python? • What is Java? • What is a computer program?
Python Review - Operators • Python has many operators. Some examples are: +, -, *, /, >, <, ==, print • Operators perform an action on one or more operands. Some operators accept operands before and after themselves: operand1 + operand2, or 3 + 5 • Others are followed by one or more operands until the end of the line, such as: print “Hi!”, 32, 48 • Question? How is this like/not like Java?
Python Review – Operators • Mathematical Operators: Addition (+), Subtraction (-), Multiplication (*), and Division (/). • Relational Operators: Less-Than (<), Greater-Than (>), Less-Than-or-Equal(<=), Greater-Than-or-Equal (>=), and Equality-Test (==). These operators produce a True or False value. • Logical Operators: AND, OR, NOT. These operators produce a True or False value. • Question? How is this like/not like Java?
Python Review - Expressions 5 + 4 Evaulates to 9 “5 + 4” Evaluates to “5 + 4” 5 / 2 Evaluates to 2 5 / 2.0 Evaluates to 2.5 “A” + 5 Evaluates to an error “A” * 5 Evaluates to “AAAAA” “A” + “B” Evaluates to “AB” Question? How is this like/not like Java?
Python Review – Data Types • In Python, all data has an associated data “Type”. Why is this important? • You can find the “Type” of any piece of data by using the type() function: type( “Hi!”) produces <type 'str'> • What types have we talked about?
Python Review - Variables • What are variables? • What’s the difference between = and ==? • What happens when you use a variable’s name?
Python Review – Example Program • How would you write a program that calculates 10*10?
Python Review – Example Program • How would you write a program that calculates 10*10? value = 10 sqr = value * value print sqr Prints 100 to the screen.
Python Review - Functions • If you want to do something (like calculate the square of a number) multiple times, you can encapsulate the code inside of a Function. • A Function is a named sequence of statements that perform some useful operation. Functions may or may not take parameters, and may or may not return results. Syntax: def NAME( LIST OF PARAMETERS): STATEMENTS STATEMENTS
Python Review - Functions • How do you execute a function?
A slight aside… • A function is made up of two main parts, the Header, and the Body. • The function header consists of: def funcName(param1,param2): • def keyword • function name • zero or more parameters, comma separated, inside of parenthesis () • A colon : • The function body consists of all statements in the block that directly follows the header. • A block is made up of statements that are at the same indentation level.
Python Review - Functions • How do you turn our square calculation code into a function? value = 10 sqr = value * value print sqr
Python Review - Functions def square(): value = 10 sqr = value * value print sqr • What will this do? Is it very useful? • How do we make it more so?
Python Review - Functions • Functions are meant to be generic – how can we generalize our square function?
Python Review - Functions • Functions are meant to be generic – how can we generalize our square function? def square(value): sqr = value * value print sqr • What will this do? Is it very useful? • How do we make it more so?
Python Review - Functions • Functions are meant to be generic – how can we generalize our square function? def square(value): sqr = value * value return sqr • What will this do? Is it very useful? • Could you write this in Java?
Python Review – Decisions! • Life is full of decisions – so our code must be able to accommodate them. In Python, we use the “If” statement. if ( boolean_expression): STATEMENT STATEMENT • The indented block of code following an if statement is executed if the boolean expression is true, otherwise it is skipped.
Python Review – ‘If’ Example numberOfWheels = 3 if ( numberOfWheels < 4): print “You don't have enough wheels!” print “I'm giving you 4 wheels!” numberOfWheels = 4 print “You now have”, numberOfWheels, “wheels” • What prints if the numberOfWheels is 3 (as above?) • What if numberOfWheels = 5?
Python Review - Repetition • We’ve talked about two ways of repetition in Python… what are they?
Python Review - Repetition • We’ve talked about two ways of repetition in Python… what are they? • while while (boolean expression) : STATEMENT STATEMENT STATEMENT • for for ELEMENT_VAR in SEQUENCE: STATEMENT STATEMENT
Python Review - Lists • Lists are a mutable data type that you can create by enclosing a series of elements in square brackets separated by commas. The elements do not have to be of the same data type:myList = [ 23, True, 'Cheese”, 3.1459 ] • Unlike Strings and tuples, individual elements in a list can be modified using the assignment operator. After the following commands: myList[0] = True myList[1] = 24 myList[3] = “Boo” myList contains: [ True, 24, 'Cheese', 'Boo' ]
Python Review - Lists You can look at list elements either one at the time or by using a list. myList = [ True, 24, 'Cheese', 'Boo' ] for eachElement in myList: print type(eachElement) produces: <type 'bool'> <type 'int'> <type 'str'> <type 'str'>
Python Review - Strings • Strings are a complex data type. These are values enclosed in quotes. • You can think of Strings as lists of characters. • You can access specific elements of the String in the same way that you can with a list: myString = “How’s it going?” • You can access a specific element using an integer index which counts from the front of the sequence (starting at ZERO!) myString[0] produces ‘H‘ myString[3] produces ‘’‘
Python Review - Randomness • Many issues come up in programming that require a bit of randomness. We discussed two ways of placing randomness in our code. What were they?
Python Review - Randomness • Many issues come up in programming that require a bit of randomness. We discussed two ways of placing randomness in our code. What were they? • Two functions: • random() • Returns a random number between 0.0 and 1.0 • randrange(A,B) • Returns a random number between A and B-1 You also have to remember to import the random library: from random import *
Test – next time! Wow, that’s a lot! Here’s a list of FAQ’s for you: • You need to know and understand all the commands in this slide show and any of the others in our notes. • You may have to produce written code on the test (given a problem, how would you use Python, or Java to solve it) • Questions will be a mix of multiple choice, matching, fill in the blank, and short answer • There will probably be a bonus – if so, it will have absolutely nothing to do with the class (it is a bonus, after all). So, you can’t study for it!