Download Session 1 - Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Science of
Graphics and Games
MONT 105S, Spring 2009
Session 1
Simple Python Programs
Using Print, Variables, Input
1
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.
2
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.
3
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.
4
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!"
5
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!"
6
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
7
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
8
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.
9
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
10
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 + "!"
11
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
12