2018.7.3 python

Source: Internet
Author: User

One, while loop

1. Cycle. While loop
While condition:
code block (loop body)
Execution process:
1. Determine if the condition is true. If it's true. Execute code block
2. Again to determine whether the condition is true ...
3. When the condition is false. Execute else jumps out of the loop. End of cycle
Cases
 count=1 
While count<3:
Print ("hehe")
Count=count+1

while True:
s=input ("Enter password:")
If s== "5":
Break
Print ("Output password" +s)
""
# while True:
# s=input ("Enter password:")
# if s== "5":
# break
#
# if ' 6 ' in S:
# print ("Okle")
# Continue
# print ("Output password" + s) # count=1
# sum=0
# while count<=100:
# sum=sum+count
# count=count+1
# print (sum)
# count= 1
# while count<=100:
# if count%2!=0:
# Print (count)
# count=count+1
# count=1
# W Hile count<=100:
# if count% 2 = = 0:
# Print (count)
# count+=1 #count = Count +1
# 1+2+3+4+5+6+7+8....+100 =?
# count = 1
# # Prepare a variable
# sum = 0
# while Count <= 100:
# # Add to Sum
# sum = sum + Count # Adds the value in sum (the result of the previous operation) and the number of the current number
# Count = count + 1
# Print (SUM)

# output 1-100 all the odd numbers.
# count = 1
# while Count <= 100:
#
# if count% 2! = 0:
# Print (count)
#
# Count = count + 1

Two, formatted output
%s: A placeholder for a string that can place any content (number)
%d: Placeholder for number
Cases
# NAME = input ("Please enter Name:")
# age = Input ("Please enter your Ages:")
# hobby = Input ("Enter your hobby:")
# gender = input ("Please enter your Gender:")
#
# # print (name+ "+age+" year old, is an old man, hobby is "+hobby+", Sex: "+gender)
# # %s: Placeholder representing a string
# print ("%s This year, is an old man, hobby is%s, gender:%s"% (name, age, hobby, gender))

# a = 108
# s = "The mission of the Liangshan water park with%d kn B"% (a)
# print (s)

# name = "a"
# print ("%s has been fond of the girl of Shahe%%2"% name) # If a placeholder is in the string. Then all the% behind is a placeholder. Need to be escaped
Print ("B-color. Like the girl in Changping%5") # This sentence does not have a placeholder. % or%


Three, operator
Logical operation:
and and the meaning. The left and right values must both be true. The result of the operation is true.
or OR. One of the left and right ends is true. The result is true. All is false. The result can be false
the meaning of not is not. It turns out to be false. It's true now. Not true or false, not false both true
Break ends the loop. Stop the current layer loop
Continue end the current cycle. Proceed to the next loop
Cases
# Computer-Used data
# print (2**0) # 1
# print (2**1) #2
# Print (2**2)
# Print (2**3)
# Print (2**4)
# Print (2**5)
# Print (2**6)
# Print (2**7)
# Print (2**8)
# Print (2**9)
# Print (2**10)

# assignment operation.
# a = 10
# A + = # a = a + 20
# Print (a)

# print (not, not, not = not False)

# and or not exist at the same time. First the parentheses, then the not, and then the and, and finally the OR
# print (3>4 or 4<3 and 1==1) # False
#
# Print (1 < 2 and 3 < 4 or 1>2)
# print (2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)
# print (1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)
# print (1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
# print (not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)

# x or Y if x==0 so is Y, otherwise x
# print (1 or 2) # 1
# Print (2 or 3) # 2
# print (0 or 3) # 3
# print (0 or 4) # 4

# print (0 or 1 or 3 or 0 or 5) #
# Print (1 and 2) # 2
# print (2 and 0) # 0
# print (0 and 3) # 0
# print (0 and 4) # 0
Print (0 or 4 and 3 or 7 or 9 and 6)

# print (2 > 3 and 3) # false equals 0
# Print (2 < 1 and 4 > 6 or 3 and 4 > 5 or 6)
Four, While...else
Cases
Count  = 1
While Count <= 10:
Print (count)
Count = Count + 1
if Count = = 5:
Break # Stops the loop completely. Do not perform the subsequent else
else: # Execute when condition is not set
Print ("Here is Else")





2018.7.3 python

Related Article

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.