Presentation is loading. Please wait.

Presentation is loading. Please wait.

PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.

Similar presentations


Presentation on theme: "PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses."— Presentation transcript:

1 PYTHON

2 Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses English keywords frequently where as other languages use punctuation and it has fewer syntactical constructions than other languages. Python is Interpreted: This means that it is processed at runtime by the interpreter and you do not need to compile your program before executing it. This is similar to PERL and PHP. Python is Interactive: This means that you can actually sit at a Python prompt and interact with the interpreter directly to write your programs. Python is Object-Oriented: This means that Python supports Object-Oriented style or technique of programming that encapsulates code within objects. Python is Beginner's Language: Python is a great language for the beginner programmers and supports the development of a wide range of applications from simple text processing to WWW browsers to games.

3 Python is a script language. It’s a high-level programming language that is interpreted by another program at runtime rather than compiled by the computer's processor as other programming languages (such as C and C++).

4 The >>> prompt is the Python interpreter’s way of asking you, “What do you want me to do next?” When we want to write a program, we use a text editor to write the Python instructions into a file, which is called a script. By convention, Python scripts have names that end with.py

5 >>> print 4 4 >>> type('Hello, World!') >>> type(17) >>> type(3.2) >>> print 1,000,000 1 0 0 Python interprets 1,000,000 as a comma separated sequence of integers, which it prints with spaces between This is the first example we have seen of a semantic error: the code runs without producing an error message, but it doesn’t do the “right” thing.

6 An assignment statement creates new variables and gives them values: >>> message = 'And now for something completely different' >>> n = 17 >>> pi = 3.1415926535897931 To display the value of a variable, you can use a print statement: >>> print n 17 >>> print pi 3.14159265359

7 If you give a variable an illegal name, you get a syntax error: >>> 76trombones = 'big parade' SyntaxError: invalid syntax >>> more@ = 1000000 SyntaxError: invalid syntax >>> class = 'Advanced Theoretical Zymurgy' SyntaxError: invalid syntax

8 RULES OF PRECEDENCE Exponentiation has the next highest precedence, so 2**1+1 is 3, not 4 3*1**3 is 3, not 27 Multiplication and Division have the same precedence, which is higher than Addition and Subtraction, which also have the same precedence. So 2*3-1 is 5, not 4, and 6+4/2 is 8, not 5. Operators with the same precedence are evaluated from left to right. So in the expression 5-3-1 is 1, not 3 because the 5-3 happens first and then 1 is subtracted from 2.

9 The modulus operator >>> quotient = 7 / 3 >>> print quotient 2 >>> remainder = 7 % 3 >>> print remainder 1 The + operator works with strings, it performs concatenation, which means joining the strings by linking them end-to-end. >>> first = 10 >>> second = 15 >>> print first + second 25 >>> first = '100‘ >>> second = '150' >>> print first + second 100150

10 Comments, the # symbol Python provides a built-in function called raw_input that gets input from the keyboard as in java Scanner user_input = new Scanner( System.in ); When this function is called, the program stops and waits for the user to type something. When the user presses Return or Enter, the program resumes and raw_input returns what the user typed as a string. >>> input = raw_input() Anything >>> print input Some stuff

11 You can pass a string to raw_input to be displayed to the user before pausing for input: >>> name = raw_input('What is your name?\n') What is your name? Chuck >>> print name Chuck

12 EXERCISES Exercise 2.2 Write a program that uses raw_input to prompt a user for their name and then welcomes them. Enter your name: Chuck Hello Chuck Exercise 2.3 Write a program to prompt the user for hours and rate per hour to compute gross pay. Enter Hours: 35 Enter Rate: 2.75 Pay: 96.25

13 Exercise 2.4 Assume that we execute the following assignment statements: width = 17 height = 12.0 For each of the following expressions, write the value of the expression and the type (of the value of the expression). 1.width/2 2.width/2.0 3.height/3 4.1 + 2 * 5 Use the Python interpreter to check your answers. Exercise 2.5 Write a program which prompts the user for a Celsius temperature, convert the temperature to Fahrenheit and print out the converted temperature.

14


Download ppt "PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses."

Similar presentations


Ads by Google