Changes between Version 55 and Version 56 of ControlSystems/SoftwareTeam/IntroToPython


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

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/IntroToPython

    v55 v56  
    206206 * `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.
    207207 * `sum(iterable)`:  Sums all the numbers in the list ''iterable''.
    208  * `<string>.isdigit()`:   Strings and some other variable types like lists have some special functions just for them, called methods.  This one returns true of all of the digits in the string are numbers, others are [[https://docs.python.org/3/library/stdtypes.html#string-methods|here]].   '123' would return ''True'', "Cat" would return ''False''. Handy for validation.  You call them *on* the variable itself.
     208 * `<string>.isdigit()`:   Strings and some other variable types like lists have some special functions just for them, called methods.  This one returns true of all of the digits in the string are numbers, others are [[https://docs.python.org/3/library/stdtypes.html#string-methods|here]].   '123' would return ''True'', "Cat" would return ''False''. Handy for validation.  You call them *on* the variable itself, and it has to have a value before you can call these methods.
    209209
    210210Here's some examples of how they're used (don't forget to look at earlier lessons for range() and print().
     
    212212{{{
    213213#!python
    214 a=input("Pick a number, any number)
     214a=input("Enter a number, any number-->")  # Enter something at the prompt.  Program will wait at this point.
    215215print(a);
    216216
    217217b = pow(2,2)
    218 print(b) [ticket:4 # Answer is 4]
     218print(b) #Result is 4 (2 to the power of 2)
    219219
    220220c = round(3.14159, 2)
    221 print(c) #Answer is 3.14
     221print(c) #Result is 3.14
    222222
    223223d = sum([1,2,3,4])
    224 print(d) # Answer is 10
    225 }}}
    226 
    227 You can combine these functions with control structures to do interesting stuff too:
    228 {{{
    229 #!python
     224print(d) #Result is 10
     225
     226e = "123"
     227f = e.isdigit()
     228print(f) #Result is True
     229}}}
     230
     231Try running these yourself, making sure the results are as expected.
     232
     233You can even combine these functions with control structures to do some interesting stuff.  For example, try running this (entering values for x and why when prompted.  Try putting in non-numeric values too):
     234
     235{{{
     236#!python
     237x="" # Put a dummy string in x and y so both while-nots below work.  Pretty slick, eh?  Programming uses tricks like this all the time.
     238y=""
    230239while not x.isdigit():  # Note the "not"...in this case if x is not all digits, run the block of code in the while loop
    231    x=input("Enter x")
     240   x=input("Enter x-->")
    232241while not y.isdigit():
    233    y=input("Enter y")
     242   y=input("Enter y-->")
    234243z = pow(x,y)
    235244if z > 100:
     
    237246}}}
    238247
     248But wait, there's more...you can create your own functions.  That way if you come up with cool way to do something you dont have to repeat yourself as much.  For instance:
     249
     250
     251
     252Now your turn...  In the 'That's-a big number exercise above" make a function called inputNumber, and modify it to use that instead of the while-not stuff.  Much easier to read, no?
     253
     254
     255
    239256----
    240257[[[wiki:ProgrammingReserve Programming]Archive | Programming Archive]]