Home > Backend Development > Python Tutorial > The magical 'Hello World' - starting the programming journey

The magical 'Hello World' - starting the programming journey

Release: 2023-08-15 15:01:56
forward
1383 people have browsed it
##01






#Hello World
Hello World as

The C Programming Language# The first demonstration program in ## is very famous. Later programmers continued this habit when learning programming or debugging equipment. It can be considered the beginning of the programming journey.

The magical 'Hello World' - starting the programming journeyThe following program output: Hello World!
"""
第一个Python程序 - hello world
Author: Python当打之年
"""
Copy after login
Copy after login
print('hello world!')
# 输出 hello world!
Copy after login

print()

Function is used to print output, which is in the python program The most common function.


We save the

above code as hello.py file , then you can also enter the following command in the terminal:

##>>>
python hello.py

同样可以输出 Hello World!,以下是一些其他输出的例子

"""
第一个Python程序 - hello world
Author: Python当打之年
"""
Copy after login
Copy after login
a = 2
print(a)
# 输出 2
b = '你好'
print(b)
# 输出 你好
c = (1,2,3)
print(c)
# 输出 (1,2,3)
d = [1,2,'3']
print(d)
# 输出 [1,2,'3']
Copy after login
The magical 'Hello World' - starting the programming journey
02






Python 标识符

Python的标识符可以作为变量名、函数名、类名、模块名以及其他对象的名称等,标识符命名时有以下几点需要注意:

The magical 'Hello World' - starting the programming journey
  • 标识符由字母、数字、下划线组成,如下所示均为符合规则的标识符:

    name
    Copy after login
    your_age
    Copy after login
    str123
    Copy after login
    User
    Copy after login
    BOOK
    book_name
    Copy after login
    _base
    Copy after login


  • 标识符不能以数字开头

    1name
    12User
    000BOOK
    Copy after login
    678user_age
    Copy after login
  • 标识符严格区分大小写,以下代表五个不同的标识符:

    name
    Name
    NAme
    NAMe
    NAME
    Copy after login
  • 以下划线开头的标识符是有特殊意义(后续会详解

  • 标识符可以是汉字,但是尽量不要这么用

  • 标识符不能和 python 关键字(保留字)相同,以下指令可以输出python所有的关键字:

    import keyword
    print(keyword.kwlist)
    #['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
    Copy after login

常用命名规则:

  • 见名知意

    起一个有意义的名字,尽量做到看一眼就知道是什么意思,例如: 定义名字可以用name,定义年龄可以用age,定义学生姓名可以用student_name等。

  • 驼峰命名法

    小驼峰式命名法:第一个单词以小写字母开始;第二个单词的首字母大写,例如:studentName、studentAge等。

    大驼峰式命名法:每一个单词的首字母都采用大写字母,例如:StudentName、StudentAge等。

The magical 'Hello World' - starting the programming journey
#03






#Python Comments
## A good memory is not as good as a bad writing

. In our daily exercises, the code is relatively simple and the number of lines is relatively small, which does not reflect the importance of comments. In actual work, a project is often written by many programmers with thousands or even hundreds of thousands or millions of lines of code. If there are no comments at this time, not only will the problem not be solved efficiently, but the question will also be consumed. It consumes both manpower and material resources, so everyone must develop the habit of writing comments.

The magical 'Hello World' - starting the programming journey
##Python comments are mainly divided into the following two categories:


#Single line comment
  • You can only comment one line of content, starting with #:

    # hello world!
    Copy after login
  • 多⾏注释

    注释多⾏内容,以下三种方式均可以实现多行注释

  • # 以下为多行注释1
    """
    第一个Python程序 - hello, world
    Author: Python当打之年
    """
    # 以下为多行注释2
    '''
    第一个Python程序 - hello, world
    Author: Python当打之年
    '''
    # 以下为多行注释3
    # 第一个Python程序 - hello, world
    # Author: Python当打之年
    Copy after login

小提示:选定要注释的代码段,使用快捷键ctrl+/,可一次性注释该代码段,重复操作可取消注释。

The magical 'Hello World' - starting the programming journey

The above is the detailed content of The magical 'Hello World' - starting the programming journey. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Python当打之年
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template