02 Python Programming Basics
02 Python Programming Basics
Page 0 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Foreword
• Python is a fully open-source high-level programming language with elegant syntax and rich
third-party libraries.
• This course aims to help you understand the programming language and Python coding style.
Page 1 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
1
10/19/2024
Objectives
On completion of this course, you will be able to:
▫ Describes the classification of programming languages.
Page 2 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Contents
1. Overview of Programming Language
2. Python Introduction
Page 3 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
2
10/19/2024
Programming Languages
• A programming language is used to write a computer program and control behavior of a computer.
• According to whether compilation is required before execution of a language, the programming language may be
classified into the compiled language, and interpreted language that does not need to be compiled.
Compiler
Interpreter: Interprets
source code line by
line.
Executable file
Page 4 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Application
temp = v [k]; TEMP = V[K]
Algorithm High-level v[k] = v[k+1]; V[K] = V[K+1]
programming v[k+1] = temp; V[K+1] = TEMP
Software
language
Increasing order of Complexity
High-Level Language
Increasing order of Abstraction
C/C++ Fortran
compiler compiler
Assembly Language
lw $t0, 0($2)
lw $t1, 4($2)
Machine Code Assembly sw $t1, 0($2)
language sw $t0, 4($2)
Instruction Set Architecture
Assembler
Micro Architecture
Hardware
Page 5 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
3
10/19/2024
• From source code to program: The source code needs to be translated into machine instructions by the compiler and assembler,
and then the linker uses the link library function to generate the machine language program. The machine language must
match the instruction set of the CPU, which is loaded to the memory by the loader during running and executed by the CPU.
Page 6 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
• Process from source code to programs: Source code of an interpreted language is generated by the compiler and then
interpreted and executed by a virtual machine (VM) (for example, JVM/PVM). The VM shields the differences between CPU
instruction sets. Therefore, portability of the interpreted language is relatively good.
Java language
Python
Python program
program
program
Compiler Compiler
Python library
Class file Java library function functions (machine .pyc file
.pyc file
(byte code) (machine language) language) (byte code)
(byte code)
JVM PVM
Page 7 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
4
10/19/2024
Contents
1. Overview of Programming Language
2. Python Introduction
Page 8 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
What Is Python?
• Python is a fully-open-source high-level programming language. Its author is Guido Van Rossum.
With support for abundant third-party libraries and advantages of the Python language, Python can be used in
many fields, such as AI, data science, apps, and scripts for automated O&M.
Page 9 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
5
10/19/2024
Page 10 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
• Interactive programming does not require script files to be created, and code is written in the interactive mode of
the Python interpreter.
C:\Users\Richard>python
Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc.
on win32
Type "help", "copyright", "credits" or "license" for more information.
1. Input -- >>> print ("hello world")
2. Output -- hello world
3. Input -- >>> a = 1
4. Input -- >>> b = 2
5. Input -- >>> print ( a + b )
6. Output -- 3
>>>
Page 11 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
6
10/19/2024
demo.py
Page 12 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
• For example, the following rules for using semicolons, parentheses, blank lines, and spaces are recommended:
• A semicolon can be added at the end of a line in Python, but is • Different functions or statement blocks can be separated by
not recommended to separate statements. spaces. A blank line helps differentiate two segments of code,
• It is recommended that each sentence be in a separate line. improving code readability.
Parentheses Space
• Parentheses can be used for the continuation of long • Spaces are not recommended in parentheses.
statements. Avoid unnecessary parentheses. • You can determine whether to add spaces on both ends of an
operator.
Page 13 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
7
10/19/2024
Identifier Naming
• A Python identifier represents the name of a constant, variable, function, or another object.
• An identifier is usually composed of letters, digits, and underscores, but cannot start with a digit. Identifiers are case sensitive
and must be unique. If an identifier does not comply with the rules, the compiler will output a SyntaxError message when
running the code.
Page 14 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Code Indentation
• In Python programs, code indentation represents the scope of a code block. If a code block contains two or more
statements, the statements must have the same indentation. For Python, code indentation is a syntax rule that
uses code indentation and colons to distinguish between layers of code.
• When writing code, you are advised to use four spaces for indentation. If incorrect indentation is used in the
program code, an IndentationError error message is displayed during code running.
if True:
Correct indentation -- print ("Hello")
else:
Correct indentation -- print (0)
a = “Python”
Incorrect indentation -- print (a)
Page 15 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
8
10/19/2024
Using Comments
• Comments are explanations added to programs to improve program readability. In the Python program,
comments are classified into single-line comments and multi-line comments.
• A multi-line comment can contain multiple lines, which are enclosed in a pair of three quotation marks ('''...''' or
'''''' ...'''''').
“””
Multi-line comment -- The output is Python.
“””
Page 16 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
• If you need to call a class of a standard library or a third-party library in a program, use "import" or "from...
import" statement to import related modules. The import statement is always after the module comment or
document string (docstring) at the top of the file.
Page 17 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
9
10/19/2024
• A module is a saved Python file. Modules can contain definitions of functions, classes, and variables that can then be utilized in
other Python programs. The only difference between a module and a regular Python program is that the module is used for
importing by other programs. Therefore, a module usually does not have a main function.
demo.py test.py
def sit(): #Define a function. import demo #Import a module.
print ('A dog is now sitting’)
demo.sit() #Call a function.
sit() #Call a function.
Execution result:
Execution result:
A dog is now sitting.
A dog is now sitting. A dog is now sitting.
Page 18 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
• The function of an instantiated class is called a method. When you define a method, a method must carry the self keyword,
which indicates the instance of the class.
demo.py
class Dog(): #Define a class. test.py
def sit(self): #Define a function
print(“A dog is now sitting.") import demo
demo.Dog.sit
Richard = Dog() #The class is instantiated.
print (type(Richard.sit)) #The function in an instantiated class is
called a method.
print (type(Dog.sit)) #The type is function. Execution result:
Page 19 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
10
10/19/2024
Summary
• This section describes the Python language and its coding style.
• To better understand the basic and advanced Python syntax, learn HCIP-Datacom-Python
Programming Basics and experience the charm of Python in practice.
Page 20 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Quiz
1. (TorF) Python is a compiled language. ( )
A. True
B. False
Page 21 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
11
10/19/2024
More Information
• For more information about Python, visit https://www.python.org/.
Page 22 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Thank
谢 谢You
www.huawei.com
Page 23 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
12