620 likes | 1.65k Views
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.
E N D
Introduction to PythonandIDLE(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. • 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.
A line of Python code to output a message Hitting the “Enter” key executes the line of code (also known as a statement)
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
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
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
Python Operators • Different types: • Arithmetic operators • Comparison operators • Assignment operators • Logical operators • We’ll consider just the arithmetic operators for now
Python Arithmetic Operators (cont.) • Ones we know already: +,-, * (multiply) and / (divide) Examples: >>> print(2*5-1) 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!
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!
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
Which operation to do first?- Operator Precedence in Python • What do you think is the value of this expression? 5 + 3 * 2
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!
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
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
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'>
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.
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.
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'
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>>>