CPS 101 Introduction to Computational ScienceWensheng ShenDepartment of Computational ScienceSUNY BrockportChapter 10: Program in PythonIntroduction to Python ProgrammingControl StructuresFunctionsLists, Tuples and DictionariesIntroduction to the Common Gateway Interface (CGI)Object-Based Programming Customizing ClassesComputer languagesMachine languagesStreams of 0’s and 1’sDefined by the computer’s hardware designMachine-dependentAssembly languagesNeed translator programs called assemblersAssemblers convert assembly language programs to machine language High-level languagesEnable programmers to write instructions that look almost like everyday English and contain common mathematical notations.High-level languagesCompiled languagesProgramming languages that are implemented by compilers, which generate machine code from source code.Interpreted languagesProgramming languages that are implemented by interpreters, which translates the source code to another form to interpret.High-level languagesFORTRAN (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 LanguageIt was developed by Guido van Rossum in 1989.An open-source programming languageA powerful general-purpose programming languageEffective for developing Internet and Web-based applications.Effective for database intensive applicationsEffective for client/server systems.10.1 Introduction to PythonFirst program in PythonModifying the first programAnother Python program: adding integersMemory conceptsArithmeticString formattingEquality and relational operatorsIndentation Thinking about objectsPython in WindowsInstall Python on your own computer.Add python.exe to system path variableTwo ways to execute Python statementsFile 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.pyInteractive mode: Execute the Python statements interactively:the shell command line runs the Python interpreter in interactive mode.the programmer types statements directly to the interpreterthe 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 ProgramDisplaying 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 ProgramDisplaying 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