Posts

Blog 5 Python 5

CHAPTER 16 PYTHON My SQL Python can be used in database applications. One of the most popular databases is MySQL. MySQL Database To be able to experiment with the code examples in this tutorial, you should have MySQL installed on your computer. You can download a free MySQL database at https://www.mysql.com/downloads/. ________________________________________ Install MySQL Driver Python needs a MySQL driver to access the MySQL database. In this tutorial we will use the driver "MySQL Connector". We recommend that you use PIP to install "MySQL Connector". PIP is most likely already installed in your Python environment. Navigate your command line to the location of PIP, and type the following: Download and install "MySQL Connector": C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>python -m pip install mysql-connector Now you have downloaded and installed a MySQL driver. ________________________________________ Test MySQL Conne...

Blog 4 Python 4

CHAPTER 12 PYTHON REGEX (REGULAR EXPRESSION) 12.1 PYTHON REGEX A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern. RegEx can be used to check if a string contains the specified search pattern. ________________________________________ RegEx Module Python has a built-in package called re, which can be used to work with Regular Expressions. Import the re module: import re ________________________________________ RegEx in Python When you have imported the re module, you can start using regular expressions: Example Search the string to see if it starts with "The" and ends with "Spain": import re txt = "The rain in Spain" x = re.search("^The.*Spain$", txt) RegEx Functions The re module offers a set of functions that allows us to search a string for a match: Function Description findall Returns a list containing all matches search Returns a Match object if there is a match anywhere in the string split Retu...

Blog 3 Python 3

CHAPTER 5 PYTHON ARRAYS Note: Python does not have built-in support for Arrays, but Python Lists can be used instead. 5.1 Arrays Arrays are used to store multiple values in one single variable: Example Create an array containing car names: cars = ["Ford", "Volvo", "BMW"] What is an Array? An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: car1 = "Ford"; car2 = "Volvo"; car3 = "BMW"; However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300? The solution is an array! An array can hold many values under a single name, and you can access the values by referring to an index number. ________________________________________ Access the Elements of an Array You refer to an array element by referring to the index number. ...

Blog 2 Python 2

1.10 String Literals String literals in python are surrounded by either single quotation marks, or double quotation marks. 'hello' is the same as "hello". Strings can be output to screen using the print function. For example: print("hello"). Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string. Get the character at position 1 (remember that the first character has the position 0): a = "Hello, World!" print(a[1]) #e Substring. Get the characters from position 2 to position 5 (not included): b = "Hello, World!" print(b[2:5]) #llo The strip() method removes any whitespace from the beginning or the end: a = " Hello, World! " print(a.strip()) # returns "Hello, World!" The len() m...