Python Course Notes (a)

Since the new coronavirus outbreak, had Internet access at home lesson, commencement has been two weeks now, today after school to complete Python, Python ready to sort out recent study notes.

Life is short, I used Python

一, Hello World

Beginner a new language, it must start from Hello World

print("Hello World")

First impression: simple, fast operation, thus individual results: Python is an interpreted language. Online Resources: Python is an interpreted language it? In fact, this can only be considered half right, accurately interpreted language is compiled with. python with java, C # are the same as part of the code will be pre-compiled (referred to as optimized)

Second, the simple understanding of eval

eval () function is used to perform a string expression, and returns the value of the expression.

a = input("请输入一段字符串:")
print(a+"110")

b = eval(input("请输入一个数字:"))
print(b+110)

out:
请输入一段字符串:110
110110
请输入一个数字:110
220

Visible: This function is the character string into the corresponding object.

eval('print("Hello World")')

out:
Hello World    

Visible: This function is converted to a string expression and execution.

Third, the first small case

Mooc perspective transformation temperature

# 进行温度的转换(摄氏度C与华氏度F)
TempStr = input("请输入带符号的字符:")
if TempStr[-1] in ['F', 'f']:
    C = (eval(TempStr[0:-1])-32)/1.8
    print("转换后的温度是{:.2f}C".format(C))
elif TempStr[-1] in ['C', 'c']:
    F = 1.8*eval(TempStr[0:-1])+32
    print("转换后的温度是{:.2f}F".format(F))
else:
    print("输入格式错误!")

Here the main study:

for learning

Slicing

img

format function

Fourth, the cycle

Speak for and while, to be a small demo

n = 1
p = 1
'''
while n < 11:
    p = p*n
    print("n=", n, "p=", p)
    n += 1
'''
for n in range(1, 11):
    p = p * n
    print("n=", n, "p=", p)
    n += 1

Here the main study:

range function

Fifth, the double loop (the multiplication table)

for i in range(1, 10):
    for j in range(1, i+1):
        print(i, "*", j, "=", i*j, end=" ")
    print("")

Note that the point here is the corresponding spaces, teacher lesson contrast C language spoken, there {C} to nest, but Python is a space in the form of this program is to develop good habits for the future, will not commit error.

Added little dots: end = '' does not mean the end of the wrap, spaces.

Sixth, job title: narcissistic number

Number of daffodils mean one hundred, ten a three-digit, and then cubed bits of each sum equal to the three-digit number.

for num in range(100, 1000):
    a = num // 100            # 百位
    b = num // 10 % 10       # 十位
    c = num % 10            # 个位
    if num == a**3+b**3+c**3:
        print(num)

By job can still learn some knowledge different from Java and C:

①python, in addition to number / after the decimal point will, in addition to the rounded number //

a=5
b=2
a/b

out:
2.5
a=5
b=2
a//b

out:
2

②python in * represents multiplication, ** represents the power

>>> 2 * 5
10

>>> 2 ** 5
32

Guess you like

Origin www.cnblogs.com/wangzheming35/p/12368370.html