Changes between Version 51 and Version 52 of ControlSystems/SoftwareTeam/IntroToPython
- Timestamp:
- Sep 4, 2017, 7:20:13 PM (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ControlSystems/SoftwareTeam/IntroToPython
v51 v52 81 81 1. The double equals in location=='HERE' on the first line. It is called an ''expression''. In this case it is an equals expression (other kinds of comparisons in python are listed here) . It returns ''True'' when the condition evaluates to ''True'', otherwise its ''False''. Note on that line the word 'HERE' is in quotes. That's to indicate its a string (a text value) not some variable (which doesn't exist). If you used ''='' instead of ''=='' it wouldn't work as you expected because it would be a variable assignment. If a IF-THEN-ELSE statement ever behaves in an unexpected way, check for this problem first, its a really common mistake. 82 82 1. Spacing and indents are important in Python. the line "We're here..." and the line below it are indented to indicate they are part of a statement ''block'' that runs when the condition evaluates to ''True''. The second block on the else is run when the condition to ''False'', and only contains one line. If you want to do stuff outside of the IF-THEN, don't indent it like the first and last print statements. 83 1. You'll see that a few of the print statements end with a {{\\n}}. This is called a ''escape sequence'', and its shorthand for a newline. When the computer sees it in a print statement, it will add an extra line to the output. See here for other escape sequences you can add.83 1. You'll see that a few of the print statements end with a \n. This is called a ''escape sequence'', and its shorthand for a newline. When the computer sees it in a print statement, it will add an extra line to the output. [[Here|https://www.quackit.com/python/reference/python_3_escape_sequences.cfm]] are some other escape sequences you can use. 84 84 85 85 Now, in the editor replace the line with the if ''expression'' with … … 88 88 if location == 'Poughkeepsie, NY': 89 89 }}} 90 90 91 and run it again. What happens? It should print "Go away, we're out eating tacos". Why? If I was in Poughkeepsie, I'd be out eating tacos too. Yummmm, tacos. 91 92 92 If you're writing your own code, you don't want to provide an ELSE, just omit the word else: and the indented block you want to run with it. For example:93 If you're writing your own code, you don't want to provide an ''else'', just omit the word else: and the indented block you want to run with it. For example: 93 94 94 95 {{{ … … 118 119 print("That's alotta fruit!") 119 120 }}} 121 120 122 It should output: 121 123 … … 200 202 201 203 * `input(prompt)`: Prompts the user for input by showing the prompt screen, returning whatever the user entered after they hit <enter>. 202 * `pow(x,y)`: Return x^y^ 203 * `round(x,[n])`: Rounds a decimal number x to n digits after the decimal point. The [] means that the n is optional, if you don't include it, n=0. 204 * `sum(iterable)`: Sums all the numbers in the list ''iterable''. 205 206 204 * `pow(x,y)`: Return x^y^ 205 * `round(x,[n])`: Rounds a decimal number x to n digits after the decimal point. The [] means that the n is optional, if you don't include it, n=0. 206 * `sum(iterable)`: Sums all the numbers in the list ''iterable''. 207 207 208 208 Here's some examples of how they're used (don't forget to look at earlier lessons for range() and print(). 209 209 210 {{ 211 210 {{{ 211 #!python 212 212 a=input("Pick a number, any number) 213 214 213 print(a); 215 214 216 215 b = pow(2,2) 217 218 print(b) [ticket:4 # Answer is 4] 216 print(b) [ticket:4 # Answer is 4] 219 217 220 218 c = round(3.14159, 2) 221 222 219 print(c) #Answer is 3.14 223 220 224 221 d = sum([1,2,3,4]) 225 226 222 print(d) # Answer is 10 227 228 223 }}} 229 224 230 225 ---- 231 232 226 [[[wiki:ProgrammingReserve Programming]Archive | Programming Archive]]