Programming for Engineers in Python Sawa 2015 Lecture 1: Introduction to Python 1.

Slides:



Advertisements
Similar presentations
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Advertisements

1 CS101 Introduction to Computing Lecture 17 Algorithms II.
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
C Programming for engineers Teaching assistant: Ben Sandbank Home page:
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Program Design and Development
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to a Programming Environment
Introduction to C Programming
Recitation 1 Programming for Engineers in Python.
1 Computing for Engineers in Python Autumn
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
The University of Texas – Pan American
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
1 Programming for Engineers in Python Autumn Lecture 2: More Basics.
An Introduction to Textual Programming
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
General Programming Introduction to Computing Science and Programming I.
Introduction to Computational Linguistics Programming I.
1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Flow of Control Part 1: Selection
Python Conditionals chapter 5
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Python Let’s get started!.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 1.
Python 1 SIGCS 1 Intro to Python March 7, 2012 Presented by Pamela A Moore & Zenia C Bahorski 1.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
CS100Lecture 21 Announcements For homework due Thursday, work alone -- do not work in pairs New class location: Olin 155 Office hour oops! Lyn: MW, 11:15-12:15.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Basic concepts of C++ Presented by Prof. Satyajit De
Introduction to Computing Science and Programming I
CMPT 120 Topic: Python’s building blocks -> More Statements
Whatcha doin'? Aims: To start using Python. To understand loops.
Topic: Python’s building blocks -> Variables, Values, and Types
CE221 – Data Structures & Algorithms Lab Introduction
Python Let’s get started!.
Introduction to Python
Topic: Python’s building blocks -> Variables, Values, and Types
IF statements.
Introduction to Programming
Primitive Data, Variables, Loops (Maybe)
Variables, Expressions, and IO
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Programming for Engineers in Python
Introduction to Programming
Learning to Program in Python
Lecture 2 Python Programming & Data Types
T. Jumana Abu Shmais – AOU - Riyadh
CS 100: Roadmap to Computing
Introduction to Programming
Lecture 2 Python Programming & Data Types
Chapter 2 Programming Basics.
Unit 3: Variables in Java
Introduction to Programming
CS 100: Roadmap to Computing
CSCI 203: Introduction to Computer Science I
Hardware is… Software is…
Introduction to Python
Presentation transcript:

Programming for Engineers in Python Sawa 2015 Lecture 1: Introduction to Python 1

2 Welcome to Programming for Engineers course! We will learn to program in Python. Goal: enable you to use programming as a tool to solve "real world" problems. Hard work is required!

3 Course Objectives Develop basic programming and algorithmic skills

4 Administration Lectures Lena Dankin lenadank at tau.post.ac.il Recitations Aviad Panhi Web Sites: Moodle

5 Administration Website via Moodle: Everything is there: Lectures Practical sessions Homework assignments, submission, grading Code examples Forum Announcements Make sure you are updated.

6 Recitations Purposes: Practice topics presented in class. Learn practical tools. Help in overcoming trivial difficulties Lectures will be harder to understand, and it is OK.

7 Home Assignments Short assignments every day. 3-4 home assignments that should be submitted. Some more assignments to practice the material, no need to submit.

8 Homework Submission in singles via Moodle It is allowed (and encouraged) to talk, share ideas, and help friends. No code-sharing!

9 A Personal Note on HW It will take time and effort to make the code work. But There is no other way to learn how to program

10 Exam Written exam Includes all course material You must pass the exam to pass the course.

11 Working Environment Lab 008 Home versus lab VS.

12 Syllabus Python programming basics Recursion Sort & Search algorithms Runtime Analysis Error Handling Object-Oriented Programming Image Processing Scientific Calculations Simulations Data analysis

13 Resources Course slides and pointers to relevant bibliography. Many Python references, tutorials or manuals. Recommended resources: Book: Think Python, by Allen B. Downey ( ) Manual: Python 2.x documentation (the official language manual) Course’s website

14 Questions?

15 Preface We assume no prior knowledge in programming. However, we advance fast. The only way to keep on track is to practice!

16 Today Brief background Python basics: Variables numbers Strings

17  Computers understand only machine language.  Basically looks like a sequence of 1’s and 0’s.  Very inconvenient to work with and non-intuitive.  All other computer languages were created for human convenience.  The computer does not understand C/Python/Java, it must be "translated" into machine language Machine Code (Language)

18 Programming Languages Basics A computer program is a sequence of instructions (text) that can be "understood" by a computer and executed. A programming language is a machine-readable artificial language designed to express computations that can be performed by a computer.

19 There are Many Programming Languages Over 500 different computer languages are listed by Wikipedia

20

21 Language Selection and Python Consider: Goal Runtime vs. Development time Operating systems Python: Quick development, small to medium tasks: Experiments / Data analysis / Small projects Short development-execution rounds

22 Computer Program (more technically) A sequence of processor instructions designed to achieve a specific purpose Unless otherwise specified, the instructions are executed sequentially, in the order they’re written in the program. In this course, we assume that no two instructions can execute simultaneously.

23 Installing and Running Python 2.7 Python 2.7 is already installed in the computers’ classroom Install: Anaconda: A “Anaconda" folder will be created in you Start Menu: IDLE (Python GUI)‎ - recommended working environment Python (command line)‎ - textual Python interpreter (faster, less convenient) A Working with IDLE video:

24 Hello World!

25 My First Python Program: Hello World!

26 Hands On

27 Memory The computer memory is composed of a long list of bits (0 and 1) Bits are grouped into bytes (8 bits) and words (4 bytes, 8 on 64-bit systems) Every byte is numbered sequentially This number is called an address

What are Variables ? A location in the computer’s memory. A variable: -has a name -holds a value -has type – according to its value (“duck typing”) This is how data is handled 28

29 Why do We Need Variables? Computer programs manipulate data Data is given as input or calculated throughout the program To access them later, variables must be remembered Thus, variables are stored in the memory Variable name  memory address

30 Why Do We Need Different Types? Saving memory Execution speed

Numbers and their Types >>> 4 4 >>> type(4) # integers type >>> >>> type( ) # floating point ("reals") type Arithmetic operations: +, -, *, /, % (modulo), ** (power) What type is 8/5 ? And 8/5.0 ? Let’s check…

Variables and Assignments >>> n = 10 >>> m = (n+4)*5 The variable's name is a sequence of letters and digits, starting with a letter. The interpreter: 1. evaluates the expression 2. assigns its value to the variable nm VariableExpression

Variables and Assignments: An Example Changing the value of a variable: >>> n = 11 >>> n 11 Changing the type of a variable: >>> n = >>> n Variables can be used in expressions: >>> pi = >>> pi *

Variables and Assignments – Cont. Referring to undefined variables leads to runtime error >>> check_this Traceback (most recent call last): File " ", line 1, in check_this NameError: name 'check_this' is not defined

35 Arithmetic Operators OperatorUseDescription +x + yAdds x to y -x - ySubtracts x from y *x * yMultiplies x by y **x ** yX to the power y /x / yDivides x by y %x % yComputes the remainder of dividing x by y

36 Playing with Variables >>> a = 3 >>> a 3 >>> b = 5 5 >>> c = a + b >>> c 8 >>> c = c * 2 16 >>> b**a 125 >>> first = (a + b) * 2 >>> second = a + b * 2 >>> first, second (16, 13) >>> b / a 1 >>> b % a 2

Strings Strings are text sequences. An ordered list of characters 37

38 String Type

Strings Access >>> a = ‘Hello’ >>> a[1:3] 'el' >>> a[1:] 'ello' >>> a[-4:-2] 'el' >>> a[:-3] 'He' >>> a[-3:] 'llo’

40 Strings concatenation >>> s1 = "He" >>> s2 = "llo" >>> s3 = s1 + s2 >>> s3 'Hello' >>> s4 = s3 + " World" >>> c = "!" >>> print s4, 2014, c Hello World 2014 !

41 Strings Structure

42 Strings Built In Methods

43 Comparison Operators OperatorNameDescription x < y Less thantrue if x is less than y, otherwise false. x > y Greater thantrue if x is greater than y, otherwise false. x <= y Less than or equal to true if x is less than or equal to y, otherwise false. x >= y Greater than or equal to true if x is greater than or equal to y, otherwise false. x == y Equaltrue if x equals y, otherwise false. x != y Not Equaltrue if x is not equal to y, otherwise false. Compares two variables and returns a Boolean type result/variable

Comparison Operators >>> 5 == 5.0 True >>> 6 != 2*3 False >>> -2 >= 1 False >>> 3 <= 3 True >>> x = 3 < 3 >>> x False

45 Logical Operators Operates on two Booleans and returns a Boolean OperatorDescription x and y Both True: True, otherwise: False. x or y At least one is rue: True, Otherwise: False. not x x is False  True, x is True  False

46 And, or, not and or not

47 Flow Control Different inputs  Different execution order –Computer games –Illegal input Control structures –if-else –for loop –while loop

48 Conditional Statement: if Used to execute statements conditionally Syntax if condition: statement1 statement2 … If condition is True, statements are executed Condition = expression that evaluates to a Boolean Indentation = determines the scope of the if block

Conditional Statements

>> num = 54 # choose a number >> if num % 18 == 0: # num is a multiplication of 18 …print num, “is divisible by 18“ …res = num / 18 >>> print “Goodbye” 54 is divisible by 18 Goodbye Conditional Statements - Examples

Conditional Statements Indentation: Following the if statement: Open a new scope = one tab to the right. Indicates the commands within the scope of this if. 51

52 if-else if condition 1 : statement 1 elif condition 2 : statement 2 else: statement 3 condition 1 is true  execute statement 1 condition 1 false and condition 2 true  execute statement 2 both conditions are false  execute statement 3

if-else

55 if-else a = 4 b = 5 c = 6 if a + b >= c and a + c >= b and b + c >= a: print "Building a triangle" else: print "Cannot build a triangle"

56 elif = else-if if price < 100: print "too cheap" elif price > 200: print "too expensive“ else: print "reasonable price"

Program Readability Real computer programs include thousands of code lines, lots of variables. Readability is very important for code maintenance, it is a requirement in this course! The next slide list some programming conventions that are crucial for readability. 57

Program Readability – Cont. Choose informative variable names: –Are i, j, m, n informative names? Depends on context. Comments: –option 1: begin with # (until end of line) –option 2: surrounded by """ (3 double quotes) when spanned over multiple lines Documentation is most important before ‘logical units’ or complex implementation. 58 wsma_string Not informative: word_countsum_of_expensesstudent_name Informative:

Class Example Donuts: Input: Define a variable named donuts and assign to it some number (of donuts). Output: If donuts is no more than 5, print 'Number of donuts: donuts.' Otherwise, if donuts is no more than 10, print 'Number of donuts: donuts...' Otherwise, print 'Number of donuts: a lot!' Note: the trailing ‘.’ / ‘…’ / ‘!’ must be printed without any space before them. Run your program. Examples are in the next slide. 59

Class Example – Cont. Examples: For 5 donuts you should get: Number of donuts: 5. For 9 donuts you should get: Number of donuts: 9... For 23 donuts you should get: Number of donuts: a lot! 60

Solution to Class Example donuts = 7 if donuts <= 5: print 'Number of donuts:', str(donuts) + '.' elif donuts <= 10: print 'Number of donuts:', str(donuts) + '...' else: print 'Number of donuts: a lot!' 61

“My Code doesn’t Work!” Go over your code carefully. Consult course slides. Google (a useful and legitimate source). Good forums to ask questions online: stackoverflow.com, underwar.co.ilstackoverflow.comunderwar.co.il Check the public forum in Moodle. Submit a question to forum (reply within 48 hours). –Please never paste solution code in the forum. 62

Understanding Errors Traceback (most recent call last): File "C:\Users\John\Desktop\blah.py", line 2, in b = 2 / a ZeroDivisionError: integer division or modulo by zero 63 Where in your program? The last line of the error is most informative.