Programming with Python 1

Similar documents
CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Computer Basics 1/6/16. Computer Organization. Computer systems consist of hardware and software.

Computer Basics 1/24/13. Computer Organization. Computer systems consist of hardware and software.

Lecture 1 - Introduction (Class Notes)

Software Development. Integrated Software Environment

Topics. Hardware and Software. Introduction. Main Memory. The CPU 9/21/2014. Introduction to Computers and Programming

Chapter 2: Operating-System Structures

CSSE Operating Systems. Project A Booting, Getting Started

Why Operating Systems? Topic 3. Operating Systems. Why Operating Systems? Why Operating Systems?

C H A P T E R 1. Introduction to Computers and Programming

Python for Earth Scientists

CS Prof J.P.Morrison

Chapter 2. Operating-System Structures

Computer Principles and Components 1

LABORATORY OF DATA SCIENCE. Python & Spyder- recap. Data Science & Business Informatics Degree

last time in cs recitations. computer commands. today s topics.

Introduction to MATLAB

Week - 01 Lecture - 04 Downloading and installing Python

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

Elements of Computers and Programming Dr. William C. Bulko. What is a Computer?

Getting Started. Chapter 1. Java Programming FROM THE BEGINNING. Chapter 1: Getting Started

assembler Machine Code Object Files linker Executable File

Introduction to C Programming. What is a C program?

Episode 1 Using the Interpreter

Chapter 2: Operating-System Structures

Python Programming: An Introduction to Computer Science

CS 177 Recitation. Week 1 Intro to Java

Chapter 2: Operating-System Structures. Operating System Concepts 9 th Edition

Chapter 2: Operating-System Structures

National 5 Computing Science Software Design & Development

Integrated Software Environment. Part 2

Introduction to Python

Chapter 1: Introduction to Computers and Programming

Introduction to Computer Programming for Non-Majors CSC 2301, Fall The Department of Computer Science

Chapter 1: Introduction to Computers and Java

8/23/2014. Chapter Topics. Introduction. Java History. Why Program? Java Applications and Applets. Chapter 1: Introduction to Computers and Java

CHAPTER 2: SYSTEM STRUCTURES. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

Introduction to Python Part 1. Brian Gregor Research Computing Services Information Services & Technology

Introduction to Python Part 2

GLY Geostatistics Fall Lecture 2 Introduction to the Basics of MATLAB. Command Window & Environment

Working with Shell Scripting. Daniel Balagué

1Z Oracle Linux Fundamentals (Oracle Partner Network) Exam Summary Syllabus Questions

CS 390 Software Engineering Lecture 3 Configuration Management

PYTHON YEAR 10 RESOURCE. Practical 01: Printing to the Shell KS3. Integrated Development Environment

Programming: detailed instructions which tell the computer hardware what to do aka software Computer Science: the study NOT of computers, but of what

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

What are some common categories of system calls? What are common ways of structuring an OS? What are the principles behind OS design and

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

8/16/12. Computer Organization. Architecture. Computer Organization. Computer Basics

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Chapter 2: Operating-System Structures

Software Lesson 1 Outline

CHAPTER 1 Introduction to Computers and Java

COSC 490 Computational Topology

Week Two. Arrays, packages, and writing programs

CPS109 Lab 1. i. To become familiar with the Ryerson Computer Science laboratory environment.

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

Game keystrokes or Calculates how fast and moves a cartoon Joystick movements how far to move a cartoon figure on screen figure on screen

The current topic: Python. Announcements. Python. Python

Science One CS : Getting Started

Part III Appendices 165

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

II Esercitazione: S.O. & Fortran 90

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Introduction to Computers and Visual Basic.Net Pearson Education, Inc. All rights reserved.

Programming with Python

CME 193: Introduction to Scientific Python Lecture 1: Introduction

Principles of computer programming. Profesor : doc. dr Marko Tanasković Assistent : doc. dr Marko Tanasković

Functions!!! Why functions? Functions provide good way to design systems!

CSC116: Introduction to Computing - Java

Chapter 1: Why Program? Computers and Programming. Why Program?

Chapter 2: Operating-System Structures. Operating System Concepts Essentials 8 th Edition

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Lecture C1 Data Representation. Computing and Art : Nature, Power, and Limits CC 3.12: Fall 2007

Molecular Statistics Exercise 1. As was shown to you this morning, the interactive python shell can add, subtract, multiply and divide numbers.

Chapter 1: Why Program? Main Hardware Component Categories 8/23/2014. Main Hardware Component Categories: Why Program?

Chapter 2: Operating-System

Computer Organization & Assembly Language Programming

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Programming 1. Lecture 1 COP 3014 Fall August 28, 2017

Computational Physics Compiling a C++ program

Java Programming. Zheng-Liang Lu. Java 308 Spring Department of Computer Science & Information Engineering National Taiwan University

CMSC131. Hardware and Software. Hardware

CMSC131. Hardware and Software

Notepad++ The COMPSCI 101 Text Editor for Windows. What is a text editor? Install Python 3. Installing Notepad++

PatternFinder is a tool that finds non-overlapping or overlapping patterns in any input sequence.

Software Project. Lecturers: Ran Caneti, Gideon Dror Teaching assistants: Nathan Manor, Ben Riva

Laboratory Exercise #0

Optimized Scientific Computing:

Operating System Services. User Services. System Operation Services. User Operating System Interface - CLI. A View of Operating System Services

Software Concepts. It is a translator that converts high level language to machine level language.

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Programming 1. Lecture 1 COP 3014 Fall August 28, 2018

VIP Documentation. Release Carlos Alberto Gomez Gonzalez, Olivier Wertz & VORTEX team

introduction week 1 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 20 imperative programming about the course

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Lecture 1: What is a computer?

ENERGY 211 / CME 211. Evolution

Cosmology with python: Beginner to Advanced in one week. Tiago Batalha de Castro

Week One: Introduction A SHORT INTRODUCTION TO HARDWARE, SOFTWARE, AND ALGORITHM DEVELOPMENT

Transcription:

Programming with Python 1 NITARP 2013: SHIPs Babar Ali Python on SHIPs 1 1

Topics Computer programs What is a program? Why program? Not useful programming Some Introductory Concepts: Components of a program Memory: Variables and Disks Current working directory Python, Anaconda, Spyder Python on SHIPs 1 2

PROGRAMS Python on SHIPs 1 3

What is a program? Merriam- Webster online dic2onary Python on SHIPs 1 4

What is a program? Its how you tell a computer what you would like it (computer) to do for you. However At the fundamental (lowest) level, computers speak assembly language. Assembly language is: Specific to the computer architecture Difficult for (most) humans Really only useful for the most fundamental operatons (e.g. clean memory register XXXNNFF) Python on SHIPs 1 5

NASM code to print hello world Python on SHIPs 1 6

Normal Humans Need Translators High- level programming languages invented to make it easier to talk to machines. But, at the expense of efficiency, and some other aspects not really our concern. High- level languages: Provide a bridge between humans and computer s preferred assembly language level Are not specific to machine architecture Require a compiler to create assembly object Python on SHIPs 1 7

Hello World in C /* Hello World program */ #include<stdio.h> main() { prinh("hello World"); } Python on SHIPs 1 8

Hello World in C But, the instructons must be compiled (read translated) from C- language to assembler Here s one way: gcc - o helloworld - ansi helloworld.c As long as gcc compiler exists on a machine, you can write and run C- code on it The resultng assembly language instructon will differ from machine to machine Python on SHIPs 1 9

Two flavors of high- level languages Compiled Require code to be compiled to run Examples: C, C++, FORTRAN, Pros: Faster, more control, more flexible Cons: Harder, and compiling gets cumbersome ScripHng Interpreter/compiler is always ON just run it. Examples: Python, Perl Pros: Easier to use, More portable Cons: SLOOWWW, less flexibility Python on SHIPs 1 10

Hello World in Python print Hello world Python on SHIPs 1 11

How does one create a program? High- level programs and scripts are simply wriuen as ASCII (a.k.a text) files. Simple programs may only use one file for all of their instructons. E.g. print Hello World Complex programs rely on system engineering concepts to organize 100s or 1000s+ files. Python on SHIPs 1 12

More on WriHng Programs You can use simple text editors: vi, textedit (MacOS), notepad (Windows), emacs, etc. You can also use applicatons specifically designed to help you write programs. Will provide automatc checks for the fundamentals: E.g. parenthesis, typos Use color- coding to highlight specific types or blocks of code. Will automatcally perform basics like indentaton And much, much more (to be covered in Spyder) Python on SHIPs 1 13

How do you execute, run programs? First, the format that is ready for executon is called an executable. An executable is usually a compiled (remember translated) version of the program for compiled languages. The equivalent for scriptng languages is simply the script itself. Python on SHIPs 1 14

Running programs, cont. Many ways to start: Command line. Usually, Unix, Linux, type systems Double click. Tap (an app on a ipod, smart phone) Issue the proper command in the development environment. Instructed by other programs at the basic level. And, many more. For SHIPs, We will (usually) rely on python development environmen (Spyder) Python on SHIPs 1 15

Jargon Code usually refers to instructons for a compiled or scriptng computer language. Script usually refers to scriptng language code. Program usually refers to compiled language code. Run = execute Have the computer carry out the instructons in your code Executable Format in which your code is able to run. Script for scriptng language and translated assembly language code for compiled language. Python on SHIPs 1 16

More Jargon Bug A fault in the computer code. As simple as a typo to as complex as fault in programming logic Debug The process by which you identfy and remove the bugs. Or, the command to do so. Portable Able to run on many types of operatng system + machine architecture combinaton. 100% portability is a myth. Python on SHIPs 1 17

Yet More Jargon FuncTon in this context. A basic programming unit. You write a functon to do one thing (usually). Library in this context. An organized collecton of programs, i. e. functons that is (usually) focused on a specific topic. Example: CFITSIO is a set of C- language functons devoted to FITS format input and output calls. App An executable Comment Line(s) in the code that the compiler or interpreter (for scripts) skips and are meant only for humans. Python on SHIPs 1 18

Why program? Use the power of computers to do just about anything. Specifically for SHIPs: Manipulate and use data that are tedious or impossible to do by hand (on a calculator, say). Computers are faster. Minimize errors: Computers are good at math (intel PENTINUM issues not withstanding). Repeat the same operaton many Tmes for a given star on a multple stars Python on SHIPs 1 19

SHIPs specific use cases Read an APT output table file into memory. Write a DS9 region file using a table of either (x,y) or (ra,dec) positons and ID. Read SHIPs ASCII format data table. Write SHIPs ASCII format data table. Merge photometry tables. Create 2- dimensional plots of various types: scauer plots, line plots, histograms. These plots may have multple data sets on them. Save plots to a JPG, PNG or similar graphic format file. Python on SHIPs 1 20

Not useful programming Obviously, anything that does not involve computers. Otherwise, few instances when dealing with computers do not benefit from programming: WriTng large chunks of text such as a novel, unless the text can be auto- generated. Anything that requires interactvity (web browsing, computer games) Python on SHIPs 1 21

SOME INTRODUCTORY CONCEPTS Python on SHIPs 1 22

Elements of a program Simply, A series of instructons for the computer. Python on SHIPs 1 23

A simple program InstrucTons contained (usually) in a single file. Typically, Write a functon to do a fundamental manipulaton or operaton Write an overseeing program to call and execute functons in a set order or logic Most of SHIPs programs should fall in this category Python on SHIPs 1 24

Adding complexity Users are prompted for input. Read data, instructons from a separate file Execute functons based on conditons. Do many, many, many things instead of a single one. Incorporate Graphical User Interfaces (GUIs). Python on SHIPs 1 25

Good prachces Comment o}en and be verbose. Imagine talking to yourself 5+ years from writng the code. Test and debug as much as possible. If using the same operaton over and over, define a functon. Define and use naming conventons for variables, files. (for SHIPs) keep the logic simple and easily readable even at the expense of efficiency. Organize code in logical blocks that are clearly separated. Develop and use an organizaton scheme for code that spans 10s+ files. Separate Code from Data Python on SHIPs 1 26

Memory There are Two types: Random Access Memory (RAM). Disk/physical Storage. RAM is live memory only available when computers are ON. Disks store informaton using some semi- permanent physical mechanism that is not dependent on power. Python on SHIPs 1 27

Memory and programs Programs rely on RAM for faster executon. Programs rely on Disks for permanent storage. When the program is running, RAM is being used. Disks are (usually) used only for storage of data/informaton. Python on SHIPs 1 28

Variables in memory Variables, in computer jargon, are elements of a programming language that deal with RAM to hold data during executon. There are many different types of variables ranging from byte (least memory) to quadruple precision arrays (most memory) to store precise numerical values. Good practce: minimize memory consumpton = use variable types appropriate for the job. Python on SHIPs 1 29

Disks Programs use physical disk space to store data and/or results in files. Accessing disks is 100s- 1000s of Tmes slower than accessing RAM. It s a bad idea to use files instead of variables for memory during executon. It s a bad idea to use variables instead of files to store final results variables cease to exist as soon as the program terminates. Python on SHIPs 1 30

Exercise NGC 281 Level 1 Herschel/PACS frames for the blue channel comprises about 150,000 images. Each image has 2048 pixels (70 micron data). Each pixel data is 64- bits = 8 Bytes in HIPE. Calculate the size (in Gigabytes) of the variable in RAM that holds the level 1 cube. Calculate the size of the FITS files on disk that holds the data, assuming that Only 16- bits = 2 bytes are used for storage. FITS format compresses data by a factor of 2. Python on SHIPs 1 31

Current Working Directory When reading or writng (storing) files, the computer reads, writes to a given folder. This folder is the current working directory. ScripTng or compiling languages have different assumptons for where the current working directory lies. Default is (usually) where you started executng the program. Best practce: Define it explicitly in your program. Python on SHIPs 1 32

USING PYTHON FOR SHIPS Python on SHIPs 1 33

Why Python? Python is a scriptng language: Easier to program than compiling languages. Example: Define the variable a and set it to contain the value 10.2 in memory float a; a = 10.2; a = 10.2 In C: First declare the variable, then use it. In Python: Simply use it. Python on SHIPs 1 34

Which Python? Barebones python is not a practcal choice. A pre- packaged distributon solves a number of issues: Useful libraries are already included and compiled. Version conflicts between libraries are avoided. Python on SHIPs 1 35

Anaconda hup://contnuum.io/downloads.html Its free. Windows, MacOS, and Linux support. numpy, scipy libraries included. plo~ng libraries included. Many (not all) useful astronomy libraries are included. Python on SHIPs 1 36

CollaboraHve Development Use and exchange code amongst team members. Issues for SHIPs code: Where do we store code? How do we know this is the correct version? Who to ask questons? Python on SHIPs 1 37