laitimes

Python basic syntax

author:Share computer learning

【Purpose of the experiment】

1) Master the basic syntax of Python

2) Master Python data types

【Experimental principle】

Python Identifier:

A Python identifier is a name used to identify a variable, function, class, module, or other object. An identifier begins with the letters A to Z or a~z or followed by zero or more letters underlined (_), underscore, and number (0 to 9).

Punctuation is not allowed within identifiers in Python, such as @, $, and %. Python is a case-sensitive programming language. Thus, Manpower and Manpower are two different identifiers in Python.

Python identifier naming conventions:

Class names are in uppercase letters and all other identifiers are in lowercase letters.

An identifier that begins with a single leading underscore indicates that the identifier is contractually meant to be private.

The identifiers that begin with the two leading underscores indicate a strongly private identifier.

If the end of the identifier also has two underscores at the end, the identifier is a language-defined special name.

Lines and indentation:

The first thing programmers encounter when learning Python is that classes and functions that do not use parentheses to represent code define blocks or flow controls. Code blocks are indented by lines, which is strictly enforced representation.

The number of indent bits is variable, but all statements in a block must be indented by the same amount.

Python Comments:

A pound sign (#), which is not a comment that begins with a string literal. The characters after the "#" sign and into the physical line are part of the comment, and the Python interpreter ignores them.

Python data types:

There are 5 commonly used data types built into Python: Number, String, More Complex Tuple, List, and Dictionary.

Variable assignment:

Variable assignments in Python do not require a type declaration.

Each variable is created in memory and includes information about the variable's identity, name, and data.

Each variable must be assigned a value before it can be used, and the variable is not created until it is assigned.

The equal sign (=) is used to assign a value to a variable.

The left side of the equal sign (=) operator is a variable name, and the right side of the equal sign (=) operator is the value stored in the variable.

【Experimental environment】

Python-2.7

【Experimental steps】

First, open Python-2.7

1.1 Double-click the program IDLE (Python GUI) on the desktop, the following screen appears, and the opening is successful. This is shown in Figure 1

Python basic syntax

Figure 1

Second, the basic syntax operation

2.1 Python-2.7 HelloWord, after the open tool enters print "HelloWord" after ">>>", as shown in Figure 2

Python basic syntax

Figure 2

2.2 Python comments, comment Hello World to Hello Word, as shown in Figure 3

Python basic syntax

Figure 3

Third, Python data types

3.1 Numeric data types are used to store numeric values. They are immutable data types, which means that changing the numeric data type assigns a new object.

When you specify a value, the Number object is created, as shown in Figure 4

Python basic syntax

Figure 4

3.2 A string or string is a string of characters consisting of numbers, letters, and underscores. Define the variable str="HelloWord", as shown in Figure 5

Python basic syntax

Figure 5

3.3 List is the most frequently used data type in Python. Define the list list, pass in the values "name", 147, 2.58, "jake", 45.3, define the list lists, pass in the value 123, "lisi" as shown in Figure 6

Python basic syntax

Figure 6

3.4 A tuple is another data type, similar to a List. Tuples are identified by "()". Internal elements are separated by commas. But tuples cannot be reassigned, which is equivalent to a read-only list. Define the tuple, pass in the values "name", 147, 2.58, "jake", 45.3, define the tuples, pass in the value 123, "lisi" as shown in Figure 7

Python basic syntax

Figure 7

3.5 Dictionary is the most flexible type of built-in data structure in Python besides lists. Lists are ordered combinations of objects, and dictionaries are unordered collections of objects. The difference between the two is that elements in a dictionary are accessed by keys, not by offsets. Dictionaries are identified by "{ }". A dictionary consists of an index (key) and its corresponding value value. This is shown in Figure 8

Python basic syntax

Figure 8