The pass command in Python

In the previous lesson, you learned how to use the `continue` and `break` commands in Python to modify loops. In this section, we will continue with another command, the `pass` command in Python, used as a placeholder for the execution of functions, loops, etc., in the future.

In the previous lesson, you learned how to use commands continueand loops breakin Python to modify loops. In this section, we will continue with another command – the `python` passin Python – used as a placeholder for the execution of functions, loops, etc., in the future.

 

In Python, pass`` is an empty statement. Simply put, the statement passdoes nothing; it only holds place for functions and loops that you've added but aren't currently using, reserving them for future expansion.

So, are commands passwith comments the same? Absolutely not. The code compiler will completely ignore comments, but commands with comments passwill not be ignored.

The pass command in Python

The structure of the pass command

pass

We often use this command as a placeholder. For example, you have a loop or a function, but you're unsure how to build it or how to code it optimally, and you want to leave it for later. However, that function or command cannot have an empty block of code; the compiler will report an error. Therefore, you can simply use this command passto build an empty block of code, and the compiler will understand and won't complain.

Example of the pass command

# pass chỉ giữ chỗ các khối lệnh trong for: sequence = {'p', 'a', 's', 's'} for val in sequence: pass

In the code above, passan empty block of code is created for the for loop . When you run this block of code, you'll see that nothing happens; a code passlike that is considered successful.

You can do the same for any functions or classes you want to save:

def function(args): pass

or:

class vidu: pass

Example: Check two numbers a and b with corresponding values ​​a=50 and b=100. If a>b, process the result later; otherwise, print the message "b is greater than a". The specific code would be as follows:

a = 50 b = 100 if b < a: pass else: print("b lớn hơn a")

In the example above, the output would be the text "" b lớn hơn a. If we swap the values ​​of the two numbers a and b: a=100 and b=50, the output will be nothing (because the command passis executed without error).

In the next lesson, you will learn about iteration techniques in Python with many illustrative examples, so don't miss it.

Related posts
  • How to use Closure in Python

    in this article, tipsmake.com will work with you to learn about closure in python, how to define a closure and why you should use it. let's go find the answer!
  • Wool () function in Python

    how is the syntax of len () function, what parameters do it have and how is it used? invites you to read the track.
  • The eval () function in Python.

    in this article, quantrimang will continue to introduce you to a built-in function in python, eval (). eval () is an interesting utility that allows you to run python code (this code is passed as parameters) right in the program.
Other Python articles
Category

System

Windows XP

Windows Server 2012

Windows 8

Windows 7

Windows 10

Wifi tips

Virus Removal - Spyware

Speed ​​up the computer

Server

Security solution

Mail Server

LAN - WAN

Ghost - Install Win

Fix computer error

Configure Router Switch

Computer wallpaper

Computer security

Mac OS X

Mac OS System software

Mac OS Security

Mac OS Office application

Mac OS Email Management

Mac OS Data - File

Mac hardware

Hardware

USB - Flash Drive

Speaker headset

Printer

PC hardware

Network equipment

Laptop hardware

Computer components

Advice Computer

Game

PC game

Online game

Mobile Game

Pokemon GO

information

Technology story

Technology comments

Quiz technology

New technology

British talent technology

Attack the network

Artificial intelligence

Technology

Smart watches

Raspberry Pi

Linux

Camera

Basic knowledge

Banking services

SEO tips

Science

Strange story

Space Science

Scientific invention

Science Story

Science photo

Science and technology

Medicine

Health Care

Fun science

Environment

Discover science

Discover nature

Archeology

Life

Travel Experience

Tips

Raise up child

Make up

Life skills

Home Care

Entertainment

DIY Handmade

Cuisine

Christmas

Application

Web Email

Website - Blog

Web browser

Support Download - Upload

Software conversion

Social Network

Simulator software

Online payment

Office information

Music Software

Map and Positioning

Installation - Uninstall

Graphic design

Free - Discount

Email reader

Edit video

Edit photo

Compress and Decompress

Chat, Text, Call

Archive - Share

Electric

Water heater

Washing machine

Television

Machine tool

Fridge

Fans

Air conditioning

Program

Unix and Linux

SQL Server

SQL

Python

Programming C

PHP

NodeJS

MongoDB

jQuery

JavaScript

HTTP

HTML

Git

Database

Data structure and algorithm

CSS and CSS3

C ++

C #

AngularJS

Mobile

Wallpapers and Ringtones

Tricks application

Take and process photos

Storage - Sync

Security and Virus Removal

Personalized

Online Social Network

Map

Manage and edit Video

Data

Chat - Call - Text

Browser and Add-on

Basic setup