Variable names
Download
Report
Transcript Variable names
Programming in Python Part I
Dr. Fatma Cemile Serçe
Atılım University
2009-2010
The Python Programming
Language
High-level language (like C, C++, Perl,
Java, etc.)
Interpreted language
Python programs executed by an
interpreter
Two ways to use the interpreter
command-line mode
script mode
The Python Programming
Language
In Command-line Mode
type Python programs and the interpreter prints the result
The first line of this example is the command that starts the
Python interpreter.
The next two lines are messages from the interpreter
The third line starts with >>>, prompt the interpreter uses to
indicate it is ready
Type print 1+1 program, and interpreter replied 2
The Python Programming
Language
In Script Mode
write a program in a file and use the interpreter to
execute the contents of the file. The file is called a
script
file name ends with “.py”
Ex: use text editor to create a file
named “hello.py”.
Then tell interpreter the name of the script
$ python hello.py
First Program: “Hello World”
in the Python
print "Hello, World!"
in the C
#include <stdio.h>
int main(void) {
printf("hello, world\n");
return 0;
}
in the C++
#include <iostream.h>
void main(){
cout << "Hello, world." << endl;
}
in the Java
public class HelloWorld{
public static void main(String args[]){
System.out.println(“Hello, World!”);
}
}
Values and Types
A value is one of the fundamental things—
like a letter or a number—that a program
manipulates
Ex:
2
“Hello, World!”
These values are belongs to different types
2: integer
“Hello, World!”: string
Values and Types
If you are not sure what type a value
has, the interpreter can tell you:
>>> type(’Hello, World!’)
<type ’str’>
>>> type(17)
<type ’int’>
>>> type(3.2)
<type ’float’>
str->String, int->Integer, float->floating-point
Exercise 1
What about values like ’17’ and ’3.2’?
>>> type(’17’)
<type ’str’>
>>> type(’3.2’)
<type ’str’>
They’re strings.
Exercise 2
What is the output?
>>> print 1,000,000
1.000.000 ? NO
1 0 0 ? YES
a semantic error: the code runs without
producing an error message, but it doesn’t
do the “right” thing.
Variables
A variable is a name that refers to a value
The assignment statement creates new
variables and gives them values
>>> message = ’Hello, World!’
>>> n = 17
>>> pi = 3.14159
Variables (cont.)
The print statement also works with variables
>>> print message
Hello, World!
>>> print n
17
>>> print pi
3.14159
Variables (cont.)
Variables also have types again, we can ask
the interpreter what they are
>>> type(message)
<type ’str’>
>>> type(n)
<type ’int’>
>>> type(pi)
<type ’float’>
The type of a variable is the type of the value
it refers to.
Variable names and
keywords
choose meaningful names
both letters and numbers, but begin with a
letter
Message and message are different (use
lowercase by convention)
use underscore character (_) in names with
multiple words
person_name
Variable names and
keywords
If you give a variable an illegal name, you get a
syntax error:
>>> 76tables = ‘seventy six tables’
SyntaxError: invalid syntax
>>> more$ = 1000000
SyntaxError: invalid syntax
>>> class = ’COMPE 111’
SyntaxError: invalid syntax
76trombones is illegal because it does not begin
with a letter.
more$ is illegal because it contains an illegal
character, the dollar sign
But what’s wrong with class? It turns out that class
is one of the Python keywords.
Variable names and
keywords
Keywords define the language’s rules and
structure
Keywords cannot be used as variable names
Python has twenty-nine keywords:
Statements
A statement is an instruction that the Python
interpreter can execute
print and assignment
The result of a print statement is a value.
Assignment statements don’t produce a result.
A script usually contains a sequence of statements.
Ex: the script
print 1
x = 2
print x
produces the output
1
2
Operators and Operands
Operators are special symbols that
represent computations like addition and
multiplication
The values the operator uses are called
operands
The symbols +, -, and /, and the use of
parenthesis for grouping, mean in Python
20+32
what they mean in mathematics
hour-1
hour*60+minute The asterisk (*) is the symbol for multiplication
minute/60
** is the symbol for exponentiation
5**2
(5+9)*(15-7) % modulo
Operators and
Operands(cont.)
When a variable name appears in the place of
an operand, it is replaced with its value
before the operation is performed
Addition, subtraction, multiplication, and
exponentiation all do what you expect,
but division! The value of minute is 59, and in
>>> minute = 59
conventional arithmetic 59 divided by 60 is
0.98333, not 0. The reason for the discrepancy
>>> minute/60
is that Python is performing integer division.
0
>>> float(minute)/60 use floating-point division
0.938888
Order of Operations
When more than one operator appears
in an expression, the order of
evaluation depends on the rules of
precedence.
Python follows the same precedence
rules for its mathematical operators that
mathematics does.
The acronym PEMDAS is a useful way
to remember the order of operations:
Order of Operations
PEMDAS
Parentheses have the highest precedence
Exponentiation has the next highest precedence,
2**1+1 is 3 and not 4, and 3*1**3 is 3 and not 27
Multiplication and Division have the same
precedence, which is higher than Addition and
Subtraction
2 * (3-1) is 4, and (1+1)**(5-2) is 8
2*3-1yields 5 rather than 4, and 2/3-1 is -1, not 1
Operators with the same precedence are
evaluated from left to right.
6*100/60 yields 10
Operations on Strings
In general, you cannot perform mathematical operations on
strings, even if the strings look like numbers
The following are illegal:
message-1
’Hello’/123
message*’Hello’
’15’+2
+ operator work with strings. It does concatenation, means
joining the two operands by linking them end-to-end
fruit = ’banana’
bakedGood = ’ nut bread’
print fruit + bakedGood
Output: banana nut bread
* operator also works on strings; it performs repetition.
’Fun’*3 is ’FunFunFun’
Warning!
There are limits on where you can use
certain expressions.
For example, the left-hand side of an
assignment statement has to be a
variable name, not an expression.
The following is illegal:
minute+1 = hour
Comments
#
Notes to your programs to explain in natural
language what the program is doing, called
comments, and they are marked with the #
symbol
Everything from the # to the end of the line
is ignored—it has no effect on the program
compute the percentage of the hour that has elapsed
percentage = (minute*100)/60
# caution:integer division