BROCKPORT CPS 101 - Chapter 10: Program in Python

Unformatted text preview:

CPS 101 Introduction to Computational ScienceWensheng ShenDepartment of Computational ScienceSUNY BrockportChapter 10: Program in PythonIntroduction to Python ProgrammingControl StructuresFunctionsLists, Tuples and DictionariesIntroduction to the Common Gateway Interface (CGI)Object-Based Programming Customizing ClassesComputer languagesMachine languagesStreams of 0’s and 1’sDefined by the computer’s hardware designMachine-dependentAssembly languagesNeed translator programs called assemblersAssemblers convert assembly language programs to machine language High-level languagesEnable programmers to write instructions that look almost like everyday English and contain common mathematical notations.High-level languagesCompiled languagesProgramming languages that are implemented by compilers, which generate machine code from source code.Interpreted languagesProgramming languages that are implemented by interpreters, which translates the source code to another form to interpret.High-level languagesFORTRAN (FORmula TRANslator) Developed by IBM between 1954 and 1957.COBOL (Common Business Oriented Language) Developed in 1959 by a group of computer manufacturers and government and industrial computer users. C  Developed at Bell Laboratory. Language for writing operating-systems software and compilers.C++ Developed by Bjarne Stroustrup at AT&T in the early 1980s.JAVA Developed by researchers at Sun Microsystems in the early 1990s. Purely object-oriented language.The origin of C language(1) C language was developed by Dennis M. Ritchie between 1972 to 1973 at Bell Laboratory. (2) It was used to develop the famous Unix operating system in 1973 by D.M. Ritchie and K. Thompson. (3) They were awarded Turing Award in 1983.(4) They were awarded National Medal of technology from President of Clinton in 1999.The Origin of Python LanguageIt was developed by Guido van Rossum in 1989.An open-source programming languageA powerful general-purpose programming languageEffective for developing Internet and Web-based applications.Effective for database intensive applicationsEffective for client/server systems.10.1 Introduction to PythonFirst program in PythonModifying the first programAnother Python program: adding integersMemory conceptsArithmeticString formattingEquality and relational operatorsIndentation Thinking about objectsPython in WindowsInstall Python on your own computer.Add python.exe to system path variableTwo ways to execute Python statementsFile mode: Create a program, save it to a file with a .pyextension, and use the Python interpreter to execute the program in the file:python file.pyInteractive mode: Execute the Python statements interactively:the shell command line runs the Python interpreter in interactive mode.the programmer types statements directly to the interpreterthe interpreter executes the statements one at a timeFirst Program in Python# printing a line of text in Python.print (“Welcome to Python!”)•Lines beginning with the ‘#’ symbol are comments. •Comments are used to document a program and to improvethe readability of the program.•The entire line is called a statement. •A string of characters are contained by a pair of quotation marks.•Output and input in Python are accomplished with streams of characters.Save the file as example1.py, and run it using file mode.First Program in PythonPython 2.6.3 (r263rc1:75186, Oct 2 2009, 20:40:30) [MSC v.1500 32 bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> print ("Welcome to Python!“)Welcome to Python!>>>^ZThe first three lines display information about the version of Python being used. “>>>” is the Python prompt (The interpreter executes the statement when a programmer types a statement at the Python prompt and presses the Enter key.)After printing the text to the screen, the interpreter waits for the userto enter the next statement.The interpreter mode can be terminated by typing the Ctrl-Z, end-of-filecharacter on Microsoft Windows systems, and pressing the Enter key.Modifying the First Python ProgramDisplaying a single line of text with multiple statements# printing a line with multiple statements.print (“Welcome”, end = ‘ ‘)print (“to Python!”)The comma (,) and (end = ‘ ‘) at the end of a print statement tells Python not to begin a new line but instead to add a space after the string.Modifying the First Python ProgramDisplaying multiple lines of text with a single statement# printing multiple lines with a single statement.print (“Welcome\nto\n\nPython!”)A single statement can display multiple lines using newline characters. Newline characters are special characters that position the screen cursor to the beginning of the next line.Special characters are formed using the backslash (\) character.Special Characters in PythonSpecial characterDescription \nNewline. Move the cursor to the beginning of the next line\tHorizontal tab. Move the cursor to the next tab stop\r Carriage return. Move the cursor to the beginning of the current line; do not advance to the next line\b Back space. Move the cursor back one space\a Alert. Sound the system bell\\ Backslash. Print a backslash character\” Double quote. Print a double quote character\’ Single quote. Print a single quote characterHow to print a blank line?Another Python Program: Adding Integers#simple addition.#prompt user for inputinteger1 = input(“Enter first integer:\n”) #read stringinteger1 = int(integer1) #convert string to integerinteger2 = input(“Enter second integer:\n:”) integer2 = int(integer2)sum = integer1 + integer2 #compute and assign sumprint (“Sum is”, sum) #print sumInputs two integers by a user from the keyboard, computes the sum of the values and displays the result.Explanation of the Programraw_input():Python’s built-in functiontakes the argument “Enter first integer:\n”reads a stringvariables:integer1 and integer2 are variablesvariable names include letters, numbers, underscore, but cannot begin with numbersPython uses dynamic typing --- determining an object’s type during program executionint(integer1): convert string to integer“=“: assignment symbol“+”: binary operatorExample --- Dynamic Typing#Dynamics typingvalue1 = input(“Enter an integer: \n”)value2 = input(“Enter


View Full Document

BROCKPORT CPS 101 - Chapter 10: Program in Python

Download Chapter 10: Program in Python
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 10: Program in Python and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 10: Program in Python 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?