Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.

Similar presentations


Presentation on theme: "1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input."— Presentation transcript:

1 1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input

2 2 The Python Programming Language Computers understand a small number of commands in machine language, which consists of all 1's and 0's. High level programming languages, such as Python, C++, Java, provide an easier language for humans to use to tell the computers what to do. The high level language is translated into machine language by a compiler or an interpreter. Python is an interpreted language. Each statement is evaluated by the interpreter as you enter it.

3 3 Using the Python Interpreter >>> print "Hello World!" Hello World! 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. 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. Python statement Python output Python prompt

4 4 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 "Holy Cross rocks!" We will demonstrate the result of running this in class.

5 5 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!"

6 6 Program Prologues All of your programs for this class must have a prologue: # Program: Hello # Author: Angela Student # Class: MONT 105S # Date: 1/14/09 # Assignment: Lab 1 # Purpose: To write out "Hello World!"

7 7 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. 'Frank'12 yourName aNumber

8 8 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

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

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

11 11 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 yourName = raw_input("Hello! What is your name? ") print "Hello " + yourName + "!"

12 12 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 printHeading Not allowed: age%98TaxRateage-of-catmy dog


Download ppt "1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input."

Similar presentations


Ads by Google