Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
63 views

02 Python Programming Basics

Uploaded by

fersd2018
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

02 Python Programming Basics

Uploaded by

fersd2018
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

10/19/2024

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.

▫ Describes the Python encoding style.

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.

Compiled language Interpreted language


(Source code) (Source code)

Compiler
Interpreter: Interprets
source code line by
line.
Executable file

Operating system (Windows/Linux/Mac OS)

CPU (x86/ARM architecture)

Page 4 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Computing Technology Stack and Program Execution


Process
Computing Technology Stack Program Execution Process

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

0000 1001 1100 0110 1010 1111 0101 1000


1010 1111 0101 1000 0000 1001 1100 0110
Gates/Registers Machine 1100 0110 1010 1111 0101 1000 0000 1001
code 0101 1000 0000 1001 1100 0110 1010 1111
Transistors
Instruction
Physics Instruction 1 Data 1
set

Page 5 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

3
10/19/2024

High-level Language - Compiled Language


• Compiled language: Before a program in a compiled language is executed, a compilation process is performed to compile the
program into a machine language file. The compilation result can be directly used without re-translation during running. Typical
compiled languages include C/C++ and Go.

• 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.

Assembly Object module: Executable code:


C/C++ source
Compiler language Assembler machine Linker machine language Loader Memory
code
program language module program

Target library: library


function (machine
language)

Page 6 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

High-level Language - Interpreted Language


• Interpreted language: Interpreted language programs do not need to be compiled before running. They are translated line by
line when running. Typically, Java and Python are interpreted languages.

• 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.

Advantages of Python: Disadvantages of Python:


• Is a dynamically typed interpreted language with elegant syntax. It • Runs slow. Is an interpreted language that
allows learners to focus on program logic rather than syntax detail runs without being compiled. Code is
learning. translated line by line at run time into
• Supports both process- and object-oriented programming. machine code that the CPU can
• Provides abundant third-party libraries. understand, which is time-consuming.
• Is nicknamed the glue language because it can call code written in
other languages.

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

Python Code Execution Process

Process of compiling and


running a Python program Operation

Python source code 1. Install Python and the running environment in an


operating system.

2. Compile Python source code.


Compiler
3. The compiler runs the Python source code and
generates a .pyc file (byte code).
.pyc file (byte code)
4. A Python VM converts the byte code into the
machine language.
Running of the Python
VM 5. Hardware executes the machine language.

Page 10 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Getting Started with Python Code –


Interactive Running
• Python runs in either interactive or script mode.

• 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

Getting Started with Python Code –


Script-based Running
• Code in script mode can run on various Python compilers or in integrated development environments. For
example, IDLE, Atom, Visual Studio, Pycharm, and Anaconda provided by Python can be used.

demo.py

print ("hello world") 1. Input -- C:\Users\Richard>python demo.py


a=1 2. Output -- hello world
b=2 3. Output -- 3
print ( a + b )

1 Write a Python script file (.py). 2 Execute the script file.

Page 12 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Code Style Guide for Python


• Code style rules refer to naming rules, code indentation, and code and statement segmentation modes that must be complied
with when Python is used to write code. Good style rules help improve code readability and facilitate code maintenance and
modification.

• For example, the following rules for using semicolons, parentheses, blank lines, and spaces are recommended:

Semicolon Blank line

• 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.

1. Assign a value -- User_ID = 10 print ( User_ID )


2. Assign a value -- user_id = 20 print ( user_id )
3. Assign a string -- User_Name = ‘Richard’ print ( User_Name )
4. Assign a value -- Count = 1 + 1 print ( Count )
5. Incorrect identifier -- 4_passwd = "Huawei" print ( 4_passwd )

print() is a built-in function of Python and is used to output content in parentheses.

Question: What is the output of the print command on the right?

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 single-line comment starts with a pound sign (#).

• A multi-line comment can contain multiple lines, which are enclosed in a pair of three quotation marks ('''...''' or
'''''' ...'''''').

Single-line comment -- # Assign a string to a.


a = “Python”
print (a)

“””
Multi-line comment -- The output is Python.
“””

Page 16 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Source Code File Structure


• A complete Python source code file generally consists of interpreter and encoding format declaration, document
string, module import, and running code.

• 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.

Interpreter declaration -- #!/usr/bin/env python


Encoding format declaration -- #-*- coding:utf-8 -*-

Module comment or document string -- “””Description of a document (docstring)

This document is intended for...


“””

Time when a module is imported -- import time


Code is running -- …

Page 17 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

9
10/19/2024

Python Functions and Modules


• A function is a block of organized, reusable code that is used to perform a single, related action. It can improve the modularity of
the program and code utilization. The function is defined using the def keyword.

• 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.

1 Write a Python file (.py). 2 Import a module.

Page 18 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Python Classes and Methods


• A class is a collection of properties and methods that are the same. The class keyword is used to define a class.

• 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:

Execution result: A dog is now sitting.


<class 'method'>
<class 'method'> <class 'function'>
<class 'function'>

1 Write a Python file (.py). 2 Import a module.

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

2. Why we need to synchronize Python coding style?

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

You might also like