Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming in Python Part I Dr. Fatma Cemile Serçe Atılım University 2009-2010.

Similar presentations


Presentation on theme: "Programming in Python Part I Dr. Fatma Cemile Serçe Atılım University 2009-2010."— Presentation transcript:

1 Programming in Python Part I Dr. Fatma Cemile Serçe Atılım University 2009-2010

2 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

3 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

4 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

5 First Program: “Hello World” in the Python print "Hello, World!" in the C #include int main(void) { printf("hello, world\n"); return 0; } in the C++ #include void main(){ cout << "Hello, world." << endl; } in the Java public class HelloWorld{ public static void main(String args[]){ System.out.println(“Hello, World!”); }

6 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

7 Values and Types If you are not sure what type a value has, the interpreter can tell you: >>> type(’Hello, World!’) >>> type(17) >>> type(3.2) str ->String, int ->Integer, float ->floating-point

8 Exercise 1 What about values like ’17’ and ’3.2’? >>> type(’17’) >>> type(’3.2’) They’re strings.

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

10 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

11 Variables (cont.) The print statement also works with variables >>> print message Hello, World! >>> print n 17 >>> print pi 3.14159

12 Variables (cont.) Variables also have types again, we can ask the interpreter what they are >>> type(message) >>> type(n) >>> type(pi) The type of a variable is the type of the value it refers to.

13 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

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

15 Variable names and keywords Keywords define the language’s rules and structure Keywords cannot be used as variable names Python has twenty-nine keywords:

16 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

17 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 Python what they mean in mathematics The asterisk (*) is the symbol for multiplication ** is the symbol for exponentiation % modulo

18 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/60 0 >>> float(minute)/60 0.938888 The value of minute is 59, and in conventional arithmetic 59 divided by 60 is 0.98333, not 0. The reason for the discrepancy is that Python is performing integer division. use floating-point division

19 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:

20 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

21 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’

22 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

23 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


Download ppt "Programming in Python Part I Dr. Fatma Cemile Serçe Atılım University 2009-2010."

Similar presentations


Ads by Google