Learn python by reading comics! A little surprise a day, fun and useful (full color version)?

How to for beginners: Setting up a Python development environment

We can download the Python installation package from the Python official website . This installation package contains the Python interpreter, the basic libraries required to run Python, and the interactive running tool-Python.

After the download is completed, Python can be installed. During the installation process, a content selection dialog box will pop up. Select the check box Add Python 3.xto PATH to add the Python installation path to the environment variable PATH, so that it can be installed anywhere. The Python command is used under the folder. Click the Install Now button to start the installation. 

Chapter 1. Get your hands dirty - write and run a Hello World program 

After the Python development environment is set up, we write and run the Hello World program to test the Python development environment.
There are two main ways to write and run Python programs:

  1. interactive mode
  2.  File mode

 

1.Interaction method 

The Python installation package provides an interactive running tool - Python Shell. After installing Python, we can click the Windows "Start" menu to open Python 3.x.

2.File method 

When writing and running Python programs through files, you first need to write Python code, and then use Python instructions to run the Python code file. When writing Python code, you can use any text editing tool or
professional IDE (Integrated Development Environments) tools.

After the code is written, it is time to run the code. Start a command prompt under Windows and enter the Python hello.py command. 

Friends who see this and want to enroll in python should know how to install and download it?

3. Practice

  1. Please install the Python environment on the Windows platform.
  2. Please use a text editing tool to write a Python program, write and run the code through a file, so that it outputs the string "Hello, world!" on the console.
  3. Please use the IDLE tool to write a Python program to output the string "Hello, world!" to the console. 

 Chapter 2, the basics of programming

 We learned and set up a development environment in Chapter 1 , and also wrote a Hello World program . In this chapter, you will learn some basic syntax in Python.

1.Identifier

Identifiers are variables, functions, properties, classes, modules, and other code elements whose names can be specified by the programmer. The characters that make up an identifier follow certain naming rules. 

The naming rules for identifiers in Python are as follows.

  1. Case sensitivity: Myname and myname are two different identifiers.
  2. The first character can be an underscore (_) or a letter, but cannot be a number.
  3. Characters other than the first character must be underscores, letters, and numbers.
  4. Keywords cannot be used as identifiers.
  5. Don't use Python's built-in functions as your own identifiers. 

Note Unicode is a character encoding scheme developed by an international organization that can accommodate all texts and symbols in the world. 

2.Keywords 

Keywords are code elements with special meanings defined by the language itself.

3.Variables

When assigning a value to a variable in Python, the variable is declared. The data type of the variable is the type of the assigned data. The variable can also receive other types of data. Move your hands

Run the sample code in the Python Shell as follows: 

Bug fixing refers to defects, loopholes, errors, etc. in the program.

4. Statement

Python code is composed of keywords, identifiers, expressions and statements, etc. Statements are an important part of the code. In Python, a line of code represents a statement, and under normal circumstances there is no semicolon at the end of the statement. Sample code: 

5. Code comments  

When using # (pound sign), # is placed at the beginning of the comment line, followed by a space, followed by the content of the comment.
An example code comment is as follows:

6.Module

In Python, a module is a file. A module is the smallest unit for saving code. Python code elements such as variables, functions, attributes, and classes can be declared in a module. 

import <module name>: In this way, all code elements of the m2 module will be imported. When accessing, you need to add the prefix "m2." 

from<module name>import<code element>: In this way, the x variable in m2 will be imported, and there is no need to add the prefix "m2." when accessing. 

from<module name>import<code element>as<code element alias>: Similar to ②, when the name of the code element (x variable) of the current m1 module conflicts with the name of the code element (x variable) of the m2 module to be imported, You can give the code element to be imported (x in m2) an alias x2 

7. Move your hands-implement code element access between two modules

(1) Create two modules hello and world in the same folder, that is, two code files: hello.py and world.py.
(2) The code of the world module is as follows:

 (3) The code of the hello module is as follows:

(4) The hello module is the entrance to the program. If you need to run the hello.py file, you can run it through the following instructions in Python. 

8. Practice

1. Which of the following are legal identifiers in Python? ()
A.2variable B.variable2 C._whatavariable D._3_ E.$anothervar F.weight
2. Which of the following are not Python keywords. ()
A.if B.then C.goto D.while
3. Judge right or wrong: In Python, a line of code represents a statement. You can add a semicolon at the end of the statement, or you can omit the semicolon.
4. Please write two modules yourself and use three types of import statements to import elements in the modules.

Guess you like

Origin blog.csdn.net/m0_59595915/article/details/132229428