Python introductory tutorial - basic syntax (1)

Table of contents

1. Notes

2. Python’s six data types

3. String and number console output exercises

4. Variables and basic operations

5. The type() statement checks the type of data

6. Three different definitions of strings

7. Conversion between data types

8. Specification of identifier naming rules

9. Arithmetic operators

10. Assignment operator

11. String expansion

11.1 Escape characters

11.2 String formatting

12. input function


=========================================================================

❤️Challenge getting started with Python in one day, come on! ❤️

=========================================================================

1. Notes

Divided into single-line comments and multi-line comments.

# 单行注释的格式

"""
多行注释的格式
"""

2. Python’s six data types

Number type (Number)

Integer (int): 10 -10

Float: 13.14 -2.52

Complex: 4+3j, ending with j to indicate a complex number

Boolean: represents true or false

String type (String)

List

Ordered mutable sequence - the most frequently used data type in Python, which can record a bunch of data in an orderly manner

Tuple

ordered immutable sequence

Set

Unordered non-repeating collection

Dictionary

Unordered Key-Value collection

3. String and number console output exercises

The string needs to be surrounded by double quotes ""

The print function can output on the console

Statements do not need to be separated by ; sign

print("hello world")
print("你好,1024")
print(123)
print(-12.3)
hello world
你好,1024
123
-12.3

The print function can output multiple copies of content, separated by commas.

print("日本排放核废水", "混蛋玩意儿",   2023+2,            "应该下地狱")
日本排放核废水 混蛋玩意儿 2025 应该下地狱

4. Variables and basic operations

Basic format of variable definition: (no need to define type)

variable name = variable value

money = 50
a = 12
b = 13.5
c = a + b
name = "李头铁"
print(c, a, b, name)
25.5 12 13.5 李头铁

5. The type() statement checks the type of data

print(type(money))
print(type(b))
print(type(b))
print(type(name))  # str是string的缩写
<class 'int'>
<class 'float'>
<class 'float'>
<class 'str'>

6. Three different definitions of strings

Double quote definition, single quote definition, triple quote definition

text1 = "我是字符串(本文)数据"
text2 = "我也是字符串(文本)数据哦"
text3 = """
没想到吧,
我既能做注释,也能作为字符串哟
"""
print(text1)
print(text2)
print(text3)
我是字符串(本文)数据
我也是字符串(文本)数据哦

没想到吧,
我既能做注释,也能作为字符串哟

7. Conversion between data types

Common conversion statements

int(x) Convert x to an integer

float(x) Convert x to a floating point number

str(x) Convert object x to string

Like the type() statement learned earlier, these three statements all have results (return values).

We can use print to output directly

Or use a variable to store the result value

However: type conversion is not a panacea. After all, the strong melon will not be sweet. We need to pay attention to:

1. Any type can be converted into a string through str()

2. The string must really be a number before the string can be converted into a number.

print(int(23.2))
print(int(23.8))
# print(int("23.8"))
print(float(50))
print(float(50.8))
print(str(23.8))
23
23
50.0
50.8
23.8

8. Specification of identifier naming rules

rule:

Identifiers are only allowed to appear: English (case sensitive), Chinese, numbers, and underscores (_)

Chinese is not recommended

Numbers cannot begin with

Keywords cannot be used

specification:

Knowing the meaning after seeing the name

English letters are all lowercase

Separate multiple words with underscores

9. Arithmetic operators

+ add
-       reduce
*       take
/       remove
// Divide by integers
% model
** Index
print(" 1 + 1 的结果是:", 1 + 1)
print(" 1 - 2 的结果是:", 1 - 2.8)
print(" 5 * 3.2 的结果是:", 5 * 3.2)
print(" 5 / 2 的结果是:", 5 / 2)
print(" 5 // 2 的结果是:", 5 // 2)
print(" 5 % 2 的结果是:", 5 % 2)
print(" 2 ** 10 的结果是:", 2 ** 10)
 1 + 1 的结果是: 2
 1 - 2 的结果是: -1.7999999999999998
 5 * 3.2 的结果是: 16.0
 5 / 2 的结果是: 2.5
 5 // 2 的结果是: 2
 5 % 2 的结果是: 1
 2 ** 10 的结果是: 1024

10. Assignment operator

=   +=  -=  *=  /=  //=  %=  **=
x = 10
y = x
print(y)
y += x
print(y)
y -= x
print(y)
y *= x
print(y)
y /= x
print(y)
y //= x
print(y)
y %= x
print(y)
y += y
print(y)
y **= x
print(y)
10
20
10
100
10.0
1.0
1.0
2.0
1024.0

11. String expansion

11.1 Escape characters

print("你好\",可以转移这个引号")
print("你好\\,可以转移这个\\反斜杠")
# 字符串拼接
print("中国人" + "不骗中国人?")
# print("中国人" + 2023 + "不骗中国人?")   # 字符串无法和非字符串变量进行拼接,必须类型一致才行
print("中国人" + str(2021 + 2) + "不骗中国人?")
你好",可以转移这个引号
你好\,可以转移这个\反斜杠
中国人不骗中国人?
中国人2023不骗中国人?

11.2 String formatting

Character transmission cannot be directly spliced ​​with non-string characters.

Then there is another formatted way to complete the splicing

name = "小萌"
age = 18
print("李华今年: %s 岁了" % age)
print("%s今年: %s 岁了" % (name, age))
李华今年: 18 岁了
小萌今年: 18 岁了

Among them, %s
% means: I want a placeholder
s means: turn the variable into a string and put it into the placeholder

Multiple variable placeholders
Variables should be enclosed in brackets
and filled in in the order of placeholders

%s Convert the content into a string and put it into the placeholder position
%f Convert the content into a floating point type and put it into the placeholder position
%d Convert the content into an integer and put it into the placeholder position

12. input function

I learned the output print function before

Now learn the input function

print("请输入您的姓名")
name = input()
print("ok,欢迎 %s 您!" % name)

Guess you like

Origin blog.csdn.net/YuanFudao/article/details/132641378