Getting started with Python(4)

Section 4 Constants and Variables
Hello, everyone, starting to learn Python syntax today. The most basic things are nothing more than: constants and variables, data types and data structures, flow control statements, etc. It is not difficult to understand them. They are like the bricks, sandstones and stones that make up a building. They don’t seem to have any profound things, but we must understand them, because in the future when we build a project, at least you have to know when we When bricks and tiles are needed, you can't make the mistake of filling them with sand and gravel. Therefore, even if it is something simple to boring, we still need to understand roughly what they are. What are the characteristics? how to use?

In this lesson, we will first understand constants and variables.

One, constant

**Mainly include: numbers, strings. **

Constants are also called literal constants. For example: numbers like 3, 2.16, or, for example, text like "This is a dog". The so-called literal constants mean that what we read, understand and use is their literal or intuitive meaning.

1、 digital

Numbers are mainly divided into two types: integers (Integers) and floating-point numbers (Floats).

(1) Integer (int): It is consistent with the meaning of integer defined by our middle school mathematics. For example: 1, 23, 1999

(2) Floating point number (float): Simply put, it is a number containing a decimal point, that is, all decimals. Such as: 3.14

This can be simply understood as integers and decimals, and the degree of difficulty: elementary school arithmetic.

2、 String

**Concept: **String is a sequence of characters (Sequence).

Usually the string is placed between quotation marks. Quotation marks can be: single quotation marks, double quotation marks, and triple quotation marks. Triple quotation marks can also identify strings of multiple lines of text. E.g:

str1 = 'redhat'

str2 = "redhat"

str3 = """red

hat"""

Need special attention:

(1), case sensitive! For example: "abc" and "Abc" are not the same.

(2) When you need to use special characters in a string, python uses a backslash () to escape characters.

**Some students may notice that it seems not exactly the same as the description of strings in other computer languages. However, when we write Python code, we will use strings at any time, so please pay special attention. Strings are an important point. **

**Immutable: **

Once a string is defined, you cannot change it because it is a constant. If you really want to modify the "value" of a string, you can define it to another string.

format:

Python supports the output of formatted strings. The basic usage is to use the format() method to insert a value into the placeholder {} corresponding to the string. E.g:

print('{0} was {1} years old when he wrote this book'.format(“zhangsan”, 20))

zhangsan was 20 years old when he wrote this book

Two, variables

Describing variables accurately is more complicated than constants. For this, we need to understand the following concepts first...

Concept 1: Identifier

Identifier is a concept that most computer languages have. It is usually a description of the naming of operating objects. We will soon be exposed to more python operation objects, such as: classes, methods, lists, dictionaries and so on. Similarly, variables are an example of identifiers. Simply put, Identifiers are the name of an operation object. When we name a variable, we define an identifier.

Identifier naming rules

Specification for the naming of identifiers

The above naming conventions are not mandatory, but they are a good habit worth developing.

Concept 2: Object

The Python language believes that everything is an object. It collectively refers to any content in the program as an object (Object). This is a general statement.

When we discuss python programming, we should get used to saying "object" instead of "something".

We have already mentioned that python is an object-oriented programming language, and I will emphasize it again: Python is strongly object-oriented, and “strong” here means that everything is an object, including numbers, strings and functions; Refers to strong typing. We will see the influence of strong typing of objects on variables shortly.

Concept 3: Data Type

Variables can save various forms of values as different data types (Data Type). The basic types are the numbers (integer and floating point) and strings we just discussed. Of course, there are many types, which are not unfamiliar to students who have experience in other languages. In the following lessons, we will cover them one by one. Even, we will learn how to create our custom type through the (Class) class.

PS: Students who have experience in other languages can pay attention to this discussion:

Are there types of variables? Many online say that python is a weakly typed language, mainly because python does not need to declare the type of a variable when defining a variable. Some people use whether the type can be implicitly converted to measure whether it is a weak type, but in fact, for python, these are not accurate. In fact, because python is a strong object type, any object in python must clearly declare the type, and the variable is just a reference to the object. In memory, each object contains two header information, one is the type identifier, which identifies the type of the object; the other is the reference counter, which is used to identify how many variables the object is referenced. If there is no variable reference, it will be Recycling. Therefore, the variable itself has no type, only the object has the type, but once the object is successfully referenced by the variable, it has the type. Therefore, the variable is a dynamic type. This is why in python, there is no need to declare a type when defining a variable.

Definition of variables

At this point, we can finally discuss the definition of variables.

**Define variable: ** When declaring a variable identifier, assign a certain value to the variable.

The variable definition process actually achieves four things:

1、 Declared an identifier;

2、 Refers to an object;

3、 Assign a value to the variable;

4、 The type of the current value of the variable is clarified.

For example: x = 5

This formula defines a variable, it looks extremely simple, but it clearly does the following 4 things:

(1) It declares an identifier x;

(2) An object is quoted: the literal constant (number) 5;

(3) Assign a value to the variable x to make it equal to 5. Note: The "=" sign here is an assignment symbol;

(4) It is clear that the type of the current value of the variable x is an integer type.

summary

In this section, we introduced the two most basic grammatical concepts of the Python language: constants and variables.

1、 Constants (numbers, strings) are immutable.

2、 A variable can be of any type, depending on what kind of object it refers to. Therefore, it is a dynamic type.

3、 Understand identifiers and naming conventions.

Recommended Posts

Getting started with Python(18)
Getting started with Python(9)
Getting started with Python(8)
Getting started with Python(4)
Getting started with Python (2)
Getting started with python-1
Getting started with Python(14)
Getting started with Python(7)
Getting started with Python(17)
Getting started with Python(15)
Getting started with Python(10)
Getting started with Python(11)
Getting started with Python(6)
Getting started with Python(3)
Getting started with Python(12)
Getting started with Python(5)
Getting started with Python (18+)
Getting started with Python(13)
Getting started with Python(16)
Getting started with Ubuntu
Getting Started with Python-4: Classes and Objects
Getting started with python-2: functions and dictionaries
04. Conditional Statements for Getting Started with Python
Getting started python learning steps
How to get started quickly with Python
python requests.get with header
Play WeChat with Python
Web Scraping with Python
Implementing student management system with python
Centos6.7 comes with python upgrade to
Played with stocks and learned Python
Gray-level co-occurrence matrix (with python code)
Speed up Python code with Cython
How to make a globe with Python
Automatically generate data analysis report with Python
Create dynamic color QR code with Python
Python | Quickly test your Python code with Hypothesis
How to process excel table with python