Python Tutorial

Python Tutorial: 12. Putting it All Together - Merge Sort

Now that we finished learning about Python’s major quirks and features, let’s put it into action. Today, let’s see how to write a “sort” algorithm for lists. 1 Python Built-In Sort Recap As aforementioned in chapter 5 of this tutorial, Python lists have a built-in list.sort() method that modifies the list in-place. There is also a sorted() built-in function that builds a new sorted list from an iterable. sorted() returns...

July 26, 2022 · Tiexin Guo | 郭铁心
Python Tutorial

Python Tutorial: 11. Virtual Environments and Managing Packages with Pip

Python apps and programs will often use packages and modules that don’t come as part of the standard library (stuff that is included in Python). Apps will sometimes need a specific version of a library, because the app may require that a particular bug has been fixed or the app may be written using an obsolete version of the library’s interface. This means it may not be possible for one...

July 21, 2022 · Tiexin Guo | 郭铁心
Python Tutorial

Python Tutorial: 10. Class

0 OOP and Classes To talk about class, we need to talk about Object-oriented programming (OOP) first. OOP is a programming paradigm based on the concept of “objects”, which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods). The class provides a way of bundling data and functionality together. Creating a...

July 20, 2022 · Tiexin Guo | 郭铁心
Python Tutorial

Python Tutorial: 9. Error Handling

We have only slightly mentioned error messages. If you have tried out some of the code examples yourself, I’m pretty sure you’ve met a few more errors and exceptions. Today, we will go over errors in general. There are (at least) two distinguishable kinds of errors: syntax errors exceptions 1 Syntax Errors Syntax errors, also known as parsing errors, are caused by incorrect syntax, or grammar. They are perhaps the...

July 19, 2022 · Tiexin Guo | 郭铁心
Python Tutorial

Python Tutorial: 8. Input and File Handling

There are several ways to present the output of a program; data can be printed in a human-readable form, or written to a file for future use. 1 Output Formatting Often you’ll want more control over the formatting of your output than using the print() function which simply prints space-separated values. There are several recommended ways to format output: 1.1 Formatted String Literals (Also called f-strings for short.) Begin a...

July 18, 2022 · Tiexin Guo | 郭铁心
Python Tutorial

Python Tutorial: 7. Modules - Organizing Code

0 Background So far, we’ve been coding in the Python interpreter, which if we quit and re-enter, the functions and variables definitions we’ve made previously are lost. Therefore, if you want to write a somewhat longer program, you’d be better off using a text editor to prepare the input for the interpreter and running it with that file as input instead - which is known as creating a script. As...

July 15, 2022 · Tiexin Guo | 郭铁心
Python Tutorial

Python Tutorial: 6. Data Structures - Part 2

Now that we’ve mastered lists, let’s learn two more data structures that could change your world. 1 Sets A set (same as a mathematical set) is an unordered collection with no duplicate elements. Basic uses include membership testing (if something is in a group,) and eliminating duplicate entries (for example, if we convert a list to a set, duplicated items are eliminated because there can be no duplicates in a...

July 14, 2022 · Tiexin Guo | 郭铁心
Python Tutorial

Python Tutorial: 5. Data Structures

In today’s tutorial, we will go over some of the stuff we’ve already learned in more detail, and add some more new stuff as well. 1 More on Lists All of the methods of the list objects: list.append(x): add an item to the end of the list. list.extend(iterable): extend the list by appending all the items from the iterable. If you’ve forgotten about the iterable, see previous tutorials. list.insert(i, x):...

July 13, 2022 · Tiexin Guo | 郭铁心
Python Tutorial

Python Tutorial: 4. Functions

1 Defining Functions We can create a function that writes the Fibonacci series to an arbitrary boundary (if you have forgotten about the Fibonacci series, refer to the second tutorial): >>> def fib(n): # write Fibonacci series up to n ... a, b = 0, 1 ... while a < n: ... print(a, end=' ') ... a, b = b, a+b ... print() ... >>> # Now call the function...

July 12, 2022 · Tiexin Guo | 郭铁心
Python Tutorial

Python Tutorial: 3. Flow Control

In the previous tutorial, we demonstrated the while statement, which is a “flow control” statement. In computer programming, control flow or flow of control is the order function calls, instructions, and statements are executed or evaluated when a program is running. In Python (as in most other programming languages,) there are more usual flow control statements than just while. 1 if Statements The most well-known flow control statement probably is...

July 11, 2022 · Tiexin Guo | 郭铁心