Recitation 1 Programming for Engineers in Python.

Slides:



Advertisements
Similar presentations
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Advertisements

Program Design and Development
Introduction to Python and programming Michael Ernst UW CSE 190p Summer 2012.
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.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Programming for Engineers in Python Sawa 2015 Lecture 1: Introduction to Python 1.
Introduction to Computational Linguistics Programming I.
Python Lesson Week 01. What we will accomplish today Install Python on your computer Using IDLE interactively to explore String Variables if/else while.
1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables.
Variables and Expressions CMSC 201 Chang (rev )
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Introduction to Python and programming Ruth Anderson UW CSE 140 Winter
Python Conditionals chapter 5
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 4: Writing programs.
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.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Variables, Types, Expressions Intro2CS – week 1b 1.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Introduction to Programming Python Lab 6: Relational Operators and Boolean Variables 12 February PythonLab6 lecture slides.ppt Ping Brennan
Introduction to Programming Python Lab 8: Loops 26 February PythonLab8 lecture slides.ppt Ping Brennan
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Introduction to Programming
Fundamentals of Programming I Overview of Programming
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Introduction to Programming
Whatcha doin'? Aims: To start using Python. To understand loops.
Topic: Python’s building blocks -> Variables, Values, and Types
Introduction to Programming
CMSC201 Computer Science I for Majors Lecture 04 – Expressions
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Introduction to Programming
Introduction to Computer Science / Procedural – 67130
Introduction to Python and programming
Introduction to Programming
Introduction to Programming
The Development Environment and Your First C Program
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Engineering Innovation Center
Programming for Engineers in Python
Introduction to Python and programming
Introduction to Programming
Introduction to Programming
Introduction to Python and programming
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Introduction to Programming
CS 100: Roadmap to Computing
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Python
Introduction to Python and programming
CHAPTER FOUR VARIABLES AND CONSTANTS
Introduction to Programming
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Introduction to Programming
Unit 3: Variables in Java
Introduction to Programming
Introduction to Programming
Introduction to Programming
CS 100: Roadmap to Computing
Introduction to Python and programming
Presentation transcript:

Recitation 1 Programming for Engineers in Python

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 Office hours: by appointment only Location: Room 409, Britannia building Noga Levy Office hours: by appointment only Location: Room 405a, Shenkar building

Course Site - Moodle 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 % 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 /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: msi 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) # 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=(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 nm

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

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’ olleH

Homework 0

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

Python Comparison Operators >>> 5 == 5.0 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 : 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: ’. 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 My Code Test (1) Run my code with some input (2) Check the output

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'