Changes between Version 51 and Version 52 of ControlSystems/SoftwareTeam/IntroToPython


Ignore:
Timestamp:
Sep 4, 2017, 7:20:13 PM (8 years ago)
Author:
cdelgigante
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/IntroToPython

    v51 v52  
    8181 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.
    8282 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.
    8484
    8585Now, in the editor replace the line with the if ''expression'' with
     
    8888if location == 'Poughkeepsie, NY':
    8989}}}
     90
    9091and 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.
    9192
    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:
     93If 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:
    9394
    9495{{{
     
    118119print("That's alotta fruit!")
    119120}}}
     121
    120122It should output:
    121123
     
    200202
    201203 * `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''.
    207207
    208208Here's some examples of how they're used (don't forget to look at earlier lessons for range() and print().
    209209
    210 {{
    211 
     210{{{
     211#!python
    212212a=input("Pick a number, any number)
    213 
    214213print(a);
    215214
    216215b = pow(2,2)
    217 
    218 print(b) [ticket:4 # Answer is 4]
     216print(b) [ticket:4 # Answer is 4]
    219217
    220218c = round(3.14159, 2)
    221 
    222219print(c) #Answer is 3.14
    223220
    224221d = sum([1,2,3,4])
    225 
    226222print(d) # Answer is 10
    227 
    228223}}}
    229224
    230225----
    231 
    232226[[[wiki:ProgrammingReserve Programming]Archive | Programming Archive]]