Basics of Python (Part 1)

The main content of this article: variables, comments, operators, keywords, data types .

Before we start with variables , let's take a look at classic programming statements → hello world

The function that outputs to the console in python3 is print()

print("hello world")

1. Variables

1.1 Syntax

Variable name = variable value
Note: Each variable must be assigned a value before use

1.2 Example

 a = 20
 b = "可乐"

1.3 Essence

In a = 20, the python interpreter has performed a total of two operations:
① Open a memory address in the memory to store data 20;
② Create an object with variable name a in the memory and point it to data 20 .

As shown in the figure:
Insert picture description here

  • Variables in python are aliases of memory addresses , that is to say, a represents 0x1002, and python has no pointers;
  • The process of storing the memory address of the data 20 in the memory space indicated by the variable a is called referencing .

2. Notes

2.1 Syntax

①Single line comment : withBeginning
②Multi -line comment : use three single quotation marks''' Or three double quotes """Enclose comments
Note: The python interpreter will not execute the commented code block

2.2 Example

 # 这是一个单行注释
 '''
 这是用三个单引号的多行注释
 这是用三个单引号的多行注释
 这是用三个单引号的多行注释
 '''
 """
 这是用三个双引号的多行注释
 这是用三个双引号的多行注释
 这是用三个双引号的多行注释
 """

Third, the operator

Operators include the following:

  • Arithmetic Operator
  • Comparison operator
  • Assignment operator
  • Logical Operators
  • Identity operator
  • Member operator

3.1 Arithmetic operators

Premise: a = 20 ; b = 10

Operator description Example
+ addition a+b Output 30
- Subtraction a-b Output 10
* multiplication a*b Output 200
/ division a/b Output 2
% Modulus (returns the remainder of the division) a%b Output 0
** power a**b Output 20 to the 10th power
// ​Round (round down) a//b Output 2

3.2 Comparison operators

Premise: a = 20 ; b = 10

Operator description Example
== Equal to (compare whether two numbers are equal) a==b output False
!= Not equal (compare whether two numbers are not equal) a!=b Output True
> more than the a>b Output True
< Less than a<b Output False
>= greater or equal to a>=b Output False
<= Less than or equal to a<=b Output False

3.3 Assignment operator

Premise: a = 20 ; b = 10

Operator description Example
= Assignment a=b Assign b to a
+= Plus equals a+=b Equivalent a = a + b
-= Minus is equal to a-=b Equivalent a = a - b
*= Multiply equals a*=b Equivalent a = a * b
/= Divide equal to a/=b Equivalent a = a / b
%= Modulo equal a%=b Equivalent a = a % b
**= Power equals a**=b Equivalent a = a ** b
//= Rounding equals a//=b Equivalent a = a // b

3.4 Logical operators

Premise: a = True ; b = False

Operator description Example
and Both are true, it is True a and b Output False
or One of the two is True, which is True a or b Output True
not Antisense of value not a Output False

3.5 Identity operator

Premise: a = True ; b = False

Operator description Example
is Determine whether the memory addresses of the two are the same a is b Output False
is not Determine whether the memory addresses of the two are different a is not b Output True

3.6 Member operators

Prerequisite: a = "可" ; b = "Coke"

Operator description Example
in Determine whether the value is in the sequence a in b Output True
not in Determine whether the value is not in the sequence a not in b Output False

3.7 Operator precedence

High to low

Operator description
** Index (highest priority)
* / % // Multiply, divide, modulo and divide
+ - Addition and subtraction
<= < > >= Comparison operator
== != Equal operator
= %= /= //= -= += *= **= Assignment operator
is is not 身份运算符
in not in 成员运算符
not and or 逻辑运算符

四、关键字

关键字指的是被python内置默认的一些变量名,由于他有特殊的含义,所以我们在定义变量的时候,不能使用关键字作为变量名,python中一共33个关键字,后面绝大部分都会用到。

and as assert break class
continue def del elif else
except finally for from False
global if import in is
lambda nonlocal not None or
pass raise return try True
while with yield

五、数据类型

通过type()函数来知晓数据的类型,或者通过isinstance()来判断数据的类型。

5.1 字符串(String)

​5.1.1 语法

单引号双引号多引号括起来。

​5.1.2 示例

a = '可乐'
b = "可乐"
c = """可乐"""

5.2 数字型(Number)

整型浮点型都是属于数字型。

​5.2.1 语法

整型:a = 10
浮点型:b = 1.1

​5.2.2 示例

a = 10  --> 整型
b = 1.1  --> 浮点型
print(type(a))  --> True
print(type(b))  --> False

5.3 布尔型(Bool)

Bool型其实也是数字型的一种,在这里可乐单独拿出来。

​5.3.1 语法

Bool型有两个关键字:
① True (真)
② False(假)

​5.3.2 示例

a = True
b = False

5.4 列表(List)

列表是有序的。

​5.4.1 语法

列表用方括号逗号隔开来表示。

​5.4.2 示例

a = [2,3,4,5,6]
b = [1]

​5.4.3 列表索引

如图:
Insert picture description here
在python中索引会有正序索引负序索引正序索引是从0开始的,负序索引是从-1开始的,例如列表a中数值2所对应的正序索引是0,负序索引是-4。

5.5 元组(Tuple)

元组是有序的。

​5.5.1 语法

元组用小括号逗号隔开来表示。
注:如果元组只有一个值,那么一定要用" ,"结尾 !!!

​5.5.2 示例

a = (2,3,4,5,6)
b = (1,)

​​5.5.3 元组索引

如图:
Insert picture description here
元组和列表的索引表示方式是一样的。

5.6 字典(Dict)

字典是无序的。

​5.6.1 语法

① 字典用大括号的键值对表示,每个键值对之间用逗号分开,键是唯一的(否则后面的会覆盖前面的),值可以不唯一。
② 键必须是不可变类型,值可以取任何类型。

​5.6.2 示例

a = {
    
     "name": "可乐", "age": 18 }
b = {
    
     "name": "kele是可乐呀" }

字典是无序的,所以没有索引的概念。

5.7 集合(Set)

集合Set是无序的不重复的数据类型。

​5.7.1 语法

Created with braces or set() , but there is no concept of key-value pairs.
Note: If you create an empty set with set(), {} means to create a dictionary .

​5.7.2 Example

a = {
    
     "可乐", 18 }
b = set()

The collection is unordered, so there is no concept of index.

5.8 Description

  • Variable types : including lists , dictionaries .
    Essence : change the value of the original variable,will notChange the original memory address.
  • Immutable types : string , number , tuple .
    Essence : change the value of the original variable,meetingChange the original memory address.

So far we have learned the variables, comments, operators, keywords and data types in python in this article. In the next article, Coke will take a look at the common methods of data types, conditional statements and loop statements .

At the end, Coke prepared a thought question for everyone to consolidate the content of this article:

To define a dictionary a, there are two key-value pairs: one key-value pair key is Coke and value is 18; the other key-value pair key is python, and value is list form 1,2,3,4,5.

Coke will leave the answer in the next article.

< END>

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_38607483/article/details/111868055