[Python] constant and variable types

Table of contents

1. Constants and expressions

2. Variables and types

2.1 What are variables

2.2 Syntax of variables

 2.3 Types of variables

2.4 Dynamic Type Features


1. Constants and expressions

We can use Python as a calculator to perform some calculations, such as

print(1 + 2 - 1)
print(1 + 2 * 2)
print(1 + 2 / 2)

 NOTE: In the above code,

Operators such as + - * / ( ) can be used for arithmetic operations. Multiplication and division are performed first, followed by addition and subtraction
Between the operator and the number, there can be no space or multiple spaces. But it is generally customary to write a space (more beautiful)

A formula like 1 + 2 - 3 is called an expression the result of the calculation is called the return value of the expression 

Among them, 1, 2, and 3 are called literal constants , + - * / are called operators or operators

2. Variables and types

2.1 What are variables

Sometimes , the calculations we need to perform may be more complicated , and some intermediate results of calculations need to be saved . At this time, we need to use variables
For example:
Given four numbers,
30.2 + 12.3 + 23.4 + 9.3, write code to find the variance of these four numbers .
ps: The calculation process of variance: take each item, subtract the average value, calculate the square, then sum, and finally divide by (number of items - 1)
In this code, it is necessary to calculate the average value of these four numbers first, and then calculate the variance. This requires the calculated average value to be saved in a variable
avg = (30.2 + 12.3 + 23.4 + 9.3) / 4
total = (30.2 - avg) ** 2 + (12.3 - avg) ** 2 + (23.4 - avg) ** 2 + (9.3 - avg) ** 2
result = total / 3
print(result)

Note:

avg, total, result are all variables.
** Represents the multiplication operation in Python. ** 2 is squaring

2.2 Syntax of variables

1) Define variables
a = 10
The statement to create a variable is very simple , where
a is the variable name. When we create many variables , we can use the name to distinguish them.
= is an assignment operator , which means to put the data on the right side of = into the space on the left side of =
Note: Variable names must follow certain rules .
 Variable names are composed of numbers, letters and underscores
number cannot start with
The variable name cannot be repeated with "keyword"
Variable names are case sensitive. num and Num are two different variable names

Soft rules ( recommended compliance ) 

Use descriptive words to express the variable name , try to express the role of the variable.
A variable name can be composed of multiple words , it doesn't matter if it is longer , but the meaning should be clear
When the variable name contains multiple words , it is recommended to use " camel case ". Such as totalCount , personInfo , etc. , except
Except for the first word , the first letter of the remaining words is capitalized

 2) Using variables

a = 10
print(a)

 

 Modify the value of a variable

# 定义变量
a = 10
# 修改变量值
a = 20
print(a)

 Note : In Python , modifying variables also uses the = operation , which does not seem to be significantly different from defining variables .

For the same variable, the variable is defined at the beginning, and the variable is modified later

Assign the value of one variable to another variable
a = 10
b = 20

# 将 b = 的值赋予 a
a = b

print(a)
print(b)

 2.3 Types of variables

Variables store not only numbers , but also other types of data . In order to distinguish different types of data , we introduce the concept of " type". The type of a Python variable does not need to be explicitly specified , but is determined at the time of assignment
1) Integer
a = 10
print(type(a))

 PS: type is similar to print , and it is also a built-in function of python . You can use type to check the type of a variable.

Python 's int type variable has no upper limit on the range of data represented . As long as the memory is large enough, it can theoretically represent data of unlimited size.
2) Floating point numbers (decimals)
a = 0.1
print(type(a))

 Python's decimal only has a type of float , not a double type . But in fact, Python's float is equivalent to C++/Java 's double, representing double-precision floating-point numbers

3) string

a = 'hello'
print(type(a))

 Enclosed with ' ' or " " , it is called a string . It can be used to represent text.

Note : In Python , there is no difference between a string composed of single quotes and a string composed of double quotes . 'hello' and "hello" are
completely equivalent.
You can use the len function to get the length of a string
a = 'hello'
print(len(a))

For two strings, you can use + to concatenate

a = 'hello'
b = 'world'
print(a + b)

 

 4) Boolean

The Boolean type is a special type with only two values , True ( true ) and False ( false ). Usually used for logical judgment
a = True
b = False
print(type(a))
print(type(b))

 (5) Others

In addition to the above types , there are list, tuple, dict, custom types, etc. in Python

2.4 Dynamic Type Features

In Python , the type of a variable can change during the " program running " . This feature is called " dynamic type " .
a = 10
print(type(a))
b = 'hello'
print(type(b))

 During the execution of the program , the type of a is initially int, and then becomes str.

Dynamic typing is a double-edged sword .
For small and medium-sized programs , the amount of code can be greatly reduced ( for example, writing a piece of code can support multiple types at the same time ).
For large programs , the interaction cost between modules is increased. ( The code provided by programmer A is difficult for B to understand ).

Guess you like

Origin blog.csdn.net/m0_60494863/article/details/127834869