Python Basics

Updated: Feb 9


Python is a cross-platform computer programming language. It was originally designed to write automated scripts. With the continuous update of the version and the addition of new language features, it is used for the development of independent and large-scale projects. Python has the following characteristics.

#Python #basics #syntax #variables #pythonlanguage #pythonlanguagecode

1. The rich library - Python standard library is really huge. It can help you handle a variety of tasks, including regular expressions, document generation, unit testing, threads, databases, web browsers, CGI, FTP, e-mail, XML, XML-RPC, HTML, WAV files, cryptographic systems, GUI (graphical user interface), TK, and other system-related operations. In addition to the standard library, there are many other high-quality libraries, such as wxPython, Python image library, and so on.

2. Object-oriented- Python supports both process-oriented programming and object-oriented programming. In "procedure-oriented" languages, programs are constructed from procedures or functions that are simply reusable code. In an "object-oriented" language, a program is constructed from objects composed of data and functions. Compared with other major languages such as C++ and Java, Python implements object-oriented programming in a very powerful and simple way.

3. Explanatory- This point requires some explanation. A program written in a compiled language such as C or C++ can be converted from a source file (C or C++ language) to a language used by your computer (binary code, 0 and 1). This process is completed by the compiler and different flags and options. When you run your program, the linker or reloader software copies your program from the hard disk to the memory and runs it. The program written in Python language does not need to be compiled into binary code. You can run the program directly from the source code. Inside the computer, the Python interpreter converts the source code into an intermediate form called bytecode, and then translates it into the machine language used by the computer and runs it. In fact, since you no longer need to worry about how to compile a program, how to ensure that the correct library is linked and reproduced, all of this makes using Python easier. Since it will work when you copy your Python program to another computer, which also makes your Python program more portable.

Python syntax

Python comments and garbled

1. Single-line comments- It starts with #, and everything on the right side of # is used as an explanation, not the program to be executed, and serves as an auxiliary explanation.

2. Multi-Line comments- It means Multi-line comments and can write multi-line functional descriptions.

3. Python garbled code problem- Since the Python source code is also a text file when your source code contains Chinese, you must specify the UTF-8 encoding when saving the source code. When the Python interpreter reads the source code, in order to make it read in UTF-8 encoding, we usually write these two lines at the beginning of the file:

Variables and types            

1. Definition of variables:            

If you need to sum two or more data in a program, you need to store these data first and then add them up. In Python, to store data, you need something called a variable.

ie: num1=100



2. Types of variables:

In order to make full use of memory space and manage memory more efficiently, there are different types of variables, as shown in the followings:

a. Integer

Python can handle integers of any numerical value, including negative integers of course. The representation in the program is exactly the same as the mathematical writing. For example, 1, 100, -8080, 0, etc. Because computers use binary, it is sometimes more convenient to use hexadecimal to represent integers. Hexadecimal is represented by 0x prefix and 0-9, a-f, for example: 0xff00, 0xa5b4c3d2, etc.

b. Floating point number            

Floating point numbers, that is, decimals, are called floating-point numbers because the decimal point position of a floating-point number can be changed according to scientific notation. For example, 1.23x109 and 12.3x108 are identical. Floating-point numbers can be written mathematically, such as 1.23, 3.14, -9.01, and so on. However, for large or small floating-point numbers, they must be expressed in scientific notation. Replace 10 with E. 1.23x109 is 1.23e9, or 12.3e8, 0.000012 can be written as 1.2e-5, etc. Integers and floating-point numbers are stored in different ways in the computer. Integer operations are always accurate, while floating-point operations may have rounding errors.

c. Boolean value            

Boolean values and Boolean algebra are exactly the same. A Boolean value has only two kinds of values: true and false, either true or false. In Python, you can directly use true and false to represent Boolean values (please pay attention to the case), or they can be calculated by the Boolean operation. Boolean values can be evaluated with and, or, and not. The and operation is an and operation. Only if all of them are true, can the result of the and operation be true.

d. Null value            

A null value is a special value in Python, expressed by none. None can be understood as 0 because 0 is meaningful and none is a special null value. In addition, python also provides a variety of data types, such as lists and dictionaries, and allows the creation of custom data types. In Python, as long as a variable is defined and it has data, then its type has been determined. There is no need for developers to actively explain its type, and the system will automatically identify it.

Recent Posts

See All