pynotes.txt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. Variables, Expressions and Statements
  2. One of the most powerful features of a programming language is the ability to manipulate
  3. variables. A variable is a name that refers to a value.
  4. Assignment Statements
  5. An assignment statement creates a new variabble and gives it a value. A common way to
  6. represent variables on paper is to write the name with an arrow pointing to its value. This
  7. kind of figure is called a state diagram.
  8. String Operations
  9. In general, you can't perform mathematical operations on strings, even if the strings look
  10. like numbers, so the following are illegal: '2' - '1', 'eggs'/'easy', 'third'*'a charm'
  11. But there are two expections + and *
  12. The + operator performs string concatenation, which means it joins the strings by linking
  13. them end-to-end. For example: >>> first = 'throat' >>> second = 'warbler' >>> first+second
  14. The * operator also works on strings; it performs repetition. For example: 'Spam'*3 is
  15. 'SpamSpamSpam'. If one of the values is a string, the other has to be an integer.
  16. semantics - the meaning of a program
  17. semantic error - an error in a program that makes it do something other than what the
  18. programmer intended
  19. Functions
  20. In the context of programming, a function is a named sequence of statements that performs a
  21. computation. When you define a function, you specify the name and the sequence of statement.
  22. A module is a file that contains a collection of related functions.
  23. >>> import math
  24. This statement creates a module object named math. If you display the module object, you get
  25. some information about it:
  26. >>> math
  27. <module 'math' (built-in)>
  28. The module object contains the functions and variables defined in the module.
  29. >>> math.log10(45) # dot notation
  30. >>> math.sin(radians) # trigonometric functions take arguments in radians. To convert from
  31. degrees to radians, divide by 180 and multiply by pi.
  32. >>> radians = degrees/180 * math.pi # the expression math.pi gets the variable pi from the
  33. math module. The value is a floating-point approximation of pi, accurate to about 15 digitsa
  34. Defining a function creates a function object, which has type function.
  35. Flow of Execution
  36. Execution always begins at the first statement of the program. Statements are run one at a
  37. time, in order from top to bottom. Function definitions do not alter the flow of execution
  38. of the progrm, but statements inside the function don't run until the function is called.
  39. Parameters and Arguments
  40. Inside the function, the arguments are assigned to variables called parameters.
  41. >>> def print_twice(bruce):
  42. print(bruce)
  43. print(bruce)
  44. >>> print_twice(math.cos(math.pi)) # the argument is evaluated before the function is
  45. called, so in the example the expressions math.cos(math.pi) is only evaluated once.
  46. Variables and Parameters are local: When you create a variable inside a function, it is
  47. local, which means that if only exists inside the function.
  48. Programming is the process of gradually debugging a program until it does what you want.
  49. traceback - a list of the functions that are executing, printed when an exception occurs
  50. Encapsulation: Wrapping a piece of code up in a function is called encapsulation. One of the
  51. benefits of encapsulation is that it attaches a name to the code, which serves as a kind of
  52. documentation.
  53. Generalization: Adding a parameter to a function is called generalization because it makes
  54. the cuntion more general.
  55. >>> import turtle as tl
  56. >>> polygon(t, n=7, length=70) # these are called keyword arguments because they include the
  57. parameter names as "keywords".
  58. When we call a function, the arguments are assigned ot the parameters.
  59. Interface: The interface of a function is a summary of how it is used: what are parameters?
  60. What does the function do? And what is the return value? An interface is "clean" if it allow
  61. the caller to do what they want without dealing with unnecessary details.
  62. Refactoring: The process of rearranging a program to improve interfaces and facilitate code
  63. reuse.
  64. Development Plan: Is a process for writing programs. The process includes "encapsulation and
  65. generalization".
  66. docstring is a string at the beginning of a function that explains the interface.
  67. def polyline(t, n, length, angle):
  68. """ Draws n line segments with the given length and
  69. angle (in degrees) between them, t is a turtle.
  70. """
  71. for i in range(n):
  72. t.fd(length)
  73. t.lt(angle)
  74. By convention, all docstrings are triple quoted strings, also known as multiline strings
  75. because the triple quotes allow the string to spin more than one line. It is terse, but it
  76. contains the essential information someone would need to use this function. It explains
  77. concisely what the function does (without getting into the details of how id does it). It
  78. explains what effect each parameter has on the behaviour of the function and what type each
  79. parameter should be (if it is not obvious).
  80. method: A function that is associated with an object and called using the dot notation.
  81. When dealing with objects, functions are known as methods. Besides the terminology, methods
  82. are invoked slightly differently than functions. When you call a function like len, you pass
  83. the arguments in a comma separated list surrounded by parentheses affter the function name.
  84. When you invoke a method, you provide the name of the object the method is to act upon,
  85. followed by a period, finally followed by the method name and the parenthesized list of
  86. additional arguments. Remember to provide empty parentheses if the method does not take any
  87. arguments, so that python can distinguish a method call with no arguments from a referance
  88. to a variable stored within the object.
  89. Strings in python are immutable objects; this means that you can't change the value of a
  90. string in place.
  91. >>> str = 'institute of mathematical sciences'
  92. >>> str.split()
  93. ['institute', 'of', 'mathematical', 'sciences']
  94. >>> str = 'institute of mathematical sciences'
  95. >>> str.split(' ')
  96. ['institute', 'of', 'mathematical', ' ', 'sciences']
  97. >>> str = 'institute-of-mathematical-sciences'
  98. >>> str.split('-')
  99. ['institute', 'of', 'mathematical', 'sciences']
  100. Python offers a variety of so-called predicate methods, which take no arguments, and return
  101. 1 if all the characters in a string are of a particular type, and 0 otherwise. These
  102. functions, whose use should be obvious from their names, include isalnum, isalpha, isdigit,
  103. islower, isspace, istitle, and isupper.