
Advanced Scientific Programming with Python at Kutztown University
Explore the advanced scientific programming course CSC 223 with Professor John Carelli at Kutztown University. Learn about prerequisites, scientific knowledge, the scientific method, scientific programming, and the application of Python for data analysis and modeling.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Advanced Scientific Programming CSC 223 Professor John Carelli Kutztown University Computer Science Department
Prerequisites You should have a working knowledge of: A basic programming language C/C++, Python, Java Specifically, a grade of C or better in csc123 or csc135 (or equivalent) It will be assumed that you understand: Variables (data types) Expressions (numerical, logical) Control structures (if/then/else, looping) Functions File access Simple data structures (like arrays) Professor John Carelli Kutztown University Computer Science Department
Science Knowledge or a system of knowledge covering general truths, or the operation of general laws, especially as obtained and tested through scientific method - Merriam Webster Professor John Carelli Kutztown University Computer Science Department
Scientific Method A systematic process used to construct an accurate and reproducible representation of the behavior of natural phenomena. Basic Steps: 1. Observe a phenomenon 2. Formulate a hypothesis to explain the phenomenon 3. Make predictions about the phenomenon based on the hypothesis 4. Test the predictions through experimentation, further observation 5. Modify the hypothesis based on results 6. Iterate Professor John Carelli Kutztown University Computer Science Department
Scientific Programming Programming can be extremely useful in the application of the scientific method. Some uses: Analyze data Visualize data Perform complex computations Create models , run simulations Perform experiments Share/publish results Professor John Carelli Kutztown University Computer Science Department
Python Developed in the late 1980 s Guido van Rossum of CWI (Netherlands) (name comes from Monty Python s Flying Circus) Designed to be modular (minimalist and fun!) Many downloadable modules available Useful for a wide variety of applications Well suited for data analysis, visualization, modeling, Professor John Carelli Kutztown University Computer Science Department
Python, cont Python is an interpreted language A Python interpreter must be installed on a computer to run Python programs Python is available in two major releases Python 2 (released in 2000) Python 3 (released in 2008) Significant differences between them Code written for one may not run in the other This course will use Python 3 Professor John Carelli Kutztown University Computer Science Department
Installing Python Python can be downloaded and installed using the Anaconda environment: Anaconda Preloaded with useful data science packages It is free! It is already installed on the CSIT classroom and lab machines Note: Python can also be installed directly from python.org Also free, but packages must be installed separately Professor John Carelli Kutztown University Computer Science Department
Basic Python Language Characteristics Python is interpreted It is dynamically typed Variable types are inferred (unlike C) It is strongly typed Variable types are not coerced (automatically changed) Strict rules on mixing data types (ex: can t add a string to a number) Professor John Carelli Kutztown University Computer Science Department
Running Python Python has 2 basic modes of operation 1. Interactive mode (REPL) Enter Python s interactive mode Type a Python statement at the prompt Hit enter to execute the statement 2. Write a complete Python script Store it in a file Execute it using the Python interpreter REPL Read the entered statement Evaluate the statement Print any resulting output Loop (repeat the process) Professor John Carelli Kutztown University Computer Science Department
Basic Interactive Mode 1. Type python to enter Python in interactive mode Windows Example 2. At the >>> prompt enter the following: print( hello world! ) (press the Enter to execute the statement) 3. Write and execute additional statements (REPL) C:\Users\carelli> python Python 3.6.8 (tags/ ] on win32 Type "help , for more information. >>> print("hello world!") hello world! >>> quit() C:\Users\carelli> * Exit python by typing quit() Professor John Carelli Kutztown University Computer Science Department
IPython $ ipython Python 3.6.8 IPython 7.16.1 -- An enhanced Interactive Python. Type '?' for help. Works like the basic python interpreter, but has additional features [ins] In [1]: print("hello world") hello world Input/Output numbering Command history Either emacs (default) or vi Syntax highlighting Auto tabs Available with Jupyter [ins] In [2]: x=2 [ins] In [3]: x Out[3]: 2 [ins] In [4]: quit() $ Professor John Carelli Kutztown University Computer Science Department
Jupyter Notebook Notebook system that allows for creation of cells More advanced than basic interactive mode Runs in a web browser Packaged with Anaconda Cells contain Python statements Can execute cells independently Useful for data science applications Also useful for embedding data visualizations We will use it with Matplotlib (plotting package) More on this later Professor John Carelli Kutztown University Computer Science Department
Creating a complete Python script The script must first be written in a text editor or an IDE Text editors: Nano, Emacs, Vim (Unix/Linux) Notepad, Word (Windows) TextEdit (Mac) IDE: Integrated Development Environment Contains integrated text editing, debugging, and execution Idle, PyCharm, Atom, Visual Studio (Windows), Xcode (Mac), Professor John Carelli Kutztown University Computer Science Department
Running a self-contained Python script Store an entire script (program) in a file Convention is to name the file with a .py extension Ex: miles.py (see next slides) At the command prompt, invoke the python interpreter and supply the file name as an argument Ex: python miles.py Professor John Carelli Kutztown University Computer Science Department
Overview of a Python Script # # miles.py # - converts distance in miles to kilometers. # miles.py this example illustrates some common elements in a Python script Comments Statements Assignments Expressions Functions # conversion factor (kilometers in a mile) KM_PER_MILE= 1.609 # get the distance in miles miles= input("Enter the distance in miles: ") # calculate the kilometers # note: convert the miles string to a number kms= KM_PER_MILE * float(miles) # output the results print("The distance in miles is:",miles) print("The distance in kilometers is:",kms) Professor John Carelli Kutztown University Computer Science Department
Comments Start with hashtag character (#) extends to the end of a line ignored by the interpreter There is no facility for multiline comments although triple quoted strings are often used for that purpose Can use single or double quotes This is a Triple quoted string # # miles.py # - converts distance in miles to kilometers. # # conversion factor KM_PER_MILE= 1.609 # km in a mile # get the distance in miles miles= input("Enter the distance in miles: ") # calculate the kilometers # note: convert the miles string to a number kms= KM_PER_MILE * float(miles) # output the results print("The distance in miles is:",miles) print("The distance in kilometers is:",kms) Professor John Carelli Kutztown University Computer Science Department
Statements A statement is the actual Python code that does something Statements can contain: Assignments Store information Using variables and = Expressions Evaluate something calls to functions Professor John Carelli Kutztown University Computer Science Department
More on Statements Statements are terminated by the end of a line Statements can be continued on a subsequent line by either: putting a backslash at the end of the line X = 1 + 2 +3 + \ 4 + 5 + 6 enclosing the expression in parenthesis X = (1 + 2 +3 + 4 + 5 + 6) Can put multiple statements on a line by terminating with a semicolon first= 1; second= 2 BUT this is discouraged!!! # # miles.py # - converts distance in miles to kilometers. # # conversion factor (kilometers in a mile) KM_PER_MILE= 1.609 # get the distance in miles miles= input("Enter the distance in miles: ") # calculate the kilometers # note: convert the miles string to a number kms= KM_PER_MILE * float(miles) # output the results print("The distance in miles is:",miles) print("The distance in kilometers is:",kms) Professor John Carelli Kutztown University Computer Science Department
Assignments # # miles.py # - converts distance in miles to kilometers. # Assignments are statements in which information is stored for future retrieval and use Information is stored using a named memory location - a variable KM_PER_MILE, miles, kms The variable is assigned its value using the variable name and = (also called the assignment operator) # conversion factor (kilometers in a mile) KM_PER_MILE= 1.609 # get the distance in miles miles= input("Enter the distance in miles: ") # calculate the kilometers # note: convert the miles string to a number kms= KM_PER_MILE * float(miles) # output the results print("The distance in miles is:",miles) print("The distance in kilometers is:",kms) Professor John Carelli Kutztown University Computer Science Department
Expressions # # miles.py # - converts distance in miles to kilometers. # Expressions are portions of a statement in which a calculation or evaluation of some sort is performed For example, a mathematical calculation # conversion factor (kilometers in a mile) KM_PER_MILE= 1.609 # get the distance in miles miles= input("Enter the distance in miles: ") # calculate the kilometers # note: convert the miles string to a number kms= KM_PER_MILE * float(miles) # output the results print("The distance in miles is:",miles) print("The distance in kilometers is:",kms) Professor John Carelli Kutztown University Computer Science Department
Function calls # # miles.py # - converts distance in miles to kilometers. # A function contains pre-written code that can be re-used in programs to perform specific tasks To use a function, it must be called or invoked by name Information is passed to the function within the required parenthesis. # conversion factor (kilometers in a mile) KM_PER_MILE= 1.609 # get the distance in miles miles= input("Enter the distance in miles: ") # calculate the kilometers # note: convert the miles string to a number kms= KM_PER_MILE * float(miles) In miles.py input() : gets information from the user print() : displays results to the screen float() : type conversion (more later ) # output the results print("The distance in miles is:",miles) print("The distance in kilometers is:",kms) Professor John Carelli Kutztown University Computer Science Department
Indentation All statements within a block of code must be indented by the same amount* Strictly enforced by the interpreter Syntax error otherwise! if age >= 16: print( You can drive! ) if age >= 18: print( You can also vote! ) Blocks of code always begin with a statement that ends with a colon Used to group statements together Ex: loops, conditionals, function definitions * Style guides often recommend indenting with 4 spaces Blocks nested within blocks require additional indentation Professor John Carelli Kutztown University Computer Science Department
Whitespace Aside from the block indentation requirement, whitespace within a line is ignored The following are all equivalent y = x + 1 y=x+1 y = x + 1 Generally, style guides recommend one space around binary operators and no space around unary operators Whatever style you adopt, be consistent Professor John Carelli Kutztown University Computer Science Department
Syntax vs. semantics Syntax refers to the structure of the language, i.e. what constitutes legal statements Semantics refer to the meaning of a legal program (A syntactically correct program can be semantically incorrect!) Professor John Carelli Kutztown University Computer Science Department
Variables A variable is a named container for a data value It binds a value to a name Variables are created automatically with an assignment statement x= 10 # assign the value 10 to the variable x Data type is automatically inferred x = 10 # this is an integer msg = hello # this is a string Professor John Carelli Kutztown University Computer Science Department
Objects Objects have two principal features Attributes (used to store internal data) Methods (functions, used to manipulate the data) Objects can be stored in variables The dot operator can be used with the variable name to access methods >>> x="hello >>> x.upper() 'HELLO' Everything in Python is an object Professor John Carelli Kutztown University Computer Science Department