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


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

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/IntroToPython

    v52 v53  
    190190The solution is [wiki:ProgrammingAnswerPotatoExercise here].   This concludes Lesson 2.
    191191
     192
    192193= Lesson 3:  Functions =
    193194So are you tired of just printing stuff yet?   In the last lesson, we introduced you to all sort of ways to make decisions and loop over code to make your program easier to read and shorter too.
     
    205206 * `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.
    206207 * `sum(iterable)`:  Sums all the numbers in the list ''iterable''.
     208 * str.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
    207209
    208210Here's some examples of how they're used (don't forget to look at earlier lessons for range() and print().
     
    223225}}}
    224226
     227You can combine these functions with control structures to do interesting stuff too:
     228{{{
     229#!python
     230while(!x.isdigit())
     231   x=input("Enter x")
     232while(!x.isdigit())
     233   y=input("Enter y")
     234z=pow(x,y)
     235if z>100:
     236  print("That's-a big number!")
     237}}}
     238
    225239----
    226240[[[wiki:ProgrammingReserve Programming]Archive | Programming Archive]]