You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: python_notes/Classes.rst
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,11 +60,11 @@ Here is an example of a simple custom class which stores information about a per
60
60
61
61
We start the class definition with the ``class`` keyword, followed by the class name and a colon. We would list any parent classes in between round brackets before the colon, but this class doesn't have any, so we can leave them out.
62
62
63
-
Inside the class body, we define two functions -- these are our objects's methods. The first is called ``__init__``, which is a special method. When we call the class object, a new instance of the class is created, and the ``__init__`` method on this new object is immediately executed with all the parameters that we passed to the class object. The purpose of this method is thus to set up a new object using data that we have provided.
63
+
Inside the class body, we define two functions -- these are our object's methods. The first is called ``__init__``, which is a special method. When we call the class object, a new instance of the class is created, and the ``__init__`` method on this new object is immediately executed with all the parameters that we passed to the class object. The purpose of this method is thus to set up a new object using data that we have provided.
64
64
65
65
The second method is a custom method which calculates the age of our person using the birthdate and the current date.
66
66
67
-
.. Note:: ``__init__`` is sometimes called the object's *constructor*, because it is used similarly to the way that constructors are used in other languages, but that is not technically correct -- it's better to call it the *initializer*. There is a different method called ``__new__`` which is more analogous to a constructor, but it is hardly ever used.
67
+
.. Note:: ``__init__`` is sometimes called the object's *constructor*, because it is used similarly to the way that constructors are used in other languages, but that is not technically correct -- it's better to call it the *initialiser*. There is a different method called ``__new__`` which is more analogous to a constructor, but it is hardly ever used.
68
68
69
69
You may have noticed that both of these method definitions have ``self`` as the first parameter, and we use this variable inside the method bodies -- but we don't appear to pass this parameter in. This is because whenever we call a method on an object, *the object itself* is automatically passed in as the first parameter. This gives us a way to access the object's properties from inside the object's methods.
Copy file name to clipboardExpand all lines: python_notes/Errors_and_Exceptions.rst
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -75,7 +75,7 @@ Sometimes there can be absolutely nothing wrong with your Python implementation
75
75
76
76
* using the wrong variable name
77
77
* indenting a block to the wrong level
78
-
* using integer division instead of floatingpoint division
78
+
* using integer division instead of floating-point division
79
79
* getting operator precedence wrong
80
80
* making a mistake in a boolean expression
81
81
* off-by-one, and other numerical errors
@@ -121,7 +121,7 @@ Handling exceptions
121
121
122
122
Until now, the programs that we have written have generally ignored the fact that things can go wrong. We have have tried to prevent runtime errors by checking data which may be incorrect before we used it, but we haven't yet seen how we can handle errors when they do occur -- our programs so far have just crashed suddenly whenever they have encountered one.
123
123
124
-
There are some situations in which runtime errors are likely to occur. Whenever we try to read a file or get input from a user, there is a chance that something unexpected will happen -- the file may have been moved or deleted, and the user may enter data which is not in the right format. Good programmers should add safeguards to their programs so that common situations like this can be handled gracefully -- a program which crashes whenever it encounters an easily forseeable problem is not very pleasant to use. Most users expect programs to be robust enough to recover from these kinds of setbacks.
124
+
There are some situations in which runtime errors are likely to occur. Whenever we try to read a file or get input from a user, there is a chance that something unexpected will happen -- the file may have been moved or deleted, and the user may enter data which is not in the right format. Good programmers should add safeguards to their programs so that common situations like this can be handled gracefully -- a program which crashes whenever it encounters an easily foreseeable problem is not very pleasant to use. Most users expect programs to be robust enough to recover from these kinds of setbacks.
125
125
126
126
If we know that a particular section of our program is likely to cause an error, we can tell Python what to do if it does happen. Instead of letting the error crash our program we can intercept it, do something about it, and allow the program to continue.
127
127
@@ -491,7 +491,7 @@ Answer to exercise 1
491
491
492
492
#.
493
493
494
-
#. The values entered by the user may not be valid integers or floatingpoint numbers.
494
+
#. The values entered by the user may not be valid integers or floating-point numbers.
495
495
#. The user may enter zero for the divisor.
496
496
#. If the ``math`` library hasn't been imported, ``math.round`` is undefined.
497
497
@@ -620,4 +620,4 @@ Answer to exercise 4
620
620
logging.info("%s already has %d elements." % (listname, len(l)))
621
621
finally:
622
622
thedict[listname].append(element)
623
-
logging.info("Added %s to %s." % (element, listname))
623
+
logging.info("Added %s to %s." % (element, listname))
Copy file name to clipboardExpand all lines: python_notes/Functions.rst
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -323,7 +323,7 @@ Remember that although we can execute a function *body* many times, a function *
323
323
Exercise 4
324
324
----------
325
325
326
-
#. Write a function called ``calculator``. It should take the following parameters: two numbers, an arithmetic operation (which can be addition, subtraction, multiplication or division and is addition by default), and an output format (which can be integer or floating point, and is floating point by default). Division should be floatingpoint division.
326
+
#. Write a function called ``calculator``. It should take the following parameters: two numbers, an arithmetic operation (which can be addition, subtraction, multiplication or division and is addition by default), and an output format (which can be integer or floating point, and is floating point by default). Division should be floating-point division.
327
327
328
328
The function should perform the requested operation on the two input numbers, and return a result in the requested format (if the format is integer, the result should be rounded and not just truncated). Raise exceptions as appropriate if any of the parameters passed to the function are invalid.
329
329
@@ -363,7 +363,7 @@ We can use ``*`` or ``**`` when we are *calling* a function to *unpack* a sequen
363
363
my_dict = {"name": "Jane", "surname": "Doe"}
364
364
print_kwargs(**my_dict)
365
365
366
-
This makes it easier to build lists of parameters programatically. Note that we can use this for *any* function, not just one which uses ``*args`` or ``**kwargs``::
366
+
This makes it easier to build lists of parameters programmatically. Note that we can use this for *any* function, not just one which uses ``*args`` or ``**kwargs``::
Copy file name to clipboardExpand all lines: python_notes/Loop_Control_Statements.rst
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -190,7 +190,7 @@ Exercise 2
190
190
191
191
#. Write a program which finds the factorial of a given number. E.g. 3 factorial, or **3!** is equal to **3 x 2 x 1**; **5!** is equal to **5 x 4 x 3 x 2 x 1**, etc.. Your program should only contain a single loop.
192
192
193
-
#. Write a program which prompts the user for 10 floatingpoint numbers and calculates their sum, product and average. Your program should only contain a single loop.
193
+
#. Write a program which prompts the user for 10 floating-point numbers and calculates their sum, product and average. Your program should only contain a single loop.
194
194
195
195
#. Rewrite the previous program so that it has two loops -- one which collects and stores the numbers, and one which processes them.
Copy file name to clipboardExpand all lines: python_notes/Selection_Control_Statements.rst
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -465,7 +465,7 @@ The following code fragment will print out a message if the given age is less th
465
465
if age < 0 or age > 120:
466
466
print("Invalid age: %d" % age)
467
467
468
-
The interpreter also performs a short-circuit evaluation for ``or`` expressions. If it evaluates the first subexpression to be true, it will not bother to evaluate the second, because this is suffifent to determine that the whole expression is true.
468
+
The interpreter also performs a short-circuit evaluation for ``or`` expressions. If it evaluates the first subexpression to be true, it will not bother to evaluate the second, because this is sufficient to determine that the whole expression is true.
Copy file name to clipboardExpand all lines: python_notes/Variables_and_Scope.rst
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -283,7 +283,7 @@ Some Python libraries define common constants for our convenience, for example::
283
283
# All the lowercase ASCII letters: 'abcdefghijklmnopqrstuvwxyz'
284
284
print(string.ascii_lowercase)
285
285
286
-
# The mathematical constants pi and e, both floatingpoint numbers
286
+
# The mathematical constants pi and e, both floating-point numbers
287
287
print(math.pi) # ratio of circumference of a circle to its diameter
288
288
print(math.e) # natural base of logarithms
289
289
@@ -405,7 +405,7 @@ Finally, we output the results::
405
405
Exercise 2
406
406
----------
407
407
408
-
#. Write a Python program to convert a temperature given in degrees Fahrenheit to its equivalent in degrees Celsius. You can assume that **T_c = (5/9) x (T_f - 32)**, where **T_c** is the temperature in °C and **T_f** is the temperature in °F. Your program should ask the user for an input value, and print the output. The input and output values should be floatingpoint numbers.
408
+
#. Write a Python program to convert a temperature given in degrees Fahrenheit to its equivalent in degrees Celsius. You can assume that **T_c = (5/9) x (T_f - 32)**, where **T_c** is the temperature in °C and **T_f** is the temperature in °F. Your program should ask the user for an input value, and print the output. The input and output values should be floating-point numbers.
409
409
410
410
#. What could make this program crash? What would we need to do to handle this situation more gracefully?
0 commit comments