Category Archives: Library

34.2 Read File

Open a File on the Server Assume we have the following file, located in the same folder as Python: demofile.txtHello! Welcome to demofile.txtThis file is for testing purposes.Good Luck! To open the file, use the built-in open() function. The open() function returns a file object, which has a read() method for reading the content of the file: Example f = open(“demofile.txt”, “r”)print(f.read()) Output:C:\Users\My… Read More »

34.1 File Open

File handling is an important part of any web application. Python has several functions for creating, reading, updating, and deleting files. File Handling The key function for working with files in Python is the open() function. The open() function takes two parameters; filename, and mode. There are four different methods (modes) for opening a file: “r” – Read – Default value. Opens… Read More »

33. String Formatting

To make sure a string will display as expected, we can format the result with the format() method. String format() The format() method allows you to format selected parts of a string. Sometimes there are parts of a text that you do not control, maybe they come from a database, or user input? To control such values, add placeholders… Read More »

32. User Input

User Input Python allows for user input. That means we are able to ask the user for input. The method is a bit different in Python 3.6 than Python 2.7. Python 3.6 uses the input() method. Python 2.7 uses the raw_input() method. The following example asks for the username, and when you entered the username, it gets printed on… Read More »

31. Try Except

The try block lets you test a block of code for errors. The except block lets you handle the error. The finally block lets you execute code, regardless of the result of the try- and except blocks. Exception Handling When an error occurs, or exception as we call it, Python will normally stop and generate an error message. These exceptions can… Read More »

30. PIP

What is PIP? PIP is a package manager for Python packages, or modules if you like. Note: If you have Python version 3.4 or later, PIP is included by default. What is a Package? A package contains all the files you need for a module. Modules are Python code libraries you can include in your project.… Read More »

29. RegEx Or Regular Expressio

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… Read More »

28. JSON

JSON is a syntax for storing and exchanging data. JSON is text, written with JavaScript object notation. JSON in Python Python has a built-in package called json, which can be used to work with JSON data. Example Import the json module:import json Parse JSON – Convert from JSON to Python If you have a JSON string, you… Read More »

27. Math

Python has a set of built-in math functions, including an extensive math module, that allows you to perform mathematical tasks on numbers. Built-in Math Functions The min() and max() functions can be used to find the lowest or highest value in an iterable: Example x = min(5, 10, 25)y = max(5, 10, 25) print(x)print(y) Output:525 The abs() function returns the absolute (positive) value of the specified number:… Read More »

26. Datetime

Python Dates A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects. Example Import the datetime module and display the current date:import datetime x = datetime.datetime.now()print(x) Output:2021-10-06 09:57:24.627387 Date Output When we execute the code from the example above the result… Read More »