Table of contents
Because I am only familiar with the C language in the language, I will compare it with the C language when writing python notes. (The layout of the process is based on the rookie tutorial)
A language always starts with hello world!
print("hello world!")
The difference from c is that print replaces printf, and there is no need to add ; after the end of the statement (multiple statements in one line need to be separated by ;)
1. Python reserved words
Python's standard library provides a keyword module that can output all keywords of the current version
import keyword
print("keyword.kwlist")
2. Notes
Single-line comments start with #, multi-line comments use ''' or """
#这是第一行注释
#这是第二行注释
"""
这是
多行
注释
"""
'''
这是
多行
注释
'''
3. Lines and indentation
python enforces indentation, no need to use {} like c
python多行语句
python = py + \
thon
4. Data type
1. Number type (integer-int, floating-point number-float, Boolean-bool a/b to get a floating-point number, a//b to get an integer, a**b means a to the bth power)
2. String type ( '' and "" in Python are used exactly the same , strings can be connected with +, and * means to copy the string)
3. List type: [10,011,101] (represents three numbers, similar to arrays in c language)
type (variable), get the variable type
python string indexing
-5-4-3-2-1
p y t h o n
0 1 2 3 4
str = "python"
print(str)
>>>python
print(str[0])
>>>p
prit(str[0:])
>>>python 从str第一个字符到结束
print(str[0:5])
>>>pytho 从str第一个字符到第5个字符
print(str[0:-1])
>>>pytho 从str第一个字符到倒数第二个字符
print(str[::-1])
>>>nohtyp
[s::-1]字符串反转 将字符串s从开始到结束采用-1的步长输出
(步长为 2(间隔一个位置))
['f','F'] means f, F two elements are separated by commas str[1] in ['f','F'] to determine whether the second element of the former is the same as an element of the latter
Data types in python do not need to be declared, variables must be assigned before use!
Run in python and assign a=b=c=1 to multiple variables at the same time
5. User input
str = input("Please enter:")
The input characters are assigned to str,
6. Output
Use the print keyword to output, print will automatically wrap +, if you don’t need to wrap, add end="" after print
str = "python"
print(str[0:], end=" ")
print(str[5])
>>>python n (空格源于end后面的" ")
7. Import module
Use import or from...import to import modules
import: reserved words to introduce other functions
from library name import function in library
import library name as library nickname import turtle as a