Learning Blog
Getting Started with Python Part 2 - October 16
Station 1: Code Cleanup Station
I improved the readability of Python programs by following PEP 8 standards and adding comments. In particular, when naming variables and functions, using underscores to separate words (such as "tax_price" and "side_1") makes the names clearer and easier to understand. In addition, I also added documentation strings and comments to help understand the functionality and structure of the code. These improvements not only make the code easier to maintain, but also enhance collaboration and reduce the possibility of misunderstandings, thereby improving the overall code quality.
Station 2: Debugging Station
While identifying grammatical errors in the three-part program, I found several common problems, including forgetting to add parentheses, missing commas, forgetting to add quotation marks, forgetting to add the equals sign, forgetting to write 0, and forgetting to add the multiplication sign. These errors are minor but can prevent the program from executing properly. In the process, I realized how important details are in coding, and that syntax errors such as omission of parentheses and commas can significantly affect the running of the code. This reminds me to be cautious when writing and debugging code to improve code quality and correctness.
Station 3: Completion Station
Through this exercise, I learned some of the functions of string methods in Python. By chaining methods such as capitalize(), lower(), and replace(), I can transform strings in a clear and concise way. This practice has enhanced my understanding of string processing in Python, allowing me to write cleaner and more efficient code. I also learned how index() and rindex() output. Similar to isnumeric() and isalpha() string methods output values that can be judged as right or wrong. After learning this station, you can understand that these methods enable me to manipulate strings according to specific requirements, which gives me a further understanding of code writing.
Reflection
There are many strategies to identify errors in code. First, reviewing the code line by line can help find syntax and logic errors. Using debugging tools, you can execute the code step by step to observe the changes in variables and the execution flow of the program. In addition, since the code we are learning now is generally simple, we can specifically look for symbols such as commas, quotation marks, equal signs, and brackets to see if there are any omissions to quickly find the error. When we really can't find the error, we can ask others for help. Using other people's perspectives, it may be easier to find hidden problems.
However, some errors are particularly difficult to identify. Logical errors often do not cause the program to crash, but they will produce incorrect output. Usually, you need to have a deep understanding of the code logic to find them. This kind of error is difficult to find, but the code logic we are learning at this stage is generally not too complicated. As long as we follow the methods of the IPO form we have learned before, we will generally not make mistakes. There is another error that I think is the most difficult to find. I forgot to switch the input method when I was typing the code before (my input method defaults to Chinese), and the code I typed could not run at all. Later, it took me several hours to find that the symbol was wrong, but these Chinese symbols look almost the same as English symbols, so it is very difficult to find that these symbols are wrong.
In short, we can find errors more quickly by checking line by line, using debugging tools, symbolic review, and seeking help from others. However, logical errors and symbolic errors are often difficult to find and require more detailed review. Flexible use of these strategies will help us be more efficient in coding.
0 being “no idea”; 5 being “I feel can confidently do it in any situation”
4 out of 5 - Program Python to output text and numbers onto the screen
3 out of 5 - Use variables in Python to store and retrieve (print out or use in a formula) values
4 out of 5 - Program Python to perform various math calculations
2 out of 5 - Write a simple program (I.e., convert inches to meters) in Python
I felt this way about writing a simple program because I had limited experience combining concepts into a complete solution. While I understood the basics of functions, variables, and calculations, translating that knowledge into a working program was challenging. I often found myself unsure of how to structure my code or which methods to use, and sometimes I didn't know where to start.
To improve, I can start by practicing some small projects, such as converting inches to meters or calculating the area, which will help strengthen my understanding of how to piece together different components. After this part is proficient, I can often write some simple code to practice, consolidate the knowledge learned, and improve proficiency. In the process of writing simple code, if there is anything I don’t understand, ask the teacher and classmates immediately, and don’t put off the question until the last minute. Finally, as long as I persevere, through continuous practice and finding challenging resources, I believe I can enhance my confidence and ability to write complete programs, and ultimately improve my overall programming skills.
Terminology Review
String: In Python, strings are used for representing textual data. A string is a sequence of characters enclosed in either single quotes ('') or double quotes (“”).
Float: Float is a function or reusable code in the Python programming language that converts values into floating point numbers. Floating point numbers are decimal values or fractional numbers like 133.5, 2897.11, and 3571.213, whereas real numbers like 56, 2, and 33 are called integers.
Initialize: Initialization refers to the assignment of an initial value for a variable or data object.
Syntax Error: Syntax errors are invalidities in the code's structure. In other words, Python syntax errors happen when the Python interpreter is unable to recognize the structure of statements in code. Some of the most common causes of syntax errors in Python are: Missing quotes. For example, print(Hello) instead of print("Hello"). Misspelled reserved keywords.
Escape Character: In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return. Conversely, prefixing a special character with "\" turns it into an ordinary character.
Function: A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function.
Integer: Integers are classified into zero, positive, or negative whole numbers with no fractional part and infinite precision, such as 0, 100, -10, and so on.
Variable: A Python variable may be assigned a value of one type and then later re-assigned a value of a different type. For example, x = "apples" can later be x = 5.
Identifier: Identifier is a user-defined name given to a variable, function, class, module, etc. The identifier is a combination of character digits and an underscore. They are case-sensitive i.e., 'num' and 'Num' and 'NUM' are three different identifiers in python.
Runtime Error: Some of the most common examples of runtime errors in Python are: Division by zero. Using an undefined variable or function name. Performing an operation on incompatible types.
Logic Error: When the code runs without any syntax or runtime errors but produces incorrect results due to flawed logic in the code. These types of errors are often caused by incorrect assumptions, an incomplete understanding of the problem, or the incorrect use of algorithms or formulas.
Method: Methods are functions that are associated with an object and can manipulate its data or perform actions on it. They are called using dot notation, with the object name followed by a period and the method name. Methods are an important part of object-oriented programming in Python.