PHY224 Practical Physics I Python Review Lecture 1 Sept , 2013

Size: px
Start display at page:

Download "PHY224 Practical Physics I Python Review Lecture 1 Sept , 2013"

Transcription

1 PHY224 Practical Physics I Python Review Lecture 1 Sept , 2013 Summary Python objects Lists and arrays Input (raw_input) and output Control Structures: iterations References M H. Goldwasser, D. Letscher: Object-oriented programming in Python, Pearson 2008 G. Daniell and J. Flynn: School of Physics and Astronomy, University of Southampton (Computing Module for PHYS2022) - used by permission Eric Jones: Introduction to Scientific Computing with Python: Enthought SciPy 2007 Conference, CalTech. Scipy:

2 Some definitions Python is a scripting language Scripts are files containing expressions and program statements. Scripts control other programs Python is an interpreted language: Expressions may be evaluated and statements may be executed: - at a Python prompt, or - from a.py file. Python is a compiled language: -a.py file may be compiled into Python bytecode. Python is an object oriented language

3 Objects in Python: everything! Whenever you come across something in Python and you ask: "What is this?" Your first correct answer is: "an object Python objects have a value. Python objects are containers of attributes (attributes are also objects!). Methods: objects which are callable. All Python objects also have a type and an id.

4 A note about code blocks in Python Flow of execution in a Python program is controlled by indention. Indention must be consistent. Four spaces are recommended. Indention instead of punctuation makes Python one of the most readable programming languages.

5 A Python Primer: numbers Python Shell vs. IDLE Use Python as a calculator: ( )*5 24/3 9**2 Insert a comment: the interpreter ignores everything from # to the end of line # this is a comment Integer vs. floating point numbers: try: 7.0/4.0 7/4

6 A Python Primer: numbers Integer division: unexpected behavior. How to avoid integer division: -Make use of Python s future division: in Python 3.0 onwards, the division operator will return a floating point number At the Python prompt do this: 15/6 from future import division 15/6 If you want to use the from future import division feature, it would normally be included at the beginning of the program

7 A Python Primer: numbers - If you mix integer and floating point numbers, they are all converted to floating point: test it: You can convert from integer to float and back, but conversion to integers is done by truncating towards zero. Try: float(1) int( ) int( ) Another way of avoiding the problem of integer division is to ensure that at least one number (numerator or denominator) is of type float: float(15)/6

8 A Python Primer: assignments, strings and types You can assign values to variables: x=3 means assign the value 3 to the variable called x What is the meaning of: x = x*x Try this and explain how Python evaluated your statements: x = 4 x = x+1 print x

9 A Python Primer: assignments, strings and types Strings are always enclosed in single or double quotes: a = eggs print a Concatenating strings: use the + operator b = eggs, + + toast and bacon print b Duplicate strings: by multiplying them by a number: c = eggs, *4 print c The split() method will separate a string where it finds white space.

10 A Python Primer: assignments, strings and types Type of a variables: attribute used by the encoding scheme You can ask what type a variable is and you can print its value. Do this at the Python prompt: a=1; b=1.0; c= 1.0 type(a) type(b) type(c)

11 A Python Primer: complex numbers Python supports complex numbers, written as 3+5j Examples: do this at the Python Shell: d=3+5j type(d) d.real d.imag abs(d) # real part # imaginary part # absolute value e=1+1j print d*e # multiplying two complex numbers # this was an output statement

12 The object-oriented paradigm We design classes to take advantage of similarities in the way we represent and interact with objects. Objects drawn from the same class support a common set of methods. A method that changes the state of an object from a class is called mutator.

13 About classes Classes are templates for creating your own custom objects. Think of a class as a blueprint. You can create lots of objects from that blueprint - known technically as instances. All instances will share the same properties class TheClass: pass This is the class definition header This is the class body This is the simplest possible class definition The first line is the class definition header All the following indented lines are the class body.

14 Example. At the Python shell, try: class Duck: color = 'brown' wings = 2 duck1 = Duck() Class body consists of attributes duck1.color duck1.wings All members of the Duck class will share the same class attributes and their values.

15 The list sequence class How to create a list a: The assignment statement creates an empty list(): a = list() a is called identifier (it is an object) Lists are mutable sequences. You can: - add, - remove, - replace elements from a list.

16 Which objects can be elements in lists? Numeric types: int - 32 bit signed integers: 5, -273, , etc. float - 64 bit IEEE-754 floating point numbers: , 0.125, long - infinite precision signed integers complex bit complex numbers: 3+4j Strings: Character strings, literals syntax: put it under quote signs (simple or double): purple or purple Tuples: Immutable list-type sequences syntax: use round brackets. Example: skyblue = (136, 207, 236)

17 Constructing a list Lists are enclosed by square brackets. String elements are put under quotation marks. At the Python shell, do this: a = list() a = [42, 23, 'OK'] # This is list a, predefined as: a = list() a[1] # Indexing starts at zero a[-1] # Explain this! a[1:] # returns a slice of the original list len(a) # returns the length of list a (how many elements)

18 General object-oriented syntax: object.method(parameters)

19 The list class The append method: a.append( apple ) object method parameter Quotation marks on parameter denote a string. Try: a.append(apple) What happens?

20 The list class: indexing, replacing an element Try: a[1] a[1] = 2**31 What happened to parameter [1] from your list a? How many elements are in list a? Use the len(a) command: len(a)

21 The list class The remove method: a.remove( OK ) The extend method adds the content of another list (otherlist)at the end of the current list: a.extend(otherlist) otherlist is another list that had to be pre-defined

22 The list class Independent work 1: a) Add the following objects to list a: 2.0, 3+4j, (42, 23, 'OK'), [1,2,3] Check the type of each object; use: type(object) b) Remove the second item from list a c) Construct another list b of your choice and extend list a by list b.

23 The range() function In scientific calculations we often want to loop over a list of numbers, indices, etc. There is a function in Python called range(n) which will output a list of n integers from 0 to (n -1). Try the following examples, using the Python shell: range(10) range(1,11) range(5,20,2) Do you understand how the function range( ) worked in each case?

24 Arrays and the use of Numpy Numpy is a Python module which uses high performance linear algebra libraries to perform vector and matrix manipulation. Numpy uses the array data structure. An array is the typical experimental data format to be used in a Python program. The syntax for creating an array is: array(argument) # argument is usually a list Try this at the Python shell: from numpy import * a = array([0,12,6,8,23,2]) type (a) #the argument is a list #this will give you the type of object a

25 Arrays and the use of Numpy Numpy provides the command arange(), in analogy with range(): Try this with the Python shell: b = arange(5) b c = arange(0.1, 1.0, 0.2) c b+c b*c print b*c sin(b) Explain how arange() and the algebraic manipulation of arrays worked

26 Arrays and the use of Numpy The following functions create arrays of zeros or ones. Run them and try to understand the output in terms of input arguments: zeros(7, int) ones(5, int)

27 Numeric types int - 32 bit signed integers, float - 64 bit IEEE-754 floating point numbers, long - infinite precision signed integers, complex bit complex numbers, Numeric data type bool - boolean values True or False. Numeric operators: division At the Python Shell, try: 9/4 9.0/4.0 9%4 The modulo operator ("%") evaluates to the remainder of two integers. This is a very problematic operator.

28 Numeric operators: try them all! x + y sum of x and y x y difference of x and y x * y product of x and y x / y quotient of x and y x // y integral quotient of x and y -x x negated +x x unchanged x ** y x to the power y Trigonometric and logarithmic functions are included in the math module. There are many advanced math packages available (FFT, linear algebra, etc.).

29 Augmented assignments: try them! x += y same as x = x + y x -= y same as x = x - y x *= y same as x = x * y x /= y same as x = x / y x //= y same as x = x // y x **= y same as x = x ** y

30 A word about True and False The Boolean or logical data type has one of two values: True or False, intended to represent the truth values of logic. Truth Value: Truth Value Equivalence: That which is not False is True That which is not equivalent to False is equivalent to True Literals True and False are instances of the bool class in Python Basic logical operators are: not, and, or, ==,!=

31 Logical operators At the Python Shell, thy the following: True and True True and False True or False not True True and not False

32 Numeric operators: comparison All comparison operations result in True or False Boolean values. Try them all, with numerical values: < strictly less than <= less than or equal > strictly greater than >= greater than or equal == equal value (equivalency)!= not equal

33 Input and Output Computer programs involve interaction with the user, called input and output. When working in an interactive session, there is no distinction between programmer and the user. The output can write data to files, can send plots to printer or prints things on the screen. The input() function is used to gather input from the user.

34 Input and Output Example : Open VIDLE (Python interactive interpreter) using the desktop shortcut. Try this: a=input ( Please give a number: ) b=input ( And another: ) print The sum of these numbers is:, a+b

35 Input and Output The print command is the simplest form for generating output. It is called standard output. This command accepts several arguments, separated by commas. It prints the arguments separated by spaces.

36 Input and Output input () can read only numbers (integers and floats). If we need to read a string (word or sentence) from the keyboard, then we should use the raw_input () function: Example: Type: age=int(raw_input( What is your age? )) print Soon you will be, age +1 Let s change the code a little bit: age=int(raw_input( What is your age? )) print Soon you will be + age +1

37 Input and Output: What happened? age=int(raw_input( What is your age? )) print Soon you will be + age +1 Our attempt to incorrectly concatenate a string and a number failed because they are different types of objects. String concatenation is the operation of joining two character strings end to end. Note that the error message clearly indicated not only the type of error, but also the file location and the line in the code where the error occurred.

38 Input and Output Independent work 2 1) Modify the previous example, so that it reads and multiplies three numbers. Previous Example a=input ( Please give a number: ) b=input ( And another: ) print The sum of these numbers is:, a+b 2) Write a program that asks the user to enter a temperature in Fahrenheit (f) and reports the equivalent temperature in Celsius (c). You may use the formula: c 5 = ( f 9 32) for doing the conversion.

39 Control structures: Iterations for loops for loops are used when you want to repeat something a fixed number of times. The for statement ends with a colon (:). The syntax is: for identifier in sequence: body The block of code that follows the for command line is called the body of the loop.

40 Control structures: for loops fruits = ['banana', 'apple', 'mango'] for fruit in fruits: print 'Current fruit :', fruit print Good Bye! Iterating by Sequence Index fruits = ['banana', 'apple', 'mango'] for index in range(len(fruits)): print 'Current fruit :', fruits[index] print Good Bye! We used the len() built-in function, which provides the total number of elements in the list, as well as the range() built-in function to give us the actual sequence to iterate over.

41 Control structures: Iterations Independent work 3 1) Use a for loop to iterate over a range to produce a simple table of the first ten positive integers, their squares and cubes. 2) Create a list a with four floating point numbers. Next, create an empty list b=[ ]. Run through the elements of a, using a for loop, successively appending the exponential of each element of a to b. Note: you may need to import the math module first.

42 Control structures: Iterations while loops In a while loop, you keep performing a sequence of instructions (the loop body), always checking beforehand that some condition is true. Typically the condition is true at the beginning of looping but after going around the loop a number of times, the condition becomes false. The interpreter skips the instructions inside the loop and moves on with the program. If the condition is false when you first reach the while loop, the instructions in the body are never executed. The syntax is: while condition: body (statements executed if the condition is true)

43 Control structures: while loops count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!" The block here, consisting of the print and increment statements, is executed repeatedly until count is no longer less than 9. With each iteration, the current value of the index count is displayed and then increased by 1.

44 Control structures: Iterations: while loops Independent work 4 Let s calculate the factorial of 9 How to build the program? Work in VIDLE You need to run an index (number < 10) Initialize the calculation (write the first value of factorial, index) Run the index from 1 to 9 by writing a while loop. Print index, factorial Run the factorial code. Check it with your TA if something doesn t work. Save the file on your memory stick (we shall use it later). Do not forget the extension.py

45 Control structures: Iterations: while loops Let s calculate the factorial of 9: fac = 1 i = 1 while i<10: fac=fac*i print i, fac i=i+1

46 Let s have some fun! Try a game written in VPython by Ruth Chabay and Bruce Sherwood from North Carolina State University. You ll find it by opening the 2nd Yr Lab Files folder from the Desktop. The file is hanoi.py

47 Control structures: Conditionals (if statements) The if statement allows us to specify one or more instructions that are to be executed ONLY when a certain condition is true. This is a very valuable tool as it allows execution to vary depending upon values that are not known until the program is running. Syntax is: if condition: body (statements executed only if condition is true) If the condition evaluates to True, the body is executed; if the condition evaluates to False, the body is bypassed. The condition can be an arbitrary expression that evaluates to a Boolean.

48 Control structures: Conditionals (if statements) Let s work on the following comparison test using a conditional statement Example for x in range(20): if x%9 == 0: print x, is divisible by 9 elif x%3 == 0: print x, is divisible by 3 Predict the output that results when the code is executed. What the elif command did?

49 The fascinating π First identified as a constant by builders of the Great Pyramids in Egypt ( B.C.): 1760 cubits around the base; height of 280 cubits yield a ratio 1760/280 = =2x That the ratio of the circumference to the diameter of a circle is the same for all circles, and that it is slightly more than 3, was known to ancient Egyptian, Babylonian, Indian and Greek geometers. Archimedes ( BC) was the first to estimate π rigorously. The series we shall use today was first found around 1400 by the Hindu mathematician and astronomer Madhava of Sangamagrama who evaluated π with 13 decimal places.

50 Independent Project We shall write a program to approximate the mathematical constant π using the following approximation up to n terms, for some n: π = Start by writing down the general expression of a series term. Run the program for n = 20, 100 and What do you notice? Save your function under a different name than pi which is a dedicated math module.

51 Control structures: Examples Here is how the Python program to calculate π looks like: pi=0.0 # intial value for k in range(0,n): # n is your number term = (-1)**k * 1.0/(2*k+1) # a code variation would be: # term = 1.0/(2*k+1) # if k%2 = =1: # term = - term pi+ = 4*term print pi #pi+= means each term is #added to the previous one

PHY224 Practical Physics I. Lecture 2

PHY224 Practical Physics I. Lecture 2 PHY224 Practical Physics I Python Review Lecture 2 Sept. 15 16 16, 2014 Summary Functions and Modules Graphs (plotting with Pylab) Scipy packages References M H. Goldwasser, D. Letscher: Object oriented

More information

PHY224 Practical Physics I. Lecture 2

PHY224 Practical Physics I. Lecture 2 PHY224 Practical Physics I Python Review Lecture 2 Sept. 19 20 20, 2013 Summary Functions and Modules Graphs (plotting with Pylab) Scipy packages References M H. Goldwasser, D. Letscher: Object oriented

More information

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

\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 information

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Intro 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 information

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines. 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 information

Introduction to programming with Python

Introduction to programming with Python Introduction to programming with Python Ing. Lelio Campanile 1/61 Main Goal - Introduce you to programming - introduce you to the most essential feature of python programming 2/61 Before to start The name

More information

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018 Python Input, output and variables Lecture 23 COMPSCI111/111G SS 2018 1 Today s lecture What is Python? Displaying text on screen using print() Variables Numbers and basic arithmetic Getting input from

More information

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT Python The way of a program Srinidhi H Asst Professor Dept of CSE, MSRIT 1 Problem Solving Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution

More information

Lecture 3. Input, Output and Data Types

Lecture 3. Input, Output and Data Types Lecture 3 Input, Output and Data Types Goals for today Variable Types Integers, Floating-Point, Strings, Booleans Conversion between types Operations on types Input/Output Some ways of getting input, and

More information

Accelerating Information Technology Innovation

Accelerating Information Technology Innovation Accelerating Information Technology Innovation http://aiti.mit.edu/program/philippines-summer-2012/ Philippines Summer 2012 Lecture 1 Introduction to Python June 19, 2012 Agenda About the Course What is

More information

CS Summer 2013

CS Summer 2013 CS 1110 - Summer 2013 intro to programming -- how to think like a robot :) we use the Python* language (www.python.org) programming environments (many choices): Eclipse (free from www.eclipse.org), or

More information

Python 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. 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 information

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15 1 LECTURE 1: INTRO Introduction to Scientific Python, CME 193 Jan. 9, 2014 web.stanford.edu/~ermartin/teaching/cme193-winter15 Eileen Martin Some slides are from Sven Schmit s Fall 14 slides 2 Course Details

More information

Variables, expressions and statements

Variables, expressions and statements Variables, expressions and statements 2.1. Values and data types A value is one of the fundamental things like a letter or a number that a program manipulates. The values we have seen so far are 2 (the

More information

Part III Appendices 165

Part III Appendices 165 Part III Appendices 165 Appendix A Technical Instructions Learning Outcomes This material will help you learn how to use the software you need to do your work in this course. You won t be tested on it.

More information

Accelerating Information Technology Innovation

Accelerating Information Technology Innovation Accelerating Information Technology Innovation http://aiti.mit.edu Cali, Colombia Summer 2012 Lesson 1 Introduction to Python Agenda What is Python? and Why Python? Basic Syntax Strings User Input Useful

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Python: common syntax

Python: common syntax Lab 09 Python! Python Intro Main Differences from C++: True and False are capitals Python floors (always down) with int division (matters with negatives): -3 / 2 = -2 No variable data types or variable

More information

Scripting Languages. Python basics

Scripting 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 information

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */ Overview Language Basics This chapter describes the basic elements of Rexx. It discusses the simple components that make up the language. These include script structure, elements of the language, operators,

More information

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems. Plan for the rest of the semester: Programming We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems. We saw earlier that computers

More information

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA Starting with a great calculator... Topic 5: Introduction to Programming in Matlab CSSE, UWA! MATLAB is a high level language that allows you to perform calculations on numbers, or arrays of numbers, in

More information

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts COMP519 Web Programming Lecture 17: Python (Part 1) Handouts Ullrich Hustadt Department of Computer Science School of Electrical Engineering, Electronics, and Computer Science University of Liverpool Contents

More information

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Introduction 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 information

ENGR 102 Engineering Lab I - Computation

ENGR 102 Engineering Lab I - Computation ENGR 102 Engineering Lab I - Computation Week 07: Arrays and Lists of Data Introduction to Arrays In last week s lecture, 1 we were introduced to the mathematical concept of an array through the equation

More information

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing SECTION 1: INTRODUCTION ENGR 112 Introduction to Engineering Computing 2 Course Overview What is Programming? 3 Programming The implementation of algorithms in a particular computer programming language

More information

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python CS95003 - Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / 2014 Subjects 1) Beginning with Python 2) Variables 3) Strings 4) Basic arithmetic operators 5) Flow control 6) Comparison

More information

Fundamentals: Expressions and Assignment

Fundamentals: Expressions and Assignment Fundamentals: Expressions and Assignment A typical Python program is made up of one or more statements, which are executed, or run, by a Python console (also known as a shell) for their side effects e.g,

More information

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

age = 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 information

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops PRG PROGRAMMING ESSENTIALS 1 Lecture 2 Program flow, Conditionals, Loops https://cw.fel.cvut.cz/wiki/courses/be5b33prg/start Michal Reinštein Czech Technical University in Prague, Faculty of Electrical

More information

Python. Objects. Geog 271 Geographic Data Analysis Fall 2010

Python. Objects. Geog 271 Geographic Data Analysis Fall 2010 Python This handout covers a very small subset of the Python language, nearly sufficient for exercises in this course. The rest of the language and its libraries are covered in many fine books and in free

More information

CSI Lab 02. Tuesday, January 21st

CSI Lab 02. Tuesday, January 21st CSI Lab 02 Tuesday, January 21st Objectives: Explore some basic functionality of python Introduction Last week we talked about the fact that a computer is, among other things, a tool to perform high speed

More information

Python Input, output and variables

Python Input, output and variables Today s lecture Python Input, output and variables Lecture 22 COMPSCI111/111G SS 2016! What is Python?! Displaying text on screen using print()! Variables! Numbers and basic arithmetic! Getting input from

More information

ENGR 102 Engineering Lab I - Computation

ENGR 102 Engineering Lab I - Computation ENGR 102 Engineering Lab I - Computation Week 03: Data Types and Console Input / Output Introduction to Types As we have already seen, 1 computers store numbers in a binary sequence of bits. The organization

More information

egrapher Language Reference Manual

egrapher 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 information

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016 Python Input, output and variables Lecture 22 COMPSCI111/111G SS 2016 Today s lecture u What is Python? u Displaying text on screen using print() u Variables u Numbers and basic arithmetic u Getting input

More information

CS1 Lecture 3 Jan. 22, 2018

CS1 Lecture 3 Jan. 22, 2018 CS1 Lecture 3 Jan. 22, 2018 Office hours for me and for TAs have been posted, locations will change check class website regularly First homework available, due Mon., 9:00am. Discussion sections tomorrow

More information

Python for Informatics

Python for Informatics Python for Informatics Exploring Information Version 0.0.6 Charles Severance Chapter 3 Conditional execution 3.1 Boolean expressions A boolean expression is an expression that is either true or false.

More information

Introduction to Python

Introduction to Python Introduction to Python خانه ریاضیات اصفهان فرزانه کاظمی زمستان 93 1 Why Python? Python is free. Python easy to lean and use. Reduce time and length of coding. Huge standard library Simple (Python code

More information

REVIEW. The C++ Programming Language. CS 151 Review #2

REVIEW. The C++ Programming Language. CS 151 Review #2 REVIEW The C++ Programming Language Computer programming courses generally concentrate on program design that can be applied to any number of programming languages on the market. It is imperative, however,

More information

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions. COMP1730/COMP6730 Programming for Scientists Data: Values, types and expressions. Lecture outline * Data and data types. * Expressions: computing values. * Variables: remembering values. What is data?

More information

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Introduction to: Computers & Programming: Review prior to 1 st Midterm Introduction to: Computers & Programming: Review prior to 1 st Midterm Adam Meyers New York University Summary Some Procedural Matters Summary of what you need to Know For the Test and To Go Further in

More information

Chapter 2 Getting Started with Python

Chapter 2 Getting Started with Python Chapter 2 Getting Started with Python Introduction Python Programming language was developed by Guido Van Rossum in February 1991. It is based on or influenced with two programming languages: 1. ABC language,

More information

Ceng 111 Fall 2015 Week 7a

Ceng 111 Fall 2015 Week 7a Ceng 111 Fall 2015 Week 7a Container data Credit: Some slides are from the Invitation to Computer Science book by G. M. Schneider, J. L. Gersting and some from the Digital Design book by M. M. Mano and

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All rights reserved. Java application A computer program that executes when you use the java command to launch the Java Virtual Machine

More information

Ch.2: Loops and lists

Ch.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 information

Algorithms and Programming I. Lecture#12 Spring 2015

Algorithms and Programming I. Lecture#12 Spring 2015 Algorithms and Programming I Lecture#12 Spring 2015 Think Python How to Think Like a Computer Scientist By :Allen Downey Installing Python Follow the instructions on installing Python and IDLE on your

More information

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Getting 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 information

>>> * *(25**0.16) *10*(25**0.16)

>>> * *(25**0.16) *10*(25**0.16) #An Interactive Session in the Python Shell. #When you type a statement in the Python Shell, #the statement is executed immediately. If the #the statement is an expression, its value is #displayed. #Lines

More information

CSI33 Data Structures

CSI33 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 information

Computational Programming with Python

Computational 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 information

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #2

More information

Course Outline. Introduction to java

Course Outline. Introduction to java Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.

More information

Introduction to Java Applications

Introduction to Java Applications 2 Introduction to Java Applications OBJECTIVES In this chapter you will learn: To write simple Java applications. To use input and output statements. Java s primitive types. Basic memory concepts. To use

More information

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions Grace Murray Hopper Expressions Eric Roberts CSCI 121 January 30, 2018 Grace Hopper was one of the pioneers of modern computing, working with

More information

CS 115 Lecture 4. More Python; testing software. Neil Moore

CS 115 Lecture 4. More Python; testing software. Neil Moore CS 115 Lecture 4 More Python; testing software Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 8 September 2015 Syntax: Statements A statement

More information

The current topic: Python. Announcements. Python. Python

The current topic: Python. Announcements. Python. Python The current topic: Python Announcements! Introduction! reasons for studying languages! language classifications! simple syntax specification Object-oriented programming: Python Types and values Syntax

More information

Expressions and Variables

Expressions and Variables Expressions and Variables Expressions print(expression) An expression is evaluated to give a value. For example: 2 + 9-6 Evaluates to: 5 Data Types Integers 1, 2, 3, 42, 100, -5 Floating points 2.5, 7.0,

More information

Lab 7: OCaml 12:00 PM, Oct 22, 2017

Lab 7: OCaml 12:00 PM, Oct 22, 2017 CS17 Integrated Introduction to Computer Science Hughes Lab 7: OCaml 12:00 PM, Oct 22, 2017 Contents 1 Getting Started in OCaml 1 2 Pervasives Library 2 3 OCaml Basics 3 3.1 OCaml Types........................................

More information

CS Advanced Unix Tools & Scripting

CS Advanced Unix Tools & Scripting & Scripting Spring 2011 Hussam Abu-Libdeh slides by David Slater March 4, 2011 Hussam Abu-Libdeh slides by David Slater & Scripting Python An open source programming language conceived in the late 1980s.

More information

Variable and Data Type I

Variable and Data Type I The Islamic University of Gaza Faculty of Engineering Dept. of Computer Engineering Intro. To Computers (LNGG 1003) Lab 2 Variable and Data Type I Eng. Ibraheem Lubbad February 18, 2017 Variable is reserved

More information

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

More information

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh C++ PROGRAMMING For Industrial And Electrical Engineering Instructor: Ruba A. Salamh CHAPTER TWO: Fundamental Data Types Chapter Goals In this chapter, you will learn how to work with numbers and text,

More information

Downloaded from Chapter 2. Functions

Downloaded from   Chapter 2. Functions Chapter 2 Functions After studying this lesson, students will be able to: Understand and apply the concept of module programming Write functions Identify and invoke appropriate predefined functions Create

More information

Advanced Algorithms and Computational Models (module A)

Advanced Algorithms and Computational Models (module A) Advanced Algorithms and Computational Models (module A) Giacomo Fiumara giacomo.fiumara@unime.it 2014-2015 1 / 34 Python's built-in classes A class is immutable if each object of that class has a xed value

More information

Introduction to Programming in Python. A Self-Study Course

Introduction to Programming in Python. A Self-Study Course Department of Physics Introduction to Programming in Python A Self-Study Course (Version 4.1.3 February 2013) Table of Contents 1 Chapter 1 - Introduction... 7 2 Chapter 2 - Resources Required for the

More information

SCRATCH MODULE 3: NUMBER CONVERSIONS

SCRATCH MODULE 3: NUMBER CONVERSIONS SCRATCH MODULE 3: NUMBER CONVERSIONS INTRODUCTION The purpose of this module is to experiment with user interactions, error checking input, and number conversion algorithms in Scratch. We will be exploring

More information

Variable and Data Type I

Variable and Data Type I Islamic University Of Gaza Faculty of Engineering Computer Engineering Department Lab 2 Variable and Data Type I Eng. Ibraheem Lubbad September 24, 2016 Variable is reserved a location in memory to store

More information

The Very Basics of the R Interpreter

The Very Basics of the R Interpreter Chapter 2 The Very Basics of the R Interpreter OK, the computer is fired up. We have R installed. It is time to get started. 1. Start R by double-clicking on the R desktop icon. 2. Alternatively, open

More information

Here n is a variable name. The value of that variable is 176.

Here n is a variable name. The value of that variable is 176. UNIT II DATA, EXPRESSIONS, STATEMENTS 9 Python interpreter and interactive mode; values and types: int, float, boolean, string, and list; variables, expressions, statements, tuple assignment, precedence

More information

PYTHON FOR MEDICAL PHYSICISTS. Radiation Oncology Medical Physics Cancer Care Services, Royal Brisbane & Women s Hospital

PYTHON FOR MEDICAL PHYSICISTS. Radiation Oncology Medical Physics Cancer Care Services, Royal Brisbane & Women s Hospital PYTHON FOR MEDICAL PHYSICISTS Radiation Oncology Medical Physics Cancer Care Services, Royal Brisbane & Women s Hospital TUTORIAL 1: INTRODUCTION Thursday 1 st October, 2015 AGENDA 1. Reference list 2.

More information

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA 1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

More information

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting Your factors.c and multtable.c files are due by Wednesday, 11:59 pm, to be submitted on the SoC handin page at http://handin.cs.clemson.edu.

More information

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program

More information

Programming with Python

Programming 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 information

Python Intro GIS Week 1. Jake K. Carr

Python Intro GIS Week 1. Jake K. Carr GIS 5222 Week 1 Why Python It s simple and easy to learn It s free - open source! It s cross platform IT S expandable!! Why Python: Example Consider having to convert 1,000 shapefiles into feature classes

More information

LISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University

LISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University LISTS WITH PYTHON José M. Garrido Department of Computer Science May 2015 College of Computing and Software Engineering Kennesaw State University c 2015, J. M. Garrido Lists with Python 2 Lists with Python

More information

Introduction to Python Programming

Introduction to Python Programming advances IN SYSTEMS AND SYNTHETIC BIOLOGY 2018 Anna Matuszyńska Oliver Ebenhöh oliver.ebenhoeh@hhu.de Ovidiu Popa ovidiu.popa@hhu.de Our goal Learning outcomes You are familiar with simple mathematical

More information

T H E I N T E R A C T I V E S H E L L

T 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 information

CS1 Lecture 3 Jan. 18, 2019

CS1 Lecture 3 Jan. 18, 2019 CS1 Lecture 3 Jan. 18, 2019 Office hours for Prof. Cremer and for TAs have been posted. Locations will change check class website regularly First homework assignment will be available Monday evening, due

More information

Introduction to the C++ Programming Language

Introduction to the C++ Programming Language LESSON SET 2 Introduction to the C++ Programming Language OBJECTIVES FOR STUDENT Lesson 2A: 1. To learn the basic components of a C++ program 2. To gain a basic knowledge of how memory is used in programming

More information

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Java application programming } Use tools from the JDK to compile and run programs. } Videos at www.deitel.com/books/jhtp9/ Help you get started

More information

Name Period Date. REAL NUMBER SYSTEM Student Pages for Packet 3: Operations with Real Numbers

Name Period Date. REAL NUMBER SYSTEM Student Pages for Packet 3: Operations with Real Numbers Name Period Date REAL NUMBER SYSTEM Student Pages for Packet : Operations with Real Numbers RNS. Rational Numbers Review concepts of experimental and theoretical probability. a Understand why all quotients

More information

Introduction to Python: Data types. HORT Lecture 8 Instructor: Kranthi Varala

Introduction to Python: Data types. HORT Lecture 8 Instructor: Kranthi Varala Introduction to Python: Data types HORT 59000 Lecture 8 Instructor: Kranthi Varala Why Python? Readability and ease-of-maintenance Python focuses on well-structured easy to read code Easier to understand

More information

Python lab session 1

Python lab session 1 Python lab session 1 Dr Ben Dudson, Department of Physics, University of York 28th January 2011 Python labs Before we can start using Python, first make sure: ˆ You can log into a computer using your username

More information

CITS2401 Computer Analysis & Visualisation

CITS2401 Computer Analysis & Visualisation FACULTY OF ENGINEERING, COMPUTING AND MATHEMATICS CITS2401 Computer Analysis & Visualisation SCHOOL OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING Topic 3 Introduction to Matlab Material from MATLAB for

More information

PYTHON. Values and Variables

PYTHON. Values and Variables December 13 2017 Naveen Sagayaselvaraj PYTHON Values and Variables Overview Integer Values Variables and Assignment Identifiers Floating-point Types User Input The eval Function Controlling the print Function

More information

Introduction to Scientific Computing

Introduction to Scientific Computing Introduction to Scientific Computing Dr Hanno Rein Last updated: October 12, 2018 1 Computers A computer is a machine which can perform a set of calculations. The purpose of this course is to give you

More information

Beyond Blocks: Python Session #1

Beyond 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 information

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control Chapter 2 Control, Quick Overview Control Selection Selection Selection is how programs make choices, and it is the process of making choices that provides a lot of the power of computing 1 Python if statement

More information

2 nd Week Lecture Notes

2 nd Week Lecture Notes 2 nd Week Lecture Notes Scope of variables All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous

More information

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming Lecture Numbers Richard E Sarkis CSC 161: The Art of Programming Class Administrivia Agenda To understand the concept of data types To be familiar with the basic numeric data types in Python To be able

More information

Introduction to Python

Introduction to Python Introduction to Python Reading assignment: Perkovic text, Ch. 1 and 2.1-2.5 Python Python is an interactive language. Java or C++: compile, run Also, a main function or method Python: type expressions

More information

Computing with Numbers

Computing with Numbers Computing with Numbers Example output: Numeric Data Types Numeric Data Types Whole numbers are represented using the integer data type (int for short).values of type int can be positive or negative whole

More information

Math Content

Math Content 2013-2014 Math Content PATHWAY TO ALGEBRA I Hundreds and Tens Tens and Ones Comparing Whole Numbers Adding and Subtracting 10 and 100 Ten More, Ten Less Adding with Tens and Ones Subtracting with Tens

More information

Chapter 17. Fundamental Concepts Expressed in JavaScript

Chapter 17. Fundamental Concepts Expressed in JavaScript Chapter 17 Fundamental Concepts Expressed in JavaScript Learning Objectives Tell the difference between name, value, and variable List three basic data types and the rules for specifying them in a program

More information

Computer 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 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 information

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

Review 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 information

Introduction to Python and programming. Ruth Anderson UW CSE 160 Winter 2017

Introduction to Python and programming. Ruth Anderson UW CSE 160 Winter 2017 Introduction to Python and programming Ruth Anderson UW CSE 160 Winter 2017 1 1. Python is a calculator 2. A variable is a container 3. Different types cannot be compared 4. A program is a recipe 2 0.

More information

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java Chapter 3 Syntax, Errors, and Debugging Objectives Construct and use numeric and string literals. Name and use variables and constants. Create arithmetic expressions. Understand the precedence of different

More information