Python-if Statement Control

Source: Internet
Author: User

If Else

A logical value contains two values

Ture: Represents a non-empty amount (string,tuple,list,set,dictionary), all non-zero.

Flase: Represents the amount of 0,none, empty.

elif statements

If expression1:

Statement1 (s)

Elif expression2:

Statement2 (s)

Elif Expression3:

Statement3 (s)

Else

STATEMENT4 (s)

Converting a string to uppercase and lowercase

A= ' abc '

A.lower () changes the string to lowercase.

#!/usr/bin/python

Yn = raw_input ("Please input [yes/no]:")

Yn = Yn.lower ()

If yn = = ' y ' or yn = = ' yes ':

Print "Programe is running ..."

Elif yn = = ' n ' or yn = = ' no ':

Print "Programe is Exit"

Else

print "Please input [yes/no]:"

Python Process Control loop

A loop is a structure that causes the program to repeat a certain number of times.

The same is true for conditional loops, where the loop ends when the condition is false

For loop:

In the sequence, use the For loop to traverse

The range function is also a sequence

Range (10)

List rewrite, which powers the value of I:

Print [i**2 for I in range (1,11) if I% 2 = = 0]

Calculation 1: 100 's and:

#!/usr/bin/python

sum = 0

For I in range (1,100):

sum = sum + I//or write as Sum+=i

Print sum

Range return is a list of the resulting

Xrange and range are similar, faster than range

Xrange does not produce a list, but an object that can be assigned to a variable and then evaluated by traversing the object.

Xrange is relatively less resource-intensive because it is placed in memory.

loop Control-For dictionary

DIC = {' A ': 1, ' B ': 2}

For k,v in Dic.iteritems ():p rint k,v

A 1

B 2

Write a multiplication formula: use nested

#!/usr/bin/python

For I in Xrange (1,10): Control number of rows

For j in Xrange (1,i+1): Control Number of columns

Print "%sx%s=%s"% (j,i,j*i),

Print

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/83/9E/wKioL1d4r3rQXfXmAAAgfpF0r4I640.png-wh_500x0-wm_3 -wmp_4-s_3288033790.png "title=" Ss.png "alt=" Wkiol1d4r3rqxfxmaaagfpf0r4i640.png-wh_50 "/>

This article is from the "less stubborn" blog, please be sure to keep this source http://xushaojie.blog.51cto.com/6205370/1795287

Python-if Statement Control

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.