Course May 18, Advanced Computational Physics. Course Hartmut Ruhl, LMU, Munich. People involved. SP in Python: 3 basic points
|
|
- Elwin Sharp
- 6 years ago
- Views:
Transcription
1 May 18, I/O
2 3 I/O 3 I/O
3 3 ASC, room A 238, phone , hartmut.ruhl@lmu.de Patrick Böhl, ASC, room A205, phone , patrick.boehl@physik.uni-muenchen.de. I/O
4 Scientific programming in Python: 3 basic points 3 There are 3 (simplified) things you should keep in mind when considering to use Python. They are: 1. Python supports lazy programmers. 2. Python performance is generally poor. 3. Python can be bloody dangerous. I/O
5 Scientific programming in Python: The cheapest possible program in C: #include <stdio. h> i n t main ( ) { p r i n t f ( Hello World \n ) ; return 0; } And the same thing written in Python: p r i n t Hello World See point 1. 3 I/O
6 Scientific programming in Python: Python supports the usual basic datatypes: int, float, bool and string. The int datatype in Python has dynamic size, there is no maximum integer number. Float is equivalent to double in C. Bool and string work as expected from C. Usage of datatypes in Python is dynamic and handled by compiler. There is no explicit declaration and the type of a variable may change at almost any point within the program. See point 3. If forced conversion between types is required, functions int() and float() are provided. bacon =3.0 # f l o a t eggs=3 # i n t e g e r spam= spam # s t r i n g p r i n t eggs /10 # = 0 p r i n t bacon /10 # = 0.3 p r i n t spam+1 # undefined, terminates program p r i n t spam * 3 # = spamspamspam p r i n t f l o a t (spam ) # t h i s w i l l crash print f l o a t ( eggs ) # 3.0 eggs=spam # eggs i s now s t r i n g c o n t a i n i n g spam 3 I/O
7 Scientific programming in Python: Strings and lists Arrays are called lists in Python. Unlike most other languages, Python supports different datatypes within a single list. See point 3. List indices use the C convention, running from 0 to size-1. Like C++ STL vectors, Python lists come with a bunch of useful functions (see example below). A Python string can be treated as a list of characters, if desired. It has some additional functionality though. bacon = [ ] #empy l i s t of size 0 spam=[ spam ] * 10 # l i s t of size 10, c o n t a i n i n g spam eggs=range (10) # l i s t of integers from 0 to 9 sausage =[42.0, True, because we can ] # mixed l i s t eggs. s o r t ( ) # what i t says sausage. s o r t ( ) # t h i s a c t u a l l y works len ( sausage ) # returns number of elements in l i s t sausage. append ( 7 ) #adds 7 to end of sausage del sausage [ 1 ] # removes element [ 1 ] from l i s t, decreasing length s= Hello World s. count ( l ) # = 3 s. f i n d ( l l ) # =2, f i r s t p o s i t i o n of l l i n s t r i n g s=s. replace ( World, Spam ) s=s. upper ( ) # i n case you r e a l l y need t h a t capslock p r i n t s 3 I/O
8 Scientific programming in Python: Control statements As usual, Python code is executed from top to bottom. Unlike C, code is not compiled as a whole before execution. If your code contains an invalid expression, everything before that point will still be executed. See point 3. Comments use #, obviously. No multi-line comments like /*stuff*/ provided. A main function is not required, if you really want you can hack one in though. There is no goto. Python uses indentation to separate code levels instead of C style brackets like {this}. Hitting the tab key is not an aesthetic choice here. The usual while and for loops are provided, as well as if-elif-else. While works exactly as expected from C, but for and if got some new tricks (see below). The command break can be used to exit any of these. x =[3,6,19,21,71,101] for i in x : # takes value for i from specified array x i f ( i * 2==42): p r i n t gotcha, found, i break for i in range ( 1 0 ) : # somewhat more t r a d i t i o n a l f o r loop p r i n t i #from 0 to 9 i f (21 in x ) : # t h i s makes searching s t u f f r a t h e r t r i v i a l p r i n t gotcha again at index, x. index (21) 3 I/O
9 Scientific programming in Python: Python functions are call by value when passing single objects as arguments, but call by reference when passing lists. See point 3. These functions can return any number of values of any type. Both amount and type of function returns may be dynamically determined by passed arguments. See point 3. Multiple values are returned as list, which means individual values can by accessed via func()[i]. See example below for syntax of function definition. def func ( a ) : i f ( a>0): return ( a ) else : return ( spam, 7. 3 ) p r i n t func ( 1 ) # 1 p r i n t func ( 1) # ( spam, ) p r i n t func ( 1 ) [ 0 ] # spam p r i n t func ( 1 ) # 1, see point 3. 3 I/O
10 Scientific programming in Python: I/O Terminal output is accomplished by the print function. C style formating flags are available. User input is handled by input() and raw input(). The former tries to read the input as a number, the later treads all data as string. File handling for both formated and binary files is provided by open(). Optional arguments are rwab for read, write, append and binary. Default is r. As a nice bonus, Python can directly open network resources with urlopen(). import u r l l i b # required for urlopen below import os # required for f i l e removal p r i n t %4.2 f i s a %s number %(1.0e3 / 3. 0, long ) x=input ( some number : ) # t h i s w i l l crash when e n t e r i n g n i! y=raw input ( anything : ) # i n d e s t r u c t i b l e f i l e =open (. / s t u f f. dat, wa ) # create f i l e with w r i t e access f i l e. w r i t e ( y ) f i l e. close ( ) os. remove (. / s t u f f. dat ) # d e l e te f i l e, see p o i n t 3 f i l e = u r l l i b. urlopen ( h t t p : / / www. google. com ) content= f i l e. r e a d l i n e s ( ) # s t u f f s content of f i l e i n t o l i s t of s t r i n g s f i l e. close ( ) p r i n t content # the google html code, in t h i s case 3 I/O
11 Scientific programming in Python: are dynamic in Python as well. Objects inside a class do not have to be defined and can be created at any point. To avoid point 3, it is good practice to use the constructor function init () of the class to create all objects within. Unlike C structs, Python classes can contain functions without having to hack them in via pointers. The first argument to any class function is always self, which points to the object itself. This argument is mandatory even when not actually used inside the function. Output of python objects as formated strings is handled via the str () function supplied by the class. Do not expect favorable results if this function is not implemented. class somevector ( object ) : def i n i t ( s e l f ) : # t h a t s 2 underscores each s e l f. x =0.0 s e l f. y =0.0 s e l f. z =0.0 def s t r ( s e l f ) : return ( (%s %s %s ) %( s e l f. x, s e l f. y, s e l f. z ) ) a=somevector ( ) #what you intended b=somevector #b i s now copy of class d e f i n i t i o n a. x=3.0 #sets x in a to 3.0 b. x =3.0 # t h i n k about t h i s f o r a moment c=b ( ) #same as c=somevector ( ) p r i n t a # ( ) p r i n t b # <class m a i n. somevector > p r i n t c # ( ) 3 I/O
12 Scientific programming in Python: Additional Python packages for various functionalities can by used in your code with the import command. You have already seen some of those (urllib, os) in the examples. from packages are called with package.function(). Most of these are not part of the standard Python distribution. They are not hard to find though. math: Equivalent to cmath, math.h or whatever C math lib you use. numpy: High performance arrays, matrix manipulation, statistics, fourier transforms, polynomial roots, etc. Quite handy. scipy: If something you need is not in numpy, look here. pylab: Used for data visualization, can do graphs, histograms, etc. random: Random number generators 3 I/O
13 Scientific programming in Python: Example 1 The first example uses performance optimized numpy arrays to create a gauss curve and write data to both binary and formated files. import numpy import math var =1.0 x=numpy. arange ( 10,10,0.001) # fast array with fixed bounds and stepsize y = ( 1. 0 / ( var * math. s q r t ( 2. 0 * math. p i ) ) ) * numpy. exp ( 0.5 * x * x / var * * 2 ) #some curve # n o t i c e how we applied the exp f u n c t i o n to the whole array x at once x. t o f i l e ( gauss binary. dat ) # array to b inary f i l e y. t o f i l e ( g a u s s a s c i i. dat, sep= \n, format= %e ) # array to formated f i l e 3 I/O
14 Scientific programming in Python: Example 2 Here we read both datasets from the last example and plot one over the other. import numpy import pylab x=numpy. f r o m f i l e ( gauss binary. dat, dtype= f l o a t ) y=numpy. f r o m f i l e ( g a u s s a s c i i. dat, sep= \n ) pylab. p l o t ( x, y ) # p l o t data pylab. show ( ) # d i s p l a y p l o t 3 I/O
15 Scientific programming in Python: Example 3 This example creates a box shaped potential, Fourier transforms it and plots both graphs side by side. import pylab import numpy x=numpy. arange ( 10,10,0.001) #x array from 100 to 99, stepsize 0.01 y=numpy. z e r o s l i k e ( x ) # array l i k e x, but f i l l e d with 0.0 y [ ( x> 5.0) & ( x <5.0)] = 1.0 #above l i n e is equivalent to : # f o r i i n range ( y. s ize ) : # i f ( ( x [ i ]> 5.0) and ( x [ i ] <5.0) ) : # y [ i ]=1.0 pylab. subplot ( 2, 1, 1 ) # 2 columns, 1 row, 1 s t p l o t pylab. p l o t ( x, y ) pylab. ylim ( [ 0, 2 ] ) # y range of t h i s subplot f f t =numpy. f f t. f f t ( y ) # f o u r i e r transform of y f r e q =numpy. f f t. f f t f r e q ( len ( x ), d=0.001) # frequencies as array f o r x axis pylab. subplot ( 2, 1, 2 ) # 2 colums, 1 row, 2nd p l o t pylab. p l o t ( freq, numpy. abs ( f f t ) ) pylab. xlim ( [ 3,3]) # x range pylab. show ( ) 3 I/O
16 3 I/O
Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur
Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture 04 Programs with IO and Loop We will now discuss the module 2,
More informationIntroduction to Python (All the Basic Stuff)
Introduction to Python (All the Basic Stuff) 1 Learning Objectives Python program development Command line, IDEs, file editing Language fundamentals Types & variables Expressions I/O Control flow Functions
More informationGetting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013
CSE 231, Rich Enbody Office Hours After class By appointment send an email 2 1 Project 1 Python arithmetic Do with pencil, paper and calculator first Idle Handin Help room 3 What is a Computer Program?
More informationIntro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming
Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals
More informationOverview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program
Overview - General Data Types - Categories of Words - The Three S s - Define Before Use - End of Statement - My First Program a description of data, defining a set of valid values and operations List of
More informationPython Working with files. May 4, 2017
Python Working with files May 4, 2017 So far, everything we have done in Python was using in-memory operations. After closing the Python interpreter or after the script was done, all our input and output
More informationIntroduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C
Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #29 Arrays in C (Refer Slide Time: 00:08) This session will learn about arrays in C. Now, what is the word array
More informationVariables and numeric types
s s and numeric types Comp Sci 1570 to C++ types Outline s types 1 2 s 3 4 types 5 6 Outline s types 1 2 s 3 4 types 5 6 s types Most programs need to manipulate data: input values, output values, store
More informationA Fast Review of C Essentials Part I
A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types
More information1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19
1 Classes 2 Exceptions 3 Using Other Code 4 Problems Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, 2009 1 / 19 Start with an Example Python is object oriented Everything is an object
More informationPython in 10 (50) minutes
Python in 10 (50) minutes https://www.stavros.io/tutorials/python/ Python for Microcontrollers Getting started with MicroPython Donald Norris, McGrawHill (2017) Python is strongly typed (i.e. types are
More informationIntroduction to Python
Introduction to Python Jon Kerr Nilsen, Dmytro Karpenko Research Infrastructure Services Group, Department for Research Computing, USIT, UiO Why Python Clean and easy-to-understand syntax alldata = cpickle.load(open(filename1,
More informationWhat advantages has it?
by What advantages has it? The Reasons for Choosing Python Python is free It is object-oriented It is interpreted It is operating-system independent It has an excellent optimization module It offers modern
More informationIntroduction to Python Code Quality
Introduction to Python Code Quality Clarity and readability are important (easter egg: type import this at the Python prompt), as well as extensibility, meaning code that can be easily enhanced and extended.
More informationT H E I N T E R A C T I V E S H E L L
3 T H E I N T E R A C T I V E S H E L L The Analytical Engine has no pretensions whatever to originate anything. It can do whatever we know how to order it to perform. Ada Lovelace, October 1842 Before
More informationComputational Programming with Python
Numerical Analysis, Lund University, 2017 1 Computational Programming with Python Lecture 1: First steps - A bit of everything. Numerical Analysis, Lund University Lecturer: Claus Führer, Alexandros Sopasakis
More informationC introduction: part 1
What is C? C is a compiled language that gives the programmer maximum control and efficiency 1. 1 https://computer.howstuffworks.com/c1.htm 2 / 26 3 / 26 Outline Basic file structure Main function Compilation
More informationReview Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts
Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts Simple Data Types There are a number of data types that are considered primitive in that they contain only a single value. These data
More informationWorksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords
Worksheet 1: Introductory Exercises Turtle Programming Calculations The Print Function Comments Syntax Semantics Strings Concatenation Quotation Marks Types Variables Restrictions on Variable Names Long
More informationLanguage Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science
Language Design COMS W4115 Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Language Design Issues Syntax: how programs look Names and reserved words Instruction
More informationC++ for Python Programmers
C++ for Python Programmers Adapted from a document by Rich Enbody & Bill Punch of Michigan State University Purpose of this document This document is a brief introduction to C++ for Python programmers
More informationIntroduction to Python! Lecture 2
.. Introduction to Python Lecture 2 Summary Summary: Lists Sets Tuples Variables while loop for loop Functions Names and values Passing parameters to functions Lists Characteristics of the Python lists
More information1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman
1. BASICS OF PYTHON JHU Physics & Astronomy Python Workshop 2017 Lecturer: Mubdi Rahman HOW IS THIS WORKSHOP GOING TO WORK? We will be going over all the basics you need to get started and get productive
More informationPython for Scientists
High level programming language with an emphasis on easy to read and easy to write code Includes an extensive standard library We use version 3 History: Exists since 1991 Python 3: December 2008 General
More informationComputer Science 121. Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans
Computer Science 121 Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans 3.1 The Organization of Computer Memory Computers store information as bits : sequences of zeros and
More informationPart I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming
2017 Table of contents 1 2 Integers and floats Integer int and float float are elementary numeric types in. integer >>> a=1 >>> a 1 >>> type (a) Integers and floats Integer int and float
More informationProgramming with Python
Programming with Python Dr Ben Dudson Department of Physics, University of York 21st January 2011 http://www-users.york.ac.uk/ bd512/teaching.shtml Dr Ben Dudson Introduction to Programming - Lecture 2
More informationIntroduction to Computer Programming in Python Dr. William C. Bulko. Data Types
Introduction to Computer Programming in Python Dr William C Bulko Data Types 2017 What is a data type? A data type is the kind of value represented by a constant or stored by a variable So far, you have
More informationIntro. Scheme Basics. scm> 5 5. scm>
Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if
More informationSolution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors
Solution sheet 1 Introduction Please note that there can be other solutions than those listed in this document. This is a literate Haskell file which is available as PDF, as well as literate Haskell source
More informationThere are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)
I. INTERACTIVE MODE VERSUS SCRIPT MODE There are two ways to use the python interpreter: interactive mode and script mode. 1. Interactive Mode (a) open a terminal shell (terminal emulator in Applications
More informationCS S-02 Python 1. Most python references use examples involving spam, parrots (deceased), silly walks, and the like
CS662-2013S-02 Python 1 02-0: Python Name python comes from Monte Python s Flying Circus Most python references use examples involving spam, parrots (deceased), silly walks, and the like Interpreted language
More informationWeek - 01 Lecture - 04 Downloading and installing Python
Programming, Data Structures and Algorithms in Python Prof. Madhavan Mukund Department of Computer Science and Engineering Indian Institute of Technology, Madras Week - 01 Lecture - 04 Downloading and
More informationOnion Language Reference Manual. By: Andrew Aday, (aza2112) Amol Kapoor (ajk2227), Jonathan Zhang (jz2814)
Onion Language Reference Manual By: Andrew Aday, (aza2112) Amol Kapoor (ajk2227), Jonathan Zhang (jz2814) Introduction 3 Types 3 Lexical Conventions 4 Identifiers 4 Keywords 4 Comments 4 Expressions 5
More informationChapter 2 Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion
More informationPointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple
More informationPython Crash Course Numpy, Scipy, Matplotlib
Python Crash Course Numpy, Scipy, Matplotlib That is what learning is. You suddenly understand something you ve understood all your life, but in a new way. Doris Lessing Steffen Brinkmann Max-Planck-Institut
More informationegrapher Language Reference Manual
egrapher Language Reference Manual Long Long: ll3078@columbia.edu Xinli Jia: xj2191@columbia.edu Jiefu Ying: jy2799@columbia.edu Linnan Wang: lw2645@columbia.edu Darren Chen: dsc2155@columbia.edu 1. Introduction
More informationCS 31: Intro to Systems C Programming. Kevin Webb Swarthmore College September 13, 2018
CS 31: Intro to Systems C Programming Kevin Webb Swarthmore College September 13, 2018 Reading Quiz Agenda Basics of C programming Comments, variables, print statements, loops, conditionals, etc. NOT the
More informationScripting Languages. Python basics
Scripting Languages Python basics Interpreter Session: python Direct conversation with python (>>>) Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linux Type "help", "copyright",
More informationExam 1 Format, Concepts, What you should be able to do, and Sample Problems
CSSE 120 Introduction to Software Development Exam 1 Format, Concepts, What you should be able to do, and Sample Problems Page 1 of 6 Format: The exam will have two sections: Part 1: Paper-and-Pencil o
More information377 Student Guide to C++
377 Student Guide to C++ c Mark Corner January 21, 2004 1 Introduction In this course you will be using the C++ language to complete several programming assignments. Up to this point we have only provided
More informationCS3157: Advanced Programming. Outline
CS3157: Advanced Programming Lecture #12 Apr 3 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Intro CPP Boring stuff: Language basics: identifiers, data types, operators, type conversions, branching
More informationIntroduction to Python. Dmytro Karpenko Research Infrastructure Services Group, Department for Research Computing, USIT, UiO
Introduction to Python Dmytro Karpenko Research Infrastructure Services Group, Department for Research Computing, USIT, UiO Research Computing Services tutorial and courses November 3-7, 2014 Why python
More informationC++ Programming Lecture 1 Software Engineering Group
C++ Programming Lecture 1 Software Engineering Group Philipp D. Schubert Contents 1. More on data types 2. Expressions 3. Const & Constexpr 4. Statements 5. Control flow 6. Recap More on datatypes: build-in
More informationPython for C programmers
Python for C programmers The basics of Python are fairly simple to learn, if you already know how another structured language (like C) works. So we will walk through these basics here. This is only intended
More informationM/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)
1 M/s Managing distributed workloads Language Reference Manual Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567) Table of Contents 1. Introduction 2. Lexical elements 2.1 Comments 2.2
More informationJME Language Reference Manual
JME Language Reference Manual 1 Introduction JME (pronounced jay+me) is a lightweight language that allows programmers to easily perform statistic computations on tabular data as part of data analysis.
More informationHaskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.
Haskell: Lists CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks
More informationPython Basics. Nakul Gopalan With help from Cam Allen-Lloyd
Python Basics Nakul Gopalan ngopalan@cs.brown.edu With help from Cam Allen-Lloyd 1 Introduction to the idea Readable, easy to learn programming language. Created by Guido van Rossum Named after the BBC
More informationC++ Programming: From Problem Analysis to Program Design, Third Edition
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine
More informationBeyond Blocks: Python Session #1
Beyond Blocks: Session #1 CS10 Spring 2013 Thursday, April 30, 2013 Michael Ball Beyond Blocks : : Session #1 by Michael Ball adapted from Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike
More informationProgramming Language Basics
Programming Language Basics Lecture Outline & Notes Overview 1. History & Background 2. Basic Program structure a. How an operating system runs a program i. Machine code ii. OS- specific commands to setup
More informationIntroduction to Python
Introduction to Python Michael Krisper Thomas Wurmitzer October 21, 2014 Michael Krisper, Thomas Wurmitzer Introduction to Python October 21, 2014 1 / 26 Schedule Tutorium I Dates & Deadlines Submission
More informationScientific Computing with Python and CUDA
Scientific Computing with Python and CUDA Stefan Reiterer High Performance Computing Seminar, January 17 2011 Stefan Reiterer () Scientific Computing with Python and CUDA HPC Seminar 1 / 55 Inhalt 1 A
More informationENGR 102 Engineering Lab I - Computation
ENGR 102 Engineering Lab I - Computation Learning Objectives by Week 1 ENGR 102 Engineering Lab I Computation 2 Credits 2. Introduction to the design and development of computer applications for engineers;
More informationDiscussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:
Discussion 1H Notes (Week 3, April 14) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 More on Arithmetic Expressions The following two are equivalent:! x = x + 5;
More informationAI Programming CS S-02 Python
AI Programming CS662-2013S-02 Python David Galles Department of Computer Science University of San Francisco 02-0: Python Name python comes from Monte Python s Flying Circus Most python references use
More informationVariables. Data Types.
Variables. Data Types. The usefulness of the "Hello World" programs shown in the previous section is quite questionable. We had to write several lines of code, compile them, and then execute the resulting
More informationPython for Non-programmers
Python for Non-programmers A Gentle Introduction 1 Yann Tambouret Scientific Computing and Visualization Information Services & Technology Boston University 111 Cummington St. yannpaul@bu.edu Winter 2013
More informationCh.1 Introduction. Why Machine Learning (ML)?
Syllabus, prerequisites Ch.1 Introduction Notation: Means pencil-and-paper QUIZ Means coding QUIZ Why Machine Learning (ML)? Two problems with conventional if - else decision systems: brittleness: The
More informationThe Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1
The Three Rules Chapter 1 Beginnings Rule 1: Think before you program Rule 2: A program is a human-readable essay on problem solving that also executes on a computer Rule 3: The best way to improve your
More informationAt full speed with Python
At full speed with Python João Ventura v0.1 Contents 1 Introduction 2 2 Installation 3 2.1 Installing on Windows............................ 3 2.2 Installing on macos............................. 5 2.3
More informationPart VI. Scientific Computing in Python. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26,
Part VI Scientific Computing in Python Compact Course @ Max-Planck, February 16-26, 2015 81 More on Maths Module math Constants pi and e Functions that operate on int and float All return values float
More informationLecture 05 I/O statements Printf, Scanf Simple statements, Compound statements
Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple
More informationText Input and Conditionals
Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()
More informationInteractive Mode Python Pylab
Short Python Intro Gerald Schuller, Nov. 2016 Python can be very similar to Matlab, very easy to learn if you already know Matlab, it is Open Source (unlike Matlab), it is easy to install, and unlike Matlab
More information8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017
8. The C++ language, 1 Programming and Algorithms II Degree in Bioinformatics Fall 2017 Hello world #include using namespace std; int main() { } cout
More informationPython Workshop. January 18, Chaitanya Talnikar. Saket Choudhary
Chaitanya Talnikar Saket Choudhary January 18, 2012 Python Named after this : Python Slide 1 was a joke! Python Slide 1 was a joke! Python : Conceived in late 1980s by Guido van Rossum as a successor to
More informationage = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s
Intro to Python Python Getting increasingly more common Designed to have intuitive and lightweight syntax In this class, we will be using Python 3.x Python 2.x is still very popular, and the differences
More informationCS112 Lecture: Primitive Types, Operators, Strings
CS112 Lecture: Primitive Types, Operators, Strings Last revised 1/24/06 Objectives: 1. To explain the fundamental distinction between primitive types and reference types, and to introduce the Java primitive
More informationChapter 2 Writing Simple Programs
Chapter 2 Writing Simple Programs Charles Severance Textbook: Python Programming: An Introduction to Computer Science, John Zelle (www.si182.com) Software Development Process Figure out the problem - for
More informationIntroduction to Python and NumPy I
Introduction to Python and NumPy I This tutorial is continued in part two: Introduction to Python and NumPy II Table of contents Overview Launching Canopy Getting started in Python Getting help Python
More informationIntroduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1
Review Midterm Exam 1 Review Midterm Exam 1 Exam on Monday, October 7 Data Types and Variables = Data Types and Variables Basic Data Types Integers Floating Point Numbers Strings Data Types and Variables
More informationAnnouncements for this Lecture
Lecture 6 Objects Announcements for this Lecture Last Call Quiz: About the Course Take it by tomorrow Also remember survey Assignment 1 Assignment 1 is live Posted on web page Due Thur, Sep. 18 th Due
More information1007 Imperative Programming Part II
Agenda 1007 Imperative Programming Part II We ve seen the basic ideas of sequence, iteration and selection. Now let s look at what else we need to start writing useful programs. Details now start to be
More informationCOMP-520 GoLite Tutorial
COMP-520 GoLite Tutorial Alexander Krolik Sable Lab McGill University Winter 2019 Plan Target languages Language constructs, emphasis on special cases General execution semantics Declarations Types Statements
More informationCIT 590 Homework 5 HTML Resumes
CIT 590 Homework 5 HTML Resumes Purposes of this assignment Reading from and writing to files Scraping information from a text file Basic HTML usage General problem specification A website is made up of
More informationCh.2: Loops and lists
Ch.2: Loops and lists Joakim Sundnes 1,2 Hans Petter Langtangen 1,2 Simula Research Laboratory 1 University of Oslo, Dept. of Informatics 2 Aug 29, 2018 Plan for 28 August Short quiz on topics from last
More informationPYTHON NUMPY TUTORIAL CIS 581
PYTHON NUMPY TUTORIAL CIS 581 VARIABLES AND SPYDER WORKSPACE Spyder is a Python IDE that s a part of the Anaconda distribution. Spyder has a Python console useful to run commands quickly and variables
More informationLanguage Design COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science
Language Design COMS W4115 Katsushika Hokusai, In the Hollow of a Wave off the Coast at Kanagawa, 1827 Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science Language Design
More informationCSI33 Data Structures
Outline Department of Mathematics and Computer Science Bronx Community College October 24, 2018 Outline Outline 1 Chapter 8: A C++ Introduction For Python Programmers Expressions and Operator Precedence
More informationProgramming for Engineers Arrays
Programming for Engineers Arrays ICEN 200 Spring 2018 Prof. Dola Saha 1 Array Ø Arrays are data structures consisting of related data items of the same type. Ø A group of contiguous memory locations that
More informationIntroduction to Programming in C Department of Computer Science and Engineering. Lecture No. #49. Structures in C -1
Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #49 Structures in C -1 So, in this lecture will talk about structures and C, which is syntactic feature that
More informationGIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes
GIS 4653/5653: Spatial Programming and GIS More Python: Statements, Types, Functions, Modules, Classes Statement Syntax The if-elif-else statement Indentation and and colons are important Parentheses and
More informationSprite an animation manipulation language Language Reference Manual
Sprite an animation manipulation language Language Reference Manual Team Leader Dave Smith Team Members Dan Benamy John Morales Monica Ranadive Table of Contents A. Introduction...3 B. Lexical Conventions...3
More informationVariables Data types Variable I/O. C introduction. Variables. Variables 1 / 14
C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition
More informationPython for Finance. Control Flow, data structures and first application (part 2) Andras Niedermayer
Python for Finance Control Flow, data structures and first application (part 2) Andras Niedermayer Outline 1 Control Flow 2 Modules 3 Data types and structures. Working with arrays and matrices. 4 Numpy
More informationObjectives. In this chapter, you will:
Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about
More informationCSC 120 Computer Science for the Sciences. Week 1 Lecture 2. UofT St. George January 11, 2016
CSC 120 Computer Science for the Sciences Week 1 Lecture 2 UofT St. George January 11, 2016 Introduction to Python & Foundations of computer Programming Variables, DataTypes, Arithmetic Expressions Functions
More information(DRAFT) PYTHON FUNDAMENTALS II: NUMPY & MATPLOTLIB
(DRAFT) PYTHON FUNDAMENTALS II: NUMPY & MATPLOTLIB TROY P. KLING Contents 1. Importing Libraries 1 2. Introduction to numpy 2 3. Introduction to matplotlib 5 4. Image Processing 8 5. The Mandelbrot Set
More informationCS201- Introduction to Programming Current Quizzes
CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements
More informationCHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS
CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS 1439-1440 1 Outline 1. Introduction 2. Why Python? 3. Compiler and Interpreter 4. The first program 5. Comments and Docstrings 6. Python Indentations
More informationARTIFICIAL INTELLIGENCE AND PYTHON
ARTIFICIAL INTELLIGENCE AND PYTHON DAY 1 STANLEY LIANG, LASSONDE SCHOOL OF ENGINEERING, YORK UNIVERSITY WHAT IS PYTHON An interpreted high-level programming language for general-purpose programming. Python
More informationSyntax and Variables
Syntax and Variables What the Compiler needs to understand your program, and managing data 1 Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line
More informationPython as a First Programming Language Justin Stevens Giselle Serate Davidson Academy of Nevada. March 6th, 2016
Python as a First Programming Language Justin Stevens Giselle Serate Davidson Academy of Nevada Under Supervision of: Dr. Richard Kelley Chief Engineer, NAASIC March 6th, 2016 Science Technology Engineering
More information4. Modules and Functions
4. Modules and Functions The Usual Idea of a Function Topics Modules Using import Using functions from math A first look at defining functions sqrt 9 3 A factory that has inputs and builds outputs. Why
More information\n is used in a string to indicate the newline character. An expression produces data. The simplest expression
Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of
More informationCS 115 Lecture 8. Selection: the if statement. Neil Moore
CS 115 Lecture 8 Selection: the if statement Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 24 September 2015 Selection Sometime we want to execute
More informationLECTURE 19. Numerical and Scientific Packages
LECTURE 19 Numerical and Scientific Packages NUMERICAL AND SCIENTIFIC APPLICATIONS As you might expect, there are a number of third-party packages available for numerical and scientific computing that
More information