1 / 20

Introduction to Python and IDLE (the IDE for Python)

Introduction to Python and IDLE (the IDE for Python). CPSC 1301 – Spring 2014 Ed Bosworth. Starting IDLE. IDLE is the Graphic User Interface that facilitates the writing of Python programs.

vance
Download Presentation

Introduction to Python and IDLE (the IDE for Python)

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. Introduction to PythonandIDLE(the IDE for Python) CPSC 1301 – Spring 2014 Ed Bosworth

  2. Starting IDLE • IDLE is the Graphic User Interface that facilitates the writing of Python programs. • Click on the IDLE icon on your computerto bring up the program. If no icon exists,then use the command button and run “idle” • All computers in CCT 407 and CCT 450should have IDLE installed. • We do have instructions for installing IDLE on your home computer.

  3. A line of Python code to output a message Hitting the “Enter” key executes the line of code (also known as a statement)

  4. Creating your program • In Python Shell, select the File menu and thenNew Window • Type in your program • Run the program by using Run menu and then Run Module

  5. Saving your program for running it later • Computer Files store documents as well as programs • Save your program in a file using the File menu and thenSave As • Be sure to specify the extension as .py • You can then run the program later by opening it in Python Shell and then using the Run Module command

  6. Strings and Expressions Statement: print(“Pi = “, 22/7) Output: Pi = 3.142857142857143 String – Group of characters enclosed in double quotes, e.g., “Pi = “, “Hello World!” (Mathematical) Expression – combination of math operators and operands, e.g., 22/7

  7. Python Operators • Different types: • Arithmetic operators • Comparison operators • Assignment operators • Logical operators • We’ll consider just the arithmetic operators for now

  8. Python Arithmetic Operators (cont.) • Ones we know already: +,-, * (multiply) and / (divide) Examples: >>> print(2*5-1) 9

  9. Python Arithmetic Operators (cont.) • Ones we know already: +, -, * (multiply) and / (divide) Examples: >>> print(2*5-1) 9 >>> print("ha" + "ha, " + "very funny!") haha, very funny!

  10. Python Arithmetic Operators (cont.) • Ones we know already: +, -, * (multiply) and / (divide) Examples: >>> print(2*5-1) 9 >>> >>> print(2 + 3 + 5) 10 You can also “add” or concatenate strings using the ‘+’ operator >>> print("ha" + "ha, " + "very funny!") haha, very funny!

  11. More Arithmetic Operators %Modulus- Divides left hand operand by right hand operand and returns remainder >>> 5%2 1 **Exponent- Performs exponential (power) calculation on operators >>> 3**2 9 //Integer division - Throws away digits after decimal point >>> 9//2 4

  12. Which operation to do first?- Operator Precedence in Python • What do you think is the value of this expression? 5 + 3 * 2

  13. Which operation to do first?- Operator Precedence in Python • What do you think is the value of this expression? 5 + 3 * 2 The answer is 11, because * has higher precedence than +, and is performed first Precedence of math operators, from highest to lowest are • Exponentiation: ** • Multiplication, division, and remainder: * / // % • Addition and subtraction: + - Any operations enclosed in parentheses are performed first Example: (5+3) *2 evaluates to 16, not 11!

  14. A Practical Note on Precedence • Every computer language has its rules of operator precedence: which operations to do first, and then what to do. • What about the following expression?What can it possibly mean?xx + yy>> 3 + z**4 + a //b • Ignore the fancy rules and follow these rules.1. Exponentiation2. Multiplication and Division3. Addition and Subtraction • Put parentheses around everything else. Added on 1/22/2014

  15. Functions and Arguments Functions are components of programs used to perform certain tasks For example, the Python print function prints strings and values of expressions print(“Pi = “, 22/7) prints Pi = 3.142857142857143 Function print here takes two arguments separated by a comma – a string, and an expression

  16. Values and Types • A value is one of the basic things a program works with, like a number, a letter or a string. • Examples seen: 22,7, “Hello, World!”. • Each value has a pre-defined type or class >>> type('Hello, World!') <class 'str'> >>> type(17) <class 'int'>

  17. Constant Expressions and Variables • In computer science we focus on the evaluation of items. • A constant expression evaluates to itself. • A variable evaluates to the value stored in it. • Here are some constants15 the integer 153.2 the real number“A Constant” a string of characters.

  18. Using IDLE to Evaluate(Copied from the IDLE Screen) • >>> ## In Python, anything preceded by two sharp (pound signs) • >>> ## is a comment, and not evaluated by the Python Shell. • >>> ## • >>> ## Here are evaluations of some numbers • >>> 4 • 4 • >>> 3.5 • 3.5 • >>> 5 + 9 • 14 • >>> ## My input is seen as what follows the >>> • >>> ## Python replies with the evaluation.

  19. More IDLE Screen Shots • >>> ## Now declare some variables • >>> able = 11 • >>> baker = 12 • >>> able • 11 • >>> baker • 12 • >>> able + baker • 23 • >>> ## But note that putting characters in double quotes • >>> ## makes those characters a string and not a variable • >>> ## • >>> "able" • 'able' • >>> "able + baker" • 'able + baker'

  20. Error Messages • The Python shell is quite verbose in complaining about mistakes. Recall that the variable able is set to 11. • >>> able • 11 • >>> ## Now an attempt to evaluate an undefined variable • >>> gammaTraceback(most recent call last):File "<pyshell#22>", line 1, in <module> gammaNameError: name 'gamma' is not defined>>>

More Related