Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Survey of Computer Science CSCI 110, Spring 2011 Lecture 2 Simple Python Programs Variables, Numbers and Strings 1 Using the Python Interpreter It's traditional to start with a program that prints out 'Hello World'. In Python, we can do this with one command to the interpreter. Python prompt Python statement >>> print "Hello World!" Hello World! Python output The read-evaluate-print cycle: •The Python interpreter waits for a command. •When the command is entered, Python evaluates the command •Python then prints the result. 2 Writing multiple statements We can continue in the interpreter mode: >>> print "Hello world!" Hello world! >>> print "How are you?" How are you? This can get tedious. We can use an editor to write multiple statements that Python can execute all at once: print "Hello world!" print "How are you?" print "CSCI 110 rocks!" We will demonstrate the result of running this in class. 3 Adding Comments •Comments are not evaluated by the interpreter •They are added to the source code to make it easier for programmers to understand the code or to add information. •Comments in Python begin with a # Example: # Program: Hello # Purpose: To write out 'Hello World' print "Hello world!" print "How are you?" print "CSCI 110 rocks!" 4 Program Prologues All of your programs for this class must have a prologue: # Program: Hello # Author: Angela Student # Class: CSCI 110 # Date: 9/5/08 # Assignment: Lab 1 # Purpose: To write out "Hello World!" 5 Variables (identifiers) If a user types something on a keyboard, the program must read it in and store it. Information is stored in the computer's memory. A variable names a piece of data. It references a location in memory where the information is stored. yourName 'Frank' aNumber 12 6 Assigning values to variables We can assign a value to a variable using the assignment operator. The assignment operator is an equals sign (=). Example: >>> yourName = "Frank" >>> yourName 'Frank' The variable, yourName, now identifies a location in memory where the text string, 'Frank', is stored. We can also assign numeric values to variables: >>> aNumber = 12 >>> aNumber 12 7 Reading Text from the Keyboard We use variables to read information in from the keyboard: The raw_input( ) command is used to read in text: >>> name = raw_input("Please enter your name: ") Please enter your name: Angela >>> name Angela 1) raw_input( ) will output the text within the brackets and then wait for the user response. 2) The user response is assigned to the name variable. 8 Reading in a Number Use the command, input( ), to enter a number from the keyboard. Example: >>> aNumber = input("Please enter a number: ") Please enter a number: 12 >>> aNumber 12 9 An example Write a program that asks for a user's name, and then says hello using the user's name. # Program: Hello # Purpose: To say hello to the user # We will write this in class yourName = raw_input("Hello! What is your name? ") print "Hello " + yourName + "!" 10 Naming Variables Rules for naming variables: 1) Do not use spaces 2) Variable names must start with a letter 3) Do not use special characters (%, *, /, etc) 4) Case (upper vs. lower case letters) matters. Good variable names: age_of_dog TaxRate98 Not allowed: age% 98TaxRate printHeading age-of-cat my dog 11 Classes and Objects Python is an object oriented programming language. All items of data are objects. Objects have properties: Things that describe the objects Objects have methods: Things an object can do. A class describes a group of objects that have the same methods and set of properties. Example: A car class Properties: color, number of doors, body type Methods: accelerate, brake, steer My car is an object that is a member of the car class. 12 Primitive Numeric Types Type integer Kind of info whole number Uses Arithmetic expressions long integer Large whole number floating point decimal number Examples: integer: 12 -37 " " " " 5672 long integer: 29387651298L floating point: 12.6 0.2 -7.615 13 Arithmetic Expressions with Floating Point Numbers Arithmetic operators for real numbers: + * / ** These work the way you would expect: 4.0 + 3.3 7.3 5.0 * 7.0 35.0 3.6 / 3 1.2 2.0**3.0 8.0 14 Arithmetic Expressions with Integers Arithmetic operators for integers: + * / % ** All work the way you would expect, except / and % DIV: Integer division truncates the decimal portion. 12 / 3 4 8/3 2 1/2 0 MOD: Gives the remainder of division of two integers: 7%2 1 11 % 4 3 15 Example Using Numbers # Program: dogYears.py # Purpose: To calculate a dogs age in dogyears. # We will write this in class. ageOfDog = input("How old is the dog? ") dogYears = 7*ageOfDog print "The dog is", dogYears, "years old in dogyears." 16 Strings A line of text is called a string. In Python, the string class is an ordered collection of characters. An ordered collection may also be called a list or an array. A string is indicated using quotes. Example: >>>"dog" 'dog' Triple quotes allow newlines in the middle of a string. Example: >>> ' ' 'This string has a break in the middle. ' ' ' 'This string has a break\nin the middle.' 17 String Methods Python provides a variety of operations with strings: Concatenation: + Repetition: * Length: len(stringName) "dog" + "house" -> "doghouse" "cool"*3 -> "coolcoolcool" len("cool") -> 4 Accessing a character by position: stringName[position] >>>name = "Angelina" >>>name[0] 'A' >>>name[5] 'i' Accessing a substring: stringName[first: last] >>>name[1:6] 'ngeli' 18