Python Learning Note 8-statements

Source: Internet
Author: User

Conditional statements

Some programs are written in/usr/bin python, which indicates that the Python interpreter is inside the/usr/bin. However, if written as/usr/bin/env, it means looking for the Python interpreter through the system search path. Different systems, can be
The location of the interpreter can be different, so this way can make the code more portable.

#/usr/bin/env python#Coding=utf-8Number = Int (Raw_input ("Please enter any one of the integers:"))ifNumber = = 10:        Print "the number you entered is:%d"% NumberelifNumber > 10:    Print "This was more than."elifNumber < 10:    Print "This was less than."Else:    Print "Is you a human?"

Ternary operator

Ternary operation, is a relatively concise method of assignment in conditional statements, it looks like this:

" 5 " if Else 3>>> a'5'"5" if Else 3>>> a3

Looping statements

For

# /usr/bin/env pythonfruits=["Apple","Orange", " Banana " ] for in fruits:    Print i

Multiple

a=[, (3,4), (5,6)]d=[] for in A:    d.append (x+y)  Print D

While

#!/usr/bin/env python
#coding: Utf-8
A = 9
While a:
If a%2 = = 0:
Break
Else
Print "%d is odd number"%a
A = 0
Print "%d is even number"%a

While...else

Once you encounter the else, it means that it is no longer within the while loop.

# !/usr/bin/env Pythoncount = 0 while count < 5:print' is Less than 5"= count + 1else:print" was not less than 5"

For...else

This loop is also commonly used when jumping out of a loop to do something.

 #  !/usr/bin/env python  #   Coding=utf-8  from  math import   sqrt  for  n in  Range (1, -1 = sqrt (n)  if  root == int (root):  print   n  break  else  :  print   " nothing.   

Python Learning Note 8-statements

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.