Python Growth Road II

Source: Internet
Author: User
Tags alphabetic character print format

Python's print format output, and the use of format to control it.

1, print string (str), exploit %s .

Print ('My name is%s' % ('taoxiao' is Taoxiao

2, print integer, floating-point number.

>>>Print("He is%d years old"% (23))#Integer%dHe is23years old>>>Print("His height is%f m"% (1.73))#floating point%fHis height is1.730000m>>>Print("His height is%.2f m"% (1.73))#floating-point numbers (Specify the number of decimal places reserved)%.2fHis height is1.73m>>>Print("His height is%.4f m"% (1.73))#floating-point number (Force 4-bit retention)%.4fHis height is1.7300 m

3, using format. This is the official recommended way, the% way will probably be eliminated in later versions.

Print ('{1},{0},{1}'. Format ('taoxiao'))  # pass through position, quite convenient, can repeat, can change position.  Print('{name}: {age}'. Format (age=24,name='  Taoxiao')   #  pass through the keyword taoxiao:24
Bug points in formatted output, only want to simply represent a%, should be expressed in percent
>>> print (' I'm%s, this year%d, My Learning Progress 1% '% (' bright and ', 28)
My name is bright and, this year 28, my study progress 1%

Operator precedence: () > Not > and > or

1, the front and back conditions for the comparison operation

Print (1 < 2 or 3 > 1)
Print (1 < 2 and 3 > 4)
Print (1 < 2 and 3 > 4 or 8 < 6 and 9 > 5 or 7 > 2)
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)

2, the condition of the front and back is the value

    • X or Y if x is True,return x
    • 0 The corresponding bool value is false, and not 0 is true.
    • True 1, False 0
Print (BOOL (+)) Print (BOOL ( -1)) Print (bool (0)) Print (int (True)) Print (int (False))

The number of bytes corresponding to different encoding methods
    • ASCII code, an English letter (not case) occupies a byte of space, a Chinese character occupies two bytes of space. A binary number sequence, which is used as a digital unit in a computer, typically a 8-bit binary number, converted to decimal. The minimum value is 0, and the maximum value is 255.
    • In UTF-8 encoding, an English character equals one byte, and a Chinese (with traditional) equals three bytes.
    • In Unicode encoding, one English equals two bytes, and one Chinese (with traditional) equals two bytes.
    • Symbol: English punctuation occupies one byte, Chinese punctuation is two bytes. Example: "." 1 bytes in size, Chinese period ". "Takes up 2 bytes in size.
    • In UTF-16 encoding, an English alphabetic character or a Chinese character store requires 2 bytes (some Chinese characters stored in the Unicode extents require 4 bytes).
    • In UTF-32 encoding, the storage of any character in the world requires 4 bytes.
While loop

1, Basic cycle

 while Conditions:          # Loop Body     # if the condition is true, then the loop body executes    # if the condition is false, then the loop body does not perform

2, Loop termination statement

    • Break
num = 0 while True:    print(num)    + = 1    if num = =:        break    
    • Continue
Count = 0 while Count <    :+ = 1    if count = =7        :continue     Print(count)
    • While ... else:

Unlike other languages else, which is usually only the same as if, there is a while ... else statement in Python

The else function behind the while loop is to execute the statement after the else is executed when the while cycle is executed properly and the middle is not aborted.

 whileFlag < 3: Flag+=1Num= Int (Input ("Please enter a number:"))    ifNum > 66:        Print("the number you typed is too big.")    elifNum < 66:        Print("the number you entered is too small.")    Else:        Print("Congratulations, you guessed it.")         BreakElse:    Print("you are so stupid .")

If it is break during execution, then the Else statement is not executed.

Count = 0 while Count <= 5 :    + = 1    if count = = 3:    break Print("Loop", Count)else:    Print (" loop normal execution ")print("-----Out of While loop------")

Homework after class

1, judge the following logical statements True,false.

1) 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6

2) not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6


2. Find the values of the following logical statements.

1), 8 or 3 and 4 or 2 and 0 or 9 and 7

2), 0 or 2 and 3 and 4 or 6 and 0 or 3

3. What are the following results?

1), 6 or 2 > 1

2), 3 or 2 > 1

3), 0 or 5 < 4

4), 5 < 4 or 3

5), 2 > 1 or 6

6), 3 and 2 > 1

7), 0 and 3 > 1

8), 2 > 1 and 3

9), 3 > 1 and 0

10), 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2

4, while loop statement basic structure?

5. Use the If statement to write the game of guessing size:

Set an ideal number such as: 66, let the user enter a number, if the larger than 66, then the result of the display is large; if it is smaller than 66, the result is smaller; only equals 66, the guess result is correct, and then exits the loop.

6, on the basis of 5 questions to upgrade:

Give the user three guesses, if three times guessed right, then show guess correct, exit the loop, if not guessed correctly within three times, then automatically exit the loop and show ' too stupid you ... '.

7. Using while loop output 1 2 3 4 5 6 8 9 10

8, 1-100 of all the number of the and (three methods)

9. All odd numbers in output 1-100 (two methods)

10. All even numbers in output 1-100 (two methods)

11, Beg 1-2+3-4+5 ... 99 of all numbers of the and

12. Login (three chance of error) and display the number of remaining errors (hint: Format the string) for each loss

13, brief description of ASCII, Unicode, utf-8 encoding relationship?

14. Describe the relationship between bits and bytes?

15, "Boy" make? UTF-8 code accounted for?? A byte? Make? GBK is the number of bytes encoded?

Python Growth Road II

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.