ecs10-10-6 - 10/6/2008 The if statement dissected Look Look...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 10/6/2008 The if statement dissected Look Look at its separate parts: ECS 10 10/6 if name == ‘’ : name = ‘Anonymous’ if if is a Python commands ‘’ ‘’ and ‘Anonymous’ are strings Test Test - name == ‘’- is actually an expression – ‘’a new kind… Boolean Boolean expressions Values Values of Boolean expressions are either True or False Boolean Boolean is a new data type. We now have four data types (and four kinds of of expressions): integer, floating point, string and Boolean. Boolean expressions Have Have value True or False True False Examples: Examples: answer == ‘R’ x < 30 30 2+2 == 4 2+2 <= 5 ‘cat’ != ‘Cat’ not equals Boolean algebra Named Named after George Boole (1815-1864) (1815Main Main idea: you can write down logic as mathematical formulas, as well as in sentences. His His book: An Investigation An into the Laws of Thought Computers Computers do logical as well as numerical and string computation. True True and False These These are the only two possible Boolean data items. NOT NOT descriptions of expressions; actual data. Maybe better names would be Truth and Falsehood, Falsehood, so we think of them as things. things We We can not only store True and False in variables, we can compute with them. 1 10/6/2008 and and or and and and or are the operators on Boolean data >>> 5+6 11 >>> (2<3) and (1<3) True Boolean, Boolean, operator, Boolean – resulting data is Boolean or >>> True or True True >>> True or False True >>> False or True True >>> False or False False Memorize this! this! Use to check input or or operator: Converting types int('5')/2 Converts the string ‘5’ to the integer 5 to if (reply == ‘D’) or (reply == ‘R’): and and operator: if (reply != ‘D’) and (reply != ‘R’): Converting Data Convert Convert an integer to a string: x = str(10) x is the string ‘10’ is y = float(‘8.3333’) y is the floating point number 8.3333 Getting numbers from the user reply = raw_input(‘Feet: ‘) feet = int(reply) reply = raw_input(‘Inches: ‘) inches = int(reply) Notice we use the variable reply twice. When we put the second string into reply, the first one gets written over. It’s gone! 2 10/6/2008 Body mass index Kilograms / (meters2) Or, Or, in `English Imperial Units’ Yao Ming (Pounds * 703) / (inches2) 25 25 or greater is overweight. 7 feet, 6 inches. 310 pounds. 3 ...
View Full Document

This note was uploaded on 03/23/2011 for the course ECS 10 taught by Professor Amenta during the Winter '07 term at UC Davis.

Ask a homework question - tutors are online