Programming for Engineers in Python. Recitation 1

Similar documents
Programming for Engineers in Python. Autumn

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

Fundamentals of Programming (Python) Getting Started with Programming

cs1114 REVIEW of details test closed laptop period

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

Introduction to Programming

Getting Started with Python

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

Decisions, Decisions. Testing, testing C H A P T E R 7

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

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

Name & Recitation Section:

Algorithms and Programming I. Lecture#12 Spring 2015

Not-So-Mini-Lecture 6. Modules & Scripts

The current topic: Python. Announcements. Python. Python

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Introduction to Programming

Python Games. Session 1 By Declan Fox

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

Conditional Expressions and Decision Statements

Lab 1: Course Intro, Getting Started with Python IDLE. Ling 1330/2330 Computational Linguistics Na-Rae Han

Lecture 4. Defining Functions

Lecture 2: Variables & Assignments

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

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture 3. Functions & Modules

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

Introduction to Python

CS1110 Lab 1 (Jan 27-28, 2015)

Introduction to Computation for the Humanities and Social Sciences. CS 3 Chris Tanner

CMSC 201 Computer Science I for Majors

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Lecture 1. Types, Expressions, & Variables

CS1 Lecture 3 Jan. 22, 2018

PREPARING FOR PRELIM 1

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

Fundamentals: Expressions and Assignment

WELCOME! (download slides and.py files and follow along!) LECTURE 1

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

CSC108: Introduction to Computer Programming. Lecture 1

Ch.2: Loops and lists

Objectives. Structure. Munster Programming Training

CMSC 201 Fall 2018 Python Coding Standards

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

CS 115 Lecture 8. Selection: the if statement. Neil Moore

CSE 115. Introduction to Computer Science I

Introduction to computers and Python. Matthieu Choplin

Session 1: Introduction to Python from the Matlab perspective. October 9th, 2017 Sandra Diaz

Programming for Engineers in Python. Recitation 2

CS1114 Spring 2015 Test ONE ANSWER KEY. page 1 of 8 pages (counting the cover sheet)

Lecture 3. Input, Output and Data Types

CMSC201 Computer Science I for Majors

My First Python Program

Lecture 1. Course Overview, Python Basics

CS1 Lecture 3 Jan. 18, 2019

Text Input and Conditionals

Introduction to Python (All the Basic Stuff)

Unit 6 - Software Design and Development LESSON 4 DATA TYPES

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

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

Course Outline Introduction to C-Programming

CS 1110, LAB 10: ASSERTIONS AND WHILE-LOOPS 1. Preliminaries

CIS192 Python Programming. Robert Rand. August 27, 2015

Introduction to Python

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

Introduction to Programming

Course Overview, Python Basics

MEIN 50010: Python Introduction

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

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

Introduction to Programming

Conditionals & Control Flow

You must pass the final exam to pass the course.

Python for Informatics

Computer Lab 1: Introduction to Python

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

CIS192: Python Programming

A simple interpreted language

CS 1301 Exam 1 Answers Fall 2009

Programming in C++ PART 2

Lecture 7. Memory in Python

STEAM Clown Productions. Python - Conditional. STEAM Clown & Productions Copyright 2018 STEAM Clown. Last Updated: Thursday, January 24, 2019.

Lecture 1. Course Overview, Python Basics

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

Lecture 4: Basic I/O

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Lecture 3. Functions & Modules

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Programming with Python

1.00/1.001 Tutorial 1

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

Lecture 2. Variables & Assignment

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Lecture 3. Strings, Functions, & Modules

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

AP Computer Science A

Transcription:

Programming for Engineers in Python Recitation 1

Plan Administration: Course site Homework submission guidelines Working environment Python: Variables Editor vs. shell Homework 0 Python Cont. Conditional Statements (if/else) Discuss Homework 1

Administration Yoav Ram Email: yoavram@post.tau.ac.il Office hours: by appointment only Location: Room 409, Britannia building Noga Levy Email: noga@post.tau.ac.il Office hours: by appointment only Location: Room 405a, Shenkar building

Course Site - Moodle http://moodle.tau.ac.il All relevant material: Slides for the lectures + recitations, homework, solutions, code examples. Automatic homework submission, (manual) grades. Forum anything you want to clarify from the lecture, recitations and homework. Announcements Instructions (how to set a working environment at home)

Homework Very important when learning to program! Therefore: Weekly hands-on assignments. Strict submission dates. 15-20% of the final grade. Note that: Grades: 0 / 60 / 80 / 100. No appeals. Where can I work? Computer lab 06, open: 8:00 20:00, use email/disk-on-key

Submission Guidelines Submission in singles! Should work on Python 2.7 No cheating! Guidelines in the course site. How to handle doesn t work situation: Go over the code Consult course slides Google (a useful and legitimate source) Check the forum in moodle Submit a question to forum (reply within 48 hours)

Working Environment Install (at home): Windows 32 bit: http://www.python.org/ftp/python/2.7.2/python- 2.7.2.msi Continue as in class Open: Start Menu All Programs Python 2.7 IDLE (Python GUI)

IDLE Editor We want to save a sequence of commands and run it later in a new Python session. The Editor: - Write Python commands - Execute them in one key-press. Open the editor from the Shell: File New Window

IDLE Editor Cont. The new window is Untitled. First choose a title: In the new window: File Save as Chose a folder and a name. The name must end with.py

IDLE Editor Cont. Run Python: The output appears in the Shell window (a new Shell might open)

What are Variables? A location in the computer s memory. A variable - has a name (for access) - holds a value - has type according to its value - This is how data is handled

Numbers and their Types >>> 4 4 >>> type(4) <class 'int'> # integers type >>> 3.14159 3.14159 >>> type(3.14159) <class 'float'> # 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=(10+4)*5 The left hand side is a variable. The right hand side is an expression. The interpreter evaluates the expression and assigns its value to the variable. The variable's name is a sequence of letters and digits, starting with a letter. n m 10 70

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

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

Documentation and Variable Names 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! Hence: Choose informative variable names: Not informative: i, j, m, n, astring, doessomething Informative: sumofexpenses, studentname, countwords Documentation add remarks ( # ) before: Functions Logical units of code complex implementation

Strings We already met Strings in class. Strings are text sequences. They are actually an ordered list of characters

Strings Cont. >>> mssg1="let there" >>> mssg2=" be light" >>> mssg1+mssg2 'Let there be light What will the next expression print? >>> mssg1 + mssg2*2

Strings Access - Reminder >>> a= Hello >>> a[1:3] 'el' >>> a[1:] 'ello' >>> a[-4:-2] 'el' >>> a[:-3] 'He' >>> a[-3:] 'llo H 0-5 e 1-4 l 2-3 l 3-2 o 4-1

Homework 0 Let the work begin: Download Homework 0 from the course website. Follow instructions Submit the file [your_name]_a0_q1.py through the course website.

Boolean Variables Comparison : Numbers by their order Strings by lexicographical order Returns boolean variabels: True or False. Comparison types: =!= > < >= <=

Python Comparison Operators >>> 5.0 == 5 True >>> 6!= 2*3 False >> not(-2 >= 1) True >>> (-2 >= 1) or (-2 <= -1) True >>> (-2 >= 1) and (-2 <= -1) False

Variables - Status We saw the classes int, 'float', 'str, bool. Some operations allow mixing" variables of different types. Assignments: variable name = expression Subsequent assignments to the same variable can change its value and even its type. int integer numbers, float real numbers. True and False are Boolean constants

Conditional Statements if <condition>: do something [else: do something else]

Conditional Statements - Examples >>> if 54% 18 == 0: # the remainder of 54 divided by 18 print 54 is divisible by 18" else: print 54 is not divisible by 18" 54 is not divisible by 18 Indentation: Following the if statement: Open a new scope = one tab to the right. Indicates the commands within the scope of this if. else - outside that scope. Note: tab!= four spaces, even if it looks identical!

Functions - Reminder def function_name(input1, input2, ): command1 command2

Exercise Donuts: Input: count - an int representing the number of donuts Output: a string of the form 'Number of donuts: <count>. However, if the count is 10 or more, use the word 'many instead of the actual count. Examples: >>> donuts(5) Number of donuts: 5 >>> donuts(23) Number of donuts: many Function prototype: def donuts(count): # +++your code here+++

Solution def donuts(count): if count < 10: return 'Number of donuts:,str(count) else: return 'Number of donuts: many Return instead of print explanation in the next slide.

Exercise Unit Testing Test (1) Run my code with some input (2) Check the output My Code

Exercise Unit Testing Use the given implementation: - Download hw1.py - Fill in the necessary code instead of the remarks # +++your code here+++ - Instead of printing the code to the shell, return it to the testing function. - Run Module Example s output: OK got: 'Number of donuts: 4' expected: 'Number of donuts: 4' OK got: 'Number of donuts: 9' expected: 'Number of donuts: 9' OK got: 'Number of donuts: many' expected: 'Number of donuts: many' OK got: 'Number of donuts: many' expected: 'Number of donuts: many'