[Python is not a python] Variables | Operators | Type conversion

Ⅰ.Variables

0x00 What is a variable?

A variable is a numerical value that changes.

A variable is a box that holds things.
A variable is a container for storing data. We obtain data through variable names, and even the data can be modified.

There are many tutorials on the Internet that say this, but this is wrong. In python, a variable is not a box, but a value with its label attached.

example:

a = 10
b = a

Let's think about it, what happens in python?

If you use pictures to represent it, it would be like this

Unexpectedly, what is stored in Python variables is not the stored value like C\C++, but the address where the data is stored.

Some people may not believe it, but here we give an example to make you believe it completely.

What is the id function?

We use the help function to get help

 Reference translation:

有关模块内置函数id的帮助:



id(obj,/)

返回对象的标识。



这保证在同时存在的对象中是唯一的。

(CPython使用对象的内存地址。)

Having run a little too far, I continue to return to the topic.

0x01 variable assignment

Python is a dynamic variable. What is a dynamic variable?

That is, the attributes of a variable can be changed, and there is no variable declaration.

Variable assignment in python can use the = operator, the syntax is

变量名 = 值

Let's go through an example

n = 666 # int类型

f1 = .0 # float类型,等价于0.0
f2 = 0. # 等价于0.0

s = "623" # 字符串类型

b = True # 布尔类型

0x02 naming rules

Not just any name is acceptable in Python

The rules are as follows:

  • Variable names can only contain letters, numbers, and underscores.
  • Variable names must start with a letter or underscore.
  • Variable names cannot be Python keywords, such as if, while, for, etc.
n@ = 10 # 错误:使用了特殊符号
变量 = 666 # 正确:Python允许使用中文作为变量名
1n = 888 # 错误:使用了数字作为开头
_n = 999 # 正确:下划线可以作为开头

 0x03 naming convention

Generally speaking, there are four programming naming conventions:

Hungarian nomenclature

The Hungarian nomenclature is an early specification, invented by a Hungarian at Microsoft, and is a product of the era when IDEs were still very retarded. In those days, when there was a lot of code, it was very troublesome to determine the type of a variable. Unlike now, IDEs will give prompts, so such a naming convention was created. I guess not many people use it now... A very systematic yet trivial naming convention.

This naming convention requires that the prefix letter should be the abbreviation of the variable type, and the rest should be the English or English abbreviation of the variable, with the first letter of the word capitalized.

like:

inumber = 666
sName = "666"

camel case nomenclature

Camel case nomenclature is also called small camel case nomenclature (so naturally there is big camel case nomenclature...).

This naming convention requires the first letter of the first word to be lowercase and the first letter of other subsequent words to be capitalized. It is simple, crude, easy to learn and use.

myAge = 18
myName = ""

Pascal nomenclature

Pascal nomenclature, also called camel case nomenclature.

The biggest difference from camelCase is that the first letter of each word is capitalized.

MyAge = 18
MyName = ""

 

Underline nomenclature

The underline nomenclature is not as highly regarded as the camel case nomenclature, but it is still a highlight. It is especially used in macro definitions and constants, and underscores are used to separate all uppercase words.

The naming convention is also very simple, requiring words to be connected by underscores.

my_age = 18
my_name = ""

 But if we insist on using a naming convention, it will definitely be useful, there is no doubt about it.

                                                                                                        —— not important

0x04 Unpacking operation

There is syntactic sugar in Python, the syntax is

变量A, 变量B, ... = 值1, 值2, ... # 等价于 变量A = 值1
                                 # 变量B = 值2 

This is the unpacking operation.

Why is it syntactic sugar?

There is no unpacking operation in C++, so exchanging variables requires

auto Temp = a;
a = b;
b = Temp;

But in Python it only takes one line of code,

a, b = b, a

Just asking you if you are handsome or not

The unpacking operation has many uses and will be used frequently later.

0x05 call variable

We can use the variable name directly in the code.

a = 10
print(a) # 输出: 10

0x06 Delete variable

Some variables in the program are no longer needed, but they will still occupy memory.

What to do?

Of course delete him!

To delete variables in python, use the del keyword.

The syntax is:

del 变量名 # 如果有多个变量需要删除用,号隔开即可

like:

a = 10
del a
print(a)

 You can see that the variable cannot be used after deletion.

0x07 variable scope

In Python, the scope of a variable refers to the block of code within which the variable can be accessed. There are three types of variable scopes in Python:

  • Local variables: defined inside a function and can only be accessed within the function.
  • Global variables: defined outside a function and accessible throughout the program.
  • Nested scope variables: defined in a function inside a function, can only be accessed within this function and nested functions.

Here is an example using local and global variables:

python
x = 5  # 全局变量

def my_func():
    x = 10  # 局部变量
    print("x = ", x)

my_func()
print("x = ", x)

In the above example, the variable x in the function my_func() is a local variable. When the function is called, Python will create a new variable called x inside the function and set its value to 10. This variable will be destroyed when the function returns. Outside the function, the variable x is still a global variable, and its value is still 5.

You don’t need to understand these codes, you just need to know the concept.

Ⅱ. Operators

0x00 What is an operator?

This chapter mainly explains Python's operators. Take a simple example 4 +5 = 9. In the example, 4 and 5 are called operands, and the "+" sign is the operator.
Commonly used operators in Python language:

  • arithmetic operators
  • Comparison (relational) operators
  • assignment operator
  • Logical Operators

Next, let us learn Python operators one by one.

0x01 Python arithmetic operators

The list of binary arithmetic operators in python is as follows:

operator grammar effect
+ a + b add a and b
- a - b subtract b from a
* a * b multiply a by b
/ a / b  divide a by b
%

a % b

Or take the remainder of a divided by b
// a // b Get the integer part of a divided by b
** a ** b Get a raised to the power b

The syntax of binary arithmetic operators can be organized as follows:

值1 运算符 值2
a = 21
b = 10
c = 0

c = a + b
print("1 - c 的值为:", c)

c = a - b
print("2 - c 的值为:", c)

c = a * b
print("3 - c 的值为:", c)

c = a / b
print("4 - c 的值为:", c)

c = a % b
print("5 - c 的值为:", c)

# 修改变量 a 、b 、c
a = 2
b = 3
c = a**b
print("6 - c 的值为:", c)

a = 10
b = 5
c = a//b
print("7 - c 的值为:", c)

0x02 Python comparison operator

operator effect
== Equals - whether the objects are equal
!= Not equal to - whether the objects are not equal
> Greater than - whether the left value is greater than the right value
< Less than - whether the left value is less than the right value
>= Greater than or equal to—whether the lvalue is greater than or equal to the rvalue
<= Less than or equal to - whether the left value is less than or equal to the rvalue

The syntax is:

左值 比较符 右值

No more spoilers here, I will talk about if judgment when the time comes

0x03 logical operator

The list of logical operators is:

Logic operator effect
not Returns False for True, returns True for False
and AND - Returns True if both lvalue and rvalue are True, otherwise returns False
or or - Return True if one of the lvalue and rvalue is True, otherwise return No

The syntax is:

左值 逻辑运算符 右值

No spoilers here, I will talk about if judgment later.

0x04 assignment operator

The list is:

assignment operator effect
= No need to talk about it
+= Equivalent to a = a+b
-= Equivalent to a = ab
...

The rest are deduced by analogy.

Ⅲ.Type conversion

0x00 conversion type function

 From the Internet

0x01 The role of conversion type

Because the return value of the input function is str and other types are needed, the conversion function is useful.

0x02 Calculate the sum of the input numbers

First define two variables

a = input("请输入a的值:")
b = input("请输入b的值:")

Then convert them to int type

a, b = int(a), int(b)

Add them together again:

c = a + b

Output the value of c

print("a + b =", c)

Complete code:

operation result:

(Garbled characters are not important)

Guess you like

Origin blog.csdn.net/m0_73552311/article/details/132838801