Posts

Showing posts from February, 2025

A nicer way to handle code errors

In most programming languages it is possible to put the code in a try/except block. Try is a piece of code where error is expected, except block is piece of code to be executed, when an error occurs within try block. But a program should work flawlessly. Exactly! Unless a program asks a person to enter data. For example code does division of one number by another one. A program may ask a user for an input, but a user may enter a character instead of a number. Dividing a number by a character is an invalid operation. Try/except block will help to handle such an error. Why try/except block is important? Instead of cryptic error messages, a coder may display a much nicer message to a user. For example "Division by zero" is not permitted, a coder may print a message that a number must be greater than zero. YouTube video Try/Except Blocks: A Study Guide Quiz (Short Answer) Answer the following questions in 2-3 sentences each. What is the primary purpose of using a try/except block...

Errors in the code

Some errors are syntactic errors, for example instead of print pritn was typed. Please note I switched nt places. However some errors will only manifest themselves during execution of the code. For example division is a perfect operation, however division by zero is not. What are the ways to avoid errors in the code? 1. Experience. As a person learns more about programming, such a person will be making errors less frequently. 2. Unit tests. In addition to writing code, a person needs to write unit tests. Unit tests are cases when known scenarios may happen. 3. Give code to review by peers. They may question the logic, or suggest better alternatives of doing things. YouTube video Error Avoidance in Programming: A Study Guide I. Quiz (Short Answer - 2-3 sentences each) What is the difference between a syntactic error and an execution-time error? Provide an example of each from the text. According to the text, what is the primary way programmers gain proficiency in avoiding errors? Explai...

Testing of the code

As a computer professional you need to run tests for the applications you install. Of course it is difficult to test all of the possibilities, but it is a good idea to test common ones. If you are writing code, then it is a good idea to implement test cases for the code that had been written. Unit tests will help to uncover issues with the code, A person who writes piece of code can be certain, that the code is good, but the unit tests will prove it. It is a bad idea to install untested code in production environment, since even a minor bug may have a big impact. A common issue is off by one, for example may start with 1, where it should had started with 0. In addition to unit tests, a person may give code to others for review. Other people may question, why the logic had been implemented in a certain way, and they even will be able to recommend a better way of doing the same thing. One of the things that helps to debug issues, is to explain code to a "rubber duck". Even so a...

Optional function parameters in Python

Optional parameters of functions in Python have default value. This value is specified by equal sign after the variable. For example: def example(abc=123) If a value of abc is not specified when this function is called, it will take default value. YouTube video

Scope of variables

Variables in programming can have either global or local scope, Global scope means that a variable is accessible everywhere in the code, Local variables are isolated to specific piece of code, for example local variables are isolated to a specific function. There are two reasons why local variables needs to be chosen over global ones. 1. Speed to access local variables are faster than accessing global variables. 2. It is a good programming practice to use local variables. For  example values to functions need to be passed as parameters, and not like global variables. If a value of a global variable is changed within a function, it will be changed for the rest of time the code will be executed. Local variables lives within a function, changing a value of a local variable within a function, does not impact value of a variable of the same name within main code execution. Also having two variables with the same name will add confusion. It does not impact naming variables within loops. ...

Approximation

Approximate value is a value close enough to the value that is expected. For example if the speed limit is 65, but a driver drives with a speed of 63, that speed is approximately equals the speed limit. A driver will not be able to drive exactly 65 mph. Does approximation always good? Even so approximate value is good for things like a speed, approximate value is not good for speed of a car, we would like to know not an approximate value, but an exact value. Same is true for the number of floors in a building, If a building to be build with 25 floors, then constructing a building which has 23 floors - approximately 25, will not going to cut it.

Number line

Number line is an imaginary line with all of the numbers on it. In the center is zero.  To the left of zero are negative numbers, and to the right of it are positive numbers. Whole numbers are represented with a short vertical bars. Number line continues to infinity to the right and negative infinity to the left. It is possible to represent fractional numbers on a number line, fractional numbers will be in between whole numbers. It is possible to have irrational numbers, an irrational number is a number, which digits after zero are goes on to infinity and never repeat. Numbers like Pi or a base for natural algorithm are irrational numbers. YouTube video Study guide Short Answer Quiz Answer the following questions concisely and accurately, using 2-3 sentences each. What is the purpose of a number line? What number occupies the center of the number line, and what is its significance? How are positive and negative numbers distinguished on the number line? How are whole numbers represe...