Getting started with Python: if statement usage

Source: Internet
Author: User

Getting started with Python: if statement usage

This article mainly introduces the usage of if Statements in the Python getting started tutorial. It is the basic knowledge of getting started with Python. For more information, see

The if statement in Python is similar to other languages. The if statement contains a logical expression that uses the data for comparison and makes a decision based on the comparison result.

Syntax:

The if statement syntax in the Python programming language is:

?

1

2

If expression:

Statement (s)

If the Boolean expression returns true, the if statement block is executed. If the Boolean expression of the if statement is calculated as false, the first group of code is executed.

The Python programming language assumes that any non-zero and non-null values are true. If it is zero or null, it is assumed that the value is false.

Example:

?

1

2

3

4

5

6

7

8

9

10

11

12

#! /Usr/bin/python

 

Var1 = 1, 100

If var1:

Print "1-Got a true expression value"

Print var1

 

Var2 = 0

If var2:

Print "2-Got a true expression value"

Print var2

Print "Good bye! "

When the above code is executed, the following results are generated:

?

1

2

3

1-Got a true expression value

100

Good bye!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.