[python]--Python basic knowledge notes

I. Introduction

Some articles refer to
https://www.zhihu.com/question/348263788
python official website
python getting started guide

2. Basic knowledge notes

1. Python basics and functions

def basicStr():
    print('I\'m ok.')
    print('I\'m learning\nPython.')
    print('\\\n\\') #\\就是\  \n换行
    print('\\\t\\')  #\t为制表符
    print('''line1
    ... line2
    ... line3''')
    print('''line1
    line2
    line3''')
    print(r'''hello,\n
    world''')  #r''不转义
    print(True or False)
    print(True and False)
    print(not 1 > 2)
    print(10 / 3) #浮点数
    print(10 // 3)  #除数
    print(10 % 3)  #余数
    print('Hello, %s' % 'world')
    print('Hi, %s, you have $%d.' % ('Michael', 1000000))
    print('%2d-%02d' % (3, 1))
    print('%.2f' % 3.1415926)
    r = 2.5
    s = 3.14 * r ** 2
    print(f'The radius {
     
      
      r} is {
     
      
      s:.2f}')
    list1 = ['Michael', 'Jack', ['asp', 'php'], 'Bob', 'Tracy']
    print(list1[0])
    print(list1[-1])
    tuple1 = ('Michael', 'Bob', 'Tracy', ['A', 'B'])  #['A', 'B']中可变,这是list
    tuple2 = ('Michael', 'Bob', 'Tracy') #初始化后,不可变的
    print('one:%s index:%s' % (tuple1[0], tuple1[0][0]))
    s = set([1, 1, 2, 2, 3, 3])
    print(s)
    a = 'abc'
    print(a.replace('a', 'A'))

basicStr()
input("basicStr:")

Output result:

I'm ok.
I'm learning
Python.
\
\
\       \
line1
    ... line2
    ... line3
line1
    line2
    line3
hello,\n
    world
True
False
True
3.3333333333333335
3
1
Hello, world
Hi, Michael, you have $1000000.
 3-01
3.14
The radius 2.5 is 19.62
Michael
Tracy
one:Michael index:M
{
   
    
    1, 2, 3}
Abc

General functions, etc.

#一般的函数
def function1():
    print(abs(-20))
    print(max(2, 3, 1, -5))
    print(int('123'))
    print(float('12.34'))
    print(str(1.23))
    print(bool(''))
    print(bool(1))

function1()

####if、for循环
K=['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']
def way2(n):
    r=[]
    if n >= len(K):
        pass
    else:
       for i in range(n):
           r.append(K[i])
    return r
print(way2(3))
print(way2(5))
print(way2(7))

####while循环
def way1(m):
    L = []
    n=1
    while n<= m:
        L.append(n)
        n=n+2
    return L

print(way1(20))
input("way1:")

Output results

20
3
123
12.34
1.23
False
True
['Michael', 'Sarah', 'Tracy']
[]
[]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

Function example

#nums为可变参数 如 (1,2,4)
def calc(*nums):
    sum=0
    for n in nums:
        sum=sum+n*n
    return sum

print(calc(5,

Guess you like

Origin blog.csdn.net/xunmengyou1990/article/details/130500513