Open a hole: a set of python artificial intelligence learning routes. Let's start from the basics of grammar today, today is day01.
Table of contents
Introduction of basic data types
Conditional statement if...else...
Nested use of conditional statements
Formatting with f (python version >= 3.6)
Precautions
1. The python interpreter of this series is python3.9.0
2. The program is written in pycharm
3. Welcome to communicate with us
Without further ado, let's start directly.
Get started quickly
coding
The default encoding of python is uft-8, which is currently the most mainstream encoding. Encoding can be understood as a codebook, which converts the corresponding text symbols into binary and stores them in the hard disk. The encoding being used in pycharm will be displayed at the bottom right, as shown in the figure:
If you need to use other encodings, you need to declare at the beginning, and modify the encoding method in the lower right corner:
# -*- coding:GBK -*-
print output
print is the main output function in python. It is used as follows:
print("Hello World")
#由于print的输出会自动在末尾加入换行符,所以要想在一行输出需要给其他参数赋值
print("Hello World", end = "")
print("My name is python")
#我们让end参数为空就去掉了末尾的换行符,使得输出在同一行
#当然end还有其他用法,比如
print("My name is Anduin", end = ",")
print("age 18", end = ".")
#输出为:My name is Anduin,age 18
Introduction of basic data types
integer int
The so-called integer is an integer (natural number), such as:
print(2 + 10)
#输出12
print(2 * 10)
#输出20
print(10 / 2)
#输出5
print(10 % 3)
#输出1,%是取余数的意思10 / 3 = 3......1
print(2 ** 4)
#输出16,即2的4次方
string str
A string is a string of text, to be quoted using single quotes, double quotes or triple quotes. There is no difference between single quotes and double quotes, and triple quotes can enter a newline string.
#如果字符串中有双引号,要用单引号
str1 = 'my name is "anduin'
#如果字符串中有单引号,要用双引号
str2 = "my name is 'anduin"
str3 = """my
name
is
anduin"""
Boolean type bool
The Boolean type has only two values, False and True, and is usually used to judge conditions, which we will talk about later.
type conversion
#int转str
str1 = str(100)
#str转int
age = int("18")
#int转bool,规则:非0为真
print(bool(1))
#输出True
print(bool(10))
#输出True
print(bool(0))
#输出False
print(bool(-10))
#输出True
str转bool,规则:有则真
print("str")
#输出True
print("")
#输出False
print(" ")
#输出True,空格也算一个字符
note
Single-line comments use #, multi-line comments use triple quotes
#注释内容
print("Hello WOrld")#输出函数
“”“
第一行是注释
第二行#后的是注释
”“”
Conditional statement if...else...
There will be a condition after the if. If the statement after the if is satisfied, the statement after the if is executed, otherwise the statement of the else is executed. There can also be no else to indicate that only the if has the effect of judgment.
num = 1
//使用==判断是否等于,注意不能使用=,=会给变量赋值
if num == 1:
print("yes")
else:
print("no")
Nested use of conditional statements
num = 8
if num > 0:
print("yes")
if num <= 10:#<=表示表示小于或者等于
print("<=10")
else:
print(">10")
else:
print("no")
while loop
The while loop will keep looping until the condition is not met
num = 1
while num >= 0:
print(num)
num = num - 1
print(num)
"""
输出:
1
0
-1
"""
while 1:
print("这是死循环")
#因为while后是条件判断,所以1会转化为True,执行死循环
string formatting
Format with %
text = "My name is %s,age %s"
text1 = "My name is %s,age 18" % "Anduin"
text2 = "My name is %s,age %s" %("Anduin", "18")
text3 = "My name is %(name)s, age %(age)s" % {"name":"Anduin", "age":18}
text4 = text % ("Anduin", "18")
#以上1-4输出都是My name is Anduin,age 18
Format with format
text1 = "My name is {0},age {1}".format("Anduin", 18)
text2 = "My name is {},age {}".format("Anduin", 18)
text3 = "My name is {n1},age {n2}.format(n1 = "Anduin", n2 = 18)"
#1-3输出My name is Anduin,age 18
text4 = "My name is {0},age {0}".format("Anduin", 18)
#4输出My name is Anduin,age Anduin
Formatting with f (python version >= 3.6)
#由于不确定3.6本版有没有普及,只讲基础用法
name = "Anduin"
agr = 18
str = f"My name is {name},age {age}"
#输出:My name is Anduin,age 18