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
Slide 3
Administration Yoav Ram Email: [email protected] Office
hours: by appointment only Location: Room 409, Britannia building
Noga Levy Email: [email protected] Office hours: by appointment
only Location: Room 405a, Shenkar building
Slide 4
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)
Slide 5
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
Slide 6
Submission Guidelines Submission in singles! Should work on
Python 2.7 No cheating! Guidelines in the course site. How to
handle doesnt 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)
Slide 7
Working Environment Install (at home): Windows 32 bit:
http://www.python.org/ftp/python/2.7.2/python- 2.7.2.msi
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)
Slide 8
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
Slide 9
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
Slide 10
IDLE Editor Cont. Run Python: The output appears in the Shell
window (a new Shell might open)
Slide 11
What are Variables ? A location in the computers memory. A
variable - has a name (for access) - holds a value - has type
according to its value - This is how data is handled
Slide 12 >> 4 4 >>> type(4) # integers type
>>> 3.14159 3.14159 >>> type(3.14159) # floating
point ("reals") type Arithmetic operation">
Numbers and their Types >>> 4 4 >>> type(4) #
integers type >>> 3.14159 3.14159 >>>
type(3.14159) # floating point ("reals") type Arithmetic
operations: +, -, *, /, % (modulo), ** (power) What type is 8/5 ?
And 8/5.0 ? Lets check
Slide 13
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. 10 70 nm
Slide 14
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
Slide 15
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
Slide 16
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
Slide 17
Strings We already met Strings in class. Strings are text
sequences. They are actually an ordered list of characters
Slide 18 >> mssg2=" be light" >>> mssg1+mssg2
'Let there be light What will the next expression print?
>>> mssg1 + mssg"> >> mssg2=" be light"
>>> mssg1+mssg2 'Let there be light What will the next
expression print? >>> mssg1 + mssg2*2"> >>
mssg2=" be light" >>> mssg1+mssg2 'Let there be light What
will the next expression print? >>> mssg1 + mssg"
title="Strings Cont. >>> mssg1="Let there" >>>
mssg2=" be light" >>> mssg1+mssg2 'Let there be light What
will the next expression print? >>> mssg1 + mssg">
Strings Cont. >>> mssg1="Let there" >>>
mssg2=" be light" >>> mssg1+mssg2 'Let there be light What
will the next expression print? >>> mssg1 + mssg2*2
Boolean Variables Comparison : Numbers by their order Strings
by lexicographical order Returns boolean variabels: True or False.
Comparison types: = != > < >=
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
Slide 24
Conditional Statements if : do something [else: do something
else]
Slide 25 >> 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 18 indentation: following the if statement: open a new
scope tab to right. indicates commands within of this if. else -
outside that scope. note: ! =four spaces, even it looks
identical!"> >> if 54% 18 == 0: # the remainder of 54
divided by 18 print 54 is divisible by 18">
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!
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+++
Slide 28
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.
Slide 29
Exercise Unit Testing My Code Test (1) Run my code with some
input (2) Check the output
Slide 30
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 Examples 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'