Python Basics Chapter 1: Getting Started with Python

Chapter 1: Getting started with Python

1. Introduction to Python

  • Baidu Encyclopedia:
    • Python was designed by Guido van Rossum of the Netherlands Research Society for Mathematics and Computer Science in the early 1990s as an alternative to a language called ABC. Python provides efficient high-level data structures and simple and effective object-oriented programming. Python syntax and dynamic types, as well as the nature of interpreted languages, make it a programming language for scripting and rapid application development on most platforms. With the continuous update of versions and the addition of new language features, it is gradually used for independent, large project development.
    • The Python interpreter is easy to extend, and new functions and data types can be extended using C language or C++ (or other languages ​​that can be called from C). Python is also used as an extension programming language in customizable software. Python's rich standard library provides source code or machine code for each major system platform.
    • In October 2021, Tiobe, the compiler of the Language Popularity Index, crowned Python the most popular programming language, placing it above Java, C, and JavaScript for the first time in 20 years.
  • Simple understanding:
    • Is a cross-platform computer programming language - can run in Window, Linux, MacOS
    • It is an interpreted language - there is no compilation link in the development process, which is different from Java
    • is an interactive language - you can execute code directly after the prompt >>>
    • is an object-oriented language - everything is an object
    • Very beginner-friendly language - simple syntax, not so complicated to write, supports the development of a wide range of applications

2. Installation of Python development environment

Choose the right version for your computer

Because the computer used is Windows 11 64-bit operating system, x64-based processor, so all operations are based on this version

2.1. Select Python interpreter

  • Official website: https://www.python.org

    • Other version download address: https://www.python.org/downloads/release/python-399/

image-20220603062613061

2.2. Install the Python interpreter

  • Enter the official website and download the appropriate Python interpreter

image-20220603064818614

  • After downloading, double-click the software python-3.9.9-amd64to install directly

    Choose the second item Customize installation: Custom installation (optional installation path and components)

    And check Add Python to PATH(add the installation path of the Python interpreter to the system variable, the purpose: to find the Python interpreter faster for the operating system)

image-20220603065449830

  • Then, all are checked by default, clicknext

image-20220603072316566

  • Check the first five items, and you can choose the software installation path below M

image-20220603072632755

  • When the wait is over, close the window

image-20220603073102862

  • Whether the verification is successful, press win+R, enter cmd, enter and press PythonEnter

image-20220603073152972

  • Press win, you can find the installed Python in the directory
    • IDLE: Python editor
    • Python: Interactive command line

image-20220603092000630

3. Integrated development environment PyCharm installation tutorial

Baidu

Fourth, the use of PyCharm template comments

4.1 Effect display

After the file is created, a string of code is generated by default in the file to declare the information

image-20220603103904813

4.2 Setup process

​ Click File——— settings, Editorexpand and click File and Code Templates, find Python Script, and write the following code

##!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Course : python 基础
# @Time : ${DATE} ${TIME}
# @Author : Saudade
# @FileName: ${NAME}.py
# @Software: ${PRODUCT_NAME} 2021.3.2 (Professional Edition)

4.3 Python common shortcut keys

Pycharm-shortcut keys

5. Output function in Python

5.1 Introduction to print() function

  • A function that can be used directly in python, which can display what you want to display on IDLE or standard console

image-20220603104934367

5.2 Use of print() function

  • print() can output those contents

    • The output of the print() function can be a number
    print(520)  # 520
    print(12.34)  # 12.34
    
    • The output of the print() function can be a string
    print("hello World")
    print('hello world')
    
    • The output of the print() function can be an expression containing operators
    print(3 + 1)  # 3和1是操作数,+为运算符
    # 输出结果为:表达式的结果为:4
    
  • The destination where the print() function can output the content

    • console monitor
    • document
      • important point
        1. The specified drive letter needs to exist
        2. use file=fp
    fp = open("D:/text.txt", 'a+')  # 如果文件不存在,则创建;存在则就在文件内容的后面继续追加
    print("hello world", file=fp)
    fp.close()
    
  • The output form of the print() function

    • Newline: the default is newline output
    • no line break
    print('hello', 'world', 'Python')
    # 输出结果 hello world Python
    

6. Escape characters

6.1 What is an escape character

  • It is the backslash + the letter of the transfer function you want to achieve

6.2 Why escape characters are needed

  • Escape characters can also be used when the string contains characters that cannot be represented directly , such as newline , carriage return , horizontal tab , or backspace .
character to represent escape character output display
backslash \\ \
apostrophe \’
Double quotes \" "
print('http:\\\\www.baidu.com')
print('老师说:\'大家好\'')
print('老师说:\"老师说\"')
  • When a string contains special-purpose characters such as backslashes , single quotes , and double quotes , these characters must be escaped (convert a meaning) with backslashes
character to represent escape character Remark
new line \n newline cursor moves to the beginning of the next line
carriage return \r return cursor moves to the beginning of the line
horizontal tab \t The tab cursor moves to the beginning of the next tab stop (occupying 4 spaces)
backspace \b backspace cursor back one character
print('hello\nworld')  
print('hello\tworld')  
print('hello0000\tworld')  
print('hello\rworld')  
print('hello\bworld')  

6.3 Original characters

  • Do not want the escape character in the string to work, just use the original character, that is, add r before the string, or R
  • Note : the last character cannot be a backslash, the last two characters can be backslashes
print(r'hello\nworld')
# 错误 print(r'hello\nworld\')
print(r'hello\nworld\\')

Guess you like

Origin blog.csdn.net/polaris3012/article/details/130441614