Changes between Version 55 and Version 56 of ControlSystems/SoftwareTeam/IntroToPython
- Timestamp:
- Sep 4, 2017, 7:41:53 PM (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ControlSystems/SoftwareTeam/IntroToPython
v55 v56 206 206 * `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. 207 207 * `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. 209 209 210 210 Here's some examples of how they're used (don't forget to look at earlier lessons for range() and print(). … … 212 212 {{{ 213 213 #!python 214 a=input(" Pick a number, any number)214 a=input("Enter a number, any number-->") # Enter something at the prompt. Program will wait at this point. 215 215 print(a); 216 216 217 217 b = pow(2,2) 218 print(b) [ticket:4 # Answer is 4]218 print(b) #Result is 4 (2 to the power of 2) 219 219 220 220 c = round(3.14159, 2) 221 print(c) # Answeris 3.14221 print(c) #Result is 3.14 222 222 223 223 d = 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 224 print(d) #Result is 10 225 226 e = "123" 227 f = e.isdigit() 228 print(f) #Result is True 229 }}} 230 231 Try running these yourself, making sure the results are as expected. 232 233 You 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 237 x="" # 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. 238 y="" 230 239 while 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-->") 232 241 while not y.isdigit(): 233 y=input("Enter y ")242 y=input("Enter y-->") 234 243 z = pow(x,y) 235 244 if z > 100: … … 237 246 }}} 238 247 248 But 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 252 Now 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 239 256 ---- 240 257 [[[wiki:ProgrammingReserve Programming]Archive | Programming Archive]]