Python learning journey

          Summary of the first week of Python learning

  • Written Before learning Python,
     I have always had an aversion to computer programming languages. I skipped computer classes from junior high school and high school; I used C language and VF language in college to please my classmates, so that they could take care of them during the final exam, in order not to fail the course; during the postgraduate period, under the supervision and urging of a small tutor , bite the bullet and learn MATLAB and IDL in order to realize the data processing requirements in the paper. Although I know that the Python language can handle and solve my problems better, but unfortunately I don't have a friend who is proficient in Python, but I have more than enough power, so I can only let it go.
      In a word, programming languages ​​seem to me naturally separated; but when I graduated from graduate school, when I started a business with my classmates and friends, I realized that I had missed too many, too many opportunities; too much wasted , Too precious time to learn even a language, so that I can have the opportunity to show off a few lines of code in front of many junior sisters.
      Of course, if you plan to learn Python, it is not for the job, but from the bottom of your heart to complete the unfinished regret. After the decision, there is no hesitation...

1. Introduction to Python

  Python is an interactive language developed by the Dutch Guido von Rossum. Due to the simplicity, readability and scalability of the Python language, more and more research institutions are using Python for scientific computing abroad. Some well-known universities have adopted Python. Come teach programming courses. Its advantages and disadvantages are as follows:
  1. Python is an interpreted language. The biggest advantage of interpreted languages ​​is platform portability, and the biggest disadvantage is low execution efficiency.
  2. Today, when computer hardware is sufficiently developed, what we pursue is not program execution efficiency, but development efficiency.

Second, the construction of the Python environment

  1. You can download the Python installer and view related documents from Python's official website .

Note : If you want to update to Python 3.x version in Linux environment, you need to build and install from source code.

  2. Installation of Python 3.x interpreter in windows10 environment
  3. Check whether the Python 3.x version is installed:

    python --version

  Or enter the interactive environment to check: (>>>: three greater than signs indicate entering the interactive environment)

import sys
    print(sys.version_info)
    print(sys.version)

  4. Install the ipython and jupyter interactive programming tools by using the Python package management tool pip.

    pip install ipython jupyter

  or

    python -m pip install ipython jupyter

  5. The first Python program: 'hello, world.'

    # 使用了Python内置的print函数打印字符串
    print('hello, world!')

  6. If you want to develop Python in an interactive environment, you can use ipython or jupyter's notebook project.

    jupyter notebook

  7. Use the turtle module to draw a graphic - the Olympic rings.

# 绘制奥运五环
import turtle
    turtle.width(8)
    turtle.hideturtle()
    turtle.speed(8)
    turtle.bgcolor("pink")  
    turtle.pencolor("blue")
    turtle.penup()
    turtle.goto(-120, -20)
    turtle.pendown()
    turtle.circle(50)
    turtle.pencolor("black")
    turtle.penup()
    turtle.goto(0, -20)
    turtle.pendown()
    turtle.circle(50)
    turtle.pencolor("red")
    turtle.penup()
    turtle.goto(120, -20)
    turtle.pendown()
    turtle.circle(50)
    turtle.pencolor("yellow")
    turtle.penup()
    turtle.goto(-60, -70)
    turtle.pendown()
    turtle.circle(50)   
    turtle.pencolor("")
    turtle.pencolor("green")
    turtle.penup()
    turtle.goto(60, -70)
    turtle.pendown()
    turtle.circle(50)   
    turtle.width(60)
    turtle.pencolor("brown")
    turtle.penup()
    turtle.goto(-20, 20)
    turtle.pendown()
    turtle.write("奥运五环")    
    turtle.mainloop()

The Olympic Rings

3. Variables

1. Instructions and Procedures

  1. The hardware system of a computer consists of five major parts: arithmetic unit, controller, memory, input device and output device.
  2. The arithmetic unit and the controller together are the central processing unit, and its functions are: execute various operations and control instructions, and process the data in the computer software.
  3. What we usually call a program is actually a collection of instructions.
  4. Program: A series of instructions are organized together in a certain way, and then these instructions are used to control the computer to do what we want it to do.
  5. Von Neumann structure features:
    1) Separate the storage device from the central processing unit.
    2) The proposed data is encoded in binary.
  6. Binary counting method: every two into one counting method. Understand the conversion relationship between binary and decimal, and the conversion relationship between binary and octal and hexadecimal.

2. Variables and Types

  1. In programming, a variable is a carrier for storing data. A variable in a computer is the actual data, or a piece of memory space where data is stored in the memory. The value of the variable can be read and modified. The basis for all calculations and controls.
  2. Data type:
   Integer (int)
   Floating-point (float) , that is, decimals, expressed in scientific notation, the position of the decimal point of floating-point numbers is variable.
   String type (str) , any text enclosed in single or double quotes.
   Boolean type (bool) , there are two kinds of True and False, the first letter is capitalized, Boolean can directly participate in the operation.
   The complex number, 3 + 2j , is the same as the complex number in mathematics, except that the i in the imaginary part is replaced by j.

3. Naming of variables

  1. Hard rules
    a. The variable name consists of letters (generalized Unicode characters, excluding special characters), numbers and underscores, and numbers cannot start.
    b. Case sensitive (uppercase and lowercase are two different variables).
    c. Do not conflict with keywords (words with special meanings) and system reserved words (such as names of functions and modules).
  2. PEP8 (abbreviation of Index of Python Enhancement Proposals, translated as: Index of Python Enhancement Proposals.) Requirements:
    a. Spell in lowercase letters, and connect multiple words with underscores.
    b. Protected instance properties begin with a single underscore.
    c. Private instance properties begin with two underscores.
    d. See the name and know the meaning.

4. Use of variables

  1. Use the input() function for input; int() for type conversion; placeholders to format the output string.

# input()函数,int(),占位符的输出。
a = int(input('a = '))
b = int(input('b = '))
print('%d + %d = %d' % (a, b, a + b))
print('%d + %d = %d' % (a, b, a - b))
print('%d + %d = %d' % (a, b, a * b))

  2. Use type() to check the type of the variable.

# type()的使用
a = 100
b = 12.345
c = 1 + 5j
d = 'hello, world!'
e = True
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))

Note : When converting variable types, you can use Python's built-in functions (to be precise, the functions listed below are not real functions, but the construction methods for creating objects that will be mentioned later).
  int() : Convert a number or string to an integer, you can specify the base.
  float() : Converts a string to a float.
  str() : Convert the specified object into the form of a string, you can specify the encoding.
  chr() : Convert an integer to a string corresponding to the encoding (one character)
  ord() : Convert a string (one character) to the corresponding encoding (integer).

5. Operators

  Precedence of Python operators
write picture description here

# 运算符的优先级
a = 5 
b = 10
c = 3
d = 4
e = 5
a += b # a = a + b
a -= c
a *= d
a /= e
print('a = ', a)
# 逻辑运算符
flag1 = 3 > 2
flag2 = 2 < 1
flag3 = flag1 and flag2
flag4 = flag1 or flag2
flag5 = not flag1
print('flag1 = ', flag1)
print('flag2 = ', flag2)
print('flag3 = ', flag3)
print('flag4 = ', flag4)
print('flag5 = ', flag5)
print(flag1 is True)
print(flag2 is not False)

5. Exercises with variables

  1. Convert Fahrenheit to Celsius.

# 将华氏温度转化为摄氏温度(F = 1.8C + 32)
f = float(input('请输入华氏温度: ')) # f: 华氏温度
c = (f - 32) / 1.8 # 摄氏温度
print(c)
print('%.2f华氏度 = %.2f摄氏度' % (f, c)) # 本段是参考老师给的答案,学习的输出形式

  2. Enter the radius of the circle and calculate the perimeter and area.

import math # 导入math函数模块
radius = float(input('请输入圆的半径:')) # 输入圆的半径
c = 2 * math.pi * radius # c : 圆的周长
s = math.pi * radius ** 2 #  s: 圆的面积
print('圆的周长为: %.2f' % c)
print('圆的面积为: %.2f' % s)

  3. Enter any year to judge whether it is a leap year, if it is a leap year, output True, otherwise output False.

year = int(input('请输入年份: '))
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
    print('该年为闰年')
else:
    print('不是闰年')

# 以下为老师给的答案
year = int(input('请输入年份: '))
# 如果代码太长,写成一行不便于阅读,可以使用\或()折行。
is_leap = (year % 4 == 0 and year % 100 != 0 or + \
          year % 400 == 0)
print(is_leap) # is_leap为bool型判断
  • Unfinished to be updated

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325778126&siteId=291194637