programming in python part i dr. fatma cemile serçe atılım university 2009-2010
TRANSCRIPT
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 Pythonprint "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 Javapublic 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 ? NO1 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 messageHello, World!>>> print n17>>> print pi3.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$ = 1000000SyntaxError: 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 1x = 2print x
produces the output12
Operators and Operands Operators are special symbols that
represent computations like addition and multiplication
The values the operator uses are called operands
20+32 hour-1 hour*60+minute minute/60 5**2 (5+9)*(15-7)
The symbols +, -, and /, and the use of parenthesis for grouping, mean in Pythonwhat they mean in mathematics
The asterisk (*) is the symbol for multiplication
** is the symbol for exponentiation
% 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!>>> minute = 59>>> minute/600>>> float(minute)/600.938888
The value of minute is 59, and in conventional arithmetic 59 divided by 60 is0.98333, not 0. The reason for the discrepancy is that Python is performing integer division.
use floating-point division
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 2 * (3-1) is 4, and (1+1)**(5-2) is 8
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-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 elapsedpercentage = (minute*100)/60 # caution:integer division