python-01 (Basics for Getting Started)

1. Download and install

  • You can download it directly from the official website, the official website address
    is https://www.python.org/downloads/ .
    Insert image description here
  • My side is a Mac. After installation, it probably looks like this, as follows:
    Insert image description here
    Insert image description here

2. Easy to get started

2.1 HelloWorld

  • After installation, it is simple and practical. You can use the one that comes with it for the first time IDLE. The syntax is very casual, with or without semicolons, as follows
    print("hello world")
    print("hello world");
    print('hello world');
    
    Insert image description here

2.2 Calculation assignment, etc.

2.2.1 Basics 1

  • This is too simple. If you have studied other languages, there is really nothing to say. Just take a screenshot and see.
    Insert image description here

2.2.2 Basics 2 (Exchanging the values ​​of two variables using python)

  • as follows:
    m1,m2 = m2,m1
    
    Insert image description here

2.3 About entry functions

  • Please see below《3.3 常见入门函数》

3. Summarize small knowledge

3.1 Shortcut keys

  1. Quickly execute the previous command:
    control + p   # mac 上的快捷键
    

3.2 Grammar points

3.2.1 print bracket problem

  • It should be noted that parentheses are not required in python2, but parentheses are required in python3, because print is a grammatical structure in python2, and print is a built-in function in python3.
  • So, if you are using python3, remember to add parentheses. If you read previous books, you may not have added them, as follows:
    Insert image description here
    Insert image description here

3.2.2

3.3 Common entry-level functions

3.3.1 print function

3.3.1.1 print function (direct printing)

  • The above briefly demonstrates hello world, and there are many other uses, as follows:

    print("hello world")    # 直接输出字符串
    print ("cat" + "dog")   # 用“+”直接拼接字符串(+号可以省略,见下面)
    
    name = "lucy"
    age =18
    print(name,age)   #可以一次输出多个对象,对象之间用逗号分隔
    
    print ("lo""ve") #如果直接输出字符串,而不是用对象表示的话,可以不使用逗号
    print("liudehua","zhouhuajian","何炅")  
    print(1,"何炅") # 一次输出多个对象的时候,也可以是不同的类型
    
    print("Hello " * 10) #打印10个hello
    s = "hi " * 3
    print (s)
    
    print("www", "susu", "com", sep=".")  # 设置间隔符
    
    list1 = ["aa","bb","cc"]
    print(list1)    #输出列表变量
    dictTest = {
          
          '男':0,'女':1}
    print(dictTest)   # 输出字典变量
    

    Insert image description here

3.3.1.2 Data format output

3.3.1.2.1 print() function
  • In print(), %字符marks the beginning of the conversion specifier.
    For example:
    print(' %10.3f' %PI)
    print('%s,%s,%s' %(name1,name2,name3))
    print('%s%d' %(dogName,dogAge))
    
    in,
    • '%s,%s,%s'This part is called:format control character
    • %(name1,name2,name3)This part is called:conversion specifier
    • %字符, indicating the beginning of the token conversion specifier
  • The complete example is as follows:
    name1 = "liudehua"
    name2 = "zhouhuajian"
    name3 = "hejiong"
    print('first name is %s,second name is %s,third name is %s' %(name1,name2,name3))
    first name is liudehua,second name is zhouhuajian,third name is hejiong
    
    
    dogName = "麦兜"
    dogAge = 3
    print("狗狗的名字是:%s,狗狗的年龄是:%d" %(dogName,dogAge))
    狗狗的名字是:麦兜,狗狗的年龄是:3
    
    
    PI = 3.1415926
    print('%10.3f' %PI)
         3.142
    
    print('%5.3f' %PI)
    3.142
    
    print('%30.3f' %PI)
                             3.142
    
    #################1(与下面3同)####################
    print("PI=%.*f" %(3,PI))
    PI=3.142
    
    print("PI=%.*f" %(4,PI))
    PI=3.1416
    
    ###############2###################
    print("PI=%*.3f" %(10,PI))
    PI=     3.142
    
    print("PI=%*.4f" %(10,PI))
    PI=    3.1416
    
    ##############3,同1,就是多加了宽度 #################3
    print("PI=%10.*f" %(3,PI))
    PI=     3.142
    print("PI=%20.*f" %(3,PI))
    PI=               3.142
    
    
    Insert image description here
    Insert image description here
  • Summary of format characters
    • %s: The string is displayed using str()
    • %d: decimal integer
    • %f,%F:Floating point number
    • ……
3.3.1.2.2 print(f) function and format
  • print(f) is a syntax construct for formatting strings. The f prefix indicates that this is a formatted string, where {} is a placeholder indicating the variable to be inserted at that position.
  • In versions below python 3.6, print(f) will cause syntax errors. You need to use python 3.6 or above or use format() to format the string.
  • Examples are as follows:
    dogName = "麦兜"
    dogAge = 3
    print("狗狗的名字是:%s,狗狗的年龄是:%d" %(dogName,dogAge)) #方式1
    
    print(f"狗狗的名字是:{dogName},狗狗的年龄是:{dogAge}") #方式2
    
    print("狗狗的名字是:{},狗狗的年龄是:{}".format(dogName,dogAge)) #方式3
    
    Insert image description here

3.3.2 Rounding function

  • This can be achieved in the following two ways, as follows:
    b = "{:.2f}".format(3.666)
    
    print(round(3.6666,2))
    
    Insert image description here

3.3.3 Date function

  • Pay attention to importing the required modules
    import datetime
    import calendar
    
    today=datetime.datetime.today() #当前时间
    print(today)
    …… # 其他的自己用到可以再查
    
    Insert image description here

3.4 Escape characters

3.4.1 Common escape characters

  • Common escape characters

    symbol illustrate
    \' apostrophe'
    \" Double quotes"
    \\ backslash\
    \n newline character
    \r carriage return
    \t Tabs
  • Example, as follows:
    Insert image description here

3.4.2 Invalidate transfer characters

  • Let’s look at a simple example first,The requirement is: print the following statement
    My\name\is\tom
    
    As a result, what I printed using the following method? , as follows:
    Insert image description here
    Obviously it is not what I want. How can I achieve the original statement I want? According to the above method, we know that we can \use escape characters here. This is one implementation method, and another is Directly disable the transfer characters. as follows:
  • Two implementation methods
    • Method 1: Use escape (suitable for those with a small number of escape characters)
      print("My\\name\\is\\tom")
      
    • Method 2: Disable escape characters
      print(r"My\name\is\tom")
      

Guess you like

Origin blog.csdn.net/suixinfeixiangfei/article/details/131845100