1 / 27

How about the Python Stuff?

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.

brita
Download Presentation

How about the Python Stuff?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. How about the Python Stuff?

  2. 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?

  3. 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?

  4. 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?

  5. 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?

  6. 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?

  7. Python Review - Variables • What are variables? • What’s the difference between = and ==? • What happens when you use a variable’s name?

  8. Python Review – Example Program • How would you write a program that calculates 10*10?

  9. 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.

  10. 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

  11. Python Review - Functions • How do you execute a function?

  12. 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.

  13. Python Review - Functions • How do you turn our square calculation code into a function? value = 10 sqr = value * value print sqr

  14. 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?

  15. Python Review - Functions • Functions are meant to be generic – how can we generalize our square function?

  16. 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?

  17. 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?

  18. 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.

  19. 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?

  20. Python Review - Repetition • We’ve talked about two ways of repetition in Python… what are they?

  21. 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

  22. 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' ]

  23. 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'>

  24. 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 ‘’‘

  25. 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?

  26. 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 *

  27. 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!

More Related